Passed
Push — master ( c88b19...443080 )
by Koen
02:19
created

atramhasis.scripts.initializedb   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 107
dl 0
loc 136
rs 10
c 0
b 0
f 0
1
import json
2
import os
3
import sys
4
from datetime import date
5
from datetime import datetime
6
7
from pyramid.paster import get_appsettings
8
from pyramid.paster import setup_logging
9
from pyramid.scripts.common import parse_vars
10
from skosprovider.providers import VocabularyProvider
11
from skosprovider_sqlalchemy import utils as skosprovider_utils
12
from skosprovider_sqlalchemy.models import ConceptScheme
13
from skosprovider_sqlalchemy.models import Label
14
from sqlalchemy import engine_from_config
15
from sqlalchemy.orm import Session
16
17
from atramhasis.data.models import ExpandStrategy
18
from atramhasis.data.models import IDGenerationStrategy
19
from atramhasis.data.models import Provider
20
21
22
def usage(argv):
23
    cmd = os.path.basename(argv[0])
24
    print('usage: %s <config_uri> [var=value]\n'
25
          '(example: "%s development.ini")' % (cmd, cmd))
26
    sys.exit(1)
27
28
29
def json_serial(obj):
30
    if isinstance(obj, (datetime, date)):
31
        return obj.isoformat()
32
    raise TypeError(f"Type {obj} not serializable")
33
34
35
def initialize_providers(session):
36
    from fixtures.data import trees, geo
37
    from fixtures.styles_and_cultures import styles_and_cultures
38
    from fixtures.materials import materials
39
    from fixtures.eventtypes import eventtypes
40
    from fixtures.heritagetypes import heritagetypes
41
    from fixtures.periods import periods
42
    from fixtures.species import species
43
    from fixtures.bluebirds import bluebirds
44
45
    import_provider(
46
        trees,
47
        session,
48
        ConceptScheme(
49
            id=1,
50
            uri='urn:x-skosprovider:trees',
51
            labels=[
52
                Label('Verschillende soorten bomen', 'prefLabel', 'nl'),
53
                Label('Different types of trees', 'prefLabel', 'en')
54
            ]
55
        ),
56
    )
57
    import_provider(
58
        geo,
59
        session,
60
        ConceptScheme(
61
            id=2,
62
            uri='urn:x-skosprovider:geo',
63
            labels=[
64
                Label('Geografie', 'prefLabel', 'nl'),
65
                Label('Geography', 'prefLabel', 'en')
66
            ]
67
        ),
68
    )
69
    import_provider(
70
        styles_and_cultures,
71
        session,
72
        ConceptScheme(
73
            id=3,
74
            uri='https://id.erfgoed.net/thesauri/stijlen_en_culturen',
75
            labels=[
76
                Label('Stijlen en Culturen', 'prefLabel', 'nl'),
77
                Label('Styles and Cultures', 'prefLabel', 'en')
78
            ]
79
        ),
80
    )
81
    import_provider(
82
        materials,
83
        session,
84
        ConceptScheme(
85
            id=4,
86
            uri='https://id.erfgoed.net/thesauri/materialen',
87
            labels=[
88
                Label('Materialen', 'prefLabel', 'nl'),
89
                Label('Materials', 'prefLabel', 'en')
90
            ]
91
        ),
92
    )
93
    import_provider(
94
        eventtypes,
95
        session,
96
        ConceptScheme(
97
            id=5,
98
            uri='https://id.erfgoed.net/thesauri/gebeurtenistypes',
99
            labels=[
100
                Label('Gebeurtenistypes', 'prefLabel', 'nl'),
101
                Label('Event types', 'prefLabel', 'en')
102
            ]
103
        ),
104
    )
105
    import_provider(
106
        heritagetypes,
107
        session,
108
        ConceptScheme(
109
            id=6,
110
            uri='https://id.erfgoed.net/thesauri/erfgoedtypes',
111
            labels=[
112
                Label('Erfgoedtypes', 'prefLabel', 'nl'),
113
                Label('Heritage types', 'prefLabel', 'en')
114
            ]
115
        ),
116
    )
117
    import_provider(
118
        periods,
119
        session,
120
        ConceptScheme(
121
            id=7,
122
            uri='https://id.erfgoed.net/thesauri/dateringen',
123
            labels=[
124
                Label('Dateringen', 'prefLabel', 'nl'),
125
                Label('Periods', 'prefLabel', 'en')
126
            ]
127
        ),
128
    )
129
    import_provider(
130
        species,
131
        session,
132
        ConceptScheme(
133
            id=8,
134
            uri='https://id.erfgoed.net/thesauri/soorten',
135
            labels=[
136
                Label('Soorten', 'prefLabel', 'nl'),
137
                Label('Species', 'prefLabel', 'en')
138
            ]
139
        ),
140
    )
141
    import_provider(
142
        bluebirds,
143
        session,
144
        ConceptScheme(
145
            id=9,
146
            uri='https://id.bluebirds.org',
147
            labels=[
148
                Label('Blauwe vogels', 'prefLabel', 'nl'),
149
                Label('Blue birds', 'prefLabel', 'en')
150
            ]
151
        ),
152
    )
153
154
155
def import_provider(
156
    provider: VocabularyProvider, session: Session, conceptscheme: ConceptScheme
157
):
158
    concept_scheme = skosprovider_utils.import_provider(
159
        provider, session, conceptscheme=conceptscheme,
160
    )
161
162
    if provider.uri_generator:
163
        uri_pattern = getattr(provider.uri_generator, 'pattern', None)
164
    else:
165
        uri_pattern = None
166
167
    db_provider = Provider()
168
    db_provider.meta = json.loads(json.dumps(provider.metadata, default=json_serial))
169
    db_provider.id_generation_strategy = IDGenerationStrategy.NUMERIC
170
    db_provider.expand_strategy = ExpandStrategy.RECURSE
171
    db_provider.conceptscheme = concept_scheme
172
    db_provider.id = provider.get_vocabulary_id()
173
    db_provider.uri_pattern = uri_pattern
174
    if 'conceptscheme_id' in db_provider.meta:
175
        del db_provider.meta['conceptscheme_id']
176
177
    session.add(db_provider)
178
179
180
def main(argv=sys.argv):
181
    if len(argv) < 2:
182
        usage(argv)
183
184
    config_uri = argv[1]
185
    options = parse_vars(argv[2:])
186
    setup_logging(config_uri)
187
    settings = get_appsettings(config_uri, options=options)
188
189
    engine = engine_from_config(settings, 'sqlalchemy.')
190
    with Session(engine) as session:
191
        initialize_providers(session)
192
        session.commit()
193
    print('--atramhasis-db-initialized--')
194
195
196
if __name__ == '__main__':
197
    main()