mirror of
https://github.com/python-escpos/python-escpos
synced 2025-09-13 09:09:58 +00:00
Drop Py37, improve typing and docstrings (#544)
Drops Py3.7, improves typing and adds a mypy config, improves the docstrings and isorts the imports. * configure isort * sort with isort * add github action * enable flake8-docstrings * fix docstrings * add mypy env * no implicit optional * add type for raw * add some type hints
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
import re
|
||||
from os import environ, path
|
||||
"""Handler for capabilities data."""
|
||||
import atexit
|
||||
import pickle
|
||||
import logging
|
||||
import pickle
|
||||
import platform
|
||||
import re
|
||||
import time
|
||||
import importlib_resources
|
||||
from contextlib import ExitStack
|
||||
from os import environ, path
|
||||
from tempfile import mkdtemp
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import importlib_resources
|
||||
import six
|
||||
import yaml
|
||||
|
||||
from contextlib import ExitStack
|
||||
from tempfile import mkdtemp
|
||||
import platform
|
||||
|
||||
from typing import Any, Dict
|
||||
|
||||
logging.basicConfig()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -60,9 +59,7 @@ PROFILES: Dict[str, Any] = CAPABILITIES["profiles"]
|
||||
|
||||
|
||||
class NotSupported(Exception):
|
||||
"""Raised if a requested feature is not supported by the
|
||||
printer profile.
|
||||
"""
|
||||
"""Raised if a requested feature is not supported by the printer profile."""
|
||||
|
||||
pass
|
||||
|
||||
@@ -80,11 +77,13 @@ class BaseProfile(object):
|
||||
profile_data: Dict[str, Any] = {}
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Get a data element from the profile."""
|
||||
return self.profile_data[name]
|
||||
|
||||
def get_font(self, font) -> int:
|
||||
"""Return the escpos index for `font`. Makes sure that
|
||||
the requested `font` is valid.
|
||||
"""Return the escpos index for `font`.
|
||||
|
||||
Makes sure that the requested `font` is valid.
|
||||
"""
|
||||
font = {"a": 0, "b": 1}.get(font, font)
|
||||
if not six.text_type(font) in self.fonts:
|
||||
@@ -107,9 +106,10 @@ class BaseProfile(object):
|
||||
return {v: k for k, v in self.codePages.items()}
|
||||
|
||||
|
||||
def get_profile(name: str = None, **kwargs):
|
||||
"""Get the profile by name; if no name is given, return the
|
||||
default profile.
|
||||
def get_profile(name: Optional[str] = None, **kwargs):
|
||||
"""Get a profile by name.
|
||||
|
||||
If no name is given, return the default profile.
|
||||
"""
|
||||
if isinstance(name, Profile):
|
||||
return name
|
||||
@@ -122,7 +122,9 @@ CLASS_CACHE = {}
|
||||
|
||||
|
||||
def get_profile_class(name: str):
|
||||
"""For the given profile name, load the data from the external
|
||||
"""Load a profile class.
|
||||
|
||||
For the given profile name, load the data from the external
|
||||
database, then generate dynamically a class.
|
||||
"""
|
||||
if name not in CLASS_CACHE:
|
||||
@@ -136,6 +138,7 @@ def get_profile_class(name: str):
|
||||
|
||||
|
||||
def clean(s):
|
||||
"""Clean profile name."""
|
||||
# Remove invalid characters
|
||||
s = re.sub("[^0-9a-zA-Z_]", "", s)
|
||||
# Remove leading characters until we find a letter or underscore
|
||||
@@ -144,17 +147,20 @@ def clean(s):
|
||||
|
||||
|
||||
class Profile(get_profile_class("default")):
|
||||
"""
|
||||
For users, who want to provide their profile
|
||||
"""Profile class for user usage.
|
||||
|
||||
For users, who want to provide their own profile.
|
||||
"""
|
||||
|
||||
def __init__(self, columns=None, features=None):
|
||||
"""Initialize profile."""
|
||||
super(Profile, self).__init__()
|
||||
|
||||
self.columns = columns
|
||||
self.features = features or {}
|
||||
|
||||
def get_columns(self, font):
|
||||
"""Get column count of printer."""
|
||||
if self.columns is not None:
|
||||
return self.columns
|
||||
|
||||
|
Reference in New Issue
Block a user