-
Notifications
You must be signed in to change notification settings - Fork 1
/
genbalbizdel
executable file
·143 lines (128 loc) · 3.69 KB
/
genbalbizdel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/env python3
# Copyright 2020 Rémy Taymans <remytms@tsmail.eu>
# License GPL-3.0 or later (https://www.gnu.org/licenses/gpl.html).
"""This tool generate a file to delete product in bizerba scale."""
import argparse
import fileinput
import sys
import textwrap
from pathlib import Path
__version__ = "0.1.0"
PROGNAME = "genbalbizdel"
def main():
"""Program starts here"""
parser = create_parser()
args = parser.parse_args()
del_pattern = "S#{sysbal_id}#{prod_id}#"
output_path = Path(args.output).expanduser().resolve()
excluded_ids = set()
included_ids = set()
generated_ids = set()
try:
excluded_ids = parse_listid_file(args.exclude)
except (OSError, ValueError, TypeError) as err:
print(
"Error: Can not parse exclude file: {}".format(err),
file=sys.stderr
)
sys.exit(1)
try:
included_ids = parse_listid_file(args.include)
except (OSError, ValueError, TypeError) as err:
print(
"Error: Can not parse exclude file: {}".format(err),
file=sys.stderr
)
sys.exit(1)
if args.generate_from_to:
generated_ids = set(
range(
args.generate_from_to[0], args.generate_from_to[1] + 1
)
)
ids_to_delete = (included_ids | generated_ids) - excluded_ids
with output_path.open(
mode="w", encoding="cp1252", newline="\r\n"
) as output:
for prod_id in ids_to_delete:
print(
del_pattern.format(
sysbal_id=args.product_scale_group, prod_id=prod_id
),
file=output,
)
def create_parser():
"""Return parser for this script."""
parser = argparse.ArgumentParser(
prog=PROGNAME,
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(
"""
Generate a csv file that can be send to the bizerba scale in order to
delete the product on the scale.
If you want to generate a list for specific id, give it with --include
or use --generate-from-to options.
"""
),
)
parser.add_argument(
"output",
metavar="OUTPUT",
help=(
"output file"
),
)
parser.add_argument(
"--product-scale-group",
type=int,
metavar="ID",
required=True,
help=(
"the external ID of the product.scale.group associated to "
"the product id"
)
)
parser.add_argument(
"--include",
metavar="FILE",
help=(
"file containing one id per line. These id will be included "
"in the generated suppression file."
)
)
parser.add_argument(
"--exclude",
metavar="FILE",
help=(
"file containing one id per line. These id will be excluded "
"from the generated suppression file."
)
)
parser.add_argument(
"--generate-from-to",
type=int,
nargs=2,
metavar="ID_START ID_END",
help=(
"if specified, generate suppression from ID_START to ID_END "
"(included)"
)
)
parser.add_argument(
"--version", action="version",
version="%(prog)s {}".format(__version__)
)
return parser
def parse_listid_file(filename):
"""
Parse file as a file containing ids. It returns a set of ids.
"""
ids = set()
if filename:
with fileinput.input(files=[filename]) as file:
for line in file:
if line.strip():
ids.add(int(line.strip()))
return ids
if __name__ == "__main__":
main()