Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Python 3.2 support

  • Loading branch information...
commit 8b6e8d6ef5566161e2be5a3bcadd7ef951c50de5 1 parent 02071ad
@Lispython authored
View
1  .travis.yml
@@ -3,6 +3,7 @@ language: python
python:
- "2.6"
- "2.7"
+ - "3.2"
branches:
only:
View
11 commandor/__init__.py
@@ -11,6 +11,8 @@
:github: http://github.com/Lispython/commandor
"""
+import sys
+
__all__ = 'VERSION', 'VERSION_INFO',\
'Commandor', 'Command'
@@ -23,7 +25,7 @@
try:
__version__ = __import__('pkg_resources') \
.get_distribution('human_curl').version
-except Exception, e:
+except Exception:
__version__ = 'unknown'
if __version__ == 'unknown':
@@ -35,9 +37,6 @@
VERSION_INFO = __version_info__
-from base import Commandor, Command
-import colors
+from commandor.base import Commandor, Command
+from commandor import colors
-assert Commandor
-assert Command
-assert colors
View
20 commandor/base.py
@@ -20,6 +20,7 @@
InvalidScriptOption, InvalidCommandOption)
from commandor.colors import blue, red
from commandor.utils import indent, parse_args
+from commandor.compat import with_metaclass
__all__ = 'Command', 'Commandor', 'OptionParser'
@@ -86,6 +87,8 @@ def __new__(mcs, name, bases, params):
cls = super(CommandMetaClass, mcs).__new__(mcs, name, bases, params)
# Re init commands property
+ if cls.__name__ == "NewBase":
+ return cls
cls.commands = {}
@@ -108,6 +111,8 @@ def __new__(mcs, name, bases, params):
return cls
+CommandBase = with_metaclass(CommandMetaClass)
+
class CommandorMetaClass(type):
"""Commandor Meta class
@@ -121,8 +126,9 @@ def __new__(mcs, name, bases, params):
cls.commands = {}
return cls
+CommandorBase = with_metaclass(CommandorMetaClass)
-class Commandor(Mixin):
+class Commandor(CommandorBase, Mixin):
"""Command manager
"""
__metaclass__ = CommandorMetaClass
@@ -172,8 +178,8 @@ def parse_args(self, args=[]):
"""
try:
return self.parser.parse_args(args)
- except SystemExit, e:
- raise InvalidScriptOption(e)
+ except SystemExit:
+ raise InvalidScriptOption(sys.exc_info()[0])
def run(self, options, args, **kwargs):
"""Execute
@@ -271,7 +277,7 @@ def register_command(cls, command):
cls.commands[command.name] = command
-class Command(Mixin):
+class Command(CommandBase, Mixin):
"""Single console command
:attribute name: command name
@@ -357,7 +363,7 @@ def print_command_help(self):
def usage(cls):
"""Print command usage
"""
- return u"Usage: {0} [options] {1}\n".format(
+ return "Usage: {0} [options] {1}\n".format(
sys.argv[0], ' '.join([x.name for x in cls.tree] + [cls.name]))
@@ -400,8 +406,8 @@ def parse_args(self):
"""
try:
return self.parser.parse_args(self._args)
- except SystemExit, e:
- raise InvalidCommandOption(e)
+ except SystemExit:
+ raise InvalidCommandOption(sys.exc_info()[1])
def process(self):
"""Execute command
View
26 commandor/compat.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Compatibility module
+~~~~~~~~~~~~~~~~~~~~
+
+Add utilities to support python2 and python3
+
+:copyright: (c) 2013 by Alexandr Lispython (alex@obout.ru).
+:license: BSD, see LICENSE for more details.
+:github: http://github.com/Lispython/commandor
+"""
+import sys
+
+def with_metaclass(meta, base=object):
+ """Create a base class with a metaclass."""
+ return meta("NewBase", (base,), {})
+
+py_ver = sys.version_info
+
+#: Python 2.x?
+is_py2 = (py_ver[0] == 2)
+
+#: Python 3.x?
+is_py3 = (py_ver[0] == 3)
View
2  commandor/utils.py
@@ -22,7 +22,7 @@ def indent(s, level=4):
:return: `s` with `level` indentation
:rtype: string
"""
- return u" " * level + s
+ return " " * level + s
def parse_args(args):
View
12 setup.py
@@ -21,9 +21,11 @@
try:
readme_content = open(os.path.join(os.path.abspath(
os.path.dirname(__file__)), "README.rst")).read()
-except Exception, e:
- print(e)
- readme_content = __doc__
+except Exception:
+ exc = sys.exc_info()[1]
+ # Current exception is 'exc'
+ print(exc)
+ readme_content = __doc__
VERSION = "0.1.4"
@@ -41,9 +43,7 @@ def run_tests():
return suite()
tests_require = [
- 'nose',
- 'unittest2',
-]
+ 'nose']
install_requires = []
View
2  tests/__init__.py
@@ -13,7 +13,7 @@
import unittest
-from core import CoreTestCase
+from .core import CoreTestCase
def suite():
View
47 tests/core.py
@@ -12,11 +12,12 @@
"""
from optparse import OptionParser, Option
-from base import BaseTestCase
from commandor.base import Commandor, Command
from commandor.utils import parse_args
from commandor.exceptions import InvalidScriptOption
+from .base import BaseTestCase
+
__all__ = 'CoreTestCase',
@@ -27,13 +28,13 @@ class CoreTestCase(BaseTestCase):
def test_parse_args(self):
args = "--config=./config.py server start --verbose --processes=8".split()
- self.assertEquals(parse_args(args), (args[:1], args[1:]))
+ self.assertEqual(parse_args(args), (args[:1], args[1:]))
args = "server start --verbose --processes=8".split()
- self.assertEquals(parse_args(args), ([], args))
+ self.assertEqual(parse_args(args), ([], args))
args = "server --type=http start --verbose --processes=8".split()
- self.assertEquals(parse_args(args), ([], args))
+ self.assertEqual(parse_args(args), ([], args))
def test_command(self):
test_self = self
@@ -65,7 +66,7 @@ class Crawler(Command):
def run(self, verbose, procss):
test_self.assertTrue(verbose)
- test_self.assertEquals(procss, 9)
+ test_self.assertEqual(procss, 9)
parser = OptionParser(
usage="%prog [options] <commands>",
@@ -74,15 +75,15 @@ def run(self, verbose, procss):
args = "--verbose --processes=8".split()
command_instance = Server(parser, args)
- self.assertEquals(command_instance.parser, parser)
+ self.assertEqual(command_instance.parser, parser)
self.assertRaises(NotImplementedError, command_instance.process)
parsed_options, parsed_args = command_instance.parse_args()
- self.assertEquals(parsed_args, [])
- self.assertEquals(parsed_options.procss, 8)
- self.assertEquals(parsed_options.verbose, True)
+ self.assertEqual(parsed_args, [])
+ self.assertEqual(parsed_options.procss, 8)
+ self.assertEqual(parsed_options.verbose, True)
parser = OptionParser(
usage="%prog [options] <commands>",
@@ -113,7 +114,7 @@ def run(self, options, args):
if options.config == './config_exit.py':
return False
else:
- test_self.assertEquals(options.config, "./config.py")
+ test_self.assertEqual(options.config, "./config.py")
self.test_commandor = True
return {"param": "value1"}
@@ -125,7 +126,7 @@ class Server(Command):
def run(self, options, args):
test_self.assertFalse(self.test_server)
- test_self.assertEquals(self._commandor_res['param'], "value1")
+ test_self.assertEqual(self._commandor_res['param'], "value1")
return "server_name"
class Start(Command):
@@ -135,9 +136,9 @@ class Start(Command):
def run(self, verbose, processes):
test_self.assertTrue(verbose)
- test_self.assertEquals(processes, 10)
+ test_self.assertEqual(processes, 10)
test_self.assertFalse(self.test_start)
- test_self.assertEquals(self._commandor_res['param'], "value1")
+ test_self.assertEqual(self._commandor_res['param'], "value1")
return "start_name"
parser = OptionParser(
@@ -151,7 +152,7 @@ def run(self, verbose, processes):
commandor_args = "--config=./config.py server start --verbose --processes=10".split()
commandor = Commandor1(parser, args=commandor_args, options=commandor_options)
- self.assertEquals(parse_args(commandor._args), (commandor_args[:1], commandor_args[1:]))
+ self.assertEqual(parse_args(commandor._args), (commandor_args[:1], commandor_args[1:]))
commandor.add_parser_options()
parsed_options, parsed_args = commandor.parse_args(["--config=./config.py"])
@@ -159,16 +160,16 @@ def run(self, verbose, processes):
self.assertRaises(InvalidScriptOption, commandor.parse_args, ["--dddd=true"])
- self.assertEquals(parsed_options.config, "./config.py")
- self.assertEquals(parsed_args, [])
+ self.assertEqual(parsed_options.config, "./config.py")
+ self.assertEqual(parsed_args, [])
command, args = commandor.__class__.find_command(['sserver'])
- self.assertEquals(command, commandor.__class__)
- self.assertEquals(args, ['sserver'])
+ self.assertEqual(command, commandor.__class__)
+ self.assertEqual(args, ['sserver'])
command, args = commandor.__class__.find_command(['server'])
- self.assertEquals(command, Server)
- self.assertEquals(args, [])
+ self.assertEqual(command, Server)
+ self.assertEqual(args, [])
parser = OptionParser(
usage="%prog [options] <commands>",
@@ -177,7 +178,7 @@ def run(self, verbose, processes):
commandor = Commandor1(parser, args=commandor_args,
options=commandor_options)
- self.assertEquals(commandor.process(), "start_name")
+ self.assertEqual(commandor.process(), "start_name")
# Test commandor run
commandor_args = "--config=./config_exit.py".split()
@@ -188,7 +189,7 @@ def run(self, verbose, processes):
commandor = Commandor1(parser, args=commandor_args,
options=commandor_options)
- self.assertEquals(commandor.process(), False)
+ self.assertEqual(commandor.process(), False)
commandor_args = []
@@ -208,5 +209,5 @@ def run(self, options, args):
commandor = Commandor2(parser, args=commandor_args,
options=commandor_options)
- self.assertEquals(commandor.process(), False)
+ self.assertEqual(commandor.process(), False)
Please sign in to comment.
Something went wrong with that request. Please try again.