forked from opengaming/osgameclones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_ext.py
53 lines (39 loc) · 1.44 KB
/
_ext.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
import sys
import pprint
import os.path as op
from datetime import date, timedelta
import yaml
from cyrax import events
def abort(msg):
sys.stderr.write(msg + '\n')
sys.exit(1)
def validate(item, key):
for name in names(item):
if not (isinstance(name, basestring) or
(len(name) == 2 and
all(isinstance(x, basestring) for x in name))):
abort('Error: %r should be a string or a list of two strings' % name)
games = item[key]
if (not isinstance(games, list) or
not all(map(lambda x: isinstance(x, dict), games))):
print 'Error: this should be a list of dicts:'
abort(pprint.pformat(games))
return names, games
def names(item):
return item.get('names') or [item['name']]
def mark_new(entry):
added = entry.get('added') or date(1970, 1, 1)
return dict(entry,
new=(date.today() - added) < timedelta(days=30))
def parse_data(site):
data = yaml.load(file(op.join(op.dirname(__file__), 'games.yaml')))
site.clones = [
(names(item), map(mark_new, item['clones']))
for item in data
if 'clones' in item and validate(item, 'clones')]
site.reimplementations = [
(names(item), map(mark_new, item['reimplementations']))
for item in data
if 'reimplementations' in item and validate(item, 'reimplementations')]
def callback(site):
events.events.connect('traverse-started', parse_data)