From 9f5eed0020e227cc061ed48c3e4b2ccd58e4ea22 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Sun, 17 Jul 2016 11:17:43 +0200 Subject: [PATCH 1/2] add version-strings into the module The version string is in the module as __version__ available. In the doc the version will be automatically parsed. The version comes from the installed module if on read the docs or directly from setuptools_scm if you are working locally. The CLI will issue the version string if you call it with the option 'version'. The CLI does not accept commands like '--version', since this would not be conform with the rest of the interface (and argparse). The configuration for loading the version-string is adapted from pimutils/vdirsyncer. It autogenerates a version string setuptools_scm at install-time and then adds it to the __version__ member in __init__.py I adapted the GitHub-template with a fitting comment and bumped the changelog. closes #141 --- .github/ISSUE_TEMPLATE.md | 2 ++ .gitignore | 1 + CHANGELOG.rst | 1 + doc/conf.py | 15 +++++++++++---- setup.py | 17 ++++++++++++++++- src/escpos/__init__.py | 18 ++++++++++++++++++ src/escpos/cli.py | 13 ++++++++++++- test/test_cli.py | 14 ++++++++++---- 8 files changed, 71 insertions(+), 10 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index df9285d..36ed5e6 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -17,6 +17,8 @@ I have: **Printer:** Manufacturer Model XVI + **python-escpos version:** 0.0.0 **python version:** 0.0 diff --git a/.gitignore b/.gitignore index 8c4de97..247392d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ temp build/ dist/ .coverage +src/escpos/version.py # testing temporary directories test/test-cli-output/ diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1e88782..ae117ac 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,7 @@ changes - packaging: configured the coverage-analysis codecov.io - GitHub: improved issues-template - documentation: add troubleshooting tip to network-interface +- the module, cli and documentation is now aware of the version of python-escpos contributors ^^^^^^^^^^^^ diff --git a/doc/conf.py b/doc/conf.py index 0c9568a..2cbe0cf 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -15,7 +15,10 @@ import sys import os on_rtd = os.getenv('READTHEDOCS') == 'True' -from setuptools_scm import get_version +if on_rtd: + import escpos +else: + from setuptools_scm import get_version # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the @@ -70,10 +73,14 @@ copyright = u'2016, Manuel F Martinez and others' # |version| and |release|, also used in various other places throughout the # built documents. # +if on_rtd: + # The full version, including alpha/beta/rc tags. + release = escpos.__version__ +else: + # locally setuptools_scm should work + release = get_version(root=root) # The short X.Y version. -version = get_version(root=root) -# The full version, including alpha/beta/rc tags. -release = version +version = '.'.join(release.split('.')[:2]) # The short X.Y version. # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index 7ca8222..e4edbf3 100755 --- a/setup.py +++ b/setup.py @@ -45,9 +45,24 @@ class Tox(test_command): errno = tox.cmdline(args=args) sys.exit(errno) + +setuptools_scm_template = """\ +# coding: utf-8 +# file generated by setuptools_scm +# don't change, don't track in version control +from __future__ import unicode_literals + +version = '{version}' + +""" + + setup( name='python-escpos', - use_scm_version=True, + use_scm_version={ + 'write_to': 'src/escpos/version.py', + 'write_to_template': setuptools_scm_template, + }, url='https://github.com/python-escpos/python-escpos', download_url='https://github.com/python-escpos/python-escpos/archive/master.zip', description='Python library to manipulate ESC/POS Printers', diff --git a/src/escpos/__init__.py b/src/escpos/__init__.py index 0459ce8..3fbc808 100644 --- a/src/escpos/__init__.py +++ b/src/escpos/__init__.py @@ -1 +1,19 @@ +# -*- coding: utf-8 -*- +""" +python-escpos enables you to manipulate escpos-printers +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + __all__ = ["constants", "escpos", "exceptions", "printer"] + +try: + from .version import version as __version__ # noqa +except ImportError: # pragma: no cover + raise ImportError( + 'Failed to find (autogenerated) version.py. ' + 'This might be because you are installing from GitHub\'s tarballs, ' + 'use the PyPI ones.' + ) diff --git a/src/escpos/cli.py b/src/escpos/cli.py index e2e8a83..7dc7287 100644 --- a/src/escpos/cli.py +++ b/src/escpos/cli.py @@ -17,6 +17,7 @@ import argparse import sys import six from . import config +from . import version # Must be defined before it's used in DEMO_FUNCTIONS @@ -448,7 +449,7 @@ def main(): # Allow config file location to be passed parser.add_argument( '-c', '--config', - help='Altnerate path to the configuration file', + help='Alternate path to the configuration file', ) # Everything interesting runs off of a subparser so we can use the format @@ -491,6 +492,10 @@ def main(): action='store_true', ) + parser_command_version = command_subparsers.add_parser('version', + help='Print the version of python-escpos') + parser_command_version.set_defaults(version=True) + # Get only arguments actually passed args_dict = vars(parser.parse_args()) if not args_dict: @@ -498,6 +503,12 @@ def main(): sys.exit() command_arguments = dict([k, v] for k, v in six.iteritems(args_dict) if v is not None) + # If version should be printed, do this, then exit + print_version = command_arguments.pop('version', None) + if print_version: + print(version.version) + sys.exit() + # If there was a config path passed, grab it config_path = command_arguments.pop('config', None) diff --git a/test/test_cli.py b/test/test_cli.py index 03beb2d..b9aebc3 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -11,6 +11,7 @@ import os import sys from scripttest import TestFileEnvironment from nose.tools import assert_equals +import escpos TEST_DIR = os.path.abspath('test/test-cli-output') @@ -29,7 +30,7 @@ printer: ) -class TestCLI: +class TestCLI(): """ Contains setups, teardowns, and tests for CLI """ @@ -60,8 +61,7 @@ class TestCLI: ) self.default_args = ( - sys.executable, - '-mescpos.cli', + 'python-escpos', '-c', CONFIGFILE, ) @@ -79,10 +79,16 @@ class TestCLI: def test_cli_help(self): """ Test getting help from cli """ - result = self.env.run(sys.executable, '-mescpos.cli', '-h') + result = self.env.run('python-escpos', '-h') assert not result.stderr assert 'usage' in result.stdout + def test_cli_version(self): + """ Test the version string """ + result = self.env.run('python-escpos', 'version') + assert not result.stderr + assert_equals(escpos.__version__, result.stdout.strip()) + def test_cli_text(self): """ Make sure text returns what we sent it """ test_text = 'this is some text' From a5cae3adb734ffdc9969ef1b6fc42eb41ffa26a6 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Sun, 17 Jul 2016 19:17:10 +0200 Subject: [PATCH 2/2] fix inconsistent behaviour of argparse-code this affected certain versions of argparse in python3 --- src/escpos/cli.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/escpos/cli.py b/src/escpos/cli.py index 7dc7287..5457df5 100644 --- a/src/escpos/cli.py +++ b/src/escpos/cli.py @@ -456,7 +456,10 @@ def main(): # cli [subparser] -args command_subparsers = parser.add_subparsers( title='ESCPOS Command', + dest='parser', ) + # fix inconsistencies in the behaviour of some versions of argparse + command_subparsers.required = False # force 'required' testing # Build the ESCPOS command arguments for command in ESCPOS_COMMANDS: @@ -522,6 +525,9 @@ def main(): target_command = command_arguments.pop('func') + # remove helper-argument 'parser' from dict + command_arguments.pop('parser', None) + if hasattr(printer, target_command): # print command with args getattr(printer, target_command)(**command_arguments)