forked from utwente-energy/demkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemkit.py
137 lines (110 loc) · 4.88 KB
/
demkit.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
# Copyright 2023 University of Twente
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import importlib
import os
import sys
from conf.usrconf import load_demkit_config
sys.stderr.write("_________________________________________________________________________________________________\n")
sys.stderr.write(' --- IECON Application DEMKit EMS ---\n')
sys.stderr.write(' Based on DEMKIT version 2023.3\n\n')
sys.stderr.write('Copyright 2023 University of Twente, Enschede, the Netherlands\n')
sys.stderr.write('Copyright 2025 Saxion University of Applied Sciences, Enschede, the Netherlands\n')
sys.stderr.write('\nLicensed under the Apache License, Version 2.0 (the "License")\n')
sys.stderr.write("_________________________________________________________________________________________________\n")
sys.stderr.flush()
# Move to the application folder
os.chdir(os.path.dirname(os.path.realpath(__file__)))
app_path = os.getcwd() # Save the current application path
sys.stderr.write(f"Current working directory: {os.getcwd()} " + "\n")
# Add config path to the system
sys.path.insert(0, os.path.join(app_path, 'conf/')) # Add to system path
# Load config file
from conf.usrconf import demCfg
try:
if demCfg['ver'] < 4:
sys.stderr.write(
"[ERROR] Incorrect configuration version found. Make sure to have a proper conf/usrconf.py file!"
" Please refer to the provided exanple.\n")
sys.stderr.flush()
exit()
if demCfg['ver'] < 4.1:
sys.stderr.write(
"\n[WARNING] Old configuration version detected."
" Please refer to the usrconf.py.misc example file to see the changes.\n"
)
sys.stderr.write(
"[WARNING] DEMKit will use the default values for missing configuration entries"
" but manual configuration is advised.\n"
)
sys.stderr.write("[WARNING] Current expected config version is: 4.1.\n")
sys.stderr.write("[WARNING] Changelog: \n")
sys.stderr.write("[WARNING] Version 4.1 adds: demCfg['var'] entries and demCfg['timezone'] entry. \n\n")
sys.stderr.flush()
# Variable output for logs ans backups (stored within the workspace folder of a model by default)
demCfg['var'] = {}
demCfg['var']['backup'] = os.path.join(app_path, "var/backup/")
demCfg['var']['databasebackup'] = os.path.join(app_path, "var/backup/database/")
demCfg['var']['log'] = os.path.join(app_path, "var/log/")
# Timezone information
from pytz import timezone
demCfg['timezonestr'] = 'Europe/Amsterdam'
demCfg['timezone'] = timezone(demCfg['timezonestr'])
# Add trailing slash
if demCfg['env']['path'][-1] != '\\' and demCfg['env']['path'][-1] != '/':
demCfg['env']['path'] += '/'
if demCfg['workspace']['path'][-1] != '\\' and demCfg['workspace']['path'][-1] != '/':
demCfg['workspace']['path'] += '/'
except:
sys.stderr.write(
"Errors occurred when loading the configuration file. Make sure to have a proper conf/usrconf.py file!"
" Please refer to the provided exanple.\n"
)
sys.stderr.flush()
exit()
sys.stderr.write('pyDEM directory: ' + demCfg['env']['path'] + '\n')
sys.stderr.write('User model directory: ' + demCfg['workspace']['path'] + '\n')
sys.stderr.flush()
# Load the DEM platform
sys.path.insert(0, os.path.join(app_path, demCfg['env']['path']))
modelPath = demCfg['workspace']['path']
try:
modelName = demCfg['model']['name']
except:
modelName = ""
# Get arguments:
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--folder')
parser.add_argument('-m', '--model')
parser.add_argument('-s', '--socket')
parser.add_argument('-u', '--smarthouseusb')
# Parse arguments
args = parser.parse_args()
if args.folder:
if (args.folder[:1] == "/") or (args.folder[:2] == "~/"):
modelPath = args.folder
else:
modelPath += args.folder
if args.model:
modelName = args.model
if args.socket:
demCfg['network']['sockPath'] = args.socket
if args.smarthouseusb:
demCfg['smarthouse']['usb'] = args.smarthouseusb
sys.stderr.write('Loading model: ' + modelName + ' from ' + modelPath + '\n')
sys.stderr.flush()
# import the model path
sys.path.insert(0, os.path.join(app_path, modelPath))
# change the working directory to the model directory
os.chdir(modelPath)
# Load the desired model
importlib.import_module(modelName)