fixed syntax (PEP8) using flake8

This commit is contained in:
Karl Voit 2014-12-20 22:21:39 +01:00
parent a4db02afaf
commit 30eb4bb093
2 changed files with 50 additions and 59 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time-stamp: <2014-12-20 22:13:46 vk>
# Time-stamp: <2014-12-20 22:17:57 vk>
## TODO:
## * fix parts marked with «FIXXME»
@ -149,7 +149,7 @@ def contains_tag(filename, tagname=False):
components = re.match(FILE_WITH_TAGS_REGEX, filename)
if not tagname:
return components != None
return components is not None
elif not components:
logging.debug("file [%s] does not match FILE_WITH_TAGS_REGEX" % filename)
return False
@ -192,7 +192,7 @@ def adding_tag_to_filename(filename, tagname):
assert tagname.__class__ == str or \
tagname.__class__ == unicode
if contains_tag(filename) == False:
if contains_tag(filename) is False:
logging.debug("adding_tag_to_filename(%s, %s): no tag found so far" % (filename, tagname))
components = re.match(FILE_WITH_EXTENSION_REGEX, filename)
@ -209,8 +209,8 @@ def adding_tag_to_filename(filename, tagname):
return filename
else:
logging.debug("adding_tag_to_filename(%s, %s): add as additional tag to existing list of tags" % \
(filename, tagname))
logging.debug("adding_tag_to_filename(%s, %s): add as additional tag to existing list of tags" %
(filename, tagname))
components = re.match(FILE_WITH_EXTENSION_REGEX, filename)
if components:
@ -323,6 +323,7 @@ def handle_file(filename, tags, do_remove, dryrun):
logging.debug(u"\"%s\"" % (new_filename))
os.rename(filename, new_filename)
def add_tag_to_countdict(tag, tags):
"""
Takes a tag (string) and a dict. Returns the dict with count value increased by one
@ -343,6 +344,7 @@ def add_tag_to_countdict(tag, tags):
return tags
def get_tags_from_files_and_subfolders():
"""
Traverses the file system starting with current working directory,
@ -432,6 +434,7 @@ def list_tags_by_alphabet(only_with_similar_tags=False):
return tag_dict
def list_tags_by_number(max_tag_count=0):
"""
Traverses the file system, extracts all tags, prints them sorted by tag usage count
@ -452,13 +455,13 @@ def list_tags_by_number(max_tag_count=0):
maxlength_count = 5
print "\n {0:{1}} : {2:{3}}".format(u'count', maxlength_count, u'tag', maxlength_tags)
print " "+ '-' * (maxlength_tags + maxlength_count + 7)
print " " + '-' * (maxlength_tags + maxlength_count + 7)
for tuple in sorted(tag_dict.items(), key=operator.itemgetter(1)):
## sort dict of (tag, count) according to count
if (max_tag_count>0 and tuple[1]<=max_tag_count) or max_tag_count == 0:
if (max_tag_count > 0 and tuple[1] <= max_tag_count) or max_tag_count == 0:
print " {0:{1}} : {2:{3}}".format(tuple[1], maxlength_count, tuple[0], maxlength_tags)
if max_tag_count>0 and tuple[1]>max_tag_count:
if max_tag_count > 0 and tuple[1] > max_tag_count:
## remove entries that exceed max_tag_count limit:
del tag_dict[tuple[0]]
print ''
@ -535,7 +538,6 @@ def main():
if (options.list_tags_by_alphabet or options.list_tags_by_number) and (options.tags or options.interactive or options.remove):
error_exit(8, "Please don't use list any option together with add/remove tag options.")
tags = []
if options.list_tags_by_alphabet:
@ -586,7 +588,7 @@ def main():
logging.debug("extracting list of files ...")
logging.debug("len(args) [%s]" % str(len(args)))
if len(args)<1 and not (options.list_tags_by_alphabet or options.list_tags_by_number or options.tag_gardening):
if len(args) < 1 and not (options.list_tags_by_alphabet or options.list_tags_by_number or options.tag_gardening):
error_exit(5, "Please add at least one file name as argument")
else:

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time-stamp: <2014-12-20 22:08:24 vk>
# Time-stamp: <2014-12-20 22:21:19 vk>
## invoke tests using following command line:
## ~/src/vktag % PYTHONPATH="~/src/filetag:" tests/unit_tests.py --verbose
@ -12,6 +12,7 @@ import tempfile
import os.path
from shutil import rmtree
class TestMethods(unittest.TestCase):
def setUp(self):
@ -31,51 +32,46 @@ class TestMethods(unittest.TestCase):
self.assertEqual(filetag.contains_tag(u'Some file name -- foo bar.jpeg'), True)
self.assertEqual(filetag.contains_tag(u'Some file name.jpeg'), False)
def test_adding_tag_to_filename(self):
self.assertEqual(filetag.adding_tag_to_filename(u'Some file name.jpeg', u'bar'), \
u'Some file name -- bar.jpeg')
self.assertEqual(filetag.adding_tag_to_filename(u'Some file name -- foo.jpeg', u'bar'), \
u'Some file name -- foo bar.jpeg')
self.assertEqual(filetag.adding_tag_to_filename(u'Some file name -- foo.jpeg', u'foo'), \
u'Some file name -- foo.jpeg')
self.assertEqual(filetag.adding_tag_to_filename(u'Some file name.jpeg', u'bar'),
u'Some file name -- bar.jpeg')
self.assertEqual(filetag.adding_tag_to_filename(u'Some file name -- foo.jpeg', u'bar'),
u'Some file name -- foo bar.jpeg')
self.assertEqual(filetag.adding_tag_to_filename(u'Some file name -- foo.jpeg', u'foo'),
u'Some file name -- foo.jpeg')
def test_removing_tag_from_filename(self):
self.assertEqual(filetag.removing_tag_from_filename(u'Some file name -- bar.jpeg', u'bar'), \
u'Some file name.jpeg')
self.assertEqual(filetag.removing_tag_from_filename(u'Some file name -- foo bar.jpeg', u'bar'), \
u'Some file name -- foo.jpeg')
self.assertEqual(filetag.removing_tag_from_filename(u'Some file name -- bar.jpeg', u'foo'), \
u'Some file name -- bar.jpeg')
self.assertEqual(filetag.removing_tag_from_filename(u'Some file name -- bar.jpeg', u'bar'),
u'Some file name.jpeg')
self.assertEqual(filetag.removing_tag_from_filename(u'Some file name -- foo bar.jpeg', u'bar'),
u'Some file name -- foo.jpeg')
self.assertEqual(filetag.removing_tag_from_filename(u'Some file name -- bar.jpeg', u'foo'),
u'Some file name -- bar.jpeg')
def test_extract_tags_from_filename(self):
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name - bar.jpeg'), [])
self.assertEqual(filetag.extract_tags_from_filename(u'-- bar.jpeg'), [])
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name.jpeg'), [])
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name - bar.jpeg'), [])
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name -- bar.jpeg'), [u'bar'])
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name -- foo bar baz.jpeg'), \
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name -- foo bar baz.jpeg'),
[u'foo', u'bar', u'baz'])
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name -- foo bar baz'), \
self.assertEqual(filetag.extract_tags_from_filename(u'Some file name -- foo bar baz'),
[u'foo', u'bar', u'baz'])
def test_add_tag_to_countdict(self):
self.assertEqual(filetag.add_tag_to_countdict(u'tag', {}), {u'tag':1})
self.assertEqual(filetag.add_tag_to_countdict(u'tag', {u'tag':0}), {u'tag':1})
self.assertEqual(filetag.add_tag_to_countdict(u'tag', {u'tag':1}), {u'tag':2})
self.assertEqual(filetag.add_tag_to_countdict(u'newtag', {u'oldtag':1}), {u'oldtag':1, u'newtag':1})
self.assertEqual(filetag.add_tag_to_countdict(u'newtag', {u'oldtag':2}), {u'oldtag':2, u'newtag':1})
self.assertEqual(filetag.add_tag_to_countdict(u'tag', {}), {u'tag': 1})
self.assertEqual(filetag.add_tag_to_countdict(u'tag', {u'tag': 0}), {u'tag': 1})
self.assertEqual(filetag.add_tag_to_countdict(u'tag', {u'tag': 1}), {u'tag': 2})
self.assertEqual(filetag.add_tag_to_countdict(u'newtag', {u'oldtag': 1}), {u'oldtag': 1, u'newtag': 1})
self.assertEqual(filetag.add_tag_to_countdict(u'newtag', {u'oldtag': 2}), {u'oldtag': 2, u'newtag': 1})
def tearDown(self):
pass
class TestFileWithoutTags(unittest.TestCase):
@ -95,16 +91,13 @@ class TestFileWithoutTags(unittest.TestCase):
## double-check set-up:
self.assertTrue(self.file_exists(self.testfilename))
def create_tmp_file(self, name):
open(os.path.join(self.tempdir, name), 'w')
def file_exists(self, name):
return os.path.isfile(os.path.join(self.tempdir, name))
return os.path.isfile(os.path.join(self.tempdir, name))
def test_add_and_remove_tags(self):
@ -137,7 +130,6 @@ class TestFileWithoutTags(unittest.TestCase):
[u'two'], do_remove=True, dryrun=False)
self.assertEqual(self.file_exists(u'a test file . for you.txt'), True)
def test_adding_a_tag_to_file_without_extension(self):
filename = u"file without extension"
@ -145,7 +137,6 @@ class TestFileWithoutTags(unittest.TestCase):
filetag.handle_file(os.path.join(self.tempdir, filename), [u'foo'], False, False)
self.assertEqual(self.file_exists(filename + u' -- foo'), True)
def test_list_tags_by_number(self):
## starting with no file with tags:
@ -153,22 +144,21 @@ class TestFileWithoutTags(unittest.TestCase):
## adding a file tag:
filetag.handle_file(os.path.join(self.tempdir, self.testfilename), [u'bar'], False, False)
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar':1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=0), {u'bar':1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar': 1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=0), {u'bar': 1})
## adding a another file tag:
filetag.handle_file(os.path.join(self.tempdir, u'a test file . for you -- bar.txt'), [u'foo'], False, False)
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar':1, u'foo':1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=0), {u'bar':1, u'foo':1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar': 1, u'foo': 1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=0), {u'bar': 1, u'foo': 1})
## adding a another file:
self.create_tmp_file(u'a second file')
filetag.handle_file(os.path.join(self.tempdir, u'a second file'), [u'foo'], False, False)
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar':1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar':1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=0), {u'bar':1, u'foo':2})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar': 1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=1), {u'bar': 1})
self.assertEqual(filetag.list_tags_by_number(max_tag_count=0), {u'bar': 1, u'foo': 2})
def test_list_tags_by_alphabet(self):
## starting with no file with tags:
@ -177,29 +167,28 @@ class TestFileWithoutTags(unittest.TestCase):
## adding a file tag:
filetag.handle_file(os.path.join(self.tempdir, self.testfilename), [u'similar1'], False, False)
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=True), {})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'similar1':1})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'similar1': 1})
## adding a file tag:
filetag.handle_file(os.path.join(self.tempdir, u'a test file . for you -- similar1.txt'), [u'foo'], False, False)
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=True), {})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'foo':1, u'similar1':1})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'foo': 1, u'similar1': 1})
## adding a another file:
self.create_tmp_file(u'a second file')
filetag.handle_file(os.path.join(self.tempdir, u'a second file'), [u'foo'], False, False)
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=True), {})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'foo':2, u'similar1':1})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'foo': 2, u'similar1': 1})
## adding similar tag:
filetag.handle_file(os.path.join(self.tempdir, u'a second file -- foo'), [u'similar2'], False, False)
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=True), {u'similar1':1, u'similar2':1})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'foo':2, u'similar1':1, u'similar2':1})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=True), {u'similar1': 1, u'similar2': 1})
self.assertEqual(filetag.list_tags_by_alphabet(only_with_similar_tags=False), {u'foo': 2, u'similar1': 1, u'similar2': 1})
def tearDown(self):
rmtree(self.tempdir)
if __name__ == '__main__':
unittest.main()