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

reformat codebase

This commit is contained in:
Patrick Kanzler
2021-10-30 18:15:22 +02:00
parent 109a5d8a92
commit 435f2bba24
41 changed files with 1706 additions and 1398 deletions

View File

@@ -14,16 +14,17 @@ from . import exceptions
class Config(object):
""" Configuration handler class.
"""Configuration handler class.
This class loads configuration from a default or specificed directory. It
can create your defined printer and return it to you.
"""
_app_name = 'python-escpos'
_config_file = 'config.yaml'
_app_name = "python-escpos"
_config_file = "config.yaml"
def __init__(self):
""" Initialize configuration.
"""Initialize configuration.
Remember to add anything that needs to be reset between configurations
to self._reset_config
@@ -35,7 +36,7 @@ class Config(object):
self._printer_config = None
def _reset_config(self):
""" Clear the loaded configuration.
"""Clear the loaded configuration.
If we are loading a changed config, we don't want to have leftover
data.
@@ -47,7 +48,7 @@ class Config(object):
self._printer_config = None
def load(self, config_path=None):
""" Load and parse the configuration file using pyyaml
"""Load and parse the configuration file using pyyaml
:param config_path: An optional file path, file handle, or byte string
for the configuration file.
@@ -58,31 +59,32 @@ class Config(object):
if not config_path:
config_path = os.path.join(
appdirs.user_config_dir(self._app_name),
self._config_file
appdirs.user_config_dir(self._app_name), self._config_file
)
try:
# First check if it's file like. If it is, pyyaml can load it.
# I'm checking type instead of catching exceptions to keep the
# exception handling simple
if hasattr(config_path, 'read'):
if hasattr(config_path, "read"):
config = yaml.safe_load(config_path)
else:
# If it isn't, it's a path. We have to open it first, otherwise
# pyyaml will try to read it as yaml
with open(config_path, 'rb') as config_file:
with open(config_path, "rb") as config_file:
config = yaml.safe_load(config_file)
except EnvironmentError:
raise exceptions.ConfigNotFoundError('Couldn\'t read config at {config_path}'.format(
config_path=str(config_path),
))
raise exceptions.ConfigNotFoundError(
"Couldn't read config at {config_path}".format(
config_path=str(config_path),
)
)
except yaml.YAMLError:
raise exceptions.ConfigSyntaxError('Error parsing YAML')
raise exceptions.ConfigSyntaxError("Error parsing YAML")
if 'printer' in config:
self._printer_config = config['printer']
self._printer_name = self._printer_config.pop('type').title()
if "printer" in config:
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):
raise exceptions.ConfigSyntaxError(
@@ -94,7 +96,7 @@ class Config(object):
self._has_loaded = True
def printer(self):
""" Returns a printer that was defined in the config, or throws an
"""Returns a printer that was defined in the config, or throws an
exception.
This method loads the default config if one hasn't beeen already loaded.
@@ -104,7 +106,7 @@ class Config(object):
self.load()
if not self._printer_name:
raise exceptions.ConfigSectionMissingError('printer')
raise exceptions.ConfigSectionMissingError("printer")
if not self._printer:
# We could catch init errors and make them a ConfigSyntaxError,