diff options
author | emkael <emkael@tlen.pl> | 2024-01-24 02:01:25 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2024-01-24 02:01:25 +0100 |
commit | adb385515ea439bb01dc0af4619251ec78b25413 (patch) | |
tree | 7f0c385fa5cab79a67d3df9b3f2da27b4e16a491 | |
parent | f48e3cbd6c74c1cb30a71b0e57f528577ff5e814 (diff) |
Moving PBN generation to SquareDeal class, SQD is now just a file parser/writer
-rw-r--r-- | squaredeal/__init__.py | 48 | ||||
-rw-r--r-- | squaredeal/sqd.py | 56 |
2 files changed, 48 insertions, 56 deletions
diff --git a/squaredeal/__init__.py b/squaredeal/__init__.py index 999785e..955fc64 100644 --- a/squaredeal/__init__.py +++ b/squaredeal/__init__.py @@ -1,4 +1,4 @@ -import re, subprocess +import base64, os, re, subprocess from squaredeal.sqd import SQD, SQDPhase, generate_session_key, validate_board_range_str @@ -19,6 +19,27 @@ def squaredeal_board_range(arg_str): return ','.join(ranges) +def parse_range_str(range_str, max_count): + range_start = 0 + range_end = max_count + if range_str: + try: + range_start = int(range_str) - 1 + range_end = range_start + 1 + except ValueError: + range_match = re.match(r'([0-9]+)-([0-9]+)', range_str) + if range_match: + range_start = int(range_match.group(1))-1 + range_end = int(range_match.group(2)) + else: + raise ValueError('Invalid range string: %s' % (range_str)) + if range_start < 0: + raise ValueError('Value out of range: 0') + if range_end > max_count: + raise ValueError('Value out of range: %d' % (range_end)) + return range(range_start, range_end) + + class SquareDeal(object): # TODO: docstrings for command parameters @@ -89,7 +110,28 @@ class SquareDeal(object): if not self.sqd.delayed_value: raise SquareDealError('Cannot generate PBN files: delayed information value not set') try: - self.sqd.generate(arguments.get('phase'), arguments.get('session'), - reserve=arguments.get('reserve'), bigdealx_path=SquareDeal.BIGDEALX_PATH) + phases_to_generate = parse_range_str(arguments.get('phase'), len(self.sqd.phases)) + for phase_idx in phases_to_generate: + phase = self.sqd.phases[phase_idx] + delayed_info = base64.b64encode(self.sqd.delayed_info.encode('utf-8')).decode() + sessions_to_generate = parse_range_str(arguments.get('session'), phase.sessions) + board_ranges = phase.parse_board_ranges(phase.boards) + for session in sessions_to_generate: + session_key = phase.s_keys[session] + session_key_len = int(len(session_key)/2) + session_left = session_key[0:session_key_len] + session_right = session_key[session_key_len:] + reserve_info = 'reserve' if arguments.get('reserve') else 'original' + args = [SquareDeal.BIGDEALX_PATH, + '-W', session_left, + '-e', session_right, + '-e', delayed_info, + '-e', reserve_info, + '-p', phase.output_file_name(session+1, arguments.get('reserve')), + '-n', board_ranges[session]] + subprocess.run( + args, + cwd=os.path.realpath(os.path.dirname(self.sqd.sqd_path)) if self.sqd.sqd_path else None, + capture_output=True, check=True) except subprocess.CalledProcessError as ex: raise SquareDealError('BigDeal invocation failed: %s' % (ex.stderr)) diff --git a/squaredeal/sqd.py b/squaredeal/sqd.py index 0fe51fe..d80c3e0 100644 --- a/squaredeal/sqd.py +++ b/squaredeal/sqd.py @@ -1,29 +1,9 @@ -import base64, hashlib, os, random, re, shutil, string, subprocess +import hashlib, os, random, re, shutil, string, subprocess def generate_session_key(): return ''.join(random.choices(string.ascii_letters + string.digits, k=60)) -def parse_range_str(range_str, max_count): - range_start = 0 - range_end = max_count - if range_str: - try: - range_start = int(range_str) - 1 - range_end = range_start + 1 - except ValueError: - range_match = re.match(r'([0-9]+)-([0-9]+)', range_str) - if range_match: - range_start = int(range_match.group(1))-1 - range_end = int(range_match.group(2)) - else: - raise ValueError('Invalid range string: %s' % (range_str)) - if range_start < 0: - raise ValueError('Value out of range: 0') - if range_end > max_count: - raise ValueError('Value out of range: %d' % (range_end)) - return range(range_start, range_end) - def validate_board_range_str(range_str): if range_str.isdigit(): @@ -53,7 +33,7 @@ class SQDPhase(object): def tostring(self): return ':'.join([str(self.sessions), str(self.boards), self.prefix, self.info or '']) - def _output_file_name(self, session, reserve=False): + def output_file_name(self, session, reserve=False): prefix = self.prefix session_search = re.findall(r'#+', prefix) for session_match in sorted(session_search, reverse=True): @@ -63,7 +43,7 @@ class SQDPhase(object): prefix += 'reserve' return prefix - def _parse_board_ranges(self, range_def): + def parse_board_ranges(self, range_def): ranges = [range_str.strip() for range_str in range_def.split(',')] for range_str in ranges: validate_board_range_str(range_str) @@ -72,27 +52,6 @@ class SQDPhase(object): output_ranges += ranges return output_ranges[0:self.sessions] - def generate(self, session, delayed_info, reserve=False, output_path=None, bigdealx_path=None): - if not bigdealx_path: - raise FileNotFoundError('bigdealx_path not set') - delayed_info = base64.b64encode(delayed_info.encode('utf-8')).decode() - sessions_to_generate = parse_range_str(session, self.sessions) - board_ranges = self._parse_board_ranges(self.boards) - for session in sessions_to_generate: - session_key = self.s_keys[session] - session_key_len = int(len(session_key)/2) - session_left = session_key[0:session_key_len] - session_right = session_key[session_key_len:] - reserve_info = 'reserve' if reserve else 'original' - args = [bigdealx_path, - '-W', session_left, - '-e', session_right, - '-e', delayed_info, - '-e', reserve_info, - '-p', self._output_file_name(session+1, reserve), - '-n', board_ranges[session]] - subprocess.run(args, cwd=output_path, capture_output=True, check=True) - class SQD(object): @@ -214,12 +173,3 @@ class SQD(object): sqd_contents.append('KH %s\n' % (self.hash)) with open(sqdpath, 'w') as sqdfile: sqdfile.writelines(sqd_contents) - - def generate(self, phase, session, reserve=False, bigdealx_path=None): - phases_to_generate = parse_range_str(phase, len(self.phases)) - for phase in phases_to_generate: - self.phases[phase].generate( - session, self.delayed_value, - reserve=reserve, - output_path=os.path.realpath(os.path.dirname(self.sqd_path)) if self.sqd_path else None, - bigdealx_path=bigdealx_path) |