Convert ini format to yaml format.
This commit is contained in:
parent
39e912bef4
commit
c26c875b61
|
@ -5,5 +5,4 @@ pyserial
|
|||
sphinx-rtd-theme
|
||||
setuptools-scm
|
||||
appdirs
|
||||
future
|
||||
localconfig
|
||||
pyyaml
|
||||
|
|
|
@ -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
|
||||
----------------------------------------
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue