From c26c875b61583da41511d8bb8ec57c1cd18cf1be Mon Sep 17 00:00:00 2001 From: Davis Goglin Date: Tue, 15 Mar 2016 13:47:23 -0700 Subject: [PATCH] Convert ini format to yaml format. --- doc/requirements.txt | 3 +-- doc/user/usage.rst | 24 ++++++++++++------------ escpos/config.py | 22 +++++++++------------- setup.py | 3 +-- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/doc/requirements.txt b/doc/requirements.txt index 2c22fdd..cba8312 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -5,5 +5,4 @@ pyserial sphinx-rtd-theme setuptools-scm appdirs -future -localconfig +pyyaml diff --git a/doc/user/usage.rst b/doc/user/usage.rst index 26fdd68..23f38e7 100644 --- a/doc/user/usage.rst +++ b/doc/user/usage.rst @@ -127,14 +127,14 @@ You can create a configuration file for python-escpos. This will allow you to use the CLI, and skip some setup when using the library programically. -The default configuration file is named ``config.ini``. For windows it is -probably at:: +The default configuration file is named ``config.yaml``. It's in the YAML +format. For windows it is probably at:: - %appdata%/python-escpos/config.ini + %appdata%/python-escpos/config.yaml And for linux:: - $HOME/.config/python-escpos/config.ini + $HOME/.config/python-escpos/config.yaml If you aren't sure, run:: @@ -157,7 +157,7 @@ To load the configured pritner, run:: The printer section ^^^^^^^^^^^^^^^^^^^ -The ``[printer]`` configuration section defines a default printer to create. +The ``printer`` configuration section defines a default printer to create. The only required paramter is ``type``. The value of this should be one of the printers defined in :doc:`/user/printers`. @@ -166,16 +166,16 @@ The rest of the parameters are whatever you want to pass to the printer. An example file printer:: - [printer] - type=File - devfile=/dev/someprinter + printer: + type: File + devfile: /dev/someprinter And for a network printer:: - [printer] - type=network - host=127.0.0.1 - port=9000 + printer: + type: network + host: 127.0.0.1 + port: 9000 How to update your code for USB printers ---------------------------------------- diff --git a/escpos/config.py b/escpos/config.py index 44bab6f..cbeaea0 100644 --- a/escpos/config.py +++ b/escpos/config.py @@ -2,7 +2,7 @@ from __future__ import absolute_import import os import appdirs -from localconfig import config +import yaml from . import printer from . import exceptions @@ -10,7 +10,7 @@ from . import exceptions class Config(object): _app_name = 'python-escpos' - _config_file = 'config.ini' + _config_file = 'config.yaml' def __init__(self): self._has_loaded = False @@ -20,28 +20,24 @@ class Config(object): self._printer_config = None def load(self, config_path=None): - # If they didn't pass one, load default if not config_path: config_path = os.path.join( appdirs.user_config_dir(self._app_name), self._config_file ) - # Deal with one config or a list of them - # Configparser does this, but I need it for the list in the error message - if isinstance(config_path, basestring): - config_path = [config_path] - - files_read = config.read(config_path) - if not files_read: + try: + with open(config_path) as f: + config = yaml.load(f) + except EnvironmentError as e: raise exceptions.ConfigNotFoundError('Couldn\'t read config at one or more of {config_path}'.format( config_path="\n".join(config_path), )) + except yaml.ParserError as e: + raise exceptions.ConfigSyntaxError('Error parsing YAML') if 'printer' in config: - # For some reason, dict(config.printer) raises - # TypeError: attribute of type 'NoneType' is not callable - self._printer_config = dict(list(config.printer)) + self._printer_config = config['printer'] self._printer_name = self._printer_config.pop('type').title() if not self._printer_name or not hasattr(printer, self._printer_name): diff --git a/setup.py b/setup.py index 23d6df9..d68d869 100755 --- a/setup.py +++ b/setup.py @@ -73,8 +73,7 @@ setup( 'pyserial', 'six', 'appdirs', - 'future', - 'localconfig', + 'pyyaml', ], setup_requires=[ 'setuptools_scm',