diff --git a/README.org b/README.org index a4246d7..77da168 100644 --- a/README.org +++ b/README.org @@ -304,6 +304,8 @@ tags that are most likely typos or abandoned - tagging lnk files within tagtrees also tag their original files - .filetags files can now be .lnk files as well - the unit tests now work on Windows and test some Windows specialities +- 2018-04-18: + - comments in =.filetags= files that contain the controlled vocabulary ** Get the most out of filetags: controlled vocabulary ~.filetags~ :PROPERTIES: @@ -326,6 +328,10 @@ Of course, you can remove existing tags by prepending a =-= character to the tag: =-tagname=. This also works interactively using the tab completion feature. +You can use comments in =.filetags= files: everything after a =#= +character is considered a comment. You can even add a comment after a +tag like "=mytag # this is a test tag=". + ** Mutually exclusive tags If you enter multiple tags in the same line in ~.filetags~, they are diff --git a/filetags/__init__.py b/filetags/__init__.py index cf84e3b..03cca28 100755 --- a/filetags/__init__.py +++ b/filetags/__init__.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -PROG_VERSION = "Time-stamp: <2018-04-15 11:47:48 vk>" +PROG_VERSION = "Time-stamp: <2018-04-18 09:51:07 karl.voit>" # TODO: # - fix parts marked with «FIXXME» @@ -1677,7 +1677,14 @@ def locate_and_parse_controlled_vocabulary(startfile): global controlled_vocabulary_filename controlled_vocabulary_filename = filename for rawline in filehandle: - line = rawline.strip() + + # remove everyting after the first hash character (which is a comment separator) + line = rawline.strip().split('#')[0].strip() # split and take everything before the first '#' as new "line" + + if len(line) == 0: + # nothing left, line consisted only of a comment or was empty + continue + if BETWEEN_TAG_SEPARATOR in line: ## if multiple tags are in one line, they are mutually exclusive: only has can be set via filetags logging.debug('locate_and_parse_controlled_vocabulary: found unique tags: %s' % @@ -1688,10 +1695,12 @@ def locate_and_parse_controlled_vocabulary(startfile): tags.append(tag) else: tags.append(line) + logging.debug('locate_and_parse_controlled_vocabulary: controlled vocabulary has %i tags' % len(tags)) logging.debug('locate_and_parse_controlled_vocabulary: controlled vocabulary has %i groups of unique tags' % (len(unique_tags) - 1)) + return tags else: logging.debug('locate_and_parse_controlled_vocabulary: controlled vocabulary is a non-existing file') diff --git a/tests/unit_tests.py b/tests/unit_tests.py index 3c9a1e2..dbfe2f7 100755 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Time-stamp: <2018-04-05 18:13:07 karl.voit> +# Time-stamp: <2018-04-17 19:16:42 karl.voit> # invoke tests using following command line: # ~/src/vktag % PYTHONPATH="~/src/filetags:" tests/unit_tests.py --verbose @@ -296,8 +296,8 @@ class TestLocateAndParseControlledVocabulary(unittest.TestCase): def setUp(self): # create temporary directories: - self.tempdir = tempfile.mkdtemp(prefix='TextControlledVocabulary_') - print("\nTextControlledVocabulary: tempdir: " + self.tempdir + ' <<<' + '#' * 10) + self.tempdir = tempfile.mkdtemp(prefix='TestControlledVocabulary_') + print("\nTestControlledVocabulary: tempdir: " + self.tempdir + ' <<<' + '#' * 10) self.subdir1 = os.path.join(self.tempdir, 'subdir1') os.makedirs(self.subdir1) self.subdir2 = os.path.join(self.tempdir, 'subdir2') @@ -424,9 +424,61 @@ class TestLocateAndParseControlledVocabulary(unittest.TestCase): def NOtest_find_cv_in_home_as_last_fallback_when_no_other_cv_is_around(self): - # I don't want to mess around in $HOME for testing purposes. + # Currently disabled because I don't want to mess around in $HOME for testing purposes. pass + def test_comment_line_in_cv(self): + """ + This tests does not use the setup from the test class. However, it does use several + other util functions defined in this class. Therefore, I set up a different test + case here and re-use the util functions. + + Test CV file looks like: + + foo + # comment + bar + ## another comment + baz + # + tag #inline-comment + + This should result in following CV: ["foo", "bar", "baz", "tag"] and not more. + + """ + + tempdir = tempfile.mkdtemp(prefix='TestControlledVocabulary_Comments_line_') + print("\ntempdir: " + tempdir + ' <<<' + '#' * 10) + assert(os.path.isdir(tempdir)) + + # create Controlled vocabulary files: + cv_file = os.path.join(tempdir, '.filetags') + self.create_file(cv_file, "foo\n# comment\nbar\n## another comment\nbaz\n#\ntag #inline-comment\n\n") + assert(os.path.isfile(cv_file)) + + # setup complete + + cv = filetags.locate_and_parse_controlled_vocabulary(cv_file) + self.assertEqual(set(cv), set(["foo", "bar", "baz", "tag"])) + + + def test_include_lines_in_cv(self): + """ + This tests does not use the setup from the test class. However, it does use several + other util functions defined in this class. Therefore, I set up a different test + case here and re-use the util functions. + + Setup looks like this: + tmpdir + `- subdir1 + | + `- .filetags with a reference to subdir2/included_filetags + - subdir2 + | + `- included_filetags with additional tags + """ + pass # FIXXME: implement + class TestFileWithoutTags(unittest.TestCase):