|
| 1 | +import copy |
| 2 | + |
1 | 3 | from datapackage_pipelines.wrapper import spew, ingest |
2 | 4 | from decimal import Decimal |
3 | 5 |
|
4 | 6 | parameters, datapackage, res_iter = ingest() |
5 | 7 |
|
| 8 | +last_completed_year = str(parameters['last-completed-year']) |
| 9 | + |
6 | 10 | amounts = [ |
7 | 11 | 'net', |
8 | 12 | 'gross', |
|
69 | 73 | resource['schema']['fields'] = new_fields |
70 | 74 |
|
71 | 75 |
|
| 76 | +def process_row(row, phase_key): |
| 77 | + for amount, factor in zip(amounts, factors): |
| 78 | + value = row[amount] |
| 79 | + if value is not None and value.strip() != '': |
| 80 | + value = Decimal(row[amount]) * factor |
| 81 | + row[amount + '_' + phase_key] = value |
| 82 | + del row[amount] |
| 83 | + |
| 84 | + save = {} |
| 85 | + for code_key, title_key in codes_and_titles: |
| 86 | + save[code_key] = row[code_key] |
| 87 | + if len(save[code_key]) % 2 == 1: |
| 88 | + save[code_key] = '0' + save[code_key] |
| 89 | + save[title_key] = row[title_key] |
| 90 | + del row[code_key] |
| 91 | + del row[title_key] |
| 92 | + |
| 93 | + if int(save[codes_and_titles[0][0]]) != 0: |
| 94 | + hierarchy = [['00', 'המדינה']] |
| 95 | + else: |
| 96 | + hierarchy = [] |
| 97 | + for i, (code_key, title_key) in enumerate(codes_and_titles): |
| 98 | + expected_length = i*2 + 4 |
| 99 | + row['code'] = '0'*(expected_length-len(save[code_key])) + save[code_key] |
| 100 | + row['title'] = save[title_key] or 'לא ידוע' |
| 101 | + row['hierarchy'] = hierarchy |
| 102 | + row['parent'] = None if len(hierarchy) == 0 else hierarchy[-1][0] |
| 103 | + yield row |
| 104 | + hierarchy.append([row['code'], row['title']]) |
| 105 | + |
| 106 | + row['hierarchy'] = None |
| 107 | + row['parent'] = None |
| 108 | + |
| 109 | + row['code'] = 'C%d' % (int(row['func_cls_code_1']),) |
| 110 | + row['title'] = '%s' % (row['func_cls_title_1'],) |
| 111 | + yield row |
| 112 | + |
| 113 | + row['code'] = 'C%d%02d' % (int(row['func_cls_code_1']), int(row['func_cls_code_2'])) |
| 114 | + row['title'] = '%s/%s' % (row['func_cls_title_1'], row['func_cls_title_2']) |
| 115 | + yield row |
| 116 | + |
| 117 | + if not row['code'].startswith('0000'): |
| 118 | + row['code'] = '00' |
| 119 | + row['title'] = 'המדינה' |
| 120 | + row['hierarchy'] = [] |
| 121 | + yield row |
| 122 | + |
| 123 | + |
72 | 124 | def process_first(rows): |
73 | | - for row in rows: |
| 125 | + for i, row in enumerate(rows): |
74 | 126 | phase_key = phases[row['phase']] |
75 | 127 | del row['phase'] |
76 | 128 |
|
77 | | - for amount, factor in zip(amounts, factors): |
78 | | - value = row[amount] |
79 | | - if value is not None and value.strip() != '': |
80 | | - value = Decimal(row[amount]) * factor |
81 | | - row[amount + '_' + phase_key] = value |
82 | | - del row[amount] |
83 | | - |
84 | | - save = {} |
85 | | - for code_key, title_key in codes_and_titles: |
86 | | - save[code_key] = row[code_key] |
87 | | - if len(save[code_key]) % 2 == 1: |
88 | | - save[code_key] = '0' + save[code_key] |
89 | | - save[title_key] = row[title_key] |
90 | | - del row[code_key] |
91 | | - del row[title_key] |
92 | | - |
93 | | - if int(save[codes_and_titles[0][0]]) != 0: |
94 | | - hierarchy = [['00', 'המדינה']] |
95 | | - else: |
96 | | - hierarchy = [] |
97 | | - for i, (code_key, title_key) in enumerate(codes_and_titles): |
98 | | - expected_length = i*2 + 4 |
99 | | - row['code'] = '0'*(expected_length-len(save[code_key])) + save[code_key] |
100 | | - row['title'] = save[title_key] or 'לא ידוע' |
101 | | - row['hierarchy'] = hierarchy |
102 | | - row['parent'] = None if len(hierarchy) == 0 else hierarchy[-1][0] |
103 | | - yield row |
104 | | - hierarchy.append([row['code'], row['title']]) |
105 | | - |
106 | | - row['hierarchy'] = None |
107 | | - row['parent'] = None |
108 | | - |
109 | | - row['code'] = 'C%d' % (int(row['func_cls_code_1']),) |
110 | | - row['title'] = '%s' % (row['func_cls_title_1'],) |
111 | | - yield row |
112 | | - |
113 | | - row['code'] = 'C%d%02d' % (int(row['func_cls_code_1']), int(row['func_cls_code_2'])) |
114 | | - row['title'] = '%s/%s' % (row['func_cls_title_1'], row['func_cls_title_2']) |
115 | | - yield row |
| 129 | + row_ = copy.deepcopy(row) |
| 130 | + yield from process_row(row_, phase_key) |
116 | 131 |
|
117 | | - if not row['code'].startswith('0000'): |
118 | | - row['code'] = '00' |
119 | | - row['title'] = 'המדינה' |
120 | | - row['hierarchy'] = [] |
121 | | - yield row |
| 132 | + if row['year'] > last_completed_year: |
| 133 | + yield from process_row(row, 'revised') |
122 | 134 |
|
123 | 135 |
|
124 | 136 | def process_resources(res_iter_): |
|
0 commit comments