From 1feb4df58ee84274321b5e3c4e68486b89f830e5 Mon Sep 17 00:00:00 2001 From: Joshua Blum Date: Sat, 25 Apr 2015 02:16:10 +0300 Subject: [PATCH] Add pep8 conformity using flake8 --- Makefile | 8 ++++++++ dotbot/cli.py | 25 ++++++++++++++++--------- dotbot/config.py | 3 +++ dotbot/dispatcher.py | 8 ++++++-- dotbot/executor/cleaner.py | 7 ++++++- dotbot/executor/commandrunner.py | 12 ++++++++---- dotbot/executor/executor.py | 2 ++ dotbot/executor/linker.py | 25 ++++++++++++++++--------- dotbot/messenger/messenger.py | 4 +++- dotbot/util/compat.py | 1 + dotbot/util/singleton.py | 4 +++- setup.cfg | 8 ++++++++ 12 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 Makefile create mode 100644 setup.cfg diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..65cb489 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: clean lint + +clean: + find . -type f -name '*.py[cod]' -delete + find . -type f -name '*.*~' -delete + +lint: clean + -flake8 . diff --git a/dotbot/cli.py b/dotbot/cli.py index fffc017..251c906 100644 --- a/dotbot/cli.py +++ b/dotbot/cli.py @@ -4,24 +4,30 @@ from .dispatcher import Dispatcher, DispatchError from .messenger import Messenger from .messenger import Level + def add_options(parser): - parser.add_argument('-Q', '--super-quiet', dest='super_quiet', action='store_true', - help='suppress almost all output') + parser.add_argument('-Q', '--super-quiet', dest='super_quiet', + action='store_true', + help='suppress almost all output') parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', - help='suppress most output') + help='suppress most output') parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', - help='enable verbose output') + help='enable verbose output') parser.add_argument('-d', '--base-directory', nargs=1, - dest='base_directory', help='execute commands from within BASEDIR', - metavar='BASEDIR', required=True) + dest='base_directory', + help='execute commands from within BASEDIR', + metavar='BASEDIR', required=True) parser.add_argument('-c', '--config-file', nargs=1, dest='config_file', - help='run commands given in CONFIGFILE', metavar='CONFIGFILE', - required=True) + help='run commands given in CONFIGFILE', + metavar='CONFIGFILE', + required=True) + def read_config(config_file): reader = ConfigReader(config_file) return reader.get_config() + def main(): log = Messenger() try: @@ -40,7 +46,8 @@ def main(): if success: log.info('\n==> All tasks executed successfully') else: - raise DispatchError('\n==> Some tasks were not executed successfully') + raise DispatchError( + '\n==> Some tasks were not executed successfully') except (ReadingError, DispatchError) as e: log.error('%s' % e) exit(1) diff --git a/dotbot/config.py b/dotbot/config.py index a6ee9da..c5500c2 100644 --- a/dotbot/config.py +++ b/dotbot/config.py @@ -1,7 +1,9 @@ import yaml from .util import string + class ConfigReader(object): + def __init__(self, config_file_path): self._config = self._read(config_file_path) @@ -17,5 +19,6 @@ class ConfigReader(object): def get_config(self): return self._config + class ReadingError(Exception): pass diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index c3c7fe8..343d12c 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -2,7 +2,9 @@ import os from .executor import Executor from .messenger import Messenger + class Dispatcher(object): + def __init__(self, base_directory): self._log = Messenger() self._set_base_directory(base_directory) @@ -28,7 +30,8 @@ class Dispatcher(object): handled = True except Exception: self._log.error( - 'An error was encountered while executing action %s' % + 'An error was encountered\ + while executing action %s' % action) if not handled: success = False @@ -37,7 +40,8 @@ class Dispatcher(object): def _load_plugins(self): self._plugins = [plugin(self._base_directory) - for plugin in Executor.__subclasses__()] + for plugin in Executor.__subclasses__()] + class DispatchError(Exception): pass diff --git a/dotbot/executor/cleaner.py b/dotbot/executor/cleaner.py index 504c0de..70f30c0 100644 --- a/dotbot/executor/cleaner.py +++ b/dotbot/executor/cleaner.py @@ -1,7 +1,9 @@ import os from . import Executor + class Cleaner(Executor): + ''' Cleans broken symbolic links. ''' @@ -39,7 +41,10 @@ class Cleaner(Executor): if not os.path.exists(path) and os.path.islink(path): if self._in_directory(path, self._base_directory): self._log.lowinfo('Removing invalid link %s -> %s' % - (path, os.path.join(os.path.dirname(path), os.readlink(path)))) + (path, + os.path.join(os.path.dirname(path), + os.readlink(path) + ))) os.remove(path) return True diff --git a/dotbot/executor/commandrunner.py b/dotbot/executor/commandrunner.py index 45d40f5..233131b 100644 --- a/dotbot/executor/commandrunner.py +++ b/dotbot/executor/commandrunner.py @@ -1,7 +1,10 @@ -import os, subprocess +import os +import subprocess from . import Executor + class CommandRunner(Executor): + ''' Run arbitrary shell commands. ''' @@ -14,7 +17,7 @@ class CommandRunner(Executor): def handle(self, directive, data): if directive != self._directive: raise ValueError('CommandRunner cannot handle directive %s' % - directive) + directive) return self._process_commands(data) def _process_commands(self, data): @@ -41,8 +44,9 @@ class CommandRunner(Executor): self._log.lowinfo(cmd) else: self._log.lowinfo('%s [%s]' % (msg, cmd)) - ret = subprocess.call(cmd, shell=True, stdin=stdin, stdout=stdout, - stderr=stderr, cwd=self._base_directory) + ret = subprocess.call(cmd, shell=True, + stdin=stdin, stdout=stdout, + stderr=stderr, cwd=self._base_directory) if ret != 0: success = False self._log.warning('Command [%s] failed' % cmd) diff --git a/dotbot/executor/executor.py b/dotbot/executor/executor.py index e6862c0..427856b 100644 --- a/dotbot/executor/executor.py +++ b/dotbot/executor/executor.py @@ -1,6 +1,8 @@ from ..messenger import Messenger + class Executor(object): + ''' Abstract base class for commands that process directives. ''' diff --git a/dotbot/executor/linker.py b/dotbot/executor/linker.py index d94a9bc..92f5a7b 100644 --- a/dotbot/executor/linker.py +++ b/dotbot/executor/linker.py @@ -1,7 +1,10 @@ -import os, shutil +import os +import shutil from . import Executor + class Linker(Executor): + ''' Symbolically links dotfiles. ''' @@ -60,7 +63,8 @@ class Linker(Executor): def _create(self, path): success = True - parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir)) + parent = os.path.abspath( + os.path.join(os.path.expanduser(path), os.pardir)) if not self._exists(parent): try: os.makedirs(parent) @@ -100,29 +104,32 @@ class Linker(Executor): if (not self._exists(link_name) and self._is_link(link_name) and self._link_destination(link_name) != source): self._log.warning('Invalid link %s -> %s' % - (link_name, self._link_destination(link_name))) + (link_name, self._link_destination(link_name))) elif not self._exists(link_name) and self._exists(source): try: os.symlink(source, os.path.expanduser(link_name)) except OSError: - self._log.warning('Linking failed %s -> %s' % (link_name, source)) + self._log.warning('Linking failed %s -> %s' % + (link_name, source)) else: - self._log.lowinfo('Creating link %s -> %s' % (link_name, source)) + self._log.lowinfo('Creating link %s -> %s' % + (link_name, source)) success = True elif self._exists(link_name) and not self._is_link(link_name): self._log.warning( '%s already exists but is a regular file or directory' % link_name) - elif self._is_link(link_name) and self._link_destination(link_name) != source: + elif self._is_link(link_name)\ + and self._link_destination(link_name) != source: self._log.warning('Incorrect link %s -> %s' % - (link_name, self._link_destination(link_name))) + (link_name, self._link_destination(link_name))) elif not self._exists(source): if self._is_link(link_name): self._log.warning('Nonexistent target %s -> %s' % - (link_name, source)) + (link_name, source)) else: self._log.warning('Nonexistent target for %s : %s' % - (link_name, source)) + (link_name, source)) else: self._log.lowinfo('Link exists %s -> %s' % (link_name, source)) success = True diff --git a/dotbot/messenger/messenger.py b/dotbot/messenger/messenger.py index f87a367..b2cbd70 100644 --- a/dotbot/messenger/messenger.py +++ b/dotbot/messenger/messenger.py @@ -4,8 +4,10 @@ from ..util.compat import with_metaclass from .color import Color from .level import Level + class Messenger(with_metaclass(Singleton, object)): - def __init__(self, level = Level.LOWINFO): + + def __init__(self, level=Level.LOWINFO): self.set_level(level) def set_level(self, level): diff --git a/dotbot/util/compat.py b/dotbot/util/compat.py index b0f8f05..cdf18ec 100644 --- a/dotbot/util/compat.py +++ b/dotbot/util/compat.py @@ -1,5 +1,6 @@ def with_metaclass(meta, *bases): class metaclass(meta): + def __new__(cls, name, this_bases, d): return meta(name, bases, d) return type.__new__(metaclass, 'temporary_class', (), {}) diff --git a/dotbot/util/singleton.py b/dotbot/util/singleton.py index d6cc857..b28e40d 100644 --- a/dotbot/util/singleton.py +++ b/dotbot/util/singleton.py @@ -1,6 +1,8 @@ class Singleton(type): _instances = {} + def __call__(cls, *args, **kwargs): if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) + cls._instances[cls] = super( + Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..2015ad7 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,8 @@ +[flake8] +exclude = lib/* + +# List of errors and warnings: +# http://flake8.readthedocs.org/en/latest/warnings.html +# Things we ignore: +# F401 module imported but unused +ignore = F401