patautoedit 1.17.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ # PatAutoEdit source package
2
+
3
+ __version__ = "1.17.0"
4
+ #: Version shown in the UIs and the CLI.
5
+ version = "V" + __version__
@@ -0,0 +1,6 @@
1
+ import sys
2
+
3
+ from patautoedit.cli import main
4
+
5
+ if __name__ == "__main__":
6
+ sys.exit(main())
@@ -0,0 +1,219 @@
1
+ '''
2
+ ATP file handling module
3
+ '''
4
+ import csv
5
+ import re
6
+ from typing import Dict, List
7
+ from patautoedit.file_ops import openfile
8
+ from patautoedit.utils import process_input_cycles
9
+ from patautoedit.logger import Logger
10
+ csv.field_size_limit(100000000)
11
+
12
+ #: Processing modes the tool understands. Single source of truth for the
13
+ #: configuration parser, the validators and every mode picker in the UIs.
14
+ MODE_LIST = ['DSSC Capture', 'DSSC Source', 'CMEM/HRAM Capture',
15
+ 'Expand Pattern', 'Expand Repeat', 'Expand Loop', 'Compress Pattern', 'WFLAG',
16
+ 'Add Opcode', 'Remove Opcode']
17
+
18
+ #: Modes that need a pin name, a cycle range, or an opcode string to do anything.
19
+ _PIN_MODES = ('DSSC Capture', 'DSSC Source', 'CMEM/HRAM Capture')
20
+ _CYCLE_MODES = _PIN_MODES + ('WFLAG', 'Add Opcode')
21
+ _USER_STRING_MODES = ('Add Opcode', 'Remove Opcode')
22
+ #: Modes whose optional User String holds preloaded counter run counts.
23
+ COUNTER_MODES = ('Expand Pattern', 'Expand Repeat', 'Expand Loop')
24
+
25
+ #: Columns a row-per-op config must carry, matched case-insensitively.
26
+ _REQUIRED_COLUMNS = ('pattern', 'process type', 'pin or method', 'cycle')
27
+ #: Columns that are read but optional (blank when absent).
28
+ _OPTIONAL_COLUMNS = ('user string',)
29
+ #: Columns that are accepted and ignored.
30
+ _IGNORED_COLUMNS = ('instance', 'status')
31
+
32
+
33
+ class ConfigError(Exception):
34
+ """Raised when a configuration file cannot be used, with a user-facing message."""
35
+
36
+
37
+ def missing_requirements(op: Dict) -> List[str]:
38
+ """Return the fields an operation is missing, given its mode.
39
+
40
+ An empty list means the operation has everything its mode needs.
41
+ """
42
+ mode = op.get("Mode", "")
43
+ if mode not in MODE_LIST:
44
+ return ["Process Type " + repr(mode) + " is not a known mode (valid: " +
45
+ ", ".join(MODE_LIST) + ")"]
46
+ missing = []
47
+ if mode in _PIN_MODES and not op.get("PinName"):
48
+ missing.append("Pin or Method")
49
+ if mode in _CYCLE_MODES and not op.get("CycleRange"):
50
+ missing.append("Cycle")
51
+ if mode in _USER_STRING_MODES and not op.get("UserString"):
52
+ missing.append("User String")
53
+ return missing
54
+
55
+ def read_csv(input_path: str) -> Dict:
56
+ """Read CSV file containing cycle ranges"""
57
+ cycle_range = {}
58
+ with openfile(input_path) as f:
59
+ f_csv = csv.reader(f)
60
+ for row in f_csv:
61
+ if len(row) > 0:
62
+ if len(row[0]) > 0 or len(row[1]) > 0:
63
+ cycle_range[row[0]] = process_input_cycles(row[1])
64
+ else:
65
+ break
66
+ else:
67
+ break
68
+ return cycle_range
69
+
70
+ def build_config_from_classical(csv_file: str, mode: str, pin_name: str,
71
+ time_mode: str, index_mode: str,
72
+ user_string: str = "") -> List[Dict]:
73
+ """Convert classical 2-column CSV + GUI settings into standard config list.
74
+
75
+ This adapter function allows the classical interface to use the same
76
+ processing code path as the simplified interface (process_atp_simple).
77
+
78
+ Args:
79
+ csv_file: Path to the 2-column CSV (filename, cycle_range)
80
+ mode: Processing mode from GUI (e.g., 'DSSC Capture')
81
+ pin_name: Pin name from GUI
82
+ time_mode: 'Single' or 'Dual' from GUI
83
+ index_mode: 'Cycle' or 'Vector' from GUI
84
+ user_string: Opcode text for 'Add Opcode' / 'Remove Opcode', or
85
+ counter run counts for the Expand modes (e.g. 'c0=<count>; c7=<count>')
86
+
87
+ Returns:
88
+ List of config dicts in the same format as analyse_merge_config output
89
+ """
90
+ cycle_ranges = read_csv(csv_file)
91
+ config_list = []
92
+ for filename, cycles in cycle_ranges.items():
93
+ config_list.append({
94
+ "ATPFile": filename,
95
+ "CycleRange": cycles,
96
+ "Mode": mode,
97
+ "PinName": pin_name,
98
+ "UserString": user_string,
99
+ "TimeMode": time_mode,
100
+ "IndexMode": index_mode
101
+ })
102
+ return config_list
103
+
104
+ def read_pinmap(pinmap_dir: str) -> Dict:
105
+ """Read pin mapping file"""
106
+ pin_dict = {}
107
+ line_cnt = 0
108
+ with open(pinmap_dir, mode='r') as f:
109
+ f_csv = csv.reader(f, delimiter="\t")
110
+ for row in f_csv:
111
+ if len(row) >= 2 and line_cnt >= 3:
112
+ if row[1] != "":
113
+ key = row[1]
114
+ if key in pin_dict.keys():
115
+ pin_dict[key].append(row[2])
116
+ else:
117
+ pin_dict[key] = [row[2]]
118
+ line_cnt += 1
119
+ return pin_dict
120
+
121
+ def analyse_merge_config(merge_config_file: str, logger: Logger) -> List[Dict]:
122
+ """Read a row-per-op configuration file into a config list.
123
+
124
+ One row is one operation. Rows sharing a `Pattern` form a single chain and
125
+ are applied in row order, each operation consuming the previous one's
126
+ output.
127
+
128
+ Columns are matched by name, case-insensitively and ignoring surrounding
129
+ whitespace, so column order does not matter. `_REQUIRED_COLUMNS` must all be
130
+ present; `_IGNORED_COLUMNS` are accepted and not read; anything else is
131
+ reported and ignored. Blank rows are skipped wherever they appear.
132
+
133
+ Raises:
134
+ ConfigError: if the file is not usable in this format.
135
+ """
136
+ def fail(message):
137
+ raise ConfigError(f"{merge_config_file}: {message}")
138
+
139
+ with openfile(merge_config_file) as f:
140
+ rows = [row for row in csv.reader(f) if any(cell.strip() for cell in row)]
141
+
142
+ if not rows:
143
+ fail("the file is empty")
144
+
145
+ header = [cell.strip().lower() for cell in rows[0]]
146
+
147
+ if len(header) >= 9:
148
+ sample = ','.join(rows[1] if len(rows) > 1 else rows[0])
149
+ fail(
150
+ "this looks like the old 9-column format, which is no longer supported. "
151
+ "Each operation now gets its own row, so a row like\n"
152
+ f" {sample}\n"
153
+ "becomes one row per operation:\n"
154
+ " Pattern,Process Type,Pin or Method,Cycle\n"
155
+ " sample.atp,DSSC Capture,TDO,[7-7];[12-12]\n"
156
+ " sample.atp,DSSC Capture,PORN,[13-14]"
157
+ )
158
+
159
+ missing = [c for c in _REQUIRED_COLUMNS if c not in header]
160
+ if missing:
161
+ fail("missing required column(s): " + ", ".join(missing) +
162
+ ". Required: " + ", ".join(_REQUIRED_COLUMNS) + " (case-insensitive)")
163
+
164
+ unknown = [c for c in header
165
+ if c and c not in _REQUIRED_COLUMNS + _OPTIONAL_COLUMNS + _IGNORED_COLUMNS]
166
+ if unknown:
167
+ logger.warning("Ignoring unrecognised column(s): " + ", ".join(unknown))
168
+
169
+ columns = {name: header.index(name) for name in header
170
+ if name in _REQUIRED_COLUMNS + _OPTIONAL_COLUMNS}
171
+
172
+ config_list = []
173
+ for line_no, row in enumerate(rows[1:], start=2):
174
+ def cell(name):
175
+ i = columns.get(name)
176
+ return row[i].strip() if i is not None and i < len(row) else ""
177
+
178
+ pattern, mode = cell('pattern'), cell('process type')
179
+ if not pattern or not mode:
180
+ fail(f"line {line_no}: Pattern and Process Type are both required")
181
+
182
+ cycle_text = cell('cycle')
183
+ try:
184
+ cycles = process_input_cycles(cycle_text) if cycle_text else []
185
+ except Exception:
186
+ fail(f"line {line_no}: cannot parse Cycle {cycle_text!r}; expected e.g. [7-7];[12-12]")
187
+
188
+ config_list.append({
189
+ "ATPFile": pattern,
190
+ "CycleRange": cycles,
191
+ "Mode": mode,
192
+ "PinName": cell('pin or method'),
193
+ "UserString": cell('user string'),
194
+ "TimeMode": "Single",
195
+ "IndexMode": "Cycle",
196
+ })
197
+
198
+ if not config_list:
199
+ fail("no operations found (the file has a header but no data rows)")
200
+
201
+ return config_list
202
+
203
+ def find_pin_index(mode:str, pin_names: str, str_line: str, logger: Logger) -> List[int]:
204
+ """Find pin indices in line"""
205
+ pin_name = pin_names.split(",")
206
+ str_line = str_line.replace(' ', '')
207
+ pattern = re.compile(r'(?<=\,)\s*([^\,^\s]+)(?=[\,\)])')
208
+ tmparray = re.findall(pattern, str_line)
209
+ index = []
210
+
211
+ for i in range(len(pin_name)):
212
+ for x in range(len(tmparray)):
213
+ if tmparray[x] == pin_name[i]:
214
+ index.append(x)
215
+
216
+ if (len(index) != len(pin_name)) and (mode not in ['WFLAG', 'Add Opcode']):
217
+ index = []
218
+ logger.error("Cannot find all given pins")
219
+ return index
patautoedit/cli.py ADDED
@@ -0,0 +1,249 @@
1
+ """Command-line interface for PatAutoEdit.
2
+
3
+ patautoedit run CONFIG.csv [--atp-dir DIR | --atp FILE ...] [--pinmap FILE]
4
+ patautoedit check CONFIG.csv [--atp-dir DIR | --atp FILE ...] [--pinmap FILE]
5
+ patautoedit modes
6
+
7
+ `run` applies the config and writes edited ATPs to --output-dir (default
8
+ Output/ under the current directory). `check` validates and prints the plan
9
+ without writing. `modes` lists the Process Types and the fields each needs.
10
+ """
11
+ import argparse
12
+ import json
13
+ import os
14
+ import sys
15
+ from typing import Dict, List
16
+
17
+ from patautoedit import version
18
+ from patautoedit.atp_handler import (ConfigError, MODE_LIST, analyse_merge_config,
19
+ missing_requirements)
20
+ from patautoedit.logger import Logger
21
+ from patautoedit.main import check_config, run_config
22
+
23
+
24
+ class _CliLogger(Logger):
25
+ """Logger that keeps warnings for the JSON result."""
26
+
27
+ def __init__(self, stream):
28
+ super().__init__(stream=stream)
29
+ self.warnings: List[str] = []
30
+
31
+ def warning(self, msg):
32
+ self.warnings.append(msg)
33
+ super().warning(msg)
34
+
35
+
36
+ def _logger(use_json: bool) -> _CliLogger:
37
+ return _CliLogger(sys.stderr if use_json else sys.stdout)
38
+
39
+
40
+ def _candidate_atps(parser: argparse.ArgumentParser, args) -> List[str]:
41
+ if args.atp:
42
+ paths = []
43
+ for path in args.atp:
44
+ if not os.path.isfile(path):
45
+ parser.error(f"ATP file not found: {path}")
46
+ paths.append(os.path.abspath(path))
47
+ return paths
48
+
49
+ atp_dir = args.atp_dir or os.path.dirname(os.path.abspath(args.config))
50
+ if not os.path.isdir(atp_dir):
51
+ parser.error(f"ATP directory not found: {atp_dir}")
52
+ names = sorted(
53
+ entry.name for entry in os.scandir(atp_dir)
54
+ if entry.is_file() and entry.name.lower().endswith((".atp", ".atp.gz"))
55
+ )
56
+ return [os.path.abspath(os.path.join(atp_dir, name)) for name in names]
57
+
58
+
59
+ def _check_paths(parser: argparse.ArgumentParser, args) -> None:
60
+ if not os.path.isfile(args.config):
61
+ parser.error(f"config file not found: {args.config}")
62
+ if args.pinmap and not os.path.isfile(args.pinmap):
63
+ parser.error(f"pinmap file not found: {args.pinmap}")
64
+
65
+
66
+ def _parse_config(parser: argparse.ArgumentParser, args, logger: Logger):
67
+ try:
68
+ return analyse_merge_config(args.config, logger)
69
+ except ConfigError as e:
70
+ return e
71
+ except OSError as e:
72
+ return e
73
+
74
+
75
+ def _failed_run(args, problems: List[str], warnings: List[str]) -> Dict:
76
+ return {
77
+ "version": version,
78
+ "ok": False,
79
+ "config": os.path.abspath(args.config),
80
+ "output_dir": os.path.abspath(args.output_dir),
81
+ "outputs": [],
82
+ "failed": [],
83
+ "skipped": [],
84
+ "warnings": warnings,
85
+ "problems": problems,
86
+ "counts": {"matched": 0, "succeeded": 0, "failed": 0},
87
+ }
88
+
89
+
90
+ def _emit_run(result: Dict, use_json: bool) -> None:
91
+ if use_json:
92
+ json.dump(result, sys.stdout, indent=2)
93
+ sys.stdout.write("\n")
94
+ return
95
+ counts = result["counts"]
96
+ if result["problems"]:
97
+ print("Configuration rejected; nothing was processed.")
98
+ elif counts["matched"] == 0:
99
+ print("No ATP files matched the config; nothing was processed.")
100
+ elif counts["failed"]:
101
+ print(f"{counts['failed']} of {counts['matched']} file(s) failed.")
102
+ else:
103
+ print(f"{counts['succeeded']} file(s) processed. Output: {result['output_dir']}")
104
+
105
+
106
+ def _run(args, parser: argparse.ArgumentParser) -> int:
107
+ _check_paths(parser, args)
108
+ atp_files = _candidate_atps(parser, args)
109
+ logger = _logger(args.json)
110
+
111
+ config_list = _parse_config(parser, args, logger)
112
+ if isinstance(config_list, Exception):
113
+ result = _failed_run(args, [str(config_list)], logger.warnings)
114
+ _emit_run(result, args.json)
115
+ return 1
116
+
117
+ run = run_config(atp_files, config_list, logger, pin_map=args.pinmap or "",
118
+ output_dir=args.output_dir)
119
+ result = {
120
+ "version": version,
121
+ "ok": run["ok"],
122
+ "config": os.path.abspath(args.config),
123
+ "output_dir": run["output_dir"],
124
+ "outputs": run["outputs"],
125
+ "failed": run["failed"],
126
+ "skipped": run["skipped"],
127
+ "warnings": logger.warnings,
128
+ "problems": run["problems"],
129
+ "counts": {
130
+ "matched": len(run["outputs"]) + len(run["failed"]),
131
+ "succeeded": len(run["outputs"]),
132
+ "failed": len(run["failed"]),
133
+ },
134
+ }
135
+ _emit_run(result, args.json)
136
+ return 0 if result["ok"] else 1
137
+
138
+
139
+ def _failed_check(args, problems: List[str], warnings: List[str]) -> Dict:
140
+ return {
141
+ "version": version,
142
+ "ok": False,
143
+ "config": os.path.abspath(args.config),
144
+ "plan": [],
145
+ "skipped": [],
146
+ "warnings": warnings,
147
+ "problems": problems,
148
+ "counts": {"matched": 0},
149
+ }
150
+
151
+
152
+ def _emit_check(result: Dict, use_json: bool) -> None:
153
+ if use_json:
154
+ json.dump(result, sys.stdout, indent=2)
155
+ sys.stdout.write("\n")
156
+ return
157
+ for item in result["plan"]:
158
+ print(f"{os.path.basename(item['source'])}: {' -> '.join(item['operations'])}")
159
+ if result["skipped"]:
160
+ print("Skipped: " + ", ".join(result["skipped"]))
161
+ if result["problems"]:
162
+ print("Configuration rejected; nothing would be processed.")
163
+ elif result["plan"]:
164
+ print(f"{len(result['plan'])} file(s) would be processed.")
165
+ else:
166
+ print("No ATP files matched the config; nothing would be processed.")
167
+
168
+
169
+ def _check(args, parser: argparse.ArgumentParser) -> int:
170
+ _check_paths(parser, args)
171
+ atp_files = _candidate_atps(parser, args)
172
+ logger = _logger(args.json)
173
+
174
+ config_list = _parse_config(parser, args, logger)
175
+ if isinstance(config_list, Exception):
176
+ result = _failed_check(args, [str(config_list)], logger.warnings)
177
+ _emit_check(result, args.json)
178
+ return 1
179
+
180
+ checked = check_config(atp_files, config_list, logger, pin_map=args.pinmap or "")
181
+ result = {
182
+ "version": version,
183
+ "ok": checked["ok"],
184
+ "config": os.path.abspath(args.config),
185
+ "plan": checked["plan"],
186
+ "skipped": checked["skipped"],
187
+ "warnings": logger.warnings,
188
+ "problems": checked["problems"],
189
+ "counts": {"matched": len(checked["plan"])},
190
+ }
191
+ _emit_check(result, args.json)
192
+ return 0 if result["ok"] else 1
193
+
194
+
195
+ def _modes(args, parser: argparse.ArgumentParser) -> int:
196
+ rows = [{"process_type": mode, "requires": missing_requirements({"Mode": mode})}
197
+ for mode in MODE_LIST]
198
+ if args.json:
199
+ json.dump(rows, sys.stdout, indent=2)
200
+ sys.stdout.write("\n")
201
+ return 0
202
+ for row in rows:
203
+ print(f"{row['process_type']}: {', '.join(row['requires']) or '-'}")
204
+ return 0
205
+
206
+
207
+ def _parser() -> argparse.ArgumentParser:
208
+ parser = argparse.ArgumentParser(
209
+ prog="patautoedit",
210
+ description="Edit ATP pattern files for DSSC/CMEM/HRAM.",
211
+ )
212
+ parser.add_argument("--version", action="version", version=version)
213
+ sub = parser.add_subparsers(dest="command", required=True)
214
+
215
+ def add_common(p):
216
+ p.add_argument("config", help="row-per-op config CSV")
217
+ p.add_argument("--atp-dir",
218
+ help="folder holding *.atp / *.atp.gz; default: the config's folder")
219
+ p.add_argument("--atp", action="append", metavar="FILE",
220
+ help="candidate ATP file; repeatable; overrides --atp-dir")
221
+ p.add_argument("--pinmap", help="optional pinmap file")
222
+ p.add_argument("--json", action="store_true",
223
+ help="print one JSON object on stdout; logs go to stderr")
224
+
225
+ run = sub.add_parser("run", help="apply a config CSV")
226
+ add_common(run)
227
+ run.add_argument("--output-dir", default="Output",
228
+ help="folder for edited files (default: ./Output)")
229
+ run.set_defaults(func=_run)
230
+
231
+ check = sub.add_parser("check", help="validate a config CSV without writing files")
232
+ add_common(check)
233
+ check.set_defaults(func=_check)
234
+
235
+ modes = sub.add_parser("modes", help="list Process Types and required fields")
236
+ modes.add_argument("--json", action="store_true",
237
+ help="print the list as JSON")
238
+ modes.set_defaults(func=_modes)
239
+ return parser
240
+
241
+
242
+ def main(argv=None) -> int:
243
+ parser = _parser()
244
+ args = parser.parse_args(argv)
245
+ return args.func(args, parser)
246
+
247
+
248
+ if __name__ == "__main__":
249
+ sys.exit(main())