From da97cd934466425d66e1ba8aea29fb71e9be2f59 Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 22 Jan 2024 01:45:09 +0100 Subject: SquareDeal interface as a class --- pysquaredeal.py | 71 ++--------------------------------------------- squaredeal/__init__.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ squaredeal/sqd.py | 24 ++++++++-------- 3 files changed, 89 insertions(+), 81 deletions(-) diff --git a/pysquaredeal.py b/pysquaredeal.py index cd4f20a..b126b74 100644 --- a/pysquaredeal.py +++ b/pysquaredeal.py @@ -1,7 +1,6 @@ import argparse, os, re, sys -from squaredeal import SquareDealError, squaredeal_board_range -from squaredeal.sqd import SQD, SQDPhase, generate_session_key +from squaredeal import SquareDeal, squaredeal_board_range argparser = argparse.ArgumentParser(prog='pysquaredeal.py') @@ -41,69 +40,5 @@ argparser_generate.add_argument('--reserve', action='store_true', help='generate arguments = argparser.parse_args() - -# TODO: this should be an interface class, also rename SquareDeal to SQD or sth and this to SquareDeal -if arguments.command == 'create': - sd = SQD() - sd.name = arguments.event_name - sd.delayed_info = arguments.delayed_information - sd.tofile(arguments.sqd_file) -elif arguments.command == 'set_name': - sd = SQD() - sd.fromfile(arguments.sqd_file, sqkpath=arguments.sqk_file) - if sd.published: - raise SquareDealError('Cannot change name: event already published') - sd.name = arguments.event_name - sd.tofile(arguments.sqd_file) -elif arguments.command == 'set_di': - sd = SQD() - sd.fromfile(arguments.sqd_file, sqkpath=arguments.sqk_file) - if sd.published: - raise SquareDealError('Cannot change delayed information description: event already published') - sd.delayed_info = arguments.delayed_information - sd.tofile(arguments.sqd_file) -elif arguments.command == 'add_phase': - sd = SQD() - sd.fromfile(arguments.sqd_file, sqkpath=arguments.sqk_file) - if sd.published: - raise SquareDealError('Cannot add phase: event already published') - sdphase = SQDPhase() - sdphase.sessions = arguments.sessions - sdphase.boards = arguments.boards - sdphase.prefix = arguments.prefix - sdphase.info = arguments.description - sd.phases.append(sdphase) - sd.tofile(arguments.sqd_file) -elif arguments.command == 'publish': - sd = SQD() - sd.fromfile(arguments.sqd_file, sqkpath=arguments.sqk_file) - if sd.published: - raise SquareDealError('Cannot mark as published: event already published') - if not sd.name: - raise SquareDealError('Cannot mark as published: event name is not set') - if not sd.delayed_info: - raise SquareDealError('Cannot mark as published: delayed information is not set') - if not sd.phases: - raise SquareDealError('Cannot mark as published: no phases are defined') - for sdphase in sd.phases: - sdphase.s_keys = [generate_session_key() for s in range(0, sdphase.sessions)] - sd.published = True - sd.tofile(arguments.sqd_file, sqkpath=arguments.sqk_file) -elif arguments.command == 'set_dv': - sd = SQD() - sd.fromfile(arguments.sqd_file, sqkpath=arguments.sqk_file) - if not sd.published: - raise SquareDealError('Cannot set delayed information value: event not published') - sd.delayed_value = arguments.delayed_information - sd.tofile(arguments.sqd_file, sqkpath=arguments.sqk_file) -elif arguments.command == 'generate': - if arguments.bigdealx_path is None: - arguments.bigdealx_path = os.environ.get('BIGDEALX_PATH', None) - SQD.BIGDEALX_PATH = arguments.bigdealx_path - sd = SQD() - sd.fromfile(arguments.sqd_file, sqkpath=arguments.sqk_file) - if not sd.published: - raise SquareDealError('Cannot generate PBN files: event info is not marked as published') - if not sd.delayed_value: - raise SquareDealError('Cannot generate PBN files: delayed information value not set') - sd.generate(arguments.phase, arguments.session, reserve=arguments.reserve) +sq = SquareDeal() +getattr(sq, arguments.command)(**vars(arguments)) diff --git a/squaredeal/__init__.py b/squaredeal/__init__.py index c0ebd1a..03b091d 100644 --- a/squaredeal/__init__.py +++ b/squaredeal/__init__.py @@ -1,3 +1,8 @@ +import os + +from squaredeal.sqd import SQD, SQDPhase, generate_session_key + + class SquareDealError(Exception): pass @@ -13,3 +18,73 @@ def squaredeal_board_range(arg_str): continue ranges += [validate_board_range_str(range_str)] return ','.join(ranges) + + +class SquareDeal(object): + + def __init__(self): + self.sqd = SQD() + + def create(self, **arguments): + self.sqd.name = arguments.get('event_name') + self.sqd.delayed_info = arguments.get('delayed_information') + self.sqd.tofile(arguments.get('sqd_file')) + + def set_name(self, **arguments): + self.sqd.fromfile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + if self.sqd.published: + raise SquareDealError('Cannot change name: event already published') + self.sqd.name = arguments.get('event_name') + self.sqd.tofile(arguments.get('sqd_file')) + + def set_di(self, **arguments): + self.sqd.fromfile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + if self.sqd.published: + raise SquareDealError('Cannot change delayed information description: event already published') + self.sqd.delayed_info = arguments.get('delayed_information') + self.sqd.tofile(arguments.get('sqd_file')) + + def add_phase(self, **arguments): + self.sqd.fromfile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + if self.sqd.published: + raise SquareDealError('Cannot add phase: event already published') + sdphase = SQDPhase() + sdphase.sessions = arguments.get('sessions') + sdphase.boards = arguments.get('boards') + sdphase.prefix = arguments.get('prefix') + sdphase.info = arguments.get('description') + self.sqd.phases.append(sdphase) + self.sqd.tofile(arguments.get('sqd_file')) + + def publish(self, **arguments): + self.sqd.fromfile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + if self.sqd.published: + raise SquareDealError('Cannot mark as published: event already published') + if not self.sqd.name: + raise SquareDealError('Cannot mark as published: event name is not set') + if not self.sqd.delayed_info: + raise SquareDealError('Cannot mark as published: delayed information is not set') + if not self.sqd.phases: + raise SquareDealError('Cannot mark as published: no phases are defined') + for sdphase in self.sqd.phases: + sdphase.s_keys = [generate_session_key() for s in range(0, sdphase.sessions)] + self.sqd.published = True + self.sqd.tofile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + + def set_dv(self, **arguments): + self.sqd.fromfile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + if not self.sqd.published: + raise SquareDealError('Cannot set delayed information value: event not published') + self.sqd.delayed_value = arguments.get('delayed_information') + self.sqd.tofile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + + def generate(self, **arguments): + if arguments.get('bigdealx_path') is None: + arguments['bigdealx_path'] = os.environ.get('BIGDEALX_PATH', None) + SQD.BIGDEALX_PATH = arguments.get('bigdealx_path') + self.sqd.fromfile(arguments.get('sqd_file'), sqkpath=arguments.get('sqk_file')) + if not self.sqd.published: + raise SquareDealError('Cannot generate PBN files: event info is not marked as published') + if not self.sqd.delayed_value: + raise SquareDealError('Cannot generate PBN files: delayed information value not set') + self.sqd.generate(arguments.get('phase'), arguments.get('session'), reserve=arguments.get('reserve')) diff --git a/squaredeal/sqd.py b/squaredeal/sqd.py index b1ea783..f7686ad 100644 --- a/squaredeal/sqd.py +++ b/squaredeal/sqd.py @@ -1,7 +1,5 @@ import base64, hashlib, os, random, re, shutil, string, subprocess -from squaredeal import SquareDealError - def generate_session_key(): return ''.join(random.choices(string.ascii_letters + string.digits, k=60)) @@ -46,7 +44,7 @@ class SQDPhase(object): def fromstring(self, phasestr): parts = phasestr.split(':') if len(parts) != 4: - raise SquareDealError('Malformed phase definition: %s' % (phasestr)) + raise ValueError('Malformed phase definition: %s' % (phasestr)) self.sessions = int(parts[0]) self.boards = parts[1] self.prefix = parts[2] @@ -76,7 +74,7 @@ class SQDPhase(object): def generate(self, session, delayed_info, reserve=False, output_path=None): if not SQD.BIGDEALX_PATH: - raise SquareDealError('Path to BigDeal is not set, initialize SQD.BIGDEALX_PATH value') + raise ValueError('Path to BigDeal is not set, initialize SQD.BIGDEALX_PATH value') 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) @@ -96,7 +94,7 @@ class SQDPhase(object): try: subprocess.run(args, cwd=output_path, capture_output=True, check=True) except subprocess.CalledProcessError as ex: - raise SquareDealError(ex.stderr) + raise Exception(ex.stderr) @@ -134,7 +132,7 @@ class SQD(object): phase.fromstring(linecontents) self.phases.append(phase) else: - raise SquareDealError('Unrecognized tag %s on line %d' % (linetype, idx)) + raise ValueError('Unrecognized tag %s on line %d' % (linetype, idx)) if self.published: for phase in self.phases: phase.s_keys = [None] * phase.sessions @@ -144,31 +142,31 @@ class SQD(object): with open(sqkpath, encoding=encoding) as sqkfile: contents = [line.strip() for line in sqkfile.readlines()] except FileNotFoundError: - raise SquareDealError('Unable to locate SQK file for %s' % (sqdpath)) + raise FileNotFoundError('Unable to locate SQK file for %s' % (sqdpath)) for line in contents: if not line.strip(): continue lineparts = line.split(':') if len(lineparts) != 2: - raise SquareDealError('Malformed SQK line: %s' % (line)) + raise ValueError('Malformed SQK line: %s' % (line)) session = lineparts[0].split(',') if len(session) != 2: - raise SquareDealError('Malformed SQK line: %s' % (line)) + raise ValueError('Malformed SQK line: %s' % (line)) phase_no = int(session[0]) session_no = int(session[1]) try: self.phases[phase_no-1].s_keys[session_no-1] = lineparts[1] except IndexError: - raise SquareDealError( + raise IndexError( 'Session %s from SQK not declared in SQD' % (lineparts[0])) for ph_idx, phase in enumerate(self.phases): for s_idx, s_key in enumerate(phase.s_keys): if s_key is None: - raise SquareDealError( + raise IndexError( 'Session %d,%d missing a key in SQK' % (ph_idx+1, s_idx+1)) sqk_hash = self._get_file_hash(sqkpath) if sqk_hash != self.hash: - raise SquareDealError( + raise ValueError( 'SQK hash mismtach: %s in SQD, % actual' % (self.hash, sqk_hash)) self.sqd_path = sqdpath @@ -197,7 +195,7 @@ class SQD(object): for ph_idx, phase in enumerate(self.phases): for s_idx, session_key in enumerate(phase.s_keys): if session_key is None: - raise SquareDealError( + raise IndexError( 'Missing session key for session %d,%d' % (ph_idx+1, s_idx+1)) sqkfile.write( ('%d,%d:%s\r\n' % (ph_idx+1, s_idx+1, session_key)).encode('utf8')) -- cgit v1.2.3