-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupdate_procedure.py
152 lines (133 loc) · 4.75 KB
/
update_procedure.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def detect_auteur_depot_from_url(url):
if '/propositions/' in url or '/PIONAN' in url:
return "Députés"
elif '/leg/ppl' in url:
return "Sénateurs"
return "Gouvernement"
def add_auteur_depot(step):
if step.get('step', '') == 'depot' and step['debats_order'] is not None:
step['auteur_depot'] = detect_auteur_depot_from_url(step.get('source_url', ''))
def remove_interventions_too_small(step, intervs):
# TODO: this warning is never triggered, maybe this code is obsolete ?
if step.get('has_interventions') and step['directory'] not in intervs:
print("[warning] [update_procedure] removing nearly empty interventions steps for", step['directory'])
step['has_interventions'] = False
def process(procedure, articles, intervs={}):
"""
Process procedure to
- add a fake `enddate` where it's useful
- detect the steps we want to display
(add a `debats_order` to those steps)
- add `auteur_depot`
- remove extra predicted steps
"""
articles = articles['articles']
good_steps = {}
for _, a in articles.items():
for s in a['steps']:
stepid = s['directory']
if stepid not in good_steps:
good_steps[stepid] = int(s['directory'].split('_')[0])
if not good_steps:
raise Exception('[update_procedure] no steps to display, parsing must have failed')
currently_debated_step = None
for s in procedure['steps']:
if s.get('in_discussion'):
currently_debated_step = s
if 'enddate' not in s and currently_debated_step is None:
s['enddate'] = s.get('date')
s['debats_order'] = None
if 'directory' in s:
if currently_debated_step == s and good_steps:
s['debats_order'] = max(good_steps.values()) + 1
elif not currently_debated_step:
s['debats_order'] = good_steps.get(s['directory'], None)
remove_interventions_too_small(s, intervs)
add_auteur_depot(s)
# remove predicted steps after the one in discussion
if not procedure.get('url_jo'):
steps_to_keep = []
for s in procedure['steps']:
if s.get('stage') == 'promulgation':
break
steps_to_keep.append(s)
if s.get('in_discussion'):
break
else:
raise Exception("[update_procedure] no step in discussion for live text")
procedure['steps'] = steps_to_keep
return procedure
if __name__ == "__main__":
# test: do not mark promulgation step as visible step even for
# unfinished texts (#95)
result = process(
{
"url_jo": None,
"steps": [
{
"date": "2011-12-01",
"directory": "00_",
"enddate": "2011-12-01",
"institution": "senat",
"stage": "1ère lecture",
"step": "depot",
},
{
"date": "2011-12-13",
"directory": "02_",
"institution": "senat",
"stage": "1ère lecture",
"step": "hemicycle",
},
{
"date": None,
"directory": "03_",
"institution": "gouvernement",
"stage": "promulgation",
},
],
},
{
"articles": {
"1er": {
"steps": [
{"directory": "00_"},
{"directory": "01_"},
{"directory": "02_"},
]
}
}
},
)
try:
assert result == {
"steps": [
{
"auteur_depot": "Gouvernement",
"date": "2011-12-01",
"debats_order": 0,
"directory": "00_",
"enddate": "2011-12-01",
"institution": "senat",
"stage": "1ère lecture",
"step": "depot",
},
{
"date": "2011-12-13",
"debats_order": 2,
"directory": "02_",
"enddate": "2011-12-13",
"institution": "senat",
"stage": "1ère lecture",
"step": "hemicycle",
}
],
"url_jo": None,
}
except Exception as e:
import pprint
pprint.pprint(result)
raise e
print("[update_procedure] TESTS OK")