-
Notifications
You must be signed in to change notification settings - Fork 0
/
addPlant.py
executable file
·80 lines (67 loc) · 2.27 KB
/
addPlant.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
os.environ.setdefault('PLANTMONITOR_MODULE_SETTINGS', 'conf.settings.devel')
import datetime
import click
from yamlns import namespace as ns
from conf.logging_configuration import LOGGING
import logging
import logging.config
logging.config.dictConfig(LOGGING)
logger = logging.getLogger("plantmonitor")
from pony import orm
from ORM.pony_manager import PonyManager
from ORM.models import (
importPlants,
)
# yaml = ns.loads("""\
# municipalities:
# - municipality:
# name: Figueres
# countryCode : ES
# country : Spain
# regionCode : '09'
# region : Cataluña
# provinceCode : '17'
# province : Girona
# ineCode : '17066'
# plants:
# - plant:
# name: alcolea
# codename: SCSOM04
# description: la bonica planta
# meters:
# - meter:
# name: '1234578'
# inverters:
# - inverter:
# name: '5555'
# - inverter:
# name: '6666'
# irradiationSensors:
# - irradiationSensor:
# name: alberto
# temperatureSensors:
# - temperatureSensor:
# name: joana
# integratedSensors:
# - integratedSensor:
# name: voki""")
def importPlantsFromFile(db, yamlFilename):
nsplants = ns.load(yamlFilename)
with orm.db_session:
importPlants(db, nsplants)
@click.command()
@click.argument('yaml', type=click.Path(exists=True))
def importPlantCLI(yaml):
yamlFilename = click.format_filename(yaml)
logger.debug(yamlFilename)
from conf import envinfo
pony = PonyManager(envinfo.DB_CONF)
# orm.set_sql_debug(debug=True, show_values=True)
pony.define_all_models()
pony.binddb(create_tables=False)
importPlantsFromFile(pony.db, yamlFilename)
if __name__ == "__main__":
importPlantCLI()