Skip to content

Commit

Permalink
Network Package file info bug fix, support for EMME Flow (#234)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bccheung authored Jan 9, 2024
1 parent d920327 commit 3bfe67d
Show file tree
Hide file tree
Showing 2 changed files with 411 additions and 399 deletions.
89 changes: 76 additions & 13 deletions TMGToolbox/src/input_output/export_network_package.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
"""
Copyright 2014 Travel Modelling Group, Department of Civil Engineering, University of Toronto
This file is part of the TMG Toolbox.
The TMG Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The TMG Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the TMG Toolbox. If not, see <http://www.gnu.org/licenses/>.
"""

from contextlib import contextmanager
from datetime import datetime
import inro.modeller as m
from os import path
import json
import shutil
import tempfile
import traceback
import zipfile
from contextlib import contextmanager
from datetime import datetime
from os import path

import inro.modeller as m
import six

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

# import six library for python2 to python3 conversion
import six
# initalize python3 types
_util.initalizeModellerTypes(m)


class ExportNetworkPackage(m.Tool()):
version = '1.2.2'
tool_run_msg = ''
version = '1.2.3'
tool_run_msg = ""
number_of_tasks = 11 # For progress reporting, enter the integer number of tasks here

Scenario = m.Attribute(m.InstanceType)
Expand Down Expand Up @@ -137,7 +143,6 @@ def check_all_flag(self):
return self.ExportAllFlag

def __call__(self, scenario_number, ExportFile, export_attributes):

self.Scenario = mm.emmebank.scenario(scenario_number)
if self.Scenario is None:
raise Exception('Scenario %s was not found!' % scenario_number)
Expand All @@ -160,7 +165,19 @@ def _execute(self):
'Scenario': str(self.Scenario.id), 'Export File': path.splitext(self.ExportFile)[0],
'Version': self.version, 'self': self.__MODELLER_NAMESPACE__
}
with m.logbook_trace(name='%s v%s' % (self.__class__.__name__, self.version), attributes=logbook_attributes):
logbook_entry_name = '%s v%s' % (self.__class__.__name__, self.version)
with m.logbook_trace(name=logbook_entry_name, attributes=logbook_attributes):
# Save a snapshot of the inputs in the logbook
snapshot = {
"Scenario": self.Scenario.id,
"ExportFile": self.ExportFile,
"ExportToEmmeOldVersion": self.ExportToEmmeOldVersion,
"ExportAllFlag": self.ExportAllFlag,
"AttributeIdsToExport": self.AttributeIdsToExport,
"ExportMetadata": self.ExportMetadata
}
m.logbook_snapshot(name=logbook_entry_name, comment='', namespace=str(self), value=json.dumps(snapshot))

# Due to the dynamic nature of the selection process, it could happen that attributes are
# selected which don't exist in the current scenario. The method checks early to catch
# any problems
Expand Down Expand Up @@ -386,7 +403,7 @@ def _get_select_attribute_options_json(self):
label = "{id} ({domain}) - {name}".format(id=att.name, domain=att.type, name=att.description)
keyval[att.name] = label
return keyval

@m.method(return_type=six.text_type)
def _get_select_attribute_options_html(self):
list_ = []
Expand All @@ -399,7 +416,53 @@ def _get_select_attribute_options_html(self):
@m.method(return_type=m.TupleType)
def percent_completed(self):
return self.TRACKER.getProgress()

@m.method(return_type=six.text_type)
def tool_run_msg_status(self):
return self.tool_run_msg
return self.tool_run_msg

# region Snapshot and stateful interfaces

def to_snapshot(self):
att_ids = None if len(self.AttributeIdsToExport) == 0 else self.AttributeIdsToExport
snapshot = {
"Scenario": self.Scenario.id,
"ExportFile": self.ExportFile,
"ExportToEmmeOldVersion": self.ExportToEmmeOldVersion,
"ExportAllFlag": self.ExportAllFlag,
"AttributeIdsToExport": att_ids,
"ExportMetadata": self.ExportMetadata
}
return json.dumps(snapshot)

def from_snapshot(self, snapshot):
snapshot = json.loads(snapshot)
att_ids = [] if snapshot["AttributeIdsToExport"] is None else snapshot["AttributeIdsToExport"]

emmebank = mm.emmebank
self.Scenario = emmebank.scenario(snapshot["Scenario"])
self.ExportFile = snapshot["ExportFile"]
self.ExportToEmmeOldVersion = bool(snapshot["ExportToEmmeOldVersion"])
self.ExportAllFlag = bool(snapshot["ExportAllFlag"])
self.AttributeIdsToExport = att_ids
self.ExportMetadata = snapshot["ExportMetadata"]

def __getitem__(self, key):
value = getattr(self, key)
return value

def __setitem__(self, key, value):
setattr(self, key, value)

def get_state(self):
state = {
"Scenario": self.Scenario,
"ExportFile": self.ExportFile,
"ExportToEmmeOldVersion": self.ExportToEmmeOldVersion,
"ExportAllFlag": self.ExportAllFlag,
"AttributeIdsToExport": self.AttributeIdsToExport,
"ExportMetadata": self.ExportMetadata
}
return state

# endregion
Loading

0 comments on commit 3bfe67d

Please sign in to comment.