-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFileKey.py
292 lines (248 loc) · 9.32 KB
/
FileKey.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
This module contains classes to convert between keys and files using the patterns defined in patterns.py
"""
import re
import string
from collections import namedtuple
from itertools import product
from pathlib import Path
from dbetto.time import unix_time
from .patterns import (
full_channel_pattern_with_extension,
get_pattern_tier,
key_pattern,
processing_pattern,
)
# key_pattern -> key
#
def regex_from_filepattern(filepattern):
f = []
wildcards = []
last = 0
for match in re.compile(r"\{(?P<name>[\w]+)\}").finditer(filepattern):
f.append(re.escape(filepattern[last : match.start()]))
wildcard = match.group("name")
if wildcard in wildcards:
f.append(f"(?P={wildcard})")
else:
wildcards.append(wildcard)
if wildcard == "ext":
f.append(
f"(?P<{wildcard}>.*)"
) # this means ext will capture everything after 1st dot
else:
f.append(f"(?P<{wildcard}>" + r"[^\.\/]+)")
last = match.end()
f.append(re.escape(filepattern[last:]))
f.append("$")
return "".join(f)
class FileKey(
namedtuple("FileKey", ["experiment", "period", "run", "datatype", "timestamp"])
):
__slots__ = ()
re_pattern = "(-(?P<experiment>[^-]+)(\\-(?P<period>[^-]+)(\\-(?P<run>[^-]+)(\\-(?P<datatype>[^-]+)(\\-(?P<timestamp>[^-]+))?)?)?)?)?$"
key_pattern = key_pattern()
@property
def name(self):
return f"{self.experiment}-{self.period}-{self.run}-{self.datatype}-{self.timestamp}"
@property
def key(self):
return f"-{self.name}"
def _list(self):
return list(self)
@property
def __str__(self):
return self.name
@classmethod
def from_string(cls, key_string):
return cls.get_filekey_from_pattern(key_string)
@classmethod
def get_filekey_from_filename(cls, filename):
return cls.get_filekey_from_pattern(filename, processing_pattern())
@classmethod
def get_filekey_from_pattern(cls, filename, pattern=None):
if isinstance(pattern, Path):
pattern = pattern.as_posix()
filename = str(filename)
key_pattern_rx = re.compile(
regex_from_filepattern(cls.key_pattern if pattern is None else pattern)
)
if key_pattern_rx.match(filename) is None:
return None
else:
d = key_pattern_rx.match(filename).groupdict()
for entry in list(d):
if entry not in cls._fields:
d.pop(entry)
for wildcard in cls._fields:
if wildcard not in d:
d[wildcard] = "*"
return cls(**d)
@classmethod
def unix_time_from_string(cls, value):
key_class = cls.from_string(value)
return unix_time(key_class.timestamp)
def get_unix_timestamp(self):
return unix_time(self.timestamp)
@classmethod
def parse_keypart(cls, keypart):
keypart_rx = re.compile(cls.re_pattern)
d = keypart_rx.match(keypart).groupdict()
for key in d:
if d[key] is None:
d[key] = "*"
return cls(**d)
def expand(self, file_pattern, **kwargs):
file_pattern = str(file_pattern)
wildcard_dict = self._asdict()
if kwargs is not None:
for key, value in kwargs.items():
wildcard_dict[key] = value
wildcard_dict = {
wildcard: [wildcard_value]
if isinstance(wildcard_value, str)
else wildcard_value
for wildcard, wildcard_value in wildcard_dict.items()
}
formatter = string.Formatter()
result = []
for combo in product(*wildcard_dict.values()):
substitution = dict(zip(list(wildcard_dict), combo))
result.append(formatter.vformat(file_pattern, (), substitution))
return result
def get_path_from_filekey(self, pattern, **kwargs):
if kwargs is None:
return self.expand(pattern, **kwargs)
else:
for entry, value in kwargs.items():
if isinstance(value, dict):
if len(next(iter(set(value).intersection(self._list())))) > 0:
kwargs[entry] = value[
next(iter(set(value).intersection(self._list())))
]
else:
kwargs.pop(entry)
return self.expand(pattern, **kwargs)
# get_path_from_key
@classmethod
def get_full_path_from_filename(cls, filename, pattern, path_pattern):
return cls.get_path_from_filekey(
cls.get_filekey_from_pattern(filename, pattern), path_pattern
)
@staticmethod
def tier_files(setup, keys, tier):
fn_pattern = get_pattern_tier(setup, tier)
files = []
for line in keys:
tier_filename = FileKey.get_full_path_from_filename(
line, FileKey.key_pattern, fn_pattern
)
files += tier_filename
return files
class ProcessingFileKey(FileKey):
_fields = (*FileKey._fields, "processing_step")
key_pattern = processing_pattern()
def __new__(cls, experiment, period, run, datatype, timestamp, processing_step):
self = super().__new__(cls, experiment, period, run, datatype, timestamp)
if "_" in processing_step:
splits = processing_step.split("_", 2)
self.processing_type = splits[0]
self.tier = splits[1]
if len(splits) > 2:
self.identifier = splits[2]
else:
self.identifier = None
else:
self.processing_type = processing_step
self.tier = None
self.identifier = None
return self
def _list(self):
return [*super()._list(), self.processing_step]
def _asdict(self):
dic = super()._asdict()
dic["processing_step"] = self.processing_step
return dic
@property
def processing_step(self):
if self.identifier is not None:
return f"{self.processing_type}_{self.tier}_{self.identifier}"
else:
return f"{self.processing_type}_{self.tier}"
@property
def name(self):
return f"{super().name}-{self.processing_step}"
def get_path_from_filekey(self, pattern, **kwargs):
if isinstance(pattern, Path):
pattern = pattern.as_posix()
if not isinstance(pattern, str):
pattern = pattern(self.tier, self.identifier)
if kwargs is None:
return self.expand(pattern, **kwargs)
else:
for entry, value in kwargs.items():
if isinstance(value, dict):
if len(next(iter(set(value).intersection(self._list())))) > 0:
kwargs[entry] = value[
next(iter(set(value).intersection(self._list())))
]
else:
kwargs.pop(entry)
return self.expand(pattern, **kwargs)
class ChannelProcKey(FileKey):
re_pattern = "all(-(?P<experiment>[^-]+)(\\-(?P<period>[^-]+)(\\-(?P<run>[^-]+)(\\-(?P<datatype>[^-]+)(\\-(?P<timestamp>[^-]+)(\\-(?P<channel>[^-]+))?)?)?)?)?)?$"
key_pattern = full_channel_pattern_with_extension()
_fields = (*FileKey._fields, "channel")
def __new__(cls, experiment, period, run, datatype, timestamp, channel):
self = super().__new__(cls, experiment, period, run, datatype, timestamp)
self.channel = channel
return self
@property
def name(self):
return f"{super().name}-{self.channel}"
def _asdict(self):
dic = super()._asdict()
dic["channel"] = self.channel
return dic
@staticmethod
def get_channel_files(keypart, par_pattern, chan_list):
if isinstance(par_pattern, Path):
par_pattern = par_pattern.as_posix()
d = ChannelProcKey.parse_keypart(keypart)
filenames = []
for chan in chan_list:
wildcards_dict = d._asdict()
wildcards_dict.pop("channel")
formatter = string.Formatter()
wildcards_dict["channel"] = chan
file = formatter.vformat(par_pattern, (), wildcards_dict)
filenames.append(file)
return filenames
def per_grouper(files):
"""
Returns list containing lists of each run
"""
pers = []
per_files = []
for file in files:
fk = ProcessingFileKey.get_filekey_from_pattern(Path(file).name)
if f"{fk.experiment}-{fk.period}" not in pers:
pers.append(f"{fk.experiment}-{fk.period}")
per_files.append([])
for i, run in enumerate(pers):
if run == f"{fk.experiment}-{fk.period}":
per_files[i].append(file)
return per_files
def run_grouper(files):
"""Returns list containing lists of each run"""
runs = []
run_files = []
for file in files:
fk = ProcessingFileKey.get_filekey_from_pattern(Path(file).name)
if f"{fk.experiment}-{fk.period}-{fk.run}" not in runs:
runs.append(f"{fk.experiment}-{fk.period}-{fk.run}")
run_files.append([])
for i, run in enumerate(runs):
if run == f"{fk.experiment}-{fk.period}-{fk.run}":
run_files[i].append(file)
return run_files