Convert ini format to yaml format.

This commit is contained in:
Davis Goglin 2016-03-15 13:47:23 -07:00 committed by Davis Goglin
parent 39e912bef4
commit c26c875b61
4 changed files with 23 additions and 29 deletions

View File

@ -5,5 +5,4 @@ pyserial
sphinx-rtd-theme sphinx-rtd-theme
setuptools-scm setuptools-scm
appdirs appdirs
future pyyaml
localconfig

View File

@ -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 allow you to use the CLI, and skip some setup when using the library
programically. programically.
The default configuration file is named ``config.ini``. For windows it is The default configuration file is named ``config.yaml``. It's in the YAML
probably at:: format. For windows it is probably at::
%appdata%/python-escpos/config.ini %appdata%/python-escpos/config.yaml
And for linux:: And for linux::
$HOME/.config/python-escpos/config.ini $HOME/.config/python-escpos/config.yaml
If you aren't sure, run:: If you aren't sure, run::
@ -157,7 +157,7 @@ To load the configured pritner, run::
The printer section 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 The only required paramter is ``type``. The value of this should be one of the
printers defined in :doc:`/user/printers`. 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:: An example file printer::
[printer] printer:
type=File type: File
devfile=/dev/someprinter devfile: /dev/someprinter
And for a network printer:: And for a network printer::
[printer] printer:
type=network type: network
host=127.0.0.1 host: 127.0.0.1
port=9000 port: 9000
How to update your code for USB printers How to update your code for USB printers
---------------------------------------- ----------------------------------------

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import
import os import os
import appdirs import appdirs
from localconfig import config import yaml
from . import printer from . import printer
from . import exceptions from . import exceptions
@ -10,7 +10,7 @@ from . import exceptions
class Config(object): class Config(object):
_app_name = 'python-escpos' _app_name = 'python-escpos'
_config_file = 'config.ini' _config_file = 'config.yaml'
def __init__(self): def __init__(self):
self._has_loaded = False self._has_loaded = False
@ -20,28 +20,24 @@ class Config(object):
self._printer_config = None self._printer_config = None
def load(self, config_path=None): def load(self, config_path=None):
# If they didn't pass one, load default
if not config_path: if not config_path:
config_path = os.path.join( config_path = os.path.join(
appdirs.user_config_dir(self._app_name), appdirs.user_config_dir(self._app_name),
self._config_file self._config_file
) )
# Deal with one config or a list of them try:
# Configparser does this, but I need it for the list in the error message with open(config_path) as f:
if isinstance(config_path, basestring): config = yaml.load(f)
config_path = [config_path] except EnvironmentError as e:
files_read = config.read(config_path)
if not files_read:
raise exceptions.ConfigNotFoundError('Couldn\'t read config at one or more of {config_path}'.format( raise exceptions.ConfigNotFoundError('Couldn\'t read config at one or more of {config_path}'.format(
config_path="\n".join(config_path), config_path="\n".join(config_path),
)) ))
except yaml.ParserError as e:
raise exceptions.ConfigSyntaxError('Error parsing YAML')
if 'printer' in config: if 'printer' in config:
# For some reason, dict(config.printer) raises self._printer_config = config['printer']
# TypeError: attribute of type 'NoneType' is not callable
self._printer_config = dict(list(config.printer))
self._printer_name = self._printer_config.pop('type').title() self._printer_name = self._printer_config.pop('type').title()
if not self._printer_name or not hasattr(printer, self._printer_name): if not self._printer_name or not hasattr(printer, self._printer_name):

View File

@ -73,8 +73,7 @@ setup(
'pyserial', 'pyserial',
'six', 'six',
'appdirs', 'appdirs',
'future', 'pyyaml',
'localconfig',
], ],
setup_requires=[ setup_requires=[
'setuptools_scm', 'setuptools_scm',