aboutsummaryrefslogtreecommitdiff
path: root/commit_classes.py
blob: c6c8bc7d235817039b375258d197d82814a77a9a (plain)
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
#!/usr/bin/env python3
import json

from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound

import pyenc
from pyenc.db import db
import pyenc.model as model

app = pyenc.create_app()
app.app_context().push()


"""
Fetch all found classes from redis, handle the data, and commit it to
our true database.
"""

# TODO this inserts already existing classes
# It shouldn't
for puppet_file in model.PuppetFile.query.all():
    data = json.loads(puppet_file.json)
    top = data['^']
    if top[0] == 'class':
        tmp = top[1]['#']
        idx = tmp.index('name')
        db.session.add(model.PuppetClass(
            class_name=tmp[idx + 1],
            comes_from=puppet_file))
        # print(tmp[idx + 1])
    elif top[0] == 'block':
        for element in top[1:]:
            if element['^'][0] == 'class':
                tmp = element['^'][1]['#']
                idx = tmp.index('name')
                db.session.add(model.PuppetClass(
                    class_name=tmp[idx + 1],
                    comes_from=puppet_file))
db.session.commit()