def do_match(args): ''' Returns: - 0, if user-provided value matches with value found in configuration file. - 1, if key exists in configuration file, but user-provided value does not matche with value found in configuration file. - 2, if requested key/section does not exists in configuration file, or if no configuration file was found. Raises: - ValueError, if user-provided value does not have an otopi-supported variable type, in the format <type>:<value> - KeyError, if user-provided value contains invalid variable type. ''' rv = 0 try: config = get_configparser(args.file) value = config.get(args.section, args.key) except ( configparser.NoOptionError, configparser.NoSectionError, FileNotFound ): rv = 2 else: value = common.parseTypedValue(value) user_value = common.parseTypedValue(args.value) if value != user_value: rv = 1 return rv
def _setupEnvironment(self, environment): """Setup environment based on command-line parameters.""" environment[constants.BaseEnv.EXECUTION_DIRECTORY] = os.environ[ constants.SystemEnvironment.EXEC_DIR ] for arg in sys.argv[1:]: for statement in shlex.split(arg): entry = statement.split('=', 1) if len(entry) == 2: key, value = entry[0], common.parseTypedValue(entry[1]) if key.startswith( constants.Const.ENVIRONMENT_APPEND_PREFIX ): key = key.replace( constants.Const.ENVIRONMENT_APPEND_PREFIX, '' ) environment.setdefault(key, '') environment[key] += ':%s' % value elif key.startswith( constants.Const.ENVIRONMENT_PREPEND_PREFIX ): key = key.replace( constants.Const.ENVIRONMENT_PREPEND_PREFIX, '' ) environment.setdefault(key, '') environment[key] = '%s:%s' % (value, environment[key]) else: environment[key] = value
def queryValue(self, name, note=None): self.logger.debug('query %s', name) if note is None: note = _("\nPlease specify value for '{name}':").format( name=name ) self.dialog.note(text=note) self.dialog.note(text=_("Format is type:value.")) value = common.parseTypedValue(self._readline()) return value
def queryValue(self, name, note=None): if note is None: note = _("\nPlease specify value for '{name}':").format( name=name ) self._writeQueryStart(name) self.dialog.note(text=note) self.dialog.note( text=_( "Response is VALUE {name}=type:value or " "ABORT {name}" ).format( name=name, ), ) self._write( text='%s%s %s\n' % ( dialogcons.DialogMachineConst.REQUEST_PREFIX, dialogcons.DialogMachineConst.QUERY_VALUE, name, ) ) self._writeQueryEnd(name) opcode, variable = self._readline().split(' ', 1) variable = variable.split('=', 1) if variable[0] != name: raise RuntimeError( _( "Expected response for {name}, " "received '{received}'" ).format( name=name, received=variable[0], ) ) if opcode == dialogcons.DialogMachineConst.QUERY_VALUE_RESPONSE_ABORT: raise context.Abort(_('Aborted by dialog')) elif opcode == \ dialogcons.DialogMachineConst.QUERY_VALUE_RESPONSE_VALUE: if len(variable) != 2: raise RuntimeError(_('Value ot provided')) return common.parseTypedValue(variable[1]) else: raise RuntimeError( _("Invalid response opcode '{code}'").format( code=opcode, ) )
def _readEnvironment(self, section, override): if self._config.has_section(section): for name, value in self._config.items(section): try: value = common.parseTypedValue(value) except Exception as e: raise RuntimeError( _("Cannot parse configuration file key " "{key} at section {section}: {exception}").format( key=name, section=section, exception=e ) ) if override: self.environment[name] = value else: self.environment.setdefault(name, value)
def do_query(args): ''' Returns: - 0, on success, and prints the value found in the configuration file, with the type, if requested by user with --with-type. Raises: - configparser.NoOptionError, configparser.NoSectionError, if key/section does not exists in configuration file. - FileNotFound, if no configuration file is found. ''' config = get_configparser(args.file) value = config.get(args.section, args.key) if args.with_type: print(value) else: print(common.parseTypedValue(value)) return 0
def _readEnvironment(self, section, override): if self._config.has_section(section): for name, value in self._config.items(section): try: value = common.parseTypedValue(value) except Exception as e: raise RuntimeError( _("Cannot parse configuration file key " "{key} at section {section}: {exception}").format( key=name, section=section, exception=e, )) if override: self.environment[name] = value else: self.environment.setdefault(name, value)
def _parse_answer_file(self): self._config.read(self._tmp_ans) for name, value in self._config.items( otopicons.Const.CONFIG_SECTION_DEFAULT ): try: value = common.parseTypedValue(value) self.logger.debug('%s=%s' % (name, value)) except Exception as e: raise RuntimeError( _( "Cannot parse configuration file key " "{key} at section {section}: {exception}" ).format( key=name, section=otopicons.Const.CONFIG_SECTION_DEFAULT, exception=e, ) ) self.environment[name] = value
def queryValue(self, name, note=None): if note is None: note = _("\nPlease specify value for '{name}':").format(name=name) self._writeQueryStart(name) self.dialog.note(text=note) self.dialog.note(text=_("Response is VALUE {name}=type:value or " "ABORT {name}").format(name=name, ), ) self._write(text='%s%s %s\n' % ( dialogcons.DialogMachineConst.REQUEST_PREFIX, dialogcons.DialogMachineConst.QUERY_VALUE, name, )) self._writeQueryEnd(name) opcode, variable = self._readline().split(' ', 1) variable = variable.split('=', 1) if variable[0] != name: raise RuntimeError( _("Expected response for {name}, " "received '{received}'").format( name=name, received=variable[0], )) if opcode == dialogcons.DialogMachineConst.QUERY_VALUE_RESPONSE_ABORT: raise context.Abort(_('Aborted by dialog')) elif opcode == \ dialogcons.DialogMachineConst.QUERY_VALUE_RESPONSE_VALUE: if len(variable) != 2: raise RuntimeError(_('Value ot provided')) return common.parseTypedValue(variable[1]) else: raise RuntimeError( _("Invalid response opcode '{code}'").format(code=opcode, ))
def _parse_answer_file(self): buf = StringIO(unicode(self._tmp_ans)) try: self._config.readfp(buf) except configparser.Error as ex: msg = _('The answer file on the shared storage is invalid, ' 'please check and fix it: {ex}').format(ex=ex) self.logger.error(msg) raise RuntimeError(msg) for name, value in self._config.items( otopicons.Const.CONFIG_SECTION_DEFAULT): try: value = common.parseTypedValue(value) self.logger.debug('%s=%s' % (name, value)) except Exception as e: raise RuntimeError( _("Cannot parse configuration file key " "{key} at section {section}: {exception}").format( key=name, section=otopicons.Const.CONFIG_SECTION_DEFAULT, exception=e, )) self.environment[name] = value