예제 #1
0
    def __call__(self, xtmf_JSON, xtmf_logbook_level):
        logbook = _m.logbook_level()
        if xtmf_logbook_level == "NONE":
            _m.logbook_level(_m.LogbookLevel.NONE)
        # xtmf_MatrixType, xtmf_MatrixNumber, ExportFile, xtmf_ScenarioNumber
        parameters = json.loads(xtmf_JSON)
        xtmf_MatrixType = parameters["matrix_type"]
        xtmf_MatrixNumber = parameters["matrix_number"]
        self.ExportFile = parameters["file_location"]
        xtmf_ScenarioNumber = parameters["scenario_number"]
        if not xtmf_MatrixType in self.MATRIX_TYPES:
            raise IOError("Matrix type '%s' is not recognized. Valid types are " %xtmf_MatrixType + 
                          "1 for scalar, 2 for origin, 3 for destination, and "+ 
                          "4 for full matrices.")
        
        self.MatrixId = self.MATRIX_TYPES[xtmf_MatrixType] + str(xtmf_MatrixNumber)
        if _bank.matrix(self.MatrixId) == None:
            raise IOError("Matrix %s does not exist." %self.MatrixId)
        
        if _util.databankHasDifferentZones(_bank):
            self.Scenario = _bank.scenario(xtmf_ScenarioNumber)
            if self.Scenario == None:
                raise Exception("A valid scenario must be specified as there are " +
                                    "multiple zone systems in this Emme project. "+
                                    "'%s' is not a valid scenario." %xtmf_ScenarioNumber)

        try:
            self._Execute()
        except Exception, e:
            msg = str(e) + "\n" + _traceback.format_exc(e)
            raise Exception(msg)
예제 #2
0
    def __call__(self, xtmf_JSON, xtmf_logbook_level):
        logbook = _m.logbook_level()
        if xtmf_logbook_level == "NONE":
            _m.logbook_level(_m.LogbookLevel.NONE)
        # xtmf_ScenarioNumber, ExportFile, xtmf_AttributeIdString
        parameters = json.loads(xtmf_JSON)
        self.ExportFile = parameters["export_file"]
        self.Scenario = _m.Modeller().emmebank.scenario(
            parameters["scenario_number"])
        if self.Scenario is None:
            raise Exception("Scenario %s was not found!" % xtmf_ScenarioNumber)
        xtmf_AttributeIdString = parameters["extra_attributes"]

        if xtmf_AttributeIdString.lower() == 'all':
            self.ExportAllFlag = True  # if true, self.AttributeIdsToExport gets set in execute
        else:
            cells = xtmf_AttributeIdString.split(',')
            self.AttributeIdsToExport = [
                str(c.strip()) for c in cells if c.strip()
            ]  # Clean out null values
        try:
            self._execute()
        except Exception, e:
            msg = str(e) + "\n" + _traceback.format_exc(e)
            raise Exception(msg)
예제 #3
0
 def set_global_logbook_level(self, props):
     self._log_level = props.get("RunModel.LogbookLevel", "ENABLED")
     log_all = _m.LogbookLevel.ATTRIBUTE | _m.LogbookLevel.VALUE | _m.LogbookLevel.COOKIE | _m.LogbookLevel.TRACE | _m.LogbookLevel.LOG
     log_states = {
         "ENABLED":
         log_all,
         "DISABLE_ON_ERROR":
         log_all,
         "NO_EXTERNAL_REPORTS":
         log_all,
         "NO_REPORTS":
         _m.LogbookLevel.ATTRIBUTE | _m.LogbookLevel.COOKIE
         | _m.LogbookLevel.TRACE | _m.LogbookLevel.LOG,
         "TITLES_ONLY":
         _m.LogbookLevel.TRACE | _m.LogbookLevel.LOG,
         "DISABLED":
         _m.LogbookLevel.NONE,
     }
     _m.logbook_write("Setting logbook level to %s" % self._log_level)
     try:
         _m.logbook_level(log_states[self._log_level])
     except KeyError:
         raise Exception(
             "properties.RunModel.LogLevel: value must be one of %s" %
             ",".join(log_states.keys()))
예제 #4
0
 def __call__(self, xtmf_JSON, xtmf_logbook_level):
     logbook = _m.logbook_level()
     if xtmf_logbook_level == "NONE":
         _m.logbook_level(_m.LogbookLevel.NONE)
     parameters = json.loads(xtmf_JSON)
     FromScenario = parameters['from_scenario']
     ToScenario = parameters['to_scenario']
     CopyStrategy = parameters['copy_strategy']
     try:
         self._execute(FromScenario, ToScenario, CopyStrategy)
     except Exception, e:
         raise Exception(_traceback.format_exc(e))
예제 #5
0
 def run_proc(self, name, arguments, log_message, capture_output=False):
     path = _join(self._path, "bin", name)
     if not os.path.exists(path):
         raise Exception("No command / batch file '%s'" % path)
     command = path + " " + " ".join([str(x) for x in arguments])
     attrs = {"command": command, "name": name, "arguments": arguments}
     with _m.logbook_trace(log_message, attributes=attrs):
         if capture_output and self._log_level != "NO_EXTERNAL_REPORTS":
             report = _m.PageBuilder(title="Process run %s" % name)
             report.add_html(
                 'Command:<br><br><div class="preformat">%s</div><br>' %
                 command)
             # temporary file to capture output error messages generated by Java
             err_file_ref, err_file_path = _tempfile.mkstemp(suffix='.log')
             err_file = os.fdopen(err_file_ref, "w")
             try:
                 output = _subprocess.check_output(command,
                                                   stderr=err_file,
                                                   cwd=self._path,
                                                   shell=True)
                 report.add_html(
                     'Output:<br><br><div class="preformat">%s</div>' %
                     output)
             except _subprocess.CalledProcessError as error:
                 report.add_html(
                     'Output:<br><br><div class="preformat">%s</div>' %
                     error.output)
                 raise
             finally:
                 err_file.close()
                 with open(err_file_path, 'r') as f:
                     error_msg = f.read()
                 os.remove(err_file_path)
                 if error_msg:
                     report.add_html(
                         'Error message(s):<br><br><div class="preformat">%s</div>'
                         % error_msg)
                 try:
                     # No raise on writing report error
                     # due to observed issue with runs generating reports which cause
                     # errors when logged
                     _m.logbook_write("Process run %s report" % name,
                                      report.render())
                 except Exception as error:
                     print _time.strftime("%Y-%M-%d %H:%m:%S")
                     print "Error writing report '%s' to logbook" % name
                     print error
                     print _traceback.format_exc(error)
                     if self._log_level == "DISABLE_ON_ERROR":
                         _m.logbook_level(_m.LogbookLevel.NONE)
         else:
             _subprocess.check_call(command, cwd=self._path, shell=True)
    def __call__(self, xtmf_JSON, xtmf_logbook_level):
        logbook = _m.logbook_level()
        if xtmf_logbook_level == "NONE":
            _m.logbook_level(_m.LogbookLevel.NONE)

        parameters_JSON = json.loads(xtmf_JSON)
        self.network_package_file = parameters_JSON['network_package_file']
        self.scenario_description = ""
        self.scenario_Id = parameters_JSON['scenario_number']
        self.overwrite_scenario_flag = True
        self.add_functions = parameters_JSON['add_functions']
        if self.add_functions == True or self.add_functions == 'True':
            self.conflict_option = parameters_JSON['conflict_option']
        else:
            self.conflict_option = 'PRESERVE'
        try:
            self._execute()
        except Exception, e:
            msg = str(e) + "\n" + _traceback.format_exc(e)
            raise Exception(msg)
예제 #7
0
        
        if _util.databankHasDifferentZones(_bank):
            self.Scenario = _bank.scenario(xtmf_ScenarioNumber)
            if self.Scenario == None:
                raise Exception("A valid scenario must be specified as there are " +
                                    "multiple zone systems in this Emme project. "+
                                    "'%s' is not a valid scenario." %xtmf_ScenarioNumber)

        try:
            self._Execute()
        except Exception, e:
            msg = str(e) + "\n" + _traceback.format_exc(e)
            raise Exception(msg)
        finally:
            if logbook != None:
                _m.logbook_level(logbook)
    
    ##########################################################################################################    
    
    #---
    #---MAIN EXECUTION CODE
    
    def _Execute(self):
        with _m.logbook_trace(name="{classname} v{version}".format(classname=(self.__class__.__name__), version=self.version),
                                     attributes=self._GetAtts()):
            
            matrix = _bank.matrix(self.MatrixId)
            
            if _util.databankHasDifferentZones(_bank):
                data = matrix.get_data(self.Scenario)
            else:
예제 #8
0
 def EnableLogbook(self):
     _m.logbook_level(self.previous_level)
예제 #9
0
 def DisableLogbook(self):
     self.previous_level = inro.modeller.logbook_level()
     _m.logbook_level(inro.modeller.LogbookLevel.NONE)
예제 #10
0
    def ExecuteModule(self):
        macroName = None
        parameterString = None
        timer = None
        # run the module here
        try:
            #figure out how long the macro's name is
            macroName = self.ReadString()
            parameterString = self.ReadString()
            logbook_level = self.ReadString()
            if not self.EnsureModellerToolExists(macroName):
                return
            self.SignalToolExists()

            tool = self.CreateTool(macroName)
            if 'run_xtmf' not in dir(tool):
                self.SendIncompatibleTool(macroName)
                return
            nameSpace = {
                'tool': tool,
                'parameters': json.loads(parameterString)
            }
            callString = 'tool.run_xtmf(parameters)'

            #Now that everything is ready, attach an instance of ourselves into
            #the tool so they can send progress reports
            tool.XTMFBridge = self

            if "percent_completed" in dir(tool):
                timer = ProgressTimer(tool.percent_completed, self)
                timer.start()
            #Execute the tool, getting the return value
            previous_logbook_level = _m.logbook_level()
            if logbook_level == 'STANDARD':
                _m.logbook_level(_m.LogbookLevel.TRACE | _m.LogbookLevel.LOG)
            elif logbook_level == "NONE":
                _m.logbook_level(_m.LogbookLevel.NONE)
            else:
                # Enable everything for debugging
                _m.logbook_level(_m.LogbookLevel.TRACE | _m.LogbookLevel.LOG
                                 | _m.LogbookLevel.COOKIE
                                 | _m.LogbookLevel.ATTRIBUTE
                                 | _m.LogbookLevel.VALUE)
            try:
                ret = eval(callString, nameSpace, None)
            finally:
                _m.logbook_level(previous_logbook_level)

            if timer != None:
                timer.stop()

            nameSpace = None
            if ret == None:
                self.SendSuccess()
            else:
                self.SendReturnSuccess(ret)
        except Exception, inst:
            if timer != None:
                timer.stop()
            _m.logbook_write("We are in the exception code for ExecuteModule")
            if (macroName != None):
                _m.logbook_write("Macro Name: " + macroName)
            else:
                _m.logbook_write("Macro Name: None")
            if (parameterString != None):
                _m.logbook_write("Parameter : " + parameterString)
            else:
                _m.logbook_write("Parameter : None")
            _m.logbook_write(str(inst))
            _m.logbook_write("Finished dumping exception")

            etype, evalue, etb = sys.exc_info()
            stackList = _traceback.extract_tb(etb)
            msg = "%s: %s\n\nStack trace below:" % (evalue.__class__.__name__,
                                                    str(evalue))
            stackList.reverse()
            for file, line, func, text in stackList:
                msg += "\n  File '%s', line %s, in %s" % (file, line, func)
            self.SendRuntimeError(msg)
            print msg
예제 #11
0
 def EnableLogbook(self):
     _m.logbook_level(self.previous_level)
예제 #12
0
 def DisableLogbook(self):
     self.previous_level = inro.modeller.logbook_level()
     _m.logbook_level(inro.modeller.LogbookLevel.NONE)