diff --git a/.gitignore b/.gitignore index 1828a69..ee694df 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.pyc *.egg-info __pycache__ + +*~ +*.sw[a-z] diff --git a/compago/__init__.py b/compago/__init__.py index 1162a14..b64df54 100644 --- a/compago/__init__.py +++ b/compago/__init__.py @@ -1,5 +1,5 @@ -__version__ = (1, 3, 0) +__version__ = (1, 5, 0) -from option import Option -from command import Command, CommandError -from application import Application, ApplicationError +from compago.option import Option +from compago.command import Command, CommandError +from compago.application import Application, ApplicationError diff --git a/compago/application.py b/compago/application.py index 65e548e..ee39832 100644 --- a/compago/application.py +++ b/compago/application.py @@ -3,18 +3,21 @@ import sys import traceback -from compago import Option, Command, CommandError +from compago.option import Option +from compago.command import Command, CommandError from compago.plugin import PluginManager +DEFAULT_PLUGINS = [] + try: - import compago_plugins -except ImportError, e: + from compago_plugins import * + + DEFAULT_PLUGINS = [logging_plugin.LoggingPlugin(), + config_plugin.ConfigPlugin()] + +except ImportError as e: compago_plugins = None DEFAULT_PLUGINS = [] -else: - DEFAULT_PLUGINS = [compago_plugins.LoggingPlugin(), - compago_plugins.ConfigPlugin()] - logger = logging.getLogger(__name__) @@ -117,7 +120,7 @@ def run(self, args=None, default=None): if cmd not in self.commands: logger.debug('Command:%s not in app.commands:%s' % ( cmd, self.commands)) - print self.usage + print(self.usage) sys.exit(0) logger.debug('Removing command:%s from args:%s' % (cmd, args)) @@ -133,9 +136,9 @@ def run(self, args=None, default=None): result = self.commands[cmd].run(*args) self.plugin_manager.run_hook('after_command_run', self.commands[cmd]) return result - except CommandError, e: + except CommandError as e: logger.error('Command failed: %s' % e) logger.error(traceback.format_exc()) - print self.usage - print '\nERROR: %s' % e + print(self.usage) + print('\nERROR: %s' % e) sys.exit(1) diff --git a/compago/command.py b/compago/command.py index 927bb99..7948c25 100644 --- a/compago/command.py +++ b/compago/command.py @@ -2,8 +2,7 @@ import inspect import logging -from compago import Option - +from compago.option import Option logger = logging.getLogger(__name__) @@ -35,7 +34,7 @@ def run(self, *args): try: logger.debug('Running target:%s' % self.target) return self.target(**kwargs) - except TypeError, e: + except TypeError as e: raise CommandError('Invalid command args: %s' % e) def default_options(self): @@ -56,7 +55,7 @@ def default_options(self): action=action, dest=arg, required=False, default=default) else: - option = Option(arg, type=unicode) + option = Option(arg, type=str) if not option.dest in [o.dest for o in self.options]: logger.debug('Option:%s not already found in options:%s' % ( option, self.options)) diff --git a/compago_plugins/__init__.py b/compago_plugins/__init__.py index 72af32c..6bc836b 100644 --- a/compago_plugins/__init__.py +++ b/compago_plugins/__init__.py @@ -1,2 +1,4 @@ -from logging_plugin import LoggingPlugin -from config_plugin import ConfigPlugin +__version__ = (1, 0, 0) + +from compago_plugins.config_plugin import ConfigPlugin +from compago_plugins.logging_plugin import LoggingPlugin diff --git a/compago_plugins/config_plugin.py b/compago_plugins/config_plugin.py index 377b374..0bb6017 100644 --- a/compago_plugins/config_plugin.py +++ b/compago_plugins/config_plugin.py @@ -1,10 +1,11 @@ -from UserDict import DictMixin import os import yaml + +from collections import MutableMapping from compago.plugin import Plugin -class Config(DictMixin): +class Config(MutableMapping): @staticmethod def load(path): @@ -21,12 +22,38 @@ def __init__(self, **kwargs): def __getitem__(self, key): try: return self.attributes[key] + except KeyError: raise Exception('{0} is not configured.'.format(key)) def keys(self): return self.attributes.keys() + def empty(self): + return True if self else False + + def __setitem__(self, key, value): + self.__dict__[key] = value + + def __delitem__(self, key): + del self.__dict__[key] + + def __iter__(self): + return iter(self.__dict__) + + def __len__(self): + return len(self.__dict__) + + # The final two methods aren't required, but nice for demo purposes: + def __str__(self): + '''returns simple dict representation of the mapping''' + return str(self.__dict__) + + def __repr__(self): + '''echoes class, id, & reproducible representation in the REPL''' + return '{}, D({})'.format(super(D, self).__repr__(), + self.__dict__) + class ConfigPlugin(Plugin):