Passed
Push — master ( 7db8f9...61eb58 )
by Fabio
01:02
created

scripts.updatedata   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 53
dl 0
loc 76
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A main() 0 2 1
B _update_municipalities_data() 0 59 2
1
# -*- coding: utf-8 -*-
2
3
from benedict import benedict
4
from slugify import slugify
5
6
import fsutil
7
8
9
def _update_municipalities_data():
10
11
    data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/ANPR_archivio_comuni.csv"
12
    data = benedict.from_csv(data_url)
13
    data.standardize()
14
15
    def map_item(item):
16
17
        status = item.get("stato", "").upper()
18
        assert len(status) == 1 and status in ["A", "C"]
19
        active = status == "A"
20
21
        code = item.get_str("codcatastale").upper()
22
        assert code == "ND" or len(code) == 4, f"Invalid code: {code}"
23
24
        name = item.get_str("denominazione_it").title()
25
        assert name != "", f"Invalid name: {name}"
26
27
        name_trans = item.get_str("denomtraslitterata").title()
28
        name_alt = item.get_str("altradenominazione").title()
29
        name_alt_trans = item.get_str("altradenomtraslitterata").title()
30
        name_slugs = list(
31
            set(
32
                filter(
33
                    bool,
34
                    [
35
                        slugify(name),
36
                        slugify(name_trans),
37
                        slugify(name_alt),
38
                        slugify(name_alt_trans),
39
                    ],
40
                )
41
            )
42
        )
43
        province = item.get("siglaprovincia", "").upper()
44
        assert len(province) == 2, f"Invalid province: {province}"
45
46
        date_created = item.get_str("dataistituzione")
47
        date_deleted = item.get_str("datacessazione")
48
        if date_deleted.startswith("9999"):
49
            date_deleted = ""
50
51
        return {
52
            "active": active,
53
            "code": code,
54
            "date_created": date_created,
55
            "date_deleted": date_deleted,
56
            "name": name,
57
            "name_trans": name_trans,
58
            "name_alt": name_alt,
59
            "name_alt_trans": name_alt_trans,
60
            "name_slugs": name_slugs,
61
            "province": province,
62
        }
63
64
    output_data = [map_item(benedict(item)) for item in data["values"]]
65
    output_path = "../codicefiscale/data/municipalities.json"
66
    output_abspath = fsutil.join_path(__file__, output_path)
67
    fsutil.write_file_json(output_abspath, output_data, indent=4)
68
69
70
def main():
71
    _update_municipalities_data()
72
73
74
if __name__ == "__main__":
75
    main()
76