Skip to content

Commit

Permalink
feat: add initial project
Browse files Browse the repository at this point in the history
  • Loading branch information
pyduh committed Jan 20, 2023
0 parents commit f422f29
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 0 deletions.
86 changes: 86 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
*.fs
*.sqlite
*.sqlite3
*.log
*.tmp
uploads/
media/

log/
logs/

bower_components/
node_modules/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
.venv/
*.egg-info/
.installed.cfg
*.egg
.python-version

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.pytest_cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

#Ipython Notebook
.ipynb_checkpoints

# .env files
.env

# IDEs & Text Editors
.idea
*.swp

.vscode
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Changelog
---------

0.1.0
~~~~~

* Initial release
53 changes: 53 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
PROJECT_NAME=zpl_image_extractor

.PHONY: clean-pyc

clean-eggs:
@find . -name '*.egg' -print0|xargs -0 rm -rf --
@rm -rf .eggs/

clean-pyc:
@find . -iname '*.py[co]' -delete
@find . -iname '__pycache__' -delete
@find . -iname '.coverage' -delete
@rm -rf htmlcov/

pre-clean: clean-eggs clean-pyc
@find . -iname '*~' -delete
@find . -iname '*.swp' -delete
@find . -iname '__pycache__' -delete

post-clean:
@rm -rf build/
@rm $(PROJECT_NAME).spec
@rm dist/$(PROJECT_NAME)

test:
poetry run pytest -vv tests

test-cov:
poetry run pytest -vv --cov=loafer tests

cov:
poetry run coverage report -m

cov-report:
poetry run pytest -vv --cov=loafer --cov-report=html tests

check-fixtures:
poetry run pytest --dead-fixtures

changelog-preview:
@echo "\nmain ("$$(date '+%Y-%m-%d')")"
@echo "-------------------\n"
@git log $$(poetry version -s)...main --oneline --reverse


version = `cat CHANGES.rst | awk '/^[0-9]+\.[0-9]+(\.[0-9]+)?/' | head -n1`
bump_version:
poetry version $(version)

bump_binary: bump_version pre-clean
pyinstaller --onefile --name $(PROJECT_NAME) $(PROJECT_NAME)/cli.py
@tar -C dist/ -czf dist/$(PROJECT_NAME).$(version).tar.gz $(PROJECT_NAME)
@make post-clean
Binary file added dist/zpl_image_extractor.0.1.0.tar.gz
Binary file not shown.
251 changes: 251 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "zpl-image-extractor"
version = "0.1.0"
description = ""
authors = ["pyduh <[email protected]>"]
readme = "README.md"
packages = [{include = "zpl_image_extractor"}]

[tool.poetry.dependencies]
python = "~3.11"
Pillow = "^9.4.0"
click = "^8.1.3"
pyinstaller = "^5.7.0"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Empty file added tests/__init__.py
Empty file.
Empty file added zpl_image_extractor/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions zpl_image_extractor/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
import click

from zpl_image_extractor.utils import normalise_zpl
from zpl_image_extractor.zpl import ZplLine

@click.group()
def entry():
...

@entry.command()
@click.option("--zpl-line", help="String with zpl instruction")
@click.option("--output", default="output.png", help="File path output with generated image")
def convert_zpl_line_to_png(zpl_line, output):
zpl_line = ZplLine.build(line=zpl_line)
file_path = zpl_line.to_image(file_path=output)
click.echo(file_path)


@entry.command()
@click.option("--zpl", help="Path to zpl")
def tokenize_zpl(zpl):
with open(zpl, 'r') as file:
click.echo(normalise_zpl(file.read()))


if __name__ == "__main__":
if getattr(sys, "frozen", False):
entry(sys.argv[1:])
else:
entry()
5 changes: 5 additions & 0 deletions zpl_image_extractor/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import re


CRC_CCITT_TABLE = None
RE_COMPRESSED = re.compile(r'[G-Zg-z]+.')
51 changes: 51 additions & 0 deletions zpl_image_extractor/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from ctypes import c_ushort

from .constants import CRC_CCITT_TABLE, RE_COMPRESSED


def _calculate_crc_ccitt(data):
"""
All CRC stuff ripped from PyCRC, GPLv3 licensed
"""
global CRC_CCITT_TABLE
if not CRC_CCITT_TABLE:
crc_ccitt_table = []
for i in range(0, 256):
crc = 0
c = i << 8

for j in range(0, 8):
if (crc ^ c) & 0x8000:
crc = c_ushort(crc << 1).value ^ 0x1021
else:
crc = c_ushort(crc << 1).value

c = c_ushort(c << 1).value

crc_ccitt_table.append(crc)
CRC_CCITT_TABLE = crc_ccitt_table

is_string = isinstance(data, str)
crc_value = 0x0000 # XModem version

for c in data:
d = ord(c) if is_string else c
tmp = ((crc_value >> 8) & 0xff) ^ d
crc_value = ((crc_value << 8) & 0xff00) ^ CRC_CCITT_TABLE[tmp]

return crc_value


def calc_crc(data):
return '%04X' % _calculate_crc_ccitt(data)


def chunked(value, n):
for i in range(0, len(value), n):
yield value[i:i+n]


def normalise_zpl(zpl):
zpl = zpl.replace('\n', '').replace('\r', '')
zpl = zpl.replace('^', '\n^').replace('~', '\n~')
return zpl.split('\n')
Loading

0 comments on commit f422f29

Please sign in to comment.