-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.py
66 lines (49 loc) · 1.58 KB
/
data.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
import logging
import json
import jsoncomment
import buv_types
from validate import incrementalValidateAndAdd
json=jsoncomment.JsonComment(json)
# in memory 'DB' of all data, mapping SHA256 strings to
# objects.
all_data={}
# initial membership proposal
genesis_members_hash=None
# maps handles to addresses, any seen
addr_for_handle={}
def reset():
global all_data
all_data={}
from buv_types import Vote, ProposalMetadata, MemberDict, ProposalText, Election
def for_type(t):
for v in all_data.itervalues():
if isinstance(v, t):
yield v
def readFile(fn):
logging.info("Reading file "+fn)
s=open(fn).read()
is_json=False
try:
J=json.loads(s)
is_json=True
except Exception:
logging.info("Not a JSON file. Interpreting as raw (proposal) data.")
obj=buv_types.ProposalText(s, fn)
if is_json:
try:
obj=buv_types.constructJSON[J["type"]](s, fn, False)
#obj_str=json.dumps(obj.asJSON(), encoding='utf-8', ensure_ascii=True))
except buv_types.InvalidSignatureError:
logging.warning("Invalid signature. Ignoring.")
return
if obj.sha256 in all_data:
logging.warning("Object %s with hash %s has already been loaded." % (obj, obj.sha256))
else:
incrementalValidateAndAdd(obj)
def readFiles(fns):
reset()
for fn in fns:
readFile(fn)
def readFilesFromJSONListfile(prefix, listfn):
filelist=json.load(open(listfn))
readFiles(prefix+"/"+fn for fn in filelist["files"])