diff --git a/AUTHORS b/AUTHORS index 44f1ada..30f2921 100644 --- a/AUTHORS +++ b/AUTHORS @@ -46,6 +46,7 @@ Sergio Pulgarin Stephan Sokolow Thijs Triemstra Thomas van den Berg +tuxmaster vendryan Yaisel Hurtado ysuolmai diff --git a/examples/codepage_tables.py b/examples/codepage_tables.py index 7128942..9436910 100644 --- a/examples/codepage_tables.py +++ b/examples/codepage_tables.py @@ -47,14 +47,14 @@ def print_codepage(printer, codepage): # Table header printer.set(font="b") - printer._raw(" {}\n".format(sep.join(map(lambda s: hex(s)[2:], range(0, 16))))) + printer._raw(f" {sep.join(map(lambda s: hex(s)[2:], range(0, 16)))}\n") printer.set() # The table for x in range(0, 16): # First column printer.set(font="b") - printer._raw("{} ".format(hex(x)[2:])) + printer._raw(f"{hex(x)[2:]} ") printer.set() for y in range(0, 16): diff --git a/src/escpos/capabilities.py b/src/escpos/capabilities.py index e58ef47..5ae5110 100644 --- a/src/escpos/capabilities.py +++ b/src/escpos/capabilities.py @@ -17,9 +17,7 @@ logging.basicConfig() logger = logging.getLogger(__name__) pickle_dir = environ.get("ESCPOS_CAPABILITIES_PICKLE_DIR", mkdtemp()) -pickle_path = path.join( - pickle_dir, "{v}.capabilities.pickle".format(v=platform.python_version()) -) +pickle_path = path.join(pickle_dir, f"{platform.python_version()}.capabilities.pickle") # get a temporary file from importlib_resources if no file is specified in env file_manager = ExitStack() atexit.register(file_manager.close) @@ -113,9 +111,7 @@ class BaseProfile: """ font = {"a": 0, "b": 1}.get(font, font) if not str(font) in self.fonts: - raise NotSupported( - '"{}" is not a valid font in the current profile'.format(font) - ) + raise NotSupported(f'"{font}" is not a valid font in the current profile') return font def get_columns(self, font) -> int: @@ -159,7 +155,7 @@ def get_profile_class(name: str) -> Type[BaseProfile]: profiles: Dict[str, Any] = CAPABILITIES["profiles"] profile_data = profiles[name] profile_name = clean(name) - class_name = "{}{}Profile".format(profile_name[0].upper(), profile_name[1:]) + class_name = f"{profile_name[0].upper()}{profile_name[1:]}Profile" new_class = type(class_name, (BaseProfile,), {"profile_data": profile_data}) CLASS_CACHE[name] = new_class diff --git a/src/escpos/config.py b/src/escpos/config.py index ecdbee6..17a2328 100644 --- a/src/escpos/config.py +++ b/src/escpos/config.py @@ -80,9 +80,7 @@ class Config: if not self._printer_name or not hasattr(printer, self._printer_name): raise exceptions.ConfigSyntaxError( - 'Printer type "{printer_name}" is invalid'.format( - printer_name=self._printer_name, - ) + f'Printer type "{self._printer_name}" is invalid' ) self._has_loaded = True diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index f5500f6..742e602 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -219,7 +219,7 @@ class Escpos(object, metaclass=ABCMeta): max_width = int(self.profile.profile_data["media"]["width"]["pixels"]) if im.width > max_width: - raise ImageWidthError("{} > {}".format(im.width, max_width)) + raise ImageWidthError(f"{im.width} > {max_width}") if center: im.center(max_width) @@ -420,9 +420,7 @@ class Escpos(object, metaclass=ABCMeta): raise ValueError("Can only output 1-4 bytes") if not 0 <= inp_number <= max_input: raise ValueError( - "Number too large. Can only output up to {0} in {1} bytes".format( - max_input, out_bytes - ) + f"Number too large. Can only output up to {max_input} in {out_bytes} bytes" ) outp = b"" for _ in range(0, out_bytes): @@ -711,21 +709,15 @@ class Escpos(object, metaclass=ABCMeta): if not function_type or not BARCODE_TYPES.get(function_type.upper()): raise BarcodeTypeError( ( - "Barcode '{bc}' not valid for barcode function type " - "{function_type}" - ).format( - bc=bc, - function_type=function_type, + f"Barcode '{bc}' not valid for barcode function type " + f"{function_type}" ) ) bc_types = BARCODE_TYPES[function_type.upper()] if check and not self.check_barcode(bc, code): raise BarcodeCodeError( - ("Barcode '{code}' not in a valid format for type '{bc}'").format( - code=code, - bc=bc, - ) + f"Barcode '{code}' not in a valid format for type '{bc}'" ) # Align Bar Code() @@ -735,12 +727,12 @@ class Escpos(object, metaclass=ABCMeta): if 1 <= height <= 255: self._raw(BARCODE_HEIGHT + six.int2byte(height)) else: - raise BarcodeSizeError("height = {height}".format(height=height)) + raise BarcodeSizeError(f"height = {height}") # Width if 2 <= width <= 6: self._raw(BARCODE_WIDTH + six.int2byte(width)) else: - raise BarcodeSizeError("width = {width}".format(width=width)) + raise BarcodeSizeError(f"width = {width}") # Font if font.upper() == "B": self._raw(BARCODE_FONT_B) @@ -833,9 +825,7 @@ class Escpos(object, metaclass=ABCMeta): # Check if barcode type exists if barcode_type not in barcode.PROVIDED_BARCODES: raise BarcodeTypeError( - "Barcode type {} not supported by software barcode renderer".format( - barcode_type - ) + f"Barcode type {barcode_type} not supported by software barcode renderer" ) # Render the barcode @@ -877,7 +867,7 @@ class Escpos(object, metaclass=ABCMeta): :param txt: text to be printed with a newline :raises: :py:exc:`~escpos.exceptions.TextError` """ - self.text("{}\n".format(txt)) + self.text(f"{txt}\n") def ln(self, count: int = 1) -> None: """Print a newline or more. @@ -1427,14 +1417,14 @@ class EscposIO: lines = text else: lines = [ - "{0}".format(text), + f"{text}", ] # TODO check unicode handling # TODO flush? or on print? (this should prob rather be handled by the _raw-method) for line in lines: self.printer.set(**params) - self.printer.text("{0}\n".format(line)) + self.printer.text(f"{line}\n") def close(self) -> None: """Close printer. diff --git a/src/escpos/exceptions.py b/src/escpos/exceptions.py index 2b3e98c..04a53d9 100644 --- a/src/escpos/exceptions.py +++ b/src/escpos/exceptions.py @@ -74,7 +74,7 @@ class BarcodeTypeError(Error): def __str__(self) -> str: """Return string representation of BarcodeTypeError.""" - return "No Barcode type is defined ({msg})".format(msg=self.msg) + return f"No Barcode type is defined ({self.msg})" class BarcodeSizeError(Error): @@ -99,7 +99,7 @@ class BarcodeSizeError(Error): def __str__(self) -> str: """Return string representation of BarcodeSizeError.""" - return "Barcode size is out of range ({msg})".format(msg=self.msg) + return f"Barcode size is out of range ({self.msg})" class BarcodeCodeError(Error): @@ -124,7 +124,7 @@ class BarcodeCodeError(Error): def __str__(self) -> str: """Return string representation of BarcodeCodeError.""" - return "No Barcode code was supplied ({msg})".format(msg=self.msg) + return f"No Barcode code was supplied ({self.msg})" class ImageSizeError(Error): @@ -147,9 +147,7 @@ class ImageSizeError(Error): def __str__(self) -> str: """Return string representation of ImageSizeError.""" - return "Image height is longer than 255px and can't be printed ({msg})".format( - msg=self.msg - ) + return f"Image height is longer than 255px and can't be printed ({self.msg})" class ImageWidthError(Error): @@ -172,7 +170,7 @@ class ImageWidthError(Error): def __str__(self) -> str: """Return string representation of ImageWidthError.""" - return "Image width is too large ({msg})".format(msg=self.msg) + return f"Image width is too large ({self.msg})" class TextError(Error): @@ -196,9 +194,7 @@ class TextError(Error): def __str__(self) -> str: """Return string representation of TextError.""" - return "Text string must be supplied to the text() method ({msg})".format( - msg=self.msg - ) + return f"Text string must be supplied to the text() method ({self.msg})" class CashDrawerError(Error): @@ -222,7 +218,7 @@ class CashDrawerError(Error): def __str__(self) -> str: """Return string representation of CashDrawerError.""" - return "Valid pin must be set to send pulse ({msg})".format(msg=self.msg) + return f"Valid pin must be set to send pulse ({self.msg})" class TabPosError(Error): @@ -249,9 +245,7 @@ class TabPosError(Error): def __str__(self) -> str: """Return string representation of TabPosError.""" - return "Valid tab positions must be in the range 0 to 16 ({msg})".format( - msg=self.msg - ) + return f"Valid tab positions must be in the range 0 to 16 ({self.msg})" class CharCodeError(Error): @@ -275,7 +269,7 @@ class CharCodeError(Error): def __str__(self) -> str: """Return string representation of CharCodeError.""" - return "Valid char code must be set ({msg})".format(msg=self.msg) + return f"Valid char code must be set ({self.msg})" class DeviceNotFoundError(Error): @@ -347,7 +341,7 @@ class SetVariableError(Error): def __str__(self) -> str: """Return string representation of SetVariableError.""" - return "Set variable out of range ({msg})".format(msg=self.msg) + return f"Set variable out of range ({self.msg})" # Configuration errors @@ -374,7 +368,7 @@ class ConfigNotFoundError(Error): def __str__(self) -> str: """Return string representation of ConfigNotFoundError.""" - return "Configuration not found ({msg})".format(msg=self.msg) + return f"Configuration not found ({self.msg})" class ConfigSyntaxError(Error): @@ -398,7 +392,7 @@ class ConfigSyntaxError(Error): def __str__(self) -> str: """Return string representation of ConfigSyntaxError.""" - return "Configuration syntax is invalid ({msg})".format(msg=self.msg) + return f"Configuration syntax is invalid ({self.msg})" class ConfigSectionMissingError(Error): @@ -422,4 +416,4 @@ class ConfigSectionMissingError(Error): def __str__(self) -> str: """Return string representation of ConfigSectionMissingError.""" - return "Configuration section is missing ({msg})".format(msg=self.msg) + return f"Configuration section is missing ({self.msg})" diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index 1af3131..4318993 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -57,9 +57,9 @@ class Encoder: if encoding not in self.codepages: raise ValueError( ( - 'Encoding "{}" cannot be used for the current profile. ' - "Valid encodings are: {}" - ).format(encoding, ",".join(self.codepages.keys())) + f'Encoding "{encoding}" cannot be used for the current profile. ' + f'Valid encodings are: {",".join(self.codepages.keys())}' + ) ) return encoding @@ -88,7 +88,7 @@ class Encoder: # Non-encodable character, just skip it pass return encodable_chars - raise LookupError("Can't find a known encoding for {}".format(encoding)) + raise LookupError(f"Can't find a known encoding for {encoding}") def _get_codepage_char_map(self, encoding): """Get codepage character map. @@ -294,9 +294,7 @@ class MagicEncode: """Write the text and inject necessary codepage switches.""" if text is not None and type(text) is not str: raise Error( - "The supplied text has to be unicode, but is of type {type}.".format( - type=type(text) - ) + f"The supplied text has to be unicode, but is of type {type(text)}." ) # We always know the current code page; if the new codepage