-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththings.py
63 lines (49 loc) · 1.76 KB
/
things.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
from collections import defaultdict
import codecs, os, datetime
import yaml
__all__ = ['Thing']
class Thing(object):
short = ""
long = ""
icon = ""
page_prefix = ""
def __init__(self, fname=None, key=None, **kwds):
if (fname is not None and key is not None) or (fname is None and key is None):
raise ValueError("Provide one of fname or key")
if fname is not None:
self._fname = fname
_, tail = os.path.split(fname)
self.key, _ = os.path.splitext(tail)
self._yaml_obj = yaml.safe_load(codecs.open(fname, 'r', encoding='utf-8'))
for k, v in self._yaml_obj.items():
setattr(self, k, v)
else:
self.key = key
for k, v in kwds.items():
setattr(self, k, v)
self.things = defaultdict(set)
if hasattr(self, 'last_updated'):
d, m, y = list(map(int, self.last_updated.split('-')))
self.last_updated = datetime.datetime(y, m, d)
if not hasattr(self, 'year'):
self.year = y
if hasattr(self, 'name') and not hasattr(self, 'title'):
self.title = self.name
if hasattr(self, 'title') and not hasattr(self, 'name'):
self.name = self.title
self.page = f'{self.page_prefix}{self.key}.html'
self.validate()
def validate(self):
pass
def add_thing(self, thing):
self.things[thing.__class__.__name__].add(thing)
self.things['all'].add(thing)
@property
def paper_count(self):
return len(self.things['Paper'])
@property
def software_count(self):
return len(self.things['Software'])
@property
def thing_count(self):
return len(self.things['all'])