diff options
author | emkael <emkael@tlen.pl> | 2019-11-07 20:46:46 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-11-07 20:46:46 +0100 |
commit | 29c7e9fe2d7e2c6b6a67d7daf38b29c1a10f77a7 (patch) | |
tree | cf48737673fba9c5c616616b292af75a45c86b11 | |
parent | 6238c63a0aa244673b0a53dd00e829f687d808a3 (diff) |
Unified way of dealing with format-specific arguments
-rw-r--r-- | dealconvert/__init__.py | 9 | ||||
-rw-r--r-- | dealconvert/formats/__init__.py | 4 | ||||
-rw-r--r-- | dealconvert/formats/pbn.py | 4 |
3 files changed, 9 insertions, 8 deletions
diff --git a/dealconvert/__init__.py b/dealconvert/__init__.py index 5158847..7bb1325 100644 --- a/dealconvert/__init__.py +++ b/dealconvert/__init__.py @@ -1,23 +1,24 @@ from .formats import * class DealConverter(object): - def __init__(self, input_file=None, jfr_only=False): + def __init__(self, input_file=None, **kwargs): self.input = input_file self.formats = {} + self.format_options = kwargs if input_file is not None: - self.parser = self.detect_format(self.input, jfr_only=jfr_only) + self.parser = self.detect_format(self.input) def output(self, output_files): deal_set = sorted(self.parser.parse(self.input), key=lambda d:d.number) for output in output_files: self.detect_format(output).output(output, deal_set, True) - def detect_format(self, filename, interactive=True, jfr_only=False): + def detect_format(self, filename, interactive=True): for deal_format in globals()['formats'].__all__: if deal_format not in self.formats: self.formats[deal_format] = getattr( globals()[deal_format], - deal_format.upper() + 'Format')(interactive, jfr_only) + deal_format.upper() + 'Format')(interactive, **self.format_options) if self.formats[deal_format].match_file(filename): return self.formats[deal_format] raise RuntimeError('Unrecognized file extension: %s' % filename) diff --git a/dealconvert/formats/__init__.py b/dealconvert/formats/__init__.py index 9ac48e3..1d8b460 100644 --- a/dealconvert/formats/__init__.py +++ b/dealconvert/formats/__init__.py @@ -4,9 +4,9 @@ import glob class DealFormat(object): cards = 'AKQJT98765432' - def __init__(self, interactive=True, jfr_only=False): + def __init__(self, interactive=True, **kwargs): self.interactive = interactive - self.jfr_only = jfr_only + self.options = kwargs def parse(self, input_file): with open(input_file, 'rb') as content: diff --git a/dealconvert/formats/pbn.py b/dealconvert/formats/pbn.py index 04db8a2..6108f4b 100644 --- a/dealconvert/formats/pbn.py +++ b/dealconvert/formats/pbn.py @@ -144,8 +144,8 @@ class PBNFormat(DealFormat): dd_board = PBNBoard(lines) dd_table = DDTable(dd_board).get_dd_table(self.interactive) dd_contract = ParScore(dd_board).get_par_contract(dd_table) - dd_board.save_dd_table(dd_table, jfr_only=self.jfr_only) - dd_board.save_par_contract(dd_contract, jfr_only=self.jfr_only) + dd_board.save_dd_table(dd_table, jfr_only=self.options['jfr_only']) + dd_board.save_par_contract(dd_contract, jfr_only=self.options['jfr_only']) lines = [field.raw_field for field in dd_board.fields] except Exception as e: warnings.warn('unable to determine double-dummy data: %s' % e) |