summaryrefslogtreecommitdiff
path: root/dealconvert/formats/__init__.py
blob: fc19280fb258ef0ae650905c20ce923cd118679c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from os.path import dirname, basename, isfile, join
import glob

class DealFormat(object):
    cards = 'AKQJT98765432'

    def __init__(self, interactive=True, **kwargs):
        self.interactive = interactive
        self.options = kwargs

    def file_to_read(self, input_path):
        return open(input_path, 'r')

    def file_to_write(self, output_path):
        return open(output_path, 'w')

    def parse(self, input_file):
        with self.file_to_read(input_file) as content:
            return self.parse_content(content)

    def output(self, output_file, deal, analyze=False):
        self.analyze = analyze
        if not len(deal):
            raise RuntimeError('Dealset is empty')
        with self.file_to_write(output_file) as out_file:
            return self.output_content(out_file, deal)

    def match_file(self, filename):
        return filename.lower().endswith(self.suffix)

    def parse_byte(self, byte):
        try:
            converted_byte = ord(byte)
            byte = converted_byte
        except TypeError:
            converted_byte = byte # in Python3, ord() is not needed
        return converted_byte

    @property
    def suffix(self):
        pass

    def parse_content(self, content):
        pass

    def output_content(self, out_file, deal):
        pass


class BinaryFormat(DealFormat):
    def file_to_read(self, input_path):
        return open(input_path, 'rb')

    def file_to_write(self, output_path):
        return open(output_path, 'wb')


modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [
    basename(f)[:-3] for f in modules
    if isfile(f) and not f.endswith('__init__.py')
]