summaryrefslogtreecommitdiff
path: root/squaredeal
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2024-01-17 23:25:23 +0100
committeremkael <emkael@tlen.pl>2024-01-17 23:31:08 +0100
commit89fb49d37d1d5763b231eb312cd640daa7cbc591 (patch)
treef9e23c228565b89d5cf4eb7299960fa83b8a1ad2 /squaredeal
parentf9c6c4341e1a52190c811c1eda6a1df9136901b9 (diff)
Bringing Python port up to date with original interface in 2.x version:
- generate SQK only on publish - do not explicitly mark SQD as published - separate field for DI value The SquareDeal and SquareDealPhase objects are now also mostly DTO, without complex modifications through internal methods, only input/output operations are their internal methods.
Diffstat (limited to 'squaredeal')
-rw-r--r--squaredeal/__init__.py90
1 files changed, 43 insertions, 47 deletions
diff --git a/squaredeal/__init__.py b/squaredeal/__init__.py
index 0e445f6..e44f780 100644
--- a/squaredeal/__init__.py
+++ b/squaredeal/__init__.py
@@ -1,6 +1,9 @@
import base64, hashlib, os, random, shutil, string, subprocess
+def generate_session_key():
+ return ''.join(random.choices(string.ascii_letters + string.digits, k=60))
+
class SquareDealError(Exception):
pass
@@ -21,7 +24,6 @@ class SquareDealPhase(object):
self.boards = int(parts[1])
self.prefix = parts[2]
self.info = parts[3]
- self.s_keys = [None] * self.sessions
def tostring(self):
return ':'.join([str(self.sessions), str(self.boards), self.prefix, self.info or ''])
@@ -47,7 +49,7 @@ class SquareDealPhase(object):
'-e', reserve_info,
'-p', self._output_file_name(session+1, reserve),
'-n', str(self.boards)]
- subprocess.run(args, cwd=os.path.realpath(SquareDeal.BIGDEALX_PATH))
+ subprocess.run(args)
class SquareDeal(object):
@@ -57,6 +59,7 @@ class SquareDeal(object):
def __init__(self):
self.name = ''
self.delayed_info = ''
+ self.delayed_value = ''
self.hash = ''
self.phases = []
self.published = False
@@ -72,9 +75,10 @@ class SquareDeal(object):
self.name = linecontents
elif linetype == 'DI':
self.delayed_info = linecontents
+ elif linetype == 'DV':
+ self.delayed_value = linecontents
elif linetype == 'KH':
self.hash = linecontents
- elif linetype == 'PU':
self.published = True
elif linetype == 'SN':
phase = SquareDealPhase()
@@ -82,35 +86,38 @@ class SquareDeal(object):
self.phases.append(phase)
else:
raise SquareDealError('Unrecognized tag %s on line %d' % (linetype, idx))
- if sqkpath is None:
- sqkpath = self._deduce_sqk_path(sqdpath)
- try:
- 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))
- for line in contents:
- if not line.strip():
- continue
- lineparts = line.split(':')
- if len(lineparts) != 2:
- raise SquareDealError('Malformed SQK line: %s' % (line))
- session = lineparts[0].split(',')
- if len(session) != 2:
- raise SquareDealError('Malformed SQK line: %s' % (line))
- phase_no = int(session[0])
- session_no = int(session[1])
+ if self.published:
+ for phase in self.phases:
+ phase.s_keys = [None] * phase.sessions
+ if sqkpath is None:
+ sqkpath = self._deduce_sqk_path(sqdpath)
try:
- self.phases[phase_no-1].s_keys[session_no-1] = lineparts[1]
- except IndexError:
- raise SquareDealError('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('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('SQK hash mismtach: %s in SQD, % actual' % (self.hash, sqk_hash))
+ 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))
+ for line in contents:
+ if not line.strip():
+ continue
+ lineparts = line.split(':')
+ if len(lineparts) != 2:
+ raise SquareDealError('Malformed SQK line: %s' % (line))
+ session = lineparts[0].split(',')
+ if len(session) != 2:
+ raise SquareDealError('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('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('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('SQK hash mismtach: %s in SQD, % actual' % (self.hash, sqk_hash))
def _deduce_sqk_path(self, sqdpath):
sqkpath = list(os.path.splitext(sqdpath))
@@ -146,31 +153,20 @@ class SquareDeal(object):
sqkpath = self._deduce_sqk_path(sqdpath)
if make_backups:
self._make_backups(sqdpath, sqkpath)
- self._write_session_keys(sqkpath)
+ if self.published:
+ self._write_session_keys(sqkpath)
sqd_contents = []
sqd_contents.append('TN %s\n' % (self.name or ''))
- sqd_contents.append('KH %s\n' % (self.hash))
sqd_contents.append('DI %s\n' % (self.delayed_info or ''))
+ if self.published:
+ sqd_contents.append('DV %s\n' % (self.delayed_value or ''))
for phase in self.phases:
sqd_contents.append('SN %s\n' % (phase.tostring()))
if self.published:
- sqd_contents.append('PU\n')
+ sqd_contents.append('KH %s\n' % (self.hash))
with open(sqdpath, 'w') as sqdfile:
sqdfile.writelines(sqd_contents)
- def _generate_session_key(self):
- return ''.join(random.choices(string.ascii_letters + string.digits, k=60))
-
- def add_phase(self, session_count, board_count, file_prefix, description=None):
- phase = SquareDealPhase()
- phase.sessions = session_count
- phase.boards = board_count
- phase.prefix = file_prefix
- phase.info = description
- for i in range(0, phase.sessions):
- phase.s_keys.append(self._generate_session_key())
- self.phases.append(phase)
-
def generate(self, phase, session, reserve=False):
phases_to_generate = range(0, len(self.phases)) if phase is None else [phase-1]
for phase in phases_to_generate: