Skip to content

Commit

Permalink
Improved TOML support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ircama committed Oct 21, 2024
1 parent 4a65352 commit c83205d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ This repository includes a Windows *epson_print_conf.exe* executable file which

Within a [report](https://codeberg.org/atufi/reinkpy/issues/12#issue-716809) in repo https://codeberg.org/atufi/reinkpy there is an interesting [attachment](https://codeberg.org/attachments/147f41a3-a6ea-45f6-8c2a-25bac4495a1d) which includes an extensive XML database of Epson model features.

The program *parse_devices.py* transforms this XML DB into the dictionary that *epson_print_conf.py* can use. It is also able to accept the TOML input format, if the `-T` option is used.
The program *parse_devices.py* transforms this XML DB into the dictionary that *epson_print_conf.py* can use. It is also able to accept the [TOML](https://toml.io/) input format used by [reinkpy](https://codeberg.org/atufi/reinkpy) in [epson.toml](https://codeberg.org/atufi/reinkpy/src/branch/main/reinkpy/epson.toml), if the `-T` option is used.

Here is a simple procedure to download that DB and run *parse_devices.py* to search for the XP-205 model and produce the related PRINTER_CONFIG dictionary to the standard output:

Expand All @@ -277,7 +277,7 @@ optional arguments:
-h, --help show this help message and exit
-m PRINTER_MODEL, --model PRINTER_MODEL
Filter printer model. Example: -m XP-205
-T, --toml Use TOML input format instead of XML
-T, --toml Use the Reinkpy TOML input format instead of XML
-l LINE_LENGTH, --line LINE_LENGTH
Set line length of the output (default: 120)
-i, --indent Indent output of 4 spaces
Expand All @@ -287,8 +287,8 @@ optional arguments:
-f, --full Generate additional tags
-e, --errors Add last_printer_fatal_errors
-c CONFIG_FILE, --config CONFIG_FILE
use the XML or TOML configuration file to generate the configuration; default
is 'devices.xml'
use the XML or the Reinkpy TOML configuration file to generate the configuration;
default is 'devices.xml'
-s DEFAULT_MODEL, --default_model DEFAULT_MODEL
Default printer model. Example: -s XP-205
-a HOSTNAME, --address HOSTNAME
Expand All @@ -305,7 +305,7 @@ optional arguments:
-S, --no_same_as Do not add "same-as" for similar printers with different names
-M, --no_maint_level Do not add "Maintenance required levelas" in "stats"
Generate printer configuration from devices.xml or from TOML
Generate printer configuration from devices.xml or from Reinkpy TOML
```

The program does not provide *printer_head_id* and *Power off timer*.
Expand Down
66 changes: 43 additions & 23 deletions parse_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def normalize_config(
logging.info("Number of obtained configuration entries: %s", len(config))
return config

def convert_toml(config):
def convert_toml(config, printer_model):
def hex_to_bytes(hex_value):
byte1 = (hex_value >> 8) & 0xFF
byte2 = hex_value & 0xFF
Expand All @@ -375,24 +375,41 @@ def parse_mem_entries(mem):

output_data = {}

for section_name, section_val in parsed_toml.items():
# Convert rkey and extract mem data
section_data = section_val[0]
read_key = hex_to_bytes(section_data["rkey"])
raw_waste_reset = parse_mem_entries(section_data["mem"])

main_model = section_data["models"][0]
alias = section_data["models"][1:]
if main_model in ["EPSON"] and alias:
main_model = alias.pop(0)

# Structure the section data in the desired format
output_data[main_model] = {
"read_key": read_key,
"write_key": section_data["wkey1"].encode(), # Write key in bytes
"raw_waste_reset": raw_waste_reset, # Raw waste reset dictionary
"alias": alias # Remaining models as aliases
}
for e_section_name, e_section_val in parsed_toml.items():
for section_data in e_section_val:
# Process "models"
if not "models" in section_data or not section_data["models"]:
continue
if printer_model and printer_model not in section_data["models"]:
continue
main_model = section_data["models"].pop(0)
output_data[main_model] = {}
alias = section_data["models"]
if alias:
output_data[main_model]["alias"] = alias

# Convert rkey and extract mem data
if "mem_low" in section_data:
output_data[main_model]["mem_low"] = section_data["mem_low"]
if "mem_high" in section_data:
output_data[main_model]["mem_high"] = section_data["mem_high"]
if "rlen" in section_data:
output_data[main_model]["rlen"] = section_data["rlen"]
if "wlen" in section_data:
output_data[main_model]["wlen"] = section_data["wlen"]
if "rkey" in section_data:
read_key = hex_to_bytes(section_data["rkey"])
output_data[main_model]["read_key"] = read_key
if "wkey1" in section_data:
output_data[main_model]["write_key"] = section_data["wkey1"].encode()
elif "wkey" in section_data:
output_data[main_model]["write_key"] = "".join(
[chr(0 if b == 0 else b - 1) for b in section_data["wkey"].encode()]
)

if "mem" in section_data:
raw_waste_reset = parse_mem_entries(section_data["mem"])
output_data[main_model]["raw_waste_reset"] = raw_waste_reset

return output_data

Expand All @@ -413,7 +430,7 @@ def main():
import pickle

parser = argparse.ArgumentParser(
epilog='Generate printer configuration from devices.xml or from TOML'
epilog='Generate printer configuration from devices.xml or from Reinkpy TOML'
)
parser.add_argument(
'-m',
Expand All @@ -428,7 +445,7 @@ def main():
'--toml',
dest='toml',
action='store_true',
help='Use TOML input format instead of XML'
help='Use the Reinkpy TOML input format instead of XML'
)
parser.add_argument(
'-l',
Expand Down Expand Up @@ -485,7 +502,7 @@ def main():
"--config",
dest='config_file',
type=argparse.FileType('r'),
help="use the XML or TOML configuration file to generate"
help="use the XML or the Reinkpy TOML configuration file to generate"
" the configuration; default is 'devices.xml'",
default=0,
nargs=1,
Expand Down Expand Up @@ -574,7 +591,10 @@ def main():
config = "devices.xml"

if args.toml:
printer_config = convert_toml(config)
printer_config = convert_toml(
config=config,
printer_model=args.printer_model
)
else:
printer_config = generate_config(
config=config,
Expand Down

0 comments on commit c83205d

Please sign in to comment.