Skip to content

Commit 3bfe67d

Browse files
authored
Network Package file info bug fix, support for EMME Flow (#234)
* Added snapshot and stateful interfaces to the export network package tool * Handle attribute id list * Code styling tweaks * Clean up of import network package code * Fixed file info processing bug (need to decode binary string) * Added snapshot and stateful interfaces to the import network package tool
1 parent d920327 commit 3bfe67d

2 files changed

Lines changed: 411 additions & 399 deletions

File tree

TMGToolbox/src/input_output/export_network_package.py

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
"""
22
Copyright 2014 Travel Modelling Group, Department of Civil Engineering, University of Toronto
3+
34
This file is part of the TMG Toolbox.
5+
46
The TMG Toolbox is free software: you can redistribute it and/or modify
57
it under the terms of the GNU General Public License as published by
68
the Free Software Foundation, either version 3 of the License, or
79
(at your option) any later version.
10+
811
The TMG Toolbox is distributed in the hope that it will be useful,
912
but WITHOUT ANY WARRANTY; without even the implied warranty of
1013
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1114
GNU General Public License for more details.
15+
1216
You should have received a copy of the GNU General Public License
1317
along with the TMG Toolbox. If not, see <http://www.gnu.org/licenses/>.
1418
"""
1519

16-
from contextlib import contextmanager
17-
from datetime import datetime
18-
import inro.modeller as m
19-
from os import path
20+
import json
2021
import shutil
2122
import tempfile
2223
import traceback
2324
import zipfile
25+
from contextlib import contextmanager
26+
from datetime import datetime
27+
from os import path
28+
29+
import inro.modeller as m
30+
import six
2431

2532
mm = m.Modeller()
2633
_util = mm.module('tmg.common.utilities')
@@ -35,14 +42,13 @@
3542
_export_functions = mm.tool('inro.emme.data.function.export_functions')
3643
_pdu = mm.module('tmg.common.pandas_utils')
3744

38-
# import six library for python2 to python3 conversion
39-
import six
4045
# initalize python3 types
4146
_util.initalizeModellerTypes(m)
4247

48+
4349
class ExportNetworkPackage(m.Tool()):
44-
version = '1.2.2'
45-
tool_run_msg = ''
50+
version = '1.2.3'
51+
tool_run_msg = ""
4652
number_of_tasks = 11 # For progress reporting, enter the integer number of tasks here
4753

4854
Scenario = m.Attribute(m.InstanceType)
@@ -137,7 +143,6 @@ def check_all_flag(self):
137143
return self.ExportAllFlag
138144

139145
def __call__(self, scenario_number, ExportFile, export_attributes):
140-
141146
self.Scenario = mm.emmebank.scenario(scenario_number)
142147
if self.Scenario is None:
143148
raise Exception('Scenario %s was not found!' % scenario_number)
@@ -160,7 +165,19 @@ def _execute(self):
160165
'Scenario': str(self.Scenario.id), 'Export File': path.splitext(self.ExportFile)[0],
161166
'Version': self.version, 'self': self.__MODELLER_NAMESPACE__
162167
}
163-
with m.logbook_trace(name='%s v%s' % (self.__class__.__name__, self.version), attributes=logbook_attributes):
168+
logbook_entry_name = '%s v%s' % (self.__class__.__name__, self.version)
169+
with m.logbook_trace(name=logbook_entry_name, attributes=logbook_attributes):
170+
# Save a snapshot of the inputs in the logbook
171+
snapshot = {
172+
"Scenario": self.Scenario.id,
173+
"ExportFile": self.ExportFile,
174+
"ExportToEmmeOldVersion": self.ExportToEmmeOldVersion,
175+
"ExportAllFlag": self.ExportAllFlag,
176+
"AttributeIdsToExport": self.AttributeIdsToExport,
177+
"ExportMetadata": self.ExportMetadata
178+
}
179+
m.logbook_snapshot(name=logbook_entry_name, comment='', namespace=str(self), value=json.dumps(snapshot))
180+
164181
# Due to the dynamic nature of the selection process, it could happen that attributes are
165182
# selected which don't exist in the current scenario. The method checks early to catch
166183
# any problems
@@ -386,7 +403,7 @@ def _get_select_attribute_options_json(self):
386403
label = "{id} ({domain}) - {name}".format(id=att.name, domain=att.type, name=att.description)
387404
keyval[att.name] = label
388405
return keyval
389-
406+
390407
@m.method(return_type=six.text_type)
391408
def _get_select_attribute_options_html(self):
392409
list_ = []
@@ -399,7 +416,53 @@ def _get_select_attribute_options_html(self):
399416
@m.method(return_type=m.TupleType)
400417
def percent_completed(self):
401418
return self.TRACKER.getProgress()
402-
419+
403420
@m.method(return_type=six.text_type)
404421
def tool_run_msg_status(self):
405-
return self.tool_run_msg
422+
return self.tool_run_msg
423+
424+
# region Snapshot and stateful interfaces
425+
426+
def to_snapshot(self):
427+
att_ids = None if len(self.AttributeIdsToExport) == 0 else self.AttributeIdsToExport
428+
snapshot = {
429+
"Scenario": self.Scenario.id,
430+
"ExportFile": self.ExportFile,
431+
"ExportToEmmeOldVersion": self.ExportToEmmeOldVersion,
432+
"ExportAllFlag": self.ExportAllFlag,
433+
"AttributeIdsToExport": att_ids,
434+
"ExportMetadata": self.ExportMetadata
435+
}
436+
return json.dumps(snapshot)
437+
438+
def from_snapshot(self, snapshot):
439+
snapshot = json.loads(snapshot)
440+
att_ids = [] if snapshot["AttributeIdsToExport"] is None else snapshot["AttributeIdsToExport"]
441+
442+
emmebank = mm.emmebank
443+
self.Scenario = emmebank.scenario(snapshot["Scenario"])
444+
self.ExportFile = snapshot["ExportFile"]
445+
self.ExportToEmmeOldVersion = bool(snapshot["ExportToEmmeOldVersion"])
446+
self.ExportAllFlag = bool(snapshot["ExportAllFlag"])
447+
self.AttributeIdsToExport = att_ids
448+
self.ExportMetadata = snapshot["ExportMetadata"]
449+
450+
def __getitem__(self, key):
451+
value = getattr(self, key)
452+
return value
453+
454+
def __setitem__(self, key, value):
455+
setattr(self, key, value)
456+
457+
def get_state(self):
458+
state = {
459+
"Scenario": self.Scenario,
460+
"ExportFile": self.ExportFile,
461+
"ExportToEmmeOldVersion": self.ExportToEmmeOldVersion,
462+
"ExportAllFlag": self.ExportAllFlag,
463+
"AttributeIdsToExport": self.AttributeIdsToExport,
464+
"ExportMetadata": self.ExportMetadata
465+
}
466+
return state
467+
468+
# endregion

0 commit comments

Comments
 (0)