/
utils.py
532 lines (397 loc) · 14.5 KB
/
utils.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""
This module contains various utilities.
"""
from collections import namedtuple
from datetime import timedelta, datetime
import tempfile
import cherrypy
from mako.lookup import TemplateLookup
import os
import pkg_resources
import re
from lxml import etree
from time import gmtime, strftime, clock
from .constants import NS
from .decorators import retry
from .logs import log
import httplib2
import hashlib
from email.utils import parsedate
from urlparse import urlparse
from threading import local
import iso8601
__author__ = 'leifj'
import i18n
_ = i18n.language.ugettext
sentinel = object()
thread_data = local()
class PyffException(Exception):
pass
def _e(error_log, m=None):
def _f(x):
if ":WARNING:" in x:
return False
if m is not None and m not in x:
return False
return True
return "\n".join(filter(_f, ["%s" % e for e in error_log]))
def debug_observer(e):
log.error(repr(e))
def resource_string(name, pfx=None):
"""
Attempt to load and return the contents (as a string) of the resource named by
the first argument in the first location of:
# as name in the current directory
# as name in the `pfx` subdirectory of the current directory if provided
# as name relative to the package
# as pfx/name relative to the package
The last two alternatives is used to locate resources distributed in the package.
This includes certain XSLT and XSD files.
:param name: The string name of a resource
:param pfx: An optional prefix to use in searching for name
"""
name = os.path.expanduser(name)
if os.path.exists(name):
with open(name) as fd:
return fd.read()
elif pfx and os.path.exists(os.path.join(pfx, name)):
with open(os.path.join(pfx, name)) as fd:
return fd.read()
elif pkg_resources.resource_exists(__name__, name):
return pkg_resources.resource_string(__name__, name)
elif pfx and pkg_resources.resource_exists(__name__, "%s/%s" % (pfx, name)):
return pkg_resources.resource_string(__name__, "%s/%s" % (pfx, name))
return None
def resource_filename(name, pfx=None):
"""
Attempt to find and return the filename of the resource named by the first argument
in the first location of:
# as name in the current directory
# as name in the `pfx` subdirectory of the current directory if provided
# as name relative to the package
# as pfx/name relative to the package
The last two alternatives is used to locate resources distributed in the package.
This includes certain XSLT and XSD files.
:param name: The string name of a resource
:param pfx: An optional prefix to use in searching for name
"""
if os.path.exists(name):
return name
elif pfx and os.path.exists(os.path.join(pfx, name)):
return os.path.join(pfx, name)
elif pkg_resources.resource_exists(__name__, name):
return pkg_resources.resource_filename(__name__, name)
elif pfx and pkg_resources.resource_exists(__name__, "%s/%s" % (pfx, name)):
return pkg_resources.resource_filename(__name__, "%s/%s" % (pfx, name))
return None
def tdelta(td):
"""
Parse a time delta from expressions like 1w 32d 4h 5s - i.e in weeks, days hours and/or seconds.
:param td: A human-friendly string representation of a timedelta
"""
keys = ["weeks", "days", "hours", "minutes"]
regex = "".join(["((?P<%s>\d+)%s ?)?" % (k, k[0]) for k in keys])
kwargs = {}
for k, v in re.match(regex, td).groupdict(default="0").items():
kwargs[k] = int(v)
return timedelta(**kwargs)
def totimestamp(dt, epoch=datetime(1970, 1, 1)):
epoch = epoch.replace(tzinfo=dt.tzinfo)
td = dt - epoch
ts = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 1e6
return int(ts)
def dumptree(t, pretty_print=False, xml_declaration=True):
"""
Return a string representation of the tree, optionally pretty_print(ed) (default False)
:param t: An ElemenTree to serialize
"""
return etree.tostring(t, encoding='UTF-8', xml_declaration=xml_declaration, pretty_print=pretty_print)
def iso_now():
"""
Current time in ISO format
"""
return iso_fmt()
def iso_fmt(tstamp=None):
"""
Timestamp in ISO format
"""
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime(tstamp))
def iso2datetime(s):
return iso8601.parse_date(s)
class ResourceResolver(etree.Resolver):
def __init__(self):
super(ResourceResolver, self).__init__()
def resolve(self, system_url, public_id, context):
"""
Resolves URIs using the resource API
"""
log.debug("resolve SYSTEM URL' %s' for '%s'" % (system_url, public_id))
path = system_url.split("/")
fn = path[len(path) - 1]
if pkg_resources.resource_exists(__name__, fn):
return self.resolve_file(pkg_resources.resource_stream(__name__, fn), context)
elif pkg_resources.resource_exists(__name__, "schema/%s" % fn):
return self.resolve_file(pkg_resources.resource_stream(__name__, "schema/%s" % fn), context)
else:
raise ValueError("Unable to locate %s" % fn)
def schema():
if not hasattr(thread_data, 'schema'):
thread_data.schema = None
if thread_data.schema is None:
try:
parser = etree.XMLParser()
parser.resolvers.add(ResourceResolver())
st = etree.parse(pkg_resources.resource_stream(__name__, "schema/schema.xsd"), parser)
thread_data.schema = etree.XMLSchema(st)
except etree.XMLSchemaParseError, ex:
log.error(_e(ex.error_log))
raise ex
return thread_data.schema
# @cached(hash_key=lambda *args, **kwargs: hash(args[0]))
def validate_document(t):
schema().assertValid(t)
def safe_write(fn, data):
"""Safely write data to a file with name fn
:param fn: a filename
:param data: some data to write
:return: True or False depending on the outcome of the write
"""
tmpn = None
try:
fn = os.path.expanduser(fn)
dirname, basename = os.path.split(fn)
with tempfile.NamedTemporaryFile('w', delete=False, prefix=".%s" % basename, dir=dirname) as tmp:
tmp.write(data)
tmpn = tmp.name
if os.path.exists(tmpn) and os.stat(tmpn).st_size > 0:
os.rename(tmpn, fn)
return True
except Exception, ex:
log.error(ex)
finally:
if tmpn is not None and os.path.exists(tmpn):
try:
os.unlink(tmpn)
except Exception, ex:
log.warn(ex)
return False
site_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "site")
templates = TemplateLookup(directories=[os.path.join(site_dir, 'templates')])
def template(name):
return templates.get_template(name)
def request_vhost(request):
return request.headers.get('X-Forwarded-Host', request.headers.get('Host', request.base))
def render_template(name, **kwargs):
kwargs.setdefault('http', cherrypy.request)
kwargs.setdefault('brand', "pyFF @ %s" % request_vhost(cherrypy.request))
kwargs.setdefault('_', _)
return template(name).render(**kwargs)
_Resource = namedtuple("Resource", ["result", "cached", "date", "last_modified", "resp", "time"])
def parse_date(s):
if s is None:
return datetime.now()
return datetime(*parsedate(s)[:6])
@retry((IOError, httplib2.HttpLib2Error))
def load_url(url, enable_cache=True, timeout=60):
start_time = clock()
cache = httplib2.FileCache(".cache")
headers = dict()
if not enable_cache:
headers['cache-control'] = 'no-cache'
log.debug("fetching (caching: %s) '%s'" % (enable_cache, url))
if url.startswith('file://'):
path = url[7:]
if not os.path.exists(path):
raise IOError("file not found: %s" % path)
with open(path, 'r') as fd:
return _Resource(result=fd.read(),
cached=False,
date=datetime.now(),
resp=None,
time=clock() - start_time,
last_modified=datetime.fromtimestamp(os.stat(path).st_mtime))
else:
h = httplib2.Http(cache=cache,
timeout=timeout,
disable_ssl_certificate_validation=True) # trust is done using signatures over here
resp, content = h.request(url, headers=headers)
if resp.status != 200:
raise IOError(resp.reason)
log.debug("last-modified header: %s" % resp.get('last-modified'))
log.debug("date header: %s" % resp.get('date'))
log.debug("last modified: %s" % resp.get('date', resp.get('last-modified', None)))
return _Resource(result=content,
cached=resp.fromcache,
date=parse_date(resp['date']),
resp=resp,
time=clock() - start_time,
last_modified=parse_date(resp.get('date', resp.get('last-modified', None))))
def root(t):
if hasattr(t, 'getroot') and hasattr(t.getroot, '__call__'):
return t.getroot()
else:
return t
def duration2timedelta(period):
regex = re.compile(
'(?P<sign>[-+]?)P(?:(?P<years>\d+)[Yy])?(?:(?P<months>\d+)[Mm])?(?:(?P<days>\d+)[Dd])?(?:T(?:(?P<hours>\d+)[Hh])?(?:(?P<minutes>\d+)[Mm])?(?:(?P<seconds>\d+)[Ss])?)?')
# Fetch the match groups with default value of 0 (not None)
m = regex.match(period)
if not m:
return None
duration = m.groupdict(0)
# Create the timedelta object from extracted groups
delta = timedelta(days=int(duration['days']) + (int(duration['months']) * 30) + (int(duration['years']) * 365),
hours=int(duration['hours']),
minutes=int(duration['minutes']),
seconds=int(duration['seconds']))
if duration['sign'] == "-":
delta *= -1
return delta
def filter_lang(elts, langs=None):
if langs is None or type(langs) is not list:
langs = ['en']
def _l(elt):
return elt.get("{http://www.w3.org/XML/1998/namespace}lang", None) in langs
if elts is None:
return []
lst = filter(_l, elts)
if lst:
return lst
else:
return elts
def xslt_transform(t, stylesheet, params=None):
if not params:
params = dict()
if not hasattr(thread_data, 'xslt'):
thread_data.xslt = dict()
transform = None
if stylesheet not in thread_data.xslt:
xsl = etree.fromstring(resource_string(stylesheet, "xslt"))
thread_data.xslt[stylesheet] = etree.XSLT(xsl)
transform = thread_data.xslt[stylesheet]
res = transform(t, **params)
return res
def total_seconds(dt):
if hasattr(dt, "total_seconds"):
return dt.total_seconds()
else:
return (dt.microseconds + (dt.seconds + dt.days * 24 * 3600) * 10 ** 6) / 10 ** 6
def etag(s):
return hash_str('sha1', s)
def hash_str(hn, s):
if hn == 'null':
return s
if not hasattr(hashlib, hn):
raise ValueError("Unknown digest mechanism: '%s'" % hn)
hash_m = getattr(hashlib, hn)
h = hash_m()
h.update(s)
return h.hexdigest()
def hash_id(entity, hn='sha1', prefix=True):
entity_id = entity
if hasattr(entity, 'get'):
entity_id = entity.get('entityID')
hstr = hex_digest(entity_id, hn)
if prefix:
return "{%s}%s" % (hn, hstr)
else:
return hstr
def hex_digest(data, hn='sha1'):
if hn == 'null':
return data
if not hasattr(hashlib, hn):
raise ValueError("Unknown digest '%s'" % hn)
m = getattr(hashlib, hn)()
m.update(data)
return m.hexdigest()
def parse_xml(io, base_url=None):
return etree.parse(io, base_url=base_url, parser=etree.XMLParser(resolve_entities=False))
class EntitySet(object):
def __init__(self, initial=None):
self._e = dict()
if initial is not None:
for e in initial:
self.add(e)
def add(self, value):
self._e[value.get('entityID')] = value
def discard(self, value):
entity_id = value.get('entityID')
if entity_id in self._e:
del self._e[entity_id]
def __iter__(self):
for e in self._e.values():
yield e
def __len__(self):
return len(self._e.keys())
def __contains__(self, item):
return item.get('entityID') in self._e.keys()
class MetadataException(Exception):
pass
class MetadataExpiredException(MetadataException):
pass
def find_merge_strategy(strategy_name):
if '.' not in strategy_name:
strategy_name = "pyff.merge_strategies.%s" % strategy_name
(mn, sep, fn) = strategy_name.rpartition('.')
# log.debug("import %s from %s" % (fn,mn))
module = None
if '.' in mn:
(pn, sep, modn) = mn.rpartition('.')
module = getattr(__import__(pn, globals(), locals(), [modn], -1), modn)
else:
module = __import__(mn, globals(), locals(), [], -1)
strategy = getattr(module, fn) # we might aswell let this fail early if the strategy is wrongly named
if strategy is None:
raise MetadataException("Unable to find merge strategy %s" % strategy_name)
return strategy
def entities_list(t=None):
"""
:param t: An EntitiesDescriptor or EntityDescriptor element
Returns the list of contained EntityDescriptor elements
"""
if t is None:
return []
elif root(t).tag == "{%s}EntityDescriptor" % NS['md']:
return [root(t)]
else:
return iter_entities(t)
def iter_entities(t):
return t.iter('{%s}EntityDescriptor' % NS['md'])
def has_tag(t, tag):
tags = t.iter(tag)
return next(tags, sentinel) is not sentinel
def url2host(url):
try:
(host, sep, port) = urlparse(url).netloc.partition(':')
return host
except ValueError:
return None
def subdomains(domain):
dl = []
dsplit = domain.split('.')
if len(dsplit) < 3:
dl.append(domain)
else:
for i in range(1, len(dsplit) - 1):
dl.append(".".join(dsplit[i:]))
return dl
def ddist(a, b):
if len(a) > len(b):
return ddist(b, a)
a = a.split('.')
b = b.split('.')
d = [x[0] == x[1] for x in zip(a[::-1], b[::-1])]
if False in d:
return d.index(False)
return len(a)
def avg_domain_distance(d1, d2):
dd = 0
n = 0
for a in d1.split(';'):
for b in d2.split(';'):
d = ddist(a, b)
if log.isDebugEnabled():
log.debug("ddist %s %s -> %d" % (a, b, d))
dd += d
n += 1
return int(dd / n)