1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-12-02 09:43:30 +00:00

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

@@ -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):