changeset 31:f77ca0963d6d 0.2.0

Fix language bug, convert to pyproject.toml
author Ben Croston <ben@croston.org>
date Wed, 12 Feb 2025 15:13:51 +0000
parents ddc251fb92aa
children 405f0751f09a
files .hgignore CHANGELOG.rst INSTALL LICENCE MANIFEST.in pyproject.toml setup.py zebra.py
diffstat 8 files changed, 106 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Sep 01 15:57:55 2020 +0100
+++ b/.hgignore	Wed Feb 12 15:13:51 2025 +0000
@@ -3,4 +3,7 @@
 *.*~
 dist/*
 build/*
-MANIFEST
+*.swp
+.venv/*
+uv.lock
+.ruff_cache/*
--- a/CHANGELOG.rst	Tue Sep 01 15:57:55 2020 +0100
+++ b/CHANGELOG.rst	Wed Feb 12 15:13:51 2025 +0000
@@ -1,6 +1,12 @@
 Changelog
 =========
 
+0.2.0
+-----
+- Fix language bug when using lpstat
+- Convert project to use pyproject.toml
+- Linted using ruff
+
 0.1.0
 -----
 - class name is now 'Zebra' instead of 'zebra'
--- a/INSTALL	Tue Sep 01 15:57:55 2020 +0100
+++ b/INSTALL	Wed Feb 12 15:13:51 2025 +0000
@@ -1,4 +1,4 @@
-To install, use pip:
+To install, using pip:
   $ pip install zebra
 
 If an alpha version is required:
--- a/LICENCE	Tue Sep 01 15:57:55 2020 +0100
+++ b/LICENCE	Wed Feb 12 15:13:51 2025 +0000
@@ -1,4 +1,4 @@
-Copyright (c) 2011-2020 Ben Croston
+Copyright (c) 2011-2025 Ben Croston
 
 Permission is hereby granted, free of charge, to any person obtaining a copy of
 this software and associated documentation files (the "Software"), to deal in
--- a/MANIFEST.in	Tue Sep 01 15:57:55 2020 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-include MANIFEST.in INSTALL README.rst LICENCE CHANGELOG.rst
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyproject.toml	Wed Feb 12 15:13:51 2025 +0000
@@ -0,0 +1,54 @@
+[project]
+name = "zebra"
+version = "0.2.0"
+description = "A package to communicate with (Zebra) label printers"
+authors = [
+  { name = "Ben Croston", email = "ben@croston.org" },
+]
+maintainers = [
+  { name = "Ben Croston", email = "ben@croston.org" },
+]
+license = "MIT"
+#license-files = ["LICEN[CS]E*"]
+readme = "README.rst"
+requires-python = ">=3.8"
+classifiers = [
+  "Development Status :: 5 - Production/Stable",
+  "Operating System :: Microsoft :: Windows",
+  "Operating System :: Unix",
+  "Operating System :: MacOS :: MacOS X",
+  "License :: OSI Approved :: MIT License",
+  "Intended Audience :: Developers",
+  "Programming Language :: Python :: 3",
+  "Topic :: Printing",
+]
+dependencies = [
+  "pywin32; platform_system == 'Windows'",
+]
+
+[project.urls]
+homepage = "https://www.wyre-it.co.uk/zebra/"
+
+[dependency-groups]
+dev = [
+    "ruff>=0.8.4",
+]
+
+[tool.ruff.format]
+quote-style = "single"
+indent-style = "space"
+
+[build-system]
+requires = ["hatchling"]
+build-backend = "hatchling.build"
+
+[tool.hatch.build.targets.sdist]
+exclude = [
+  ".hgtags",
+]
+
+#[[tool.uv.index]]
+#name = "testpypi"
+#url = "https://test.pypi.org/simple/"
+#publish-url = "https://test.pypi.org/legacy/"
+
--- a/setup.py	Tue Sep 01 15:57:55 2020 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#!/usr/bin/env python
-import sys
-from setuptools import setup
-
-classifiers = ['Development Status :: 5 - Production/Stable',
-               'Operating System :: Microsoft :: Windows',
-               'Operating System :: Unix',
-               "Operating System :: MacOS :: MacOS X",
-               'License :: OSI Approved :: MIT License',
-               'Intended Audience :: Developers',
-               'Programming Language :: Python :: 3',
-               'Topic :: Printing']
-
-setup(name             = 'zebra',
-      version          = '0.1.0',
-      py_modules       = ['zebra'],
-      author           = 'Ben Croston',
-      author_email     = 'ben@croston.org',
-      maintainer       = 'Ben Croston',
-      maintainer_email = 'ben@croston.org',
-      url              = 'http://www.wyre-it.co.uk/zebra/',
-      description      = 'A package to communicate with (Zebra) label printers',
-      long_description = open('README.rst').read() + '\n' + open('CHANGELOG.rst').read(),
-      long_description_content_type = 'text/x-rst',
-      platforms        = 'Windows, Unix, MacOSX',
-      classifiers      = classifiers,
-      license          = 'MIT',
-      install_requires = ["pywin32;platform_system=='Windows'"])
--- a/zebra.py	Tue Sep 01 15:57:55 2020 +0100
+++ b/zebra.py	Wed Feb 12 15:13:51 2025 +0000
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-# Copyright (c) 2011-2020 Ben Croston
+# Copyright (c) 2011-2025 Ben Croston
 #
 # Permission is hereby granted, free of charge, to any person obtaining a copy of
 # this software and associated documentation files (the "Software"), to deal in
@@ -30,6 +30,7 @@
     IS_WINDOWS = False
     import subprocess
 
+
 class Zebra:
     """A class to communicate with (Zebra) label printers"""
 
@@ -39,9 +40,11 @@
 
     def _output_unix(self, commands):
         if self.queue == 'zebra_python_unittest':
-            p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE)
+            p = subprocess.Popen(['cat', '-'], stdin=subprocess.PIPE)
         else:
-            p = subprocess.Popen(['lpr','-P{}'.format(self.queue),'-oraw'], stdin=subprocess.PIPE)
+            p = subprocess.Popen(
+                ['lpr', '-P{}'.format(self.queue), '-oraw'], stdin=subprocess.PIPE
+            )
         p.communicate(commands)
         p.stdin.close()
 
@@ -51,7 +54,7 @@
             return
         hPrinter = win32print.OpenPrinter(self.queue)
         try:
-            hJob = win32print.StartDocPrinter(hPrinter, 1, ('Label',None,'RAW'))
+            win32print.StartDocPrinter(hPrinter, 1, ('Label', None, 'RAW'))
             try:
                 win32print.StartPagePrinter(hPrinter)
                 win32print.WritePrinter(hPrinter, commands)
@@ -68,7 +71,7 @@
         encoding - Encoding used if 'commands' is not a byte string
         """
         assert self.queue is not None
-        if type(commands) != bytes:
+        if not isinstance(commands, bytes):
             commands = str(commands).encode(encoding=encoding)
         if IS_WINDOWS:
             self._output_win(commands)
@@ -84,7 +87,11 @@
     def _getqueues_unix(self):
         queues = []
         try:
-            output = subprocess.check_output(['lpstat','-p'], universal_newlines=True)
+            output = subprocess.check_output(
+                ['lpstat', '-p'],
+                universal_newlines=True,
+                env={"LANG": "en_US.UTF-8"}
+            )
         except subprocess.CalledProcessError:
             return []
         for line in output.split('\n'):
@@ -94,7 +101,7 @@
 
     def _getqueues_win(self):
         printers = []
-        for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL):
+        for a, b, name, d in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL):
             printers.append(name)
         return printers
 
@@ -121,9 +128,9 @@
         if direct_thermal:
             commands += 'OD\n'
         if label_height:
-           commands += 'Q%s,%s\n'%(label_height[0],label_height[1])
+            commands += 'Q%s,%s\n' % (label_height[0], label_height[1])
         if label_width:
-            commands += 'q%s\n'%label_width
+            commands += 'q%s\n' % label_width
         self.output(commands)
 
     def reset_default(self):
@@ -135,10 +142,10 @@
         self.output('\n^@\n')
 
     def autosense(self):
-         """Run AutoSense by sending an EPL2 command
-         Get the printer to detect label and gap length and set the sensor levels
-         """
-         self.output('\nxa\n')
+        """Run AutoSense by sending an EPL2 command
+        Get the printer to detect label and gap length and set the sensor levels
+        """
+        self.output('\nxa\n')
 
     def store_graphic(self, name, filename):
         """Store a 1 bit PCX file on the label printer, using EPL2.
@@ -147,12 +154,12 @@
         filename - local filename
         """
         assert filename.lower().endswith('.pcx')
-        commands = '\nGK"%s"\n'%name
-        commands += 'GK"%s"\n'%name
+        commands = '\nGK"%s"\n' % name
+        commands += 'GK"%s"\n' % name
         size = os.path.getsize(filename)
-        commands += 'GM"%s"%s\n'%(name,size)
+        commands += 'GM"%s"%s\n' % (name, size)
         self.output(commands)
-        self.output(open(filename,'rb').read())
+        self.output(open(filename, 'rb').read())
 
     def print_graphic(self, x, y, width, length, data, qty):
         """Print a label from 1 bit data, using EPL2
@@ -163,18 +170,28 @@
         data   - raw graphical data, in bytes
         qty    - number of labels to print
         """
-        assert type(data) == bytes
+        assert isinstance(data, bytes)
         assert width % 8 == 0  # make sure width is a multiple of 8
-        assert (width//8) * length == len(data)
-        commands = b"\nN\nGW%d,%d,%d,%d,%s\nP%d\n"%(x, y, width//8, length, data, qty)
+        assert (width // 8) * length == len(data)
+        commands = b'\nN\nGW%d,%d,%d,%d,%s\nP%d\n' % (
+            x,
+            y,
+            width // 8,
+            length,
+            data,
+            qty,
+        )
         self.output(commands)
 
+
 if __name__ == '__main__':
     z = Zebra()
-    print('Printer queues found:',z.getqueues())
+    print('Printer queues found:', z.getqueues())
     z.setqueue('zebra_python_unittest')
-    z.setup(direct_thermal=True, label_height=(406,32), label_width=609)    # 3" x 2" direct thermal label
-    z.store_graphic('logo','logo.pcx')
+    z.setup(
+        direct_thermal=True, label_height=(406, 32), label_width=609
+    )  # 3" x 2" direct thermal label
+    z.store_graphic('logo', 'logo.pcx')
     label = """
 N
 GG419,40,"logo"
@@ -185,4 +202,3 @@
 P1
 """
     z.output(label)
-