-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblemparser.py
144 lines (124 loc) · 5.22 KB
/
problemparser.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import re, os, sys
from enum import Enum
import re_helper
class ProblemType(Enum):
# Supported problem types.
pb = 1,
probdef = 2
@staticmethod
def get_problem_type(problem_str):
# The list of supported types can be found in ProblemType enum.
for problem_type in ProblemType:
regex = '{}\d{{0,1}}\s*\("'.format(problem_type.name)
if re.match(regex, problem_str):
return problem_type
return None
class ProblemParser:
def __init__(self, problem_str, section):
self._section = section
self._id, self._name, self._link = ["" for _ in range(3)]
self._tl, self._ml = [0 for _ in range(2)]
self.parse(problem_str)
print('Parsing problem {}: {}\n{}'.format(self._id, self._name, self.latex()), file=sys.stderr)
self._statements = self.find_statements()
def parse(self, problem_str):
raise NotImplementedError
@staticmethod
def create(problem_str, problem_type, section):
problem_class_name = 'ProblemParser{}'.format(problem_type.name.capitalize())
if problem_class_name not in globals():
raise NotImplementedError('No ProblemParser definition for {}.'.format(problem_type.name))
return globals()[problem_class_name](problem_str, section)
@staticmethod
def normalize_tl(tl):
if len(tl) <= 2:
return int(tl)
if int(tl) % 1000 == 0:
return int(tl) // 1000
else:
return float(tl) / 1000
@staticmethod
def normalize_ml(ml):
ml = int(ml)
if ml > 1024:
return ml // 1024
return ml
def id(self):
return self._id
def name(self):
return self._name
def statements(self):
return self._statements
def find_statements(self):
root_link = os.path.join('D:\problems', self._link)
sources = []
for curr_folder, dirs, files in os.walk(root_link):
if 'statement' in curr_folder:
files = [os.path.join(curr_folder, x) for x in files if x.endswith('.tex')]
# In case of two source files one of them might be the english translation.
if len(files) == 2 and any(x.endswith('.en.tex') for x in files):
candidates = [x for x in files if not x.endswith('.en.tex')]
if candidates:
sources.append(os.path.join(curr_folder, candidates[0]))
else:
sources.extend(files)
if not sources:
raise FileNotFoundError("Couldn't find any statement files"
" for problem {}.".format(self._link))
sources.sort()
if len(sources) == 1:
return sources[0]
print('Pdfmaker found more than one tex source for problem {}:'.format(self.id()))
print('[\n\t{}\n]'.format(',\n\t'.join(sources)))
print('Input a number from 1 to {} to determine which source to use.'.format(len(sources)))
return sources[int(input()) - 1]
def graphics(self):
link = self.statements()
with open(link, 'r', encoding = 'utf8') as f:
pics = [os.path.join(os.path.split(link)[0], x) for x in
re.findall(r'\\includegraphics(?:\s*\[.*?\]\s*)*{\s*(.+?)\s*}', f.read())]
return pics
def latex(self):
return '\probl{{{}}}{{{}}}{{{} sec}}{{{} mb}}'.format(
self._name, self._id, self._tl, self._ml)
def section(self):
return self._section
class ProblemParserPb(ProblemParser):
def parse(self, problem_str):
match = re_helper.create_regex(
'pb\d?',
re_helper.OPENING_BRACKET,
*([re_helper.QUOTED_STRING,
re_helper.COMMA] * 4),
re_helper.INT_VALUE,
re_helper.COMMA,
re_helper.INT_VALUE,
re_helper.CLOSING_BRACKET,
re_helper.COMMENT
).fullmatch(problem_str)
if match is None:
raise AssertionError('Line\n{}\nis not formatted correctly.'.format(problem_str))
self._id, self._name = match.group(1, 3)
# This particular problem type targets everything under burunduk1/problems/yyyy-mm/<problem>.
self._link = os.path.join('burunduk1', 'problems', *match.group(4, 3))
self._tl = ProblemParser.normalize_tl(match.group(5))
self._ml = ProblemParser.normalize_ml(match.group(6))
class ProblemParserProbdef(ProblemParser):
def parse(self, problem_str):
match = re_helper.create_regex(
'probdef\d?',
re_helper.OPENING_BRACKET,
*([re_helper.QUOTED_STRING,
re_helper.COMMA] * 3),
re_helper.INT_VALUE,
re_helper.COMMA,
re_helper.INT_VALUE,
re_helper.CLOSING_BRACKET,
re_helper.COMMENT
).fullmatch(problem_str)
if match is None:
raise AssertionError('Line\n{}\nis not formatted correctly.'.format(problem_str))
self._id, self._link = match.group(1, 3)
self._name = re.split(r'[\\\/]', match.group(3))[-1]
self._tl = ProblemParser.normalize_tl(match.group(4))
self._ml = ProblemParser.normalize_ml(match.group(5))