def parse_args(self, *args, **kwargs): """Parse the arguments as usual, then add default processing.""" if _debug: ConfigArgumentParser._debug("parse_args") # pass along to the parent class result_args = ArgumentParser.parse_args(self, *args, **kwargs) # read in the configuration file config = _ConfigParser() config.read(result_args.ini) if _debug: _log.debug(" - config: %r", config) # check for BACpypes section if not config.has_section('BACpypes'): raise RuntimeError("INI file with BACpypes section required") # convert the contents to an object ini_obj = type('ini', (object, ), dict(config.items('BACpypes'))) if _debug: _log.debug(" - ini_obj: %r", ini_obj) # add the object to the parsed arguments setattr(result_args, 'ini', ini_obj) # return what was parsed return result_args
def parse_args(self, *args, **kwargs): """Parse the arguments as usual, then add default processing.""" if _debug: ConfigArgumentParser._debug("parse_args") # pass along to the parent class result_args = ArgumentParser.parse_args(self, *args, **kwargs) # read in the configuration file config = _ConfigParser() config.read(result_args.ini) if _debug: _log.debug(" - config: %r", config) # check for BACpypes section if not config.has_section('BACpypes'): raise RuntimeError("INI file with BACpypes section required") # convert the contents to an object ini_obj = type('ini', (object,), dict(config.items('BACpypes'))) if _debug: _log.debug(" - ini_obj: %r", ini_obj) # add the object to the parsed arguments setattr(result_args, 'ini', ini_obj) # return what was parsed return result_args
def __parseConfig(self, filename): try: text = awlFileRead(filename, encoding="UTF-8") p = _ConfigParser() p.readfp(StringIO(text), filename) self.__debug = p.getint("PROFIBUS", "debug", fallback=0) self.__phyType = p.get("PHY", "type", fallback="serial") self.__phyDev = p.get("PHY", "dev", fallback="/dev/ttyS0") self.__phyBaud = p.getint("PHY", "baud", fallback=19200) self.__dpMasterClass = p.getint("DP", "master_class", fallback=1) if self.__dpMasterClass not in {1, 2}: raise ValueError("Invalid master_class") self.__dpMasterAddr = p.getint("DP", "master_addr", fallback=0x02) if self.__dpMasterAddr < 0 or self.__dpMasterAddr > 127: raise ValueError("Invalid master_addr") self.__slaveConfs = [] for section in p.sections(): m = self.__reSlave.match(section) if not m: continue s = self._SlaveConf() s.addr = p.getint(section, "addr") s.gsd = self.pyprofibus.gsd.interp.GsdInterp.fromFile( p.get(section, "gsd")) s.sync_mode = p.getboolean(section, "sync_mode", fallback=False) s.freeze_mode = p.getboolean(section, "freeze_mode", fallback=False) s.group_mask = p.getboolean(section, "group_mask", fallback=1) if s.group_mask < 0 or s.group_mask > 0xFF: raise ValueError("Invalid group_mask") s.watchdog_ms = p.getint(section, "watchdog_ms", fallback=5000) if s.watchdog_ms < 0 or s.watchdog_ms > 255 * 255: raise ValueError("Invalid watchdog_ms") s.input_size = p.getint(section, "input_size") if s.input_size < 0 or s.input_size > 246: raise ValueError("Invalid input_size") s.output_size = p.getint(section, "output_size") if s.output_size < 0 or s.output_size > 246: raise ValueError("Invalid output_size") mods = [o for o in p.options(section) if self.__reMod.match(o)] mods.sort(key=lambda o: self.__reMod.match(o).group(1)) for option in mods: s.gsd.setConfiguredModule(p.get(section, option)) self.__slaveConfs.append(s) except (_ConfigParserError, AwlParserError, ValueError) as e: self.raiseException("Profibus config file parse " "error:\n%s" % str(e)) except self.pyprofibus.gsd.parser.GsdError as e: self.raiseException("Failed to parse GSD file:\n%s" % str(e))
def _save_state_to_s3(self): # Dump immutable state data to a config state = _ConfigParser(allow_no_value=True) state.optionxform = str state.add_section(PredictiveService._SERVICE_INFO_SECTION_NAME) state.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'Name', self.name) state.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'Description', self._description) state.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'API Key', self._api_key) # Save environment, if we have one if self._environment: state.add_section(PredictiveService._ENVIRONMENT_SECTION_NAME) for (key, value) in self._environment._get_state().iteritems(): state.set(PredictiveService._ENVIRONMENT_SECTION_NAME, key, value) # Save deployment version data to config state.add_section(PredictiveService._DEPLOYMENT_SECTION_NAME) current_predictive_objects = _copy(self._all_predictive_objects) for (model_name, info) in current_predictive_objects.iteritems(): state.set(PredictiveService._DEPLOYMENT_SECTION_NAME, model_name, info['version']) state.add_section(PredictiveService._PREDICTIVE_OBJECT_DOCSTRING) for (model_name, info) in current_predictive_objects.iteritems(): state.set(PredictiveService._PREDICTIVE_OBJECT_DOCSTRING, model_name, info['docstring'].encode('string_escape')) if self._has_state_changed_on_s3(): raise IOError("Can not save changes. The Predictive Service has changed on S3. Please " "reload from S3.") # Save any new predictive objects to S3. for predictive_object_name in self._local_changes: (predictive_object, po_info) = self._local_changes[predictive_object_name] if predictive_object: # if this is not a model deletion: save_path = self._get_predictive_object_save_path(predictive_object_name, po_info['version']) dependency_path = self._get_dependency_save_path(predictive_object_name, po_info['version']) predictive_object.save(save_path, dependency_path, self.aws_credentials) # Update the revision number after we have successfully written all predictive objects self._revision_number += 1 state.add_section(self._META_SECTION_NAME) state.set(self._META_SECTION_NAME, 'Revision Number', self._revision_number) state.set(self._META_SECTION_NAME, 'Schema Version', self._schema_version) # Write state file to S3 with _NamedTemporaryFile() as temp_file: state.write(temp_file) temp_file.flush() conn = _connect_s3(**self.aws_credentials) bucket = conn.get_bucket(self._s3_bucket_name, validate=False) key = _s3_key(bucket) key.key = self._s3_state_key key.set_contents_from_filename(temp_file.name) temp_file.close() # deletes temp file # Update our state self._local_changes = {} self._predictive_objects = dict(zip(current_predictive_objects.keys(), [{'version':info['version'], 'docstring': info['docstring']} for info in current_predictive_objects.values()]))
def generate_init_state_file(name, environment, description, api_key, admin_key, aws_credentials, cors_origin, global_cache_state, system_config): ''' Initialize a new Predictive Service state file and save to s3/hdfs/local. ''' # Construct a few states all_keys = {} all_keys[api_key] = { "id": api_key, "type": "api", "endpoints": None, "adhoc_execute": False } all_keys[admin_key] = {"id": admin_key, "type": "admin"} scale_factor = 1 revision_number = 1 # Dump state data to a config state = _ConfigParser(allow_no_value=True) state.optionxform = str state.add_section(_SERVICE_INFO_SECTION_NAME) state.set(_SERVICE_INFO_SECTION_NAME, 'Name', name) state.set(_SERVICE_INFO_SECTION_NAME, 'Creation Time', int(_time())) state.set(_SERVICE_INFO_SECTION_NAME, 'Description', description) state.set(_SERVICE_INFO_SECTION_NAME, 'Keys', _json.dumps(all_keys)) state.set(_SERVICE_INFO_SECTION_NAME, 'CORS Origin', cors_origin) state.set(_SERVICE_INFO_SECTION_NAME, 'Global Cache State', global_cache_state) state.set(_SERVICE_INFO_SECTION_NAME, 'Global Scale Factor', scale_factor) # Save environment if environment: state.add_section(_ENVIRONMENT_SECTION_NAME) for (key, value) in environment._get_state().iteritems(): state.set(_ENVIRONMENT_SECTION_NAME, key, value) # initialize deployment version data to config state.add_section(_DEPLOYMENT_SECTION_NAME) state.add_section(_PREDICTIVE_OBJECT_DOCSTRING) # Update the revision number after we have successfully written all endpoints revision_number += 1 state.add_section(_META_SECTION_NAME) state.set(_META_SECTION_NAME, 'Revision Number', revision_number) # Save system config. params state.add_section(_SYSTEM_SECTION_NAME) system_config.set_state_in_config(state, _SYSTEM_SECTION_NAME) # Add environment variables section state.add_section(_ENVIRONMENT_VARIABLE_SECTION_NAME) return state
def _get_state_from_file(state_path): state_key = os.path.join(state_path, 'state.ini') tmp_state_file = state_key if not os.path.exists(tmp_state_file): raise ValueError("Missing config file at %r" % (tmp_state_file,)) config = _ConfigParser(allow_no_value=True) config.optionxform = str config.read(tmp_state_file) if not config.has_section('Service Info'): raise ValueError("Config error: Expected 'Service Info' section in %s" % (tmp_state_file,)) return config
def _read_inifile( file=_os.environ.get("KB_DEPLOYMENT_CONFIG", _os.environ["HOME"] + "/.kbase_config") # @ReservedAssignment ): # Another bandaid to read in the ~/.kbase_config file if one is present authdata = None if _os.path.exists(file): try: config = _ConfigParser() config.read(file) # strip down whatever we read to only what is legit authdata = { x: config.get("authentication", x) if config.has_option("authentication", x) else None for x in ("user_id", "token", "client_secret", "keyfile", "keyfile_passphrase", "password") } except Exception, e: print "Error while reading INI file %s: %s" % (file, e)
def _read_inifile(file=_os.environ.get( # @ReservedAssignment 'KB_DEPLOYMENT_CONFIG', _os.environ['HOME'] + '/.kbase_config')): # Another bandaid to read in the ~/.kbase_config file if one is present authdata = None if _os.path.exists(file): try: config = _ConfigParser() config.read(file) # strip down whatever we read to only what is legit authdata = {x: config.get('authentication', x) if config.has_option('authentication', x) else None for x in ('user_id', 'token', 'client_secret', 'keyfile', 'keyfile_passphrase', 'password')} except Exception, e: print "Error while reading INI file %s: %s" % (file, e)
def _get_s3_state_config(s3_bucket_name, s3_key_name, credentials): conn = _connect_s3(**credentials) bucket = conn.get_bucket(s3_bucket_name, validate=False) key = bucket.get_key(s3_key_name) if not key: raise IOError("No Predictive Service at the specified location.") with _NamedTemporaryFile() as temp_file: key.get_contents_to_file(temp_file) temp_file.flush() config = _ConfigParser(allow_no_value=True) config.optionxform = str config.read(temp_file.name) temp_file.close() # deletes temp file return config
def save_client_config(self, file_path, predictive_service_cname): ''' Create the config file that can be used by applications accessing the Predictive Service. The file is stored in an .ini format, with the specific information necessary to query this deployment. Parameters ---------- file_path : str The path where the config file will be saved. predictive_service_cname : str The CNAME for the Predictive Service endpoint. It is *highly recommended* that all client connect through a CNAME record that you have created. If this value is set to None, the "A" record will be used. Using the "A" record is only advisable for testing, since this value may change over time. ''' self._ensure_not_terminated(); out = _ConfigParser(allow_no_value=True) out.optionxform = str out.add_section(PredictiveService._SERVICE_INFO_SECTION_NAME) out.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'api key', self._api_key) if self._environment.certificate_name: out.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'verify certificate', not(self._environment.certificate_is_self_signed)) schema = 'https://' else: schema = 'http://' if predictive_service_cname: out.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'endpoint', schema + predictive_service_cname) else: _logger.warn('Creating client config using the "A" Record name. You\'re crazy to use' \ ' this for anything other than testing!') out.set(PredictiveService._SERVICE_INFO_SECTION_NAME, 'endpoint', schema + self._environment.load_balancer_dns_name) with open(file_path, 'w') as f: out.write(f)
def _get_state_from_file(state_path, logger=logging.getLogger(__name__)): state_key = os.path.join(state_path, "state.ini") tmp_state_file = state_key if not os.path.exists(tmp_state_file): msg = f"Missing config file at {tmp_state_file}" logger.log(logging.CRITICAL, msg) raise ValueError(msg) config = _ConfigParser(allow_no_value=True) config.optionxform = str config.read(tmp_state_file) if not config.has_section("Service Info"): msg = "Config error: Expected [Service Info] section in " f"{tmp_state_file}" logger.log(logging.CRITICAL, msg) raise ValueError(msg) return config
def expand_args(self, result_args): """Take the result of parsing the args and interpret them.""" if _debug: ConfigArgumentParser._debug("expand_args %r", result_args) # read in the configuration file config = _ConfigParser() config.read(result_args.ini) if _debug: _log.debug(" - config: %r", config) # check for BACpypes section if not config.has_section('BACpypes'): raise RuntimeError("INI file with BACpypes section required") # convert the contents to an object ini_obj = Settings(dict(config.items('BACpypes'))) if _debug: _log.debug(" - ini_obj: %r", ini_obj) # add the object to the parsed arguments setattr(result_args, 'ini', ini_obj) # continue with normal expansion ArgumentParser.expand_args(self, result_args)
def update_config(self): loglevel = self.get_log_level() logfile = self.get_log_file() self._api_log_level = -1 self._msgs_since_config_update = 0 self._time_at_config_update = _datetime.datetime.now() # Retrieving the control API defined log level api_url = None if _os.path.isfile(self._mlog_config_file): cfg = _ConfigParser() cfg.read(self._mlog_config_file) cfgitems = self._get_config_items(cfg, _GLOBAL) cfgitems.update(self._get_config_items(cfg, self._subsystem)) if MLOG_LOG_LEVEL in cfgitems: try: self._config_log_level = int(cfgitems[MLOG_LOG_LEVEL]) except: _warnings.warn( 'Cannot parse log level {} from file {} to int'.format( cfgitems[MLOG_LOG_LEVEL], self._mlog_config_file) + '. Keeping current log level.') if MLOG_API_URL in cfgitems: api_url = cfgitems[MLOG_API_URL] if MLOG_LOG_FILE in cfgitems: self._config_log_file = cfgitems[MLOG_LOG_FILE] elif self._mlog_config_file: _warnings.warn('Cannot read config file ' + self._mlog_config_file) if(api_url): subsystem_api_url = api_url + "/" + self._subsystem try: data = _json.load(_urllib2.urlopen(subsystem_api_url, timeout=5)) except _urllib2.URLError, e: code_ = None if hasattr(e, 'code'): code_ = ' ' + str(e.code) _warnings.warn( 'Could not connect to mlog api server at ' + '{}:{} {}. Using default log level {}.'.format( subsystem_api_url, code_, str(e.reason), str(DEFAULT_LOG_LEVEL))) else: max_matching_level = -1 for constraint_set in data['log_levels']: level = constraint_set['level'] constraints = constraint_set['constraints'] if level <= max_matching_level: continue matches = 1 for constraint in constraints: if constraint not in self._log_constraints: matches = 0 elif (self._log_constraints[constraint] != constraints[constraint]): matches = 0 if matches == 1: max_matching_level = level self._api_log_level = max_matching_level
def fromText(cls, text, projectFile): if not cls.dataIsProject(text.encode("utf-8")): raise AwlSimError("Project file: The data is "\ "not an awlsim project.") projectDir = os.path.dirname(projectFile) awlSources = [] symTabSources = [] cpuSpecs = S7CPUSpecs() obTempPresetsEn = False extInsnsEn = False try: p = _ConfigParser() p.readfp(StringIO(text), projectFile) version = p.getint("AWLSIM_PROJECT", "file_version") expectedVersion = 0 if version != expectedVersion: raise AwlSimError("Project file: Unsupported version. "\ "File version is %d, but expected %d." %\ (version, expectedVersion)) # CPU section for i in range(0xFFFF): option = "awl_file_%d" % i if not p.has_option("CPU", option): break path = p.get("CPU", option) src = AwlSource.fromFile(path, cls.__generic2path(path, projectDir)) awlSources.append(src) for i in range(0xFFFF): srcOption = "awl_%d" % i nameOption = "awl_name_%d" % i if not p.has_option("CPU", srcOption): break awlBase64 = p.get("CPU", srcOption) name = None if p.has_option("CPU", nameOption): try: name = base64.b64decode(p.get("CPU", nameOption)) name = name.decode("utf-8", "ignore") except (TypeError, binascii.Error) as e: pass if name is None: name = "AWL/STL #%d" % i src = AwlSource.fromBase64(name, awlBase64) awlSources.append(src) if p.has_option("CPU", "mnemonics"): mnemonics = p.getint("CPU", "mnemonics") cpuSpecs.setConfiguredMnemonics(mnemonics) if p.has_option("CPU", "nr_accus"): nrAccus = p.getint("CPU", "nr_accus") cpuSpecs.setNrAccus(nrAccus) if p.has_option("CPU", "clock_memory_byte"): clockMemByte = p.getint("CPU", "clock_memory_byte") cpuSpecs.setClockMemByte(clockMemByte) if p.has_option("CPU", "ob_startinfo_enable"): obTempPresetsEn = p.getboolean("CPU", "ob_startinfo_enable") if p.has_option("CPU", "ext_insns_enable"): extInsnsEn = p.getboolean("CPU", "ext_insns_enable") # SYMBOLS section for i in range(0xFFFF): option = "sym_tab_file_%d" % i if not p.has_option("SYMBOLS", option): break path = p.get("SYMBOLS", option) src = SymTabSource.fromFile(path, cls.__generic2path(path, projectDir)) symTabSources.append(src) for i in range(0xFFFF): srcOption = "sym_tab_%d" % i nameOption = "sym_tab_name_%d" % i if not p.has_option("SYMBOLS", srcOption): break symTabBase64 = p.get("SYMBOLS", srcOption) name = None if p.has_option("SYMBOLS", nameOption): try: name = base64.b64decode(p.get("SYMBOLS", nameOption)) name = name.decode("utf-8", "ignore") except (TypeError, binascii.Error) as e: pass if name is None: name = "Symbol table #%d" % i src = SymTabSource.fromBase64(name, symTabBase64) symTabSources.append(src) except _ConfigParserError as e: raise AwlSimError("Project parser error: " + str(e)) return cls(projectFile = projectFile, awlSources = awlSources, symTabSources = symTabSources, cpuSpecs = cpuSpecs, obTempPresetsEn = obTempPresetsEn, extInsnsEn = extInsnsEn)
def fromText(cls, text, projectFile): if not cls.dataIsProject(text.encode("utf-8")): raise AwlSimError("Project file: The data is "\ "not an awlsim project.") projectDir = os.path.dirname(projectFile) createDate = None modifyDate = None awlSources = [] symTabSources = [] libSelections = [] cpuSpecs = S7CPUSpecs() obTempPresetsEn = False extInsnsEn = False guiSettings = GuiSettings() linkSettings = CoreLinkSettings() hwmodSettings = HwmodSettings() try: p = _ConfigParser() p.readfp(StringIO(text), projectFile) # AWLSIM_PROJECT section version = p.getint("AWLSIM_PROJECT", "file_version") expectedVersion = 0 if version != expectedVersion: raise AwlSimError("Project file: Unsupported version. "\ "File version is %d, but expected %d." %\ (version, expectedVersion)) if p.has_option("AWLSIM_PROJECT", "date"): # Compatibility only. "date" is deprecated. dStr = p.get("AWLSIM_PROJECT", "date") createDate = datetime.datetime.strptime(dStr, cls.DATETIME_FMT) modifyDate = createDate if p.has_option("AWLSIM_PROJECT", "create_date"): dStr = p.get("AWLSIM_PROJECT", "create_date") createDate = datetime.datetime.strptime(dStr, cls.DATETIME_FMT) if p.has_option("AWLSIM_PROJECT", "modify_date"): dStr = p.get("AWLSIM_PROJECT", "modify_date") modifyDate = datetime.datetime.strptime(dStr, cls.DATETIME_FMT) # CPU section for i in range(0xFFFF): option = "awl_file_%d" % i if not p.has_option("CPU", option): break path = p.get("CPU", option) src = AwlSource.fromFile(path, cls.__generic2path(path, projectDir)) awlSources.append(src) for i in range(0xFFFF): srcOption = "awl_%d" % i nameOption = "awl_name_%d" % i if not p.has_option("CPU", srcOption): break awlBase64 = p.get("CPU", srcOption) name = None if p.has_option("CPU", nameOption): try: name = base64ToStr(p.get("CPU", nameOption)) except ValueError as e: pass if name is None: name = "AWL/STL #%d" % i src = AwlSource.fromBase64(name, awlBase64) awlSources.append(src) if p.has_option("CPU", "mnemonics"): mnemonics = p.getint("CPU", "mnemonics") cpuSpecs.setConfiguredMnemonics(mnemonics) if p.has_option("CPU", "nr_accus"): nrAccus = p.getint("CPU", "nr_accus") cpuSpecs.setNrAccus(nrAccus) if p.has_option("CPU", "clock_memory_byte"): clockMemByte = p.getint("CPU", "clock_memory_byte") cpuSpecs.setClockMemByte(clockMemByte) if p.has_option("CPU", "ob_startinfo_enable"): obTempPresetsEn = p.getboolean("CPU", "ob_startinfo_enable") if p.has_option("CPU", "ext_insns_enable"): extInsnsEn = p.getboolean("CPU", "ext_insns_enable") # SYMBOLS section for i in range(0xFFFF): option = "sym_tab_file_%d" % i if not p.has_option("SYMBOLS", option): break path = p.get("SYMBOLS", option) src = SymTabSource.fromFile(path, cls.__generic2path(path, projectDir)) symTabSources.append(src) for i in range(0xFFFF): srcOption = "sym_tab_%d" % i nameOption = "sym_tab_name_%d" % i if not p.has_option("SYMBOLS", srcOption): break symTabBase64 = p.get("SYMBOLS", srcOption) name = None if p.has_option("SYMBOLS", nameOption): try: name = base64ToStr(p.get("SYMBOLS", nameOption)) except ValueError as e: pass if name is None: name = "Symbol table #%d" % i src = SymTabSource.fromBase64(name, symTabBase64) symTabSources.append(src) # LIBS section for i in range(0xFFFF): nameOption = "lib_name_%d" % i blockOption = "lib_block_%d" % i effOption = "lib_block_effective_%d" % i if not p.has_option("LIBS", nameOption): break try: libName = base64ToStr(p.get("LIBS", nameOption)) except ValueError as e: continue block = p.get("LIBS", blockOption).upper().strip() effBlock = p.getint("LIBS", effOption) try: if block.startswith("FC"): entryType = AwlLibEntrySelection.TYPE_FC entryIndex = int(block[2:].strip()) elif block.startswith("FB"): entryType = AwlLibEntrySelection.TYPE_FB entryIndex = int(block[2:].strip()) elif block.startswith("UNKNOWN"): entryType = AwlLibEntrySelection.TYPE_UNKNOWN entryIndex = int(block[7:].strip()) else: raise ValueError if entryIndex < -1 or entryIndex > 0xFFFF: raise ValueError except ValueError: raise AwlSimError("Project file: Invalid " "library block: %s" % block) libSelections.append( AwlLibEntrySelection(libName = libName, entryType = entryType, entryIndex = entryIndex, effectiveEntryIndex = effBlock) ) # CORE_LINK section if p.has_option("CORE_LINK", "spawn_local"): linkSettings.setSpawnLocalEn( p.getboolean("CORE_LINK", "spawn_local")) if p.has_option("CORE_LINK", "spawn_local_port_range"): pRange = p.get("CORE_LINK", "spawn_local_port_range") try: pRange = pRange.split(":") begin = int(pRange[0]) end = int(pRange[1]) if end < begin: raise ValueError pRange = range(begin, end + 1) except (IndexError, ValueError) as e: raise AwlSimError("Project file: Invalid port range") linkSettings.setSpawnLocalPortRange(pRange) if p.has_option("CORE_LINK", "spawn_local_interpreters"): interp = p.get("CORE_LINK", "spawn_local_interpreters") try: interp = base64ToStr(interp) except ValueError as e: raise AwlSimError("Project file: " "Invalid interpreter list") linkSettings.setSpawnLocalInterpreters(interp) if p.has_option("CORE_LINK", "connect_host"): host = p.get("CORE_LINK", "connect_host") try: host = base64ToStr(host) except ValueError as e: raise AwlSimError("Project file: " "Invalid host name") linkSettings.setConnectHost(host) if p.has_option("CORE_LINK", "connect_port"): port = p.getint("CORE_LINK", "connect_port") linkSettings.setConnectPort(port) if p.has_option("CORE_LINK", "connect_timeout_ms"): timeout = p.getint("CORE_LINK", "connect_timeout_ms") linkSettings.setConnectTimeoutMs(timeout) # HWMODS section for i in range(0xFFFF): nameOption = "loaded_mod_%d" % i if not p.has_option("HWMODS", nameOption): break modName = base64ToStr(p.get("HWMODS", nameOption)) modDesc = HwmodDescriptor(modName) for j in range(0x3FF): paramOption = "loaded_mod_%d_p%d" % (i, j) if not p.has_option("HWMODS", paramOption): break param = p.get("HWMODS", paramOption) try: param = param.split(":") if len(param) != 2: raise ValueError paramName = base64ToStr(param[0]) if param[1].strip(): paramValue = base64ToStr(param[1]) else: paramValue = None except ValueError: raise AwlSimError("Project file: " "Invalid hw mod parameter") modDesc.addParameter(paramName, paramValue) hwmodSettings.addLoadedModule(modDesc) # GUI section if p.has_option("GUI", "editor_autoindent"): guiSettings.setEditorAutoIndentEn( p.getboolean("GUI", "editor_autoindent")) if p.has_option("GUI", "editor_paste_autoindent"): guiSettings.setEditorPasteIndentEn( p.getboolean("GUI", "editor_paste_autoindent")) if p.has_option("GUI", "editor_validation"): guiSettings.setEditorValidationEn( p.getboolean("GUI", "editor_validation")) if p.has_option("GUI", "editor_font"): guiSettings.setEditorFont(p.get("GUI", "editor_font").strip()) except _ConfigParserError as e: raise AwlSimError("Project parser error: " + str(e)) return cls(projectFile = projectFile, createDate = createDate, modifyDate = modifyDate, awlSources = awlSources, symTabSources = symTabSources, libSelections = libSelections, cpuSpecs = cpuSpecs, obTempPresetsEn = obTempPresetsEn, extInsnsEn = extInsnsEn, guiSettings = guiSettings, coreLinkSettings = linkSettings, hwmodSettings = hwmodSettings)
def parse(cls, projectClass, text, projectFile): from awlsim.common.project import GuiSettings from awlsim.common.project import CoreLinkSettings from awlsim.common.project import HwmodSettings projectDir = os.path.dirname(projectFile) createDate = None modifyDate = None awlSources = [] fupSources = [] kopSources = [] symTabSources = [] libSelections = [] specs = S7CPUSpecs() conf = S7CPUConfig() guiSettings = GuiSettings() linkSettings = CoreLinkSettings() hwmodSettings = HwmodSettings() try: p = _ConfigParser() textIO = StringIO(text) if hasattr(p, "read_file"): #@nocov p.read_file(textIO, projectFile) else: #@nocov p.readfp(textIO, projectFile) # AWLSIM_PROJECT section version = p.getint("AWLSIM_PROJECT", "file_version") expectedVersion = 0 if version != expectedVersion: raise AwlSimError("Project file: Unsupported version. "\ "File version is %d, but expected %d." %\ (version, expectedVersion)) if p.has_option("AWLSIM_PROJECT", "date"): # Compatibility only. "date" is deprecated. dStr = p.get("AWLSIM_PROJECT", "date") try: createDate = datetime.datetime.strptime( dStr, projectClass.DATETIME_FMT) except (ValueError, TypeError) as e: createDate = None modifyDate = createDate if p.has_option("AWLSIM_PROJECT", "create_date"): dStr = p.get("AWLSIM_PROJECT", "create_date") try: createDate = datetime.datetime.strptime( dStr, projectClass.DATETIME_FMT) except (ValueError, TypeError) as e: createDate = None if p.has_option("AWLSIM_PROJECT", "modify_date"): dStr = p.get("AWLSIM_PROJECT", "modify_date") try: modifyDate = datetime.datetime.strptime( dStr, projectClass.DATETIME_FMT) except (ValueError, TypeError) as e: modifyDate = None def getSrcs(srcList, section, prefix, SrcClass): for i in range(0xFFFF): option = "%s_file_%d" % (prefix, i) if not p.has_option(section, option): break path = p.get(section, option) src = SrcClass.fromFile( name=path, filepath=RelPath(projectDir).fromRelative(path), compatReEncode=True) srcList.append(src) for i in range(0xFFFF): srcOption = "%s_%d" % (prefix, i) nameOption = "%s_name_%d" % (prefix, i) if not p.has_option(section, srcOption): break awlBase64 = p.get(section, srcOption) name = None if p.has_option(section, nameOption): with contextlib.suppress(ValueError): name = base64ToStr(p.get(section, nameOption)) if name is None: name = "%s #%d" % (SrcClass.SRCTYPE, i) src = SrcClass.fromBase64(name, awlBase64) srcList.append(src) # CPU section getSrcs(awlSources, "CPU", "awl", AwlSource) if p.has_option("CPU", "mnemonics"): mnemonics = p.getint("CPU", "mnemonics") conf.setConfiguredMnemonics(mnemonics) if p.has_option("CPU", "nr_accus"): nrAccus = p.getint("CPU", "nr_accus") specs.setNrAccus(nrAccus) if p.has_option("CPU", "clock_memory_byte"): clockMemByte = p.getint("CPU", "clock_memory_byte") conf.setClockMemByte(clockMemByte) if p.has_option("CPU", "ob_startinfo_enable"): conf.setOBStartinfoEn( p.getboolean("CPU", "ob_startinfo_enable")) if p.has_option("CPU", "ext_insns_enable"): conf.setExtInsnsEn(p.getboolean("CPU", "ext_insns_enable")) # FUP section getSrcs(fupSources, "FUP", "fup", FupSource) # KOP section getSrcs(kopSources, "KOP", "kop", KopSource) # SYMBOLS section getSrcs(symTabSources, "SYMBOLS", "sym_tab", SymTabSource) # LIBS section for i in range(0xFFFF): nameOption = "lib_name_%d" % i blockOption = "lib_block_%d" % i effOption = "lib_block_effective_%d" % i if not p.has_option("LIBS", nameOption): break try: libName = base64ToStr(p.get("LIBS", nameOption)) except ValueError as e: continue block = p.get("LIBS", blockOption).upper().strip() effBlock = p.getint("LIBS", effOption) try: if block.startswith("FC"): entryType = AwlLibEntrySelection.TYPE_FC entryIndex = int(block[2:].strip()) elif block.startswith("FB"): entryType = AwlLibEntrySelection.TYPE_FB entryIndex = int(block[2:].strip()) elif block.startswith("UNKNOWN"): entryType = AwlLibEntrySelection.TYPE_UNKNOWN entryIndex = int(block[7:].strip()) else: raise ValueError if entryIndex < -1 or entryIndex > 0xFFFF: raise ValueError except ValueError: raise AwlSimError("Project file: Invalid " "library block: %s" % block) libSelections.append( AwlLibEntrySelection(libName=libName, entryType=entryType, entryIndex=entryIndex, effectiveEntryIndex=effBlock)) # CORE_LINK section if p.has_option("CORE_LINK", "spawn_local"): linkSettings.setSpawnLocalEn( p.getboolean("CORE_LINK", "spawn_local")) if p.has_option("CORE_LINK", "spawn_local_port_range"): pRange = p.get("CORE_LINK", "spawn_local_port_range") try: pRange = pRange.split(":") begin = int(pRange[0]) end = int(pRange[1]) if end < begin: raise ValueError pRange = range(begin, end + 1) except (IndexError, ValueError) as e: raise AwlSimError("Project file: Invalid port range") linkSettings.setSpawnLocalPortRange(pRange) if p.has_option("CORE_LINK", "spawn_local_interpreters"): interp = p.get("CORE_LINK", "spawn_local_interpreters") try: interp = base64ToStr(interp) except ValueError as e: raise AwlSimError("Project file: " "Invalid interpreter list") linkSettings.setSpawnLocalInterpreters(interp) if p.has_option("CORE_LINK", "connect_host"): host = p.get("CORE_LINK", "connect_host") try: host = base64ToStr(host) except ValueError as e: raise AwlSimError("Project file: " "Invalid host name") linkSettings.setConnectHost(host) if p.has_option("CORE_LINK", "connect_port"): port = p.getint("CORE_LINK", "connect_port") linkSettings.setConnectPort(port) if p.has_option("CORE_LINK", "connect_timeout_ms"): timeout = p.getint("CORE_LINK", "connect_timeout_ms") linkSettings.setConnectTimeoutMs(timeout) if p.has_option("CORE_LINK", "tunnel"): tunnel = p.getint("CORE_LINK", "tunnel") linkSettings.setTunnel(tunnel) if p.has_option("CORE_LINK", "tunnel_local_port"): tunnelLocalPort = p.getint("CORE_LINK", "tunnel_local_port") linkSettings.setTunnelLocalPort(tunnelLocalPort) if p.has_option("CORE_LINK", "ssh_user"): sshUser = p.get("CORE_LINK", "ssh_user") try: sshUser = base64ToStr(sshUser) except ValueError as e: raise AwlSimError("Project file: " "Invalid ssh_user") linkSettings.setSSHUser(sshUser) if p.has_option("CORE_LINK", "ssh_port"): sshPort = p.getint("CORE_LINK", "ssh_port") linkSettings.setSSHPort(sshPort) if p.has_option("CORE_LINK", "ssh_executable"): sshExecutable = p.get("CORE_LINK", "ssh_executable") try: sshExecutable = base64ToStr(sshExecutable) except ValueError as e: raise AwlSimError("Project file: " "Invalid ssh_executable") linkSettings.setSSHExecutable(sshExecutable) # HWMODS section for i in range(0xFFFF): nameOption = "loaded_mod_%d" % i if not p.has_option("HWMODS", nameOption): break modName = base64ToStr(p.get("HWMODS", nameOption)) modDesc = HwmodDescriptor(modName) for j in range(0x3FF): paramOption = "loaded_mod_%d_p%d" % (i, j) if not p.has_option("HWMODS", paramOption): break param = p.get("HWMODS", paramOption) try: param = param.split(":") if len(param) != 2: raise ValueError paramName = base64ToStr(param[0]) if param[1].strip(): paramValue = base64ToStr(param[1]) else: paramValue = None except ValueError: raise AwlSimError("Project file: " "Invalid hw mod parameter") modDesc.addParameter(paramName, paramValue) hwmodSettings.addLoadedModule(modDesc) # GUI section if p.has_option("GUI", "editor_autoindent"): guiSettings.setEditorAutoIndentEn( p.getboolean("GUI", "editor_autoindent")) if p.has_option("GUI", "editor_paste_autoindent"): guiSettings.setEditorPasteIndentEn( p.getboolean("GUI", "editor_paste_autoindent")) if p.has_option("GUI", "editor_validation"): guiSettings.setEditorValidationEn( p.getboolean("GUI", "editor_validation")) if p.has_option("GUI", "editor_font"): guiSettings.setEditorFont(p.get("GUI", "editor_font").strip()) except _ConfigParserError as e: raise AwlSimError("Project parser error: " + str(e)) return projectClass(projectFile=projectFile, createDate=createDate, modifyDate=modifyDate, awlSources=awlSources, fupSources=fupSources, kopSources=kopSources, symTabSources=symTabSources, libSelections=libSelections, cpuSpecs=specs, cpuConf=conf, guiSettings=guiSettings, coreLinkSettings=linkSettings, hwmodSettings=hwmodSettings)
def __parseConfig(self, filename): try: text = awlFileRead(filename, encoding="UTF-8") p = _ConfigParser() p.readfp(StringIO(text), filename) self.__debug = p.getint("PROFIBUS", "debug", fallback = 0) self.__phyType = p.get("PHY", "type", fallback = "serial") self.__phyDev = p.get("PHY", "dev", fallback = "/dev/ttyS0") self.__phyBaud = p.getint("PHY", "baud", fallback = 19200) self.__dpMasterClass = p.getint("DP", "master_class", fallback = 1) if self.__dpMasterClass not in {1, 2}: raise ValueError("Invalid master_class") self.__dpMasterAddr = p.getint("DP", "master_addr", fallback = 0x02) if self.__dpMasterAddr < 0 or self.__dpMasterAddr > 127: raise ValueError("Invalid master_addr") self.__slaveConfs = [] for section in p.sections(): m = self.__reSlave.match(section) if not m: continue s = self._SlaveConf() s.addr = p.getint(section, "addr") s.gsd = self.pyprofibus.gsd.interp.GsdInterp.fromFile( p.get(section, "gsd")) s.sync_mode = p.getboolean(section, "sync_mode", fallback = False) s.freeze_mode = p.getboolean(section, "freeze_mode", fallback = False) s.group_mask = p.getboolean(section, "group_mask", fallback = 1) if s.group_mask < 0 or s.group_mask > 0xFF: raise ValueError("Invalid group_mask") s.watchdog_ms = p.getint(section, "watchdog_ms", fallback = 5000) if s.watchdog_ms < 0 or s.watchdog_ms > 255 * 255: raise ValueError("Invalid watchdog_ms") s.input_size = p.getint(section, "input_size") if s.input_size < 0 or s.input_size > 246: raise ValueError("Invalid input_size") s.output_size = p.getint(section, "output_size") if s.output_size < 0 or s.output_size > 246: raise ValueError("Invalid output_size") mods = [ o for o in p.options(section) if self.__reMod.match(o) ] mods.sort(key = lambda o: self.__reMod.match(o).group(1)) for option in mods: s.gsd.setConfiguredModule(p.get(section, option)) self.__slaveConfs.append(s) except (_ConfigParserError, AwlParserError, ValueError) as e: self.raiseException("Profibus config file parse " "error:\n%s" % str(e)) except self.pyprofibus.gsd.parser.GsdError as e: self.raiseException("Failed to parse GSD file:\n%s" % str(e))
def fromText(cls, text, projectFile): if not cls.dataIsProject(text.encode("utf-8")): raise AwlSimError("Project file: The data is "\ "not an awlsim project.") projectDir = os.path.dirname(projectFile) awlSources = [] symTabSources = [] cpuSpecs = S7CPUSpecs() try: p = _ConfigParser() p.readfp(StringIO(text), projectFile) version = p.getint("AWLSIM_PROJECT", "file_version") expectedVersion = 0 if version != expectedVersion: raise AwlSimError("Project file: Unsupported version. "\ "File version is %d, but expected %d." %\ (version, expectedVersion)) # CPU section for i in range(0xFFFF): option = "awl_file_%d" % i if not p.has_option("CPU", option): break path = p.get("CPU", option) src = AwlSource.fromFile(path, cls.__generic2path(path, projectDir)) awlSources.append(src) for i in range(0xFFFF): option = "awl_%d" % i if not p.has_option("CPU", option): break awlBase64 = p.get("CPU", option) src = AwlSource.fromBase64("#%d" % i, awlBase64) awlSources.append(src) if p.has_option("CPU", "mnemonics"): mnemonics = p.getint("CPU", "mnemonics") cpuSpecs.setConfiguredMnemonics(mnemonics) if p.has_option("CPU", "nr_accus"): nrAccus = p.getint("CPU", "nr_accus") cpuSpecs.setNrAccus(nrAccus) # SYMBOLS section for i in range(0xFFFF): option = "sym_tab_file_%d" % i if not p.has_option("SYMBOLS", option): break path = p.get("SYMBOLS", option) src = SymTabSource.fromFile(path, cls.__generic2path(path, projectDir)) symTabSources.append(src) for i in range(0xFFFF): option = "sym_tab_%d" % i if not p.has_option("SYMBOLS", option): break symTabBase64 = p.get("SYMBOLS", option) src = SymTabSource.fromBase64("#%d" % i, symTabBase64) symTabSources.append(src) except _ConfigParserError as e: raise AwlSimError("Project parser error: " + str(e)) return cls(projectFile = projectFile, awlSources = awlSources, symTabSources = symTabSources, cpuSpecs = cpuSpecs)
def __init__(self, text, filename=None): def get(section, option, fallback=None): if p.has_option(section, option): return p.get(section, option) if fallback is None: raise ValueError("Option [%s] '%s' does not exist." % (section, option)) return fallback def getboolean(section, option, fallback=None): if p.has_option(section, option): return p.getboolean(section, option) if fallback is None: raise ValueError("Option [%s] '%s' does not exist." % (section, option)) return fallback def getint(section, option, fallback=None): if p.has_option(section, option): return p.getint(section, option) if fallback is None: raise ValueError("Option [%s] '%s' does not exist." % (section, option)) return fallback try: p = _ConfigParser() textIO = StringIO(text) if hasattr(p, "read_file"): p.read_file(textIO, filename) else: p.readfp(textIO, filename) # [PROFIBUS] self.debug = getint("PROFIBUS", "debug", fallback=0) # [PHY] self.phyType = get("PHY", "type", fallback="serial") self.phyDev = get("PHY", "dev", fallback="/dev/ttyS0") self.phyBaud = getint("PHY", "baud", fallback=9600) self.phyRtsCts = getboolean("PHY", "rtscts", fallback=False) self.phyDsrDtr = getboolean("PHY", "dsrdtr", fallback=False) self.phySpiBus = getint("PHY", "spiBus", fallback=0) self.phySpiCS = getint("PHY", "spiCS", fallback=0) self.phySpiSpeedHz = getint("PHY", "spiSpeedHz", fallback=1000000) # [DP] self.dpMasterClass = getint("DP", "master_class", fallback=1) if self.dpMasterClass not in {1, 2}: raise ValueError("Invalid master_class") self.dpMasterAddr = getint("DP", "master_addr", fallback=0x02) if self.dpMasterAddr < 0 or self.dpMasterAddr > 127: raise ValueError("Invalid master_addr") self.slaveConfs = [] for section in p.sections(): m = self.__reSlave.match(section) if not m: continue s = self._SlaveConf() s.addr = getint(section, "addr") s.gsd = GsdInterp.fromFile(get(section, "gsd")) s.syncMode = getboolean(section, "sync_mode", fallback=False) s.freezeMode = getboolean(section, "freeze_mode", fallback=False) s.groupMask = getboolean(section, "group_mask", fallback=1) if s.groupMask < 0 or s.groupMask > 0xFF: raise ValueError("Invalid group_mask") s.watchdogMs = getint(section, "watchdog_ms", fallback=5000) if s.watchdogMs < 0 or s.watchdogMs > 255 * 255: raise ValueError("Invalid watchdog_ms") s.inputSize = getint(section, "input_size") if s.inputSize < 0 or s.inputSize > 246: raise ValueError("Invalid input_size") s.outputSize = getint(section, "output_size") if s.outputSize < 0 or s.outputSize > 246: raise ValueError("Invalid output_size") mods = [o for o in p.options(section) if self.__reMod.match(o)] mods.sort(key=lambda o: self.__reMod.match(o).group(1)) for option in mods: s.gsd.setConfiguredModule(get(section, option)) self.slaveConfs.append(s) except (_ConfigParserError, ValueError) as e: raise PbConfError("Profibus config file parse " "error:\n%s" % str(e)) except GsdError as e: raise PbConfError("Failed to parse GSD file:\n%s" % str(e))
def load_config(path): _ConfigParser.optionxform = lambda self, key: key.replace('-', '_') cfg = _ConfigParser() cfg.read([path]) return dict(sum(map(cfg.items, cfg.sections()), []))
def parse(cls, projectClass, text, projectFile): from awlsim.common.project import GuiSettings from awlsim.common.project import CoreLinkSettings from awlsim.common.project import HwmodSettings projectDir = os.path.dirname(projectFile) createDate = None modifyDate = None awlSources = [] fupSources = [] kopSources = [] symTabSources = [] libSelections = [] specs = S7CPUSpecs() conf = S7CPUConfig() guiSettings = GuiSettings() linkSettings = CoreLinkSettings() hwmodSettings = HwmodSettings() try: p = _ConfigParser() textIO = StringIO(text) if hasattr(p, "read_file"): #@nocov p.read_file(textIO, projectFile) else: #@nocov p.readfp(textIO, projectFile) # AWLSIM_PROJECT section version = p.getint("AWLSIM_PROJECT", "file_version") expectedVersion = 0 if version != expectedVersion: raise AwlSimError("Project file: Unsupported version. "\ "File version is %d, but expected %d." %\ (version, expectedVersion)) if p.has_option("AWLSIM_PROJECT", "date"): # Compatibility only. "date" is deprecated. dStr = p.get("AWLSIM_PROJECT", "date") try: createDate = datetime.datetime.strptime(dStr, projectClass.DATETIME_FMT) except (ValueError, TypeError) as e: createDate = None modifyDate = createDate if p.has_option("AWLSIM_PROJECT", "create_date"): dStr = p.get("AWLSIM_PROJECT", "create_date") try: createDate = datetime.datetime.strptime(dStr, projectClass.DATETIME_FMT) except (ValueError, TypeError) as e: createDate = None if p.has_option("AWLSIM_PROJECT", "modify_date"): dStr = p.get("AWLSIM_PROJECT", "modify_date") try: modifyDate = datetime.datetime.strptime(dStr, projectClass.DATETIME_FMT) except (ValueError, TypeError) as e: modifyDate = None def getSrcs(srcList, section, prefix, SrcClass): for i in range(0xFFFF): option = "%s_file_%d" % (prefix, i) if not p.has_option(section, option): break path = p.get(section, option) src = SrcClass.fromFile( name=path, filepath=RelPath(projectDir).fromRelative(path), compatReEncode=True) srcList.append(src) for i in range(0xFFFF): srcOption = "%s_%d" % (prefix, i) nameOption = "%s_name_%d" % (prefix, i) if not p.has_option(section, srcOption): break awlBase64 = p.get(section, srcOption) name = None if p.has_option(section, nameOption): with contextlib.suppress(ValueError): name = base64ToStr( p.get(section, nameOption)) if name is None: name = "%s #%d" % (SrcClass.SRCTYPE, i) src = SrcClass.fromBase64(name, awlBase64) srcList.append(src) # CPU section getSrcs(awlSources, "CPU", "awl", AwlSource) if p.has_option("CPU", "mnemonics"): mnemonics = p.getint("CPU", "mnemonics") conf.setConfiguredMnemonics(mnemonics) if p.has_option("CPU", "nr_accus"): nrAccus = p.getint("CPU", "nr_accus") specs.setNrAccus(nrAccus) if p.has_option("CPU", "clock_memory_byte"): clockMemByte = p.getint("CPU", "clock_memory_byte") conf.setClockMemByte(clockMemByte) if p.has_option("CPU", "ob_startinfo_enable"): conf.setOBStartinfoEn(p.getboolean("CPU", "ob_startinfo_enable")) if p.has_option("CPU", "ext_insns_enable"): conf.setExtInsnsEn(p.getboolean("CPU", "ext_insns_enable")) # FUP section getSrcs(fupSources, "FUP", "fup", FupSource) # KOP section getSrcs(kopSources, "KOP", "kop", KopSource) # SYMBOLS section getSrcs(symTabSources, "SYMBOLS", "sym_tab", SymTabSource) # LIBS section for i in range(0xFFFF): nameOption = "lib_name_%d" % i blockOption = "lib_block_%d" % i effOption = "lib_block_effective_%d" % i if not p.has_option("LIBS", nameOption): break try: libName = base64ToStr(p.get("LIBS", nameOption)) except ValueError as e: continue block = p.get("LIBS", blockOption).upper().strip() effBlock = p.getint("LIBS", effOption) try: if block.startswith("FC"): entryType = AwlLibEntrySelection.TYPE_FC entryIndex = int(block[2:].strip()) elif block.startswith("FB"): entryType = AwlLibEntrySelection.TYPE_FB entryIndex = int(block[2:].strip()) elif block.startswith("UNKNOWN"): entryType = AwlLibEntrySelection.TYPE_UNKNOWN entryIndex = int(block[7:].strip()) else: raise ValueError if entryIndex < -1 or entryIndex > 0xFFFF: raise ValueError except ValueError: raise AwlSimError("Project file: Invalid " "library block: %s" % block) libSelections.append( AwlLibEntrySelection(libName = libName, entryType = entryType, entryIndex = entryIndex, effectiveEntryIndex = effBlock) ) # CORE_LINK section if p.has_option("CORE_LINK", "spawn_local"): linkSettings.setSpawnLocalEn( p.getboolean("CORE_LINK", "spawn_local")) if p.has_option("CORE_LINK", "spawn_local_port_range"): pRange = p.get("CORE_LINK", "spawn_local_port_range") try: pRange = pRange.split(":") begin = int(pRange[0]) end = int(pRange[1]) if end < begin: raise ValueError pRange = range(begin, end + 1) except (IndexError, ValueError) as e: raise AwlSimError("Project file: Invalid port range") linkSettings.setSpawnLocalPortRange(pRange) if p.has_option("CORE_LINK", "spawn_local_interpreters"): interp = p.get("CORE_LINK", "spawn_local_interpreters") try: interp = base64ToStr(interp) except ValueError as e: raise AwlSimError("Project file: " "Invalid interpreter list") linkSettings.setSpawnLocalInterpreters(interp) if p.has_option("CORE_LINK", "connect_host"): host = p.get("CORE_LINK", "connect_host") try: host = base64ToStr(host) except ValueError as e: raise AwlSimError("Project file: " "Invalid host name") linkSettings.setConnectHost(host) if p.has_option("CORE_LINK", "connect_port"): port = p.getint("CORE_LINK", "connect_port") linkSettings.setConnectPort(port) if p.has_option("CORE_LINK", "connect_timeout_ms"): timeout = p.getint("CORE_LINK", "connect_timeout_ms") linkSettings.setConnectTimeoutMs(timeout) if p.has_option("CORE_LINK", "tunnel"): tunnel = p.getint("CORE_LINK", "tunnel") linkSettings.setTunnel(tunnel) if p.has_option("CORE_LINK", "tunnel_local_port"): tunnelLocalPort = p.getint("CORE_LINK", "tunnel_local_port") linkSettings.setTunnelLocalPort(tunnelLocalPort) if p.has_option("CORE_LINK", "ssh_user"): sshUser = p.get("CORE_LINK", "ssh_user") try: sshUser = base64ToStr(sshUser) except ValueError as e: raise AwlSimError("Project file: " "Invalid ssh_user") linkSettings.setSSHUser(sshUser) if p.has_option("CORE_LINK", "ssh_port"): sshPort = p.getint("CORE_LINK", "ssh_port") linkSettings.setSSHPort(sshPort) if p.has_option("CORE_LINK", "ssh_executable"): sshExecutable = p.get("CORE_LINK", "ssh_executable") try: sshExecutable = base64ToStr(sshExecutable) except ValueError as e: raise AwlSimError("Project file: " "Invalid ssh_executable") linkSettings.setSSHExecutable(sshExecutable) # HWMODS section for i in range(0xFFFF): nameOption = "loaded_mod_%d" % i if not p.has_option("HWMODS", nameOption): break modName = base64ToStr(p.get("HWMODS", nameOption)) modDesc = HwmodDescriptor(modName) for j in range(0x3FF): paramOption = "loaded_mod_%d_p%d" % (i, j) if not p.has_option("HWMODS", paramOption): break param = p.get("HWMODS", paramOption) try: param = param.split(":") if len(param) != 2: raise ValueError paramName = base64ToStr(param[0]) if param[1].strip(): paramValue = base64ToStr(param[1]) else: paramValue = None except ValueError: raise AwlSimError("Project file: " "Invalid hw mod parameter") modDesc.addParameter(paramName, paramValue) hwmodSettings.addLoadedModule(modDesc) # GUI section if p.has_option("GUI", "editor_autoindent"): guiSettings.setEditorAutoIndentEn( p.getboolean("GUI", "editor_autoindent")) if p.has_option("GUI", "editor_paste_autoindent"): guiSettings.setEditorPasteIndentEn( p.getboolean("GUI", "editor_paste_autoindent")) if p.has_option("GUI", "editor_validation"): guiSettings.setEditorValidationEn( p.getboolean("GUI", "editor_validation")) if p.has_option("GUI", "editor_font"): guiSettings.setEditorFont(p.get("GUI", "editor_font").strip()) except _ConfigParserError as e: raise AwlSimError("Project parser error: " + str(e)) return projectClass(projectFile = projectFile, createDate = createDate, modifyDate = modifyDate, awlSources = awlSources, fupSources = fupSources, kopSources = kopSources, symTabSources = symTabSources, libSelections = libSelections, cpuSpecs = specs, cpuConf = conf, guiSettings = guiSettings, coreLinkSettings = linkSettings, hwmodSettings = hwmodSettings)
if __name__ == "__main__": if len(sys.argv) != 3: print( "Usage: <program> <deploy_cfg_template_file> <file_with_properties>" ) print( "Properties from <file_with_properties> will be applied to <deploy_cfg_template_file>" ) print( "template which will be overwritten with .orig copy saved in the same folder first." ) sys.exit(1) file = open(sys.argv[1], 'r') text = file.read() t = Template(text) config = _ConfigParser() if os.path.isfile(sys.argv[2]): config.read(sys.argv[2]) elif "KBASE_ENDPOINT" in os.environ: kbase_endpoint = os.environ.get("KBASE_ENDPOINT") props = "[global]\n" + \ "kbase_endpoint = " + kbase_endpoint + "\n" + \ "job_service_url = " + kbase_endpoint + "/userandjobstate\n" + \ "workspace_url = " + kbase_endpoint + "/ws\n" + \ "shock_url = " + kbase_endpoint + "/shock-api\n" + \ "handle_url = " + kbase_endpoint + "/handle_service\n" + \ "srv_wiz_url = " + kbase_endpoint + "/service_wizard\n" + \ "njsw_url = " + kbase_endpoint + "/njs_wrapper\n" if "AUTH_SERVICE_URL" in os.environ: props += "auth_service_url = " + os.environ.get( "AUTH_SERVICE_URL") + "\n"
def update_config(self): loglevel = self.get_log_level() logfile = self.get_log_file() self._api_log_level = -1 self._msgs_since_config_update = 0 self._time_at_config_update = time.time() # Retrieving the control API defined log level api_url = None if _os.path.isfile(self._mlog_config_file): cfg = _ConfigParser() cfg.read(self._mlog_config_file) cfgitems = self._get_config_items(cfg, _GLOBAL) cfgitems.update(self._get_config_items(cfg, self._subsystem)) if MLOG_LOG_LEVEL in cfgitems: try: self._config_log_level = int(cfgitems[MLOG_LOG_LEVEL]) except: _warnings.warn( 'Cannot parse log level {} from file {} to int'.format( cfgitems[MLOG_LOG_LEVEL], self._mlog_config_file) + '. Keeping current log level.') if MLOG_API_URL in cfgitems: api_url = cfgitems[MLOG_API_URL] if MLOG_LOG_FILE in cfgitems: self._config_log_file = cfgitems[MLOG_LOG_FILE] elif self._mlog_config_file: _warnings.warn('Cannot read config file ' + self._mlog_config_file) if (api_url): subsystem_api_url = api_url + "/" + self._subsystem try: data = _json.load( _urllib2.urlopen(subsystem_api_url, timeout=5)) except _urllib2.URLError, e: code_ = None if hasattr(e, 'code'): code_ = ' ' + str(e.code) _warnings.warn('Could not connect to mlog api server at ' + '{}:{} {}. Using default log level {}.'.format( subsystem_api_url, code_, str(e.reason), str(DEFAULT_LOG_LEVEL))) else: max_matching_level = -1 for constraint_set in data['log_levels']: level = constraint_set['level'] constraints = constraint_set['constraints'] if level <= max_matching_level: continue matches = 1 for constraint in constraints: if constraint not in self._log_constraints: matches = 0 elif (self._log_constraints[constraint] != constraints[constraint]): matches = 0 if matches == 1: max_matching_level = level self._api_log_level = max_matching_level
def __init__(self, fd, filename=None): def get(section, option, fallback=None): if p.has_option(section, option): return p.get(section, option) if fallback is None: raise ValueError("Option [%s] '%s' does not exist." % (section, option)) return fallback def getboolean(section, option, fallback=None): if p.has_option(section, option): return p.getboolean(section, option) if fallback is None: raise ValueError("Option [%s] '%s' does not exist." % (section, option)) return fallback def getint(section, option, fallback=None): if p.has_option(section, option): return p.getint(section, option) if fallback is None: raise ValueError("Option [%s] '%s' does not exist." % (section, option)) return fallback try: p = _ConfigParser() if hasattr(p, "read_file"): p.read_file(fd, filename) else: p.readfp(fd, filename) # [PROFIBUS] self.debug = getint("PROFIBUS", "debug", fallback=0) # [PHY] self.phyType = get("PHY", "type", fallback="serial") self.phyDev = get("PHY", "dev", fallback="/dev/ttyS0") self.phyBaud = getint("PHY", "baud", fallback=9600) self.phyRtsCts = getboolean("PHY", "rtscts", fallback=False) self.phyDsrDtr = getboolean("PHY", "dsrdtr", fallback=False) self.phySpiBus = getint("PHY", "spiBus", fallback=0) self.phySpiCS = getint("PHY", "spiCS", fallback=0) self.phySpiSpeedHz = getint("PHY", "spiSpeedHz", fallback=1000000) # [DP] self.dpMasterClass = getint("DP", "master_class", fallback=1) if self.dpMasterClass not in {1, 2}: raise ValueError("Invalid master_class") self.dpMasterAddr = getint("DP", "master_addr", fallback=0x02) if self.dpMasterAddr < 0 or self.dpMasterAddr > 127: raise ValueError("Invalid master_addr") self.slaveConfs = [] for section in p.sections(): m = self.__reSlave.match(section) if not m: continue index = int(m.group(1)) s = self._SlaveConf() s.index = index s.name = get(section, "name", section) s.addr = getint(section, "addr") try: s.gsd = GsdInterp.fromFile(get(section, "gsd"), debug=(self.debug > 0)) except GsdError as origExc: try: s.gsd = GsdInterp.fromPy(get(section, "gsd").replace(".", "_"), debug=(self.debug > 0)) except GsdError as e: raise GsdError("%s\n%s" % (str(origExc), str(e))) s.syncMode = getboolean(section, "sync_mode", fallback=False) s.freezeMode = getboolean(section, "freeze_mode", fallback=False) s.groupMask = getboolean(section, "group_mask", fallback=1) if s.groupMask < 0 or s.groupMask > 0xFF: raise ValueError("Invalid group_mask") s.watchdogMs = getint(section, "watchdog_ms", fallback=5000) if s.watchdogMs < 0 or s.watchdogMs > 255 * 255: raise ValueError("Invalid watchdog_ms") s.inputSize = getint(section, "input_size") if s.inputSize < 0 or s.inputSize > 246: raise ValueError("Invalid input_size") s.outputSize = getint(section, "output_size") if s.outputSize < 0 or s.outputSize > 246: raise ValueError("Invalid output_size") mods = [o for o in p.options(section) if self.__reMod.match(o)] mods.sort(key=lambda o: self.__reMod.match(o).group(1)) if s.gsd.isModular(): for option in mods: s.gsd.setConfiguredModule(get(section, option)) elif mods: print( "Warning: Some modules are specified in the config file, " "but the station is 'Compact': Modular_Station=0.", file=sys.stderr) self.slaveConfs.append(s) except (IOError, UnicodeError) as e: raise PbConfError("Failed to read '%s': %s" %\ (filename, str(e))) except (_ConfigParserError, ValueError) as e: raise PbConfError("Profibus config file parse " "error:\n%s" % str(e)) except GsdError as e: raise PbConfError("Failed to parse GSD file:\n%s" % str(e))
def fromText(cls, text, projectFile): if not cls.dataIsProject(text.encode("utf-8")): raise AwlSimError("Project file: The data is "\ "not an awlsim project.") projectDir = os.path.dirname(projectFile) awlSources = [] symTabSources = [] cpuSpecs = S7CPUSpecs() try: p = _ConfigParser() p.readfp(StringIO(text), projectFile) version = p.getint("AWLSIM_PROJECT", "file_version") expectedVersion = 0 if version != expectedVersion: raise AwlSimError("Project file: Unsupported version. "\ "File version is %d, but expected %d." %\ (version, expectedVersion)) # CPU section for i in range(0xFFFF): option = "awl_file_%d" % i if not p.has_option("CPU", option): break path = p.get("CPU", option) src = AwlSource.fromFile(path, cls.__generic2path(path, projectDir)) awlSources.append(src) for i in range(0xFFFF): option = "awl_%d" % i if not p.has_option("CPU", option): break awlBase64 = p.get("CPU", option) src = AwlSource.fromBase64("#%d" % i, awlBase64) awlSources.append(src) if p.has_option("CPU", "mnemonics"): mnemonics = p.getint("CPU", "mnemonics") cpuSpecs.setConfiguredMnemonics(mnemonics) if p.has_option("CPU", "nr_accus"): nrAccus = p.getint("CPU", "nr_accus") cpuSpecs.setNrAccus(nrAccus) # SYMBOLS section for i in range(0xFFFF): option = "sym_tab_file_%d" % i if not p.has_option("SYMBOLS", option): break path = p.get("SYMBOLS", option) src = SymTabSource.fromFile( path, cls.__generic2path(path, projectDir)) symTabSources.append(src) for i in range(0xFFFF): option = "sym_tab_%d" % i if not p.has_option("SYMBOLS", option): break symTabBase64 = p.get("SYMBOLS", option) src = SymTabSource.fromBase64("#%d" % i, symTabBase64) symTabSources.append(src) except _ConfigParserError as e: raise AwlSimError("Project parser error: " + str(e)) return cls(projectFile=projectFile, awlSources=awlSources, symTabSources=symTabSources, cpuSpecs=cpuSpecs)