error handling and refactor
This commit is contained in:
parent
b16d72415e
commit
2cfe957473
|
@ -10,6 +10,7 @@ Generate label and print them via brother_ql to
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
`brother_ql print -l 62 Untitled.png`
|
`brother_ql print -l 62 Untitled.png`
|
||||||
|
|
||||||
## Printer/Paper Info ##
|
## Printer/Paper Info ##
|
||||||
|
@ -23,5 +24,6 @@ Paper With: 62mm or 696px
|
||||||
|
|
||||||
see https://github.com/pklaus/brother_ql
|
see https://github.com/pklaus/brother_ql
|
||||||
|
|
||||||
|
pip install qrcode
|
||||||
|
|
||||||
|
|
||||||
|
|
98
layouter.py
98
layouter.py
|
@ -3,31 +3,18 @@
|
||||||
|
|
||||||
""" Author: Hendrik Schutter, localhorst@mosad.xyz
|
""" Author: Hendrik Schutter, localhorst@mosad.xyz
|
||||||
Date of creation: 2022/11/16
|
Date of creation: 2022/11/16
|
||||||
Date of last modification: 2022/11/16
|
Date of last modification: 2022/11/23
|
||||||
|
|
||||||
pip install qrcode
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
import subprocess
|
|
||||||
import shlex
|
|
||||||
import shutil
|
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import glob
|
import glob
|
||||||
import PIL
|
|
||||||
from PIL import Image
|
|
||||||
from PIL import ImageFont
|
|
||||||
from PIL import ImageDraw
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import qrcode
|
import qrcode
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import ImageFont
|
||||||
|
from PIL import ImageDraw
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class DriveData:
|
class DriveData:
|
||||||
|
@ -64,13 +51,6 @@ class ReHddInfo:
|
||||||
class DriveDataJson:
|
class DriveDataJson:
|
||||||
drive: DriveData
|
drive: DriveData
|
||||||
rehdd: ReHddInfo
|
rehdd: ReHddInfo
|
||||||
|
|
||||||
rehdd_info = ReHddInfo("https://git.mosad.xyz/localhorst/reHDD", "bV0.2.2") # read this from rehdd process
|
|
||||||
|
|
||||||
temp_drive = DriveData(drive_index=0, drive_state="shredded", modelfamiliy="Toshiba 2.5\\ HDD MK..65GSSX",\
|
|
||||||
modelname="TOSHIBA MK3265GSDX", capacity=343597383680, serialnumber="YG6742U56UDRL123", power_on_hours=7074,\
|
|
||||||
power_cycle=4792, smart_error_count=1, shred_timestamp=1647937421, shred_duration=81718)
|
|
||||||
|
|
||||||
|
|
||||||
def get_font_path_regular():
|
def get_font_path_regular():
|
||||||
path = "/usr/share/fonts"
|
path = "/usr/share/fonts"
|
||||||
|
@ -99,17 +79,6 @@ def cut_string(max_lenght, data):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def format_to_printable(drive):
|
def format_to_printable(drive):
|
||||||
|
|
||||||
#print(cut_string(20, re.sub(r"[^a-zA-Z0-9. ]", "", drive.modelfamiliy)))
|
|
||||||
#print(cut_string(20, re.sub(r"[^a-zA-Z0-9. ]", "", drive.modelname)))
|
|
||||||
#print(cut_string(20, human_readable_capacity(drive.capacity)))
|
|
||||||
#print(cut_string(20, re.sub(r"[^a-zA-Z0-9.-_]", "", drive.serialnumber)))
|
|
||||||
#print(cut_string(30, human_readable_power_on_hours(drive.power_on_hours)))
|
|
||||||
#print(cut_string(10, str(drive.power_cycle)))
|
|
||||||
#print(cut_string(10, str(drive.smart_error_count)))
|
|
||||||
#print(cut_string(30, datetime.datetime.utcfromtimestamp(drive.shred_timestamp).strftime('%Y-%m-%d %H:%M:%S')))
|
|
||||||
#print(cut_string(30, str(datetime.timedelta(seconds = drive.shred_duration))))
|
|
||||||
|
|
||||||
return DriveDataPrintable(
|
return DriveDataPrintable(
|
||||||
cut_string(20, re.sub(r"[^a-zA-Z0-9. ]", "", drive.modelfamiliy)),\
|
cut_string(20, re.sub(r"[^a-zA-Z0-9. ]", "", drive.modelfamiliy)),\
|
||||||
cut_string(20, re.sub(r"[^a-zA-Z0-9. ]", "", drive.modelname)),\
|
cut_string(20, re.sub(r"[^a-zA-Z0-9. ]", "", drive.modelname)),\
|
||||||
|
@ -122,8 +91,13 @@ def format_to_printable(drive):
|
||||||
cut_string(30, str(datetime.timedelta(seconds = drive.shred_duration))))
|
cut_string(30, str(datetime.timedelta(seconds = drive.shred_duration))))
|
||||||
|
|
||||||
def draw_text(drawable, printable_data, text_x_offset):
|
def draw_text(drawable, printable_data, text_x_offset):
|
||||||
font_file_regular = get_font_path_regular()
|
try:
|
||||||
font_file_bold = get_font_path_bold()
|
font_file_regular = get_font_path_regular()
|
||||||
|
font_file_bold = get_font_path_bold()
|
||||||
|
except Exception as ex:
|
||||||
|
print("unable to find font: " + str(ex))
|
||||||
|
return
|
||||||
|
|
||||||
font_size = 20
|
font_size = 20
|
||||||
text_y_offset = 10
|
text_y_offset = 10
|
||||||
text_y_offset_increment = 25
|
text_y_offset_increment = 25
|
||||||
|
@ -172,27 +146,57 @@ def draw_qr_code(image, data):
|
||||||
image.paste(qr_img, (7, 7))
|
image.paste(qr_img, (7, 7))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def generate_image(drive, rehdd_info, output_file):
|
||||||
output_width = 696
|
output_width = 696 #in px set by used paper
|
||||||
output_height = 300
|
output_height = 300 #in px
|
||||||
|
text_x_offset= 300 #in px
|
||||||
|
|
||||||
printable_data = format_to_printable(temp_drive)
|
qr_data = DriveDataJson(drive, rehdd_info)
|
||||||
|
try:
|
||||||
qr_data = DriveDataJson(temp_drive, rehdd_info)
|
json_qr_daten = json.dumps(dataclasses.asdict(qr_data))
|
||||||
json_qr_daten = json.dumps(dataclasses.asdict(qr_data))
|
except Exception as ex:
|
||||||
|
print("unable to generate json: " + str(ex))
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
printable_data = format_to_printable(drive)
|
||||||
|
except Exception as ex:
|
||||||
|
print("unable to format data: " + str(ex))
|
||||||
|
return
|
||||||
|
|
||||||
#print(printable_data)
|
#print(printable_data)
|
||||||
|
|
||||||
text_x_offset= 300
|
#create black and white (binary) image with white background
|
||||||
|
|
||||||
output_image = Image.new('1', (output_width, output_height), "white")
|
output_image = Image.new('1', (output_width, output_height), "white")
|
||||||
|
|
||||||
|
#create draw pane
|
||||||
draw = ImageDraw.Draw(output_image)
|
draw = ImageDraw.Draw(output_image)
|
||||||
|
|
||||||
draw_outline(draw, 1, 4, output_width, output_height)
|
draw_outline(draw, 1, 4, output_width, output_height)
|
||||||
draw_text(draw, printable_data, text_x_offset)
|
draw_text(draw, printable_data, text_x_offset)
|
||||||
draw_qr_code(output_image, str(json_qr_daten).replace(" ", ""))
|
draw_qr_code(output_image, str(json_qr_daten).replace(" ", ""))
|
||||||
|
|
||||||
output_image.save("output.png")
|
output_image.save(output_file)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
rehdd_info = ReHddInfo("https://git.mosad.xyz/localhorst/reHDD", "bV0.2.2") # read this from rehdd process
|
||||||
|
|
||||||
|
temp_drive = DriveData(
|
||||||
|
drive_index=0,\
|
||||||
|
drive_state="shredded",\
|
||||||
|
modelfamiliy="Toshiba 2.5\\ HDD MK..65GSSX",\
|
||||||
|
modelname="TOSHIBA MK3265GSDX",\
|
||||||
|
capacity=343597383680,\
|
||||||
|
serialnumber="YG6742U56UDRL123",\
|
||||||
|
power_on_hours=7074,\
|
||||||
|
power_cycle=4792,\
|
||||||
|
smart_error_count=1,\
|
||||||
|
shred_timestamp=1647937421,\
|
||||||
|
shred_duration=81718)
|
||||||
|
|
||||||
|
generate_image(temp_drive, rehdd_info, "output.png")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
""" Author: Hendrik Schutter, localhorst@mosad.xyz
|
||||||
|
Date of creation: 2022/11/23
|
||||||
|
Date of last modification: 2022/11/23
|
||||||
|
"""
|
||||||
|
|
||||||
|
import layouter
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
rehdd_info = layouter.ReHddInfo("https://git.mosad.xyz/localhorst/reHDD", "bV0.2.2") # read this from rehdd process
|
||||||
|
|
||||||
|
temp_drive = layouter.DriveData(
|
||||||
|
drive_index=0,\
|
||||||
|
drive_state="shredded",\
|
||||||
|
modelfamiliy="Toshiba 2.5\\ HDD MK..65GSSX",\
|
||||||
|
modelname="TOSHIBA MK3265GSDX",\
|
||||||
|
capacity=343597383680,\
|
||||||
|
serialnumber="YG6742U56UDRL123",\
|
||||||
|
power_on_hours=7074,\
|
||||||
|
power_cycle=4792,\
|
||||||
|
smart_error_count=1,\
|
||||||
|
shred_timestamp=1647937421,\
|
||||||
|
shred_duration=81718)
|
||||||
|
|
||||||
|
layouter.generate_image(temp_drive, rehdd_info, "output.png")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue