-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog_path.py
405 lines (294 loc) · 13.2 KB
/
log_path.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
from collections import namedtuple
from datetime import date
from operator import itemgetter
from itertools import chain
import os
import re
from natsort import natsorted
from natsort import ns
import cachetools
import fastcache
import config
import exceptions
import looseboy
import util
LOG_INTERMEDIATE_BASE = "moddata/log"
LOG_FILENAME_REGEX = re.compile("(?P<filename>(?P<network>(default|znc)+)_(?P<channel>[#&]*[a-zA-Z0-9\u4e00-\u9fff/_\-\.\?\$]+)_(?P<date>\d{8})\.log)")
LogResult = namedtuple('LogResult', ['log', 'before', 'after'])
ldp = looseboy.LooseDateParser()
@fastcache.clru_cache(maxsize=1024)
def parse_date(date_string):
if len(date_string) == 8:
components = date_string[0:4], date_string[4:6], date_string[6:8]
elif len(date_string) == 10:
components = date_string[0:4], date_string[5:7], date_string[8:10]
components = map(int, components)
return date(*components)
class LogPath:
def __init__(self, ac):
self.ac = ac
def networks(self):
base_contents = os.listdir(config.LOG_BASE)
dirs = [
network for network in base_contents
if os.path.isdir(
self.network_to_path(network)
) and self.ac.evaluate(network, None)
]
return sorted(dirs)
def channels(self, network):
matches = self._channels_list(network)
# This network doesn't actually exist.
if matches is None:
raise exceptions.NoResultsException()
channels = natsorted(
{
filename['channel']
for filename in matches
if self.ac.evaluate(network, filename['channel'])
},
alg=ns.G | ns.LF,
)
if not channels:
raise exceptions.NoResultsException()
return channels
def channel_dates(self, network, channel):
matches = self._channels_list(network)
if matches is None:
raise exceptions.NoResultsException()
if not self.ac.evaluate(network, channel):
raise exceptions.NoResultsException()
try:
self._maybe_channel(network, channel, matches)
except (
exceptions.NoResultsException,
exceptions.MultipleResultsException,
exceptions.CanonicalNameException,
):
raise
dates = [filename['date'] for filename in matches if filename['channel'] == channel]
return sorted(dates, reverse=True)
def channels_dates(self, network, channels):
"""For search use.
Further filtering will be performed on the search side.
"""
matches = self._channels_list(network)
if matches is None:
raise exceptions.NoResultsException()
# This behavior is slightly different from elsewhere.
channels = [ch for ch in channels if self.ac.evaluate(network, ch)]
files = [filename for filename in matches if filename['channel'] in channels]
return sorted(files, key=itemgetter('date_obj'), reverse=True)
def log(self, network, channel, date):
matches = self._channels_list(network)
if matches is None:
raise exceptions.NoResultsException()
if not self.ac.evaluate(network, channel):
raise exceptions.NoResultsException()
try:
self._maybe_channel(network, channel, matches)
except (
exceptions.NoResultsException,
exceptions.MultipleResultsException,
exceptions.CanonicalNameException,
):
raise
channel_files = [filename for filename in matches if filename['channel'] == channel]
channel_files = sorted(channel_files, key=itemgetter('date'))
latest = channel_files[-1]['date']
parsed_date = ldp.parse(date, latest)
if not parsed_date:
raise exceptions.NoResultsException()
elif parsed_date != date:
raise exceptions.CanonicalNameException(util.Scope.DATE, parsed_date)
log = [filename for filename in channel_files if filename['date'] == date]
if len(log) == 0:
raise exceptions.NoResultsException()
log = log[0]
log_idx = channel_files.index(log)
before, after = None, None
if log_idx > 0:
before = channel_files[log_idx - 1]['date']
if log_idx < len(channel_files) - 1:
after = channel_files[log_idx + 1]['date']
log_path = os.path.join(self.network_to_path(network), log['filename'])
# Enumerate at 1: these are log line numbers.
log_file = enumerate(open(log_path, errors='ignore').readlines(), start=1)
return LogResult(log_file, before, after)
@fastcache.clru_cache(maxsize=128)
def network_to_path(self, network):
return os.path.join(config.LOG_BASE, network, LOG_INTERMEDIATE_BASE)
def _maybe_channel(self, network, channel, matches):
# This looks a bit intensive.
# Accomodate partial matches to see if containing-match only matches
# one channel. If it does, we can 302 to the real URL, but otherwise
# we should 404.
maybe_channels = {filename['channel'] for filename in matches if channel.lower() in filename['channel'].lower()}
# Bail if it's ambiguous...
if len(maybe_channels) > 1:
# unless one of them is an exact match.
exact = [maybe_channel for maybe_channel in maybe_channels if maybe_channel == channel]
if len(exact) != 1:
raise exceptions.MultipleResultsException()
elif len(maybe_channels) == 1:
canonical_channel = list(maybe_channels)[0]
if channel != canonical_channel:
raise exceptions.CanonicalNameException(util.Scope.CHANNEL, canonical_channel)
elif len(maybe_channels) == 0:
# We have nothing. It is unfortunate.
raise exceptions.NoResultsException()
def _channels_list(self, network):
channel_base = self.network_to_path(network)
if not os.path.exists(channel_base):
return None
files = os.listdir(channel_base)
file_matches = [LOG_FILENAME_REGEX.match(filename) for filename in files]
file_matches = [match.groupdict() for match in file_matches if match is not None]
for match in file_matches:
match['date_obj'] = parse_date(match['date'])
return file_matches
class DirectoryDelimitedLogPath(LogPath):
LOG_SUFFIX = '.log'
def channel_dates(self, network, channel):
dates = self._dates_list(network, channel)
if not dates:
raise exceptions.NoResultsException()
if not self.ac.evaluate(network, channel):
raise exceptions.NoResultsException()
dates = [x['date'] for x in dates]
return sorted(dates, reverse=True)
def channels_dates(self, network, channels):
"""
For search use. Further filtering will be performed on the search side.
"""
matches = self._channels_list(network)
if matches is None:
raise exceptions.NoResultsException()
# This behavior is slightly different from elsewhere.
channels = [ch for ch in channels if self.ac.evaluate(network, ch)]
files = chain.from_iterable([self._dates_list(network, ch) for ch in channels])
return sorted(files, key=itemgetter('date_obj'), reverse=True)
def log(self, network, channel, date):
channels = self._channels_list(network)
dates = self._dates_list(network, channel)
if channels is None or dates is None:
raise exceptions.NoResultsException()
if not self.ac.evaluate(network, channel):
raise exceptions.NoResultsException()
try:
self._maybe_channel(network, channel, channels)
except (
exceptions.NoResultsException,
exceptions.MultipleResultsException,
exceptions.CanonicalNameException,
):
raise
latest = max(dates, key=itemgetter('date'))['date']
parsed_date = ldp.parse(date, latest)
if not parsed_date:
raise exceptions.NoResultsException()
elif parsed_date != date:
raise exceptions.CanonicalNameException(util.Scope.DATE, parsed_date)
# Reverse the human-friendly ordering here.
channel_dates = self.channel_dates(network, channel)[::-1]
log = [log_date for log_date in channel_dates if log_date == date]
if len(log) == 0:
raise exceptions.NoResultsException()
log = log[0]
log_idx = channel_dates.index(log)
before, after = None, None
if log_idx > 0:
before = channel_dates[log_idx - 1]
if log_idx < len(channel_dates) - 1:
after = channel_dates[log_idx + 1]
log_path = os.path.join(self.channel_to_path(network, channel),
log + DirectoryDelimitedLogPath.LOG_SUFFIX)
log_file = enumerate(open(log_path, errors='ignore').readlines(), start=1)
return LogResult(log_file, before, after)
# This lets us use LogPath.networks instead of reimplementing.
@fastcache.clru_cache(maxsize=128)
def network_to_path(self, network):
return os.path.join(config.LOG_BASE, network)
@fastcache.clru_cache(maxsize=128)
def channel_to_path(self, network, channel):
return os.path.join(self.network_to_path(network), channel)
# This lets us use LogPath.channels instead of reimplementing.
@cachetools.cached(cachetools.TTLCache(maxsize=128, ttl=21600))
def _channels_list(self, network):
network_base = self.network_to_path(network)
if not os.path.exists(network_base):
return None
files = os.listdir(network_base)
files = [{'channel': channel} for channel in files]
return files
def _dates_list(self, network, channel):
channel_base = self.channel_to_path(network, channel)
if not os.path.exists(channel_base):
return None
files = os.listdir(channel_base)
files = [{
'channel': channel,
'date': filename[:-1 * len(DirectoryDelimitedLogPath.LOG_SUFFIX)],
'filename': os.path.join(channel_base, filename)
} for filename in files if filename.endswith(DirectoryDelimitedLogPath.LOG_SUFFIX)]
for f in files:
f['date_obj'] = parse_date(f['date'])
return files
class ZNC16DirectoryDelimitedLogPath(DirectoryDelimitedLogPath):
# Assume people are creating user-per-network. If they aren't they can subclass
# something themselves. This assumption is embedded into LOG_FILENAME_REGEX
# anyway.
NETWORK_DEFAULT_USER = "default"
@fastcache.clru_cache(maxsize=128)
def network_to_path(self, network):
return os.path.join(config.LOG_BASE, network, LOG_INTERMEDIATE_BASE, ZNC16DirectoryDelimitedLogPath.NETWORK_DEFAULT_USER)
def _dates_list(self, network, channel):
"""ZNC 1.6 default stores files with a %Y-%m-%d format. Preserve the %Y%m%d format in display
by coercing it back and forth every single time. Performance impact: yet to determined
"""
files = super(ZNC16DirectoryDelimitedLogPath, self)._dates_list(network, channel)
if not files:
return files
for f in files:
f['date'] = f['date_obj'].strftime("%Y%m%d")
return files
def log(self, network, channel, date):
channels = self._channels_list(network)
dates = self._dates_list(network, channel)
if channels is None or dates is None:
raise exceptions.NoResultsException()
if not self.ac.evaluate(network, channel):
raise exceptions.NoResultsException()
try:
self._maybe_channel(network, channel, channels)
except (
exceptions.NoResultsException,
exceptions.MultipleResultsException,
exceptions.CanonicalNameException,
):
raise
latest = max(dates, key=itemgetter('date'))['date']
parsed_date = ldp.parse(date, latest)
if not parsed_date:
raise exceptions.NoResultsException()
elif parsed_date != date:
raise exceptions.CanonicalNameException(util.Scope.DATE, parsed_date)
# Reverse the human-friendly ordering here.
channel_dates = self.channel_dates(network, channel)[::-1]
log = [log_date for log_date in channel_dates if log_date == date]
if len(log) == 0:
raise exceptions.NoResultsException()
log = log[0]
log_idx = channel_dates.index(log)
# Convert the no-dash presentation date...
log = log[0:4] + '-' + log[4:6] + '-' + log[6:8]
before, after = None, None
if log_idx > 0:
before = channel_dates[log_idx - 1]
if log_idx < len(channel_dates) - 1:
after = channel_dates[log_idx + 1]
log_path = os.path.join(self.channel_to_path(network, channel),
log + DirectoryDelimitedLogPath.LOG_SUFFIX)
log_file = enumerate(open(log_path, errors='ignore').readlines(), start=1)
return LogResult(log_file, before, after)