summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2024-01-24 01:39:38 +0100
committeremkael <emkael@tlen.pl>2024-01-24 01:39:38 +0100
commitf48e3cbd6c74c1cb30a71b0e57f528577ff5e814 (patch)
tree38b83ce4556134f482c78f834ac81657435bc797
parent03b12e38ffa1b135b08573b9d26bfa61c7b9715e (diff)
Moving bigdealx path management to SquareDeal interface
-rw-r--r--pysquaredeal.py5
-rw-r--r--squaredeal/__init__.py16
-rw-r--r--squaredeal/sqd.py20
3 files changed, 23 insertions, 18 deletions
diff --git a/pysquaredeal.py b/pysquaredeal.py
index b126b74..6411d10 100644
--- a/pysquaredeal.py
+++ b/pysquaredeal.py
@@ -8,7 +8,7 @@ argparser = argparse.ArgumentParser(prog='pysquaredeal.py')
argparser.add_argument('sqd_file', metavar='SQD_FILE', help='path to SQD file')
argparser.add_argument('--sqk-file', metavar='SQK_FILE', help='path to SQK file, if not provided, deduced from SQD file', required=False)
argparser.add_argument('--encoding', required=False, default='utf-8', metavar='ENCODING', help='SQD/SQK input file encoding, defaults to UTF-8, output is always UTF-8')
-argparser.add_argument('--bigdealx-path', required=False, metavar='BIGDEALX_PATH', help='path to bigdealx executable, defaults to BIGDEALX_PATH environment variable')
+argparser.add_argument('--bigdealx-path', required=False, default=os.environ.get('BIGDEALX_PATH'), metavar='BIGDEALX_PATH', help='path to bigdealx executable, defaults to BIGDEALX_PATH environment variable')
subparsers = argparser.add_subparsers(title='command-specific arguments', metavar='COMMAND', dest='command')
@@ -40,5 +40,8 @@ argparser_generate.add_argument('--reserve', action='store_true', help='generate
arguments = argparser.parse_args()
+
+SquareDeal.BIGDEALX_PATH = arguments.bigdealx_path
+
sq = SquareDeal()
getattr(sq, arguments.command)(**vars(arguments))
diff --git a/squaredeal/__init__.py b/squaredeal/__init__.py
index aba6cad..999785e 100644
--- a/squaredeal/__init__.py
+++ b/squaredeal/__init__.py
@@ -1,4 +1,4 @@
-import os
+import re, subprocess
from squaredeal.sqd import SQD, SQDPhase, generate_session_key, validate_board_range_str
@@ -20,6 +20,9 @@ def squaredeal_board_range(arg_str):
class SquareDeal(object):
+# TODO: docstrings for command parameters
+
+ BIGDEALX_PATH = None
def __init__(self):
self.sqd = SQD()
@@ -78,12 +81,15 @@ class SquareDeal(object):
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')
+ if not SquareDeal.BIGDEALX_PATH:
+ raise SquareDealError('Path to BigDeal is not set, initialize SquareDeal.BIGDEALX_PATH value')
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'))
+ try:
+ self.sqd.generate(arguments.get('phase'), arguments.get('session'),
+ reserve=arguments.get('reserve'), bigdealx_path=SquareDeal.BIGDEALX_PATH)
+ except subprocess.CalledProcessError as ex:
+ raise SquareDealError('BigDeal invocation failed: %s' % (ex.stderr))
diff --git a/squaredeal/sqd.py b/squaredeal/sqd.py
index f7686ad..0fe51fe 100644
--- a/squaredeal/sqd.py
+++ b/squaredeal/sqd.py
@@ -72,9 +72,9 @@ class SQDPhase(object):
output_ranges += ranges
return output_ranges[0:self.sessions]
- def generate(self, session, delayed_info, reserve=False, output_path=None):
- if not SQD.BIGDEALX_PATH:
- raise ValueError('Path to BigDeal is not set, initialize SQD.BIGDEALX_PATH value')
+ 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)
@@ -84,24 +84,19 @@ class SQDPhase(object):
session_left = session_key[0:session_key_len]
session_right = session_key[session_key_len:]
reserve_info = 'reserve' if reserve else 'original'
- args = [SQD.BIGDEALX_PATH,
+ 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]]
- try:
- subprocess.run(args, cwd=output_path, capture_output=True, check=True)
- except subprocess.CalledProcessError as ex:
- raise Exception(ex.stderr)
+ subprocess.run(args, cwd=output_path, capture_output=True, check=True)
class SQD(object):
- BIGDEALX_PATH=None
-
def __init__(self):
self.name = ''
self.delayed_info = ''
@@ -220,10 +215,11 @@ class SQD(object):
with open(sqdpath, 'w') as sqdfile:
sqdfile.writelines(sqd_contents)
- def generate(self, phase, session, reserve=False):
+ 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)
+ output_path=os.path.realpath(os.path.dirname(self.sqd_path)) if self.sqd_path else None,
+ bigdealx_path=bigdealx_path)