def __init__(self, fileName, sep="\t"): self.sep = sep with open(fileName, 'rb') as f: self.lines = f.readlines() self.meta = ConfigObj(self.lines, invalid=True, configspec=cfgspec) self.meta.validate(Validator()) #seems like a bug in nt-mdt software: # "Table" is actually 2 tables with pair of columns, # "Columns" is actually table with all columns if (self.meta["State"] == "Table"): self.data = np.vstack((self.readColumnsTSVPart(fileName, "Fw"), self.readColumnsTSVPart(fileName, "Bk", usecols=(1)))) elif (self.meta["State"] == "Row"): self.data = np.vstack( (np.linspace(self.meta["Bias X"], self.meta["Bias X"] + self.meta["Scale X"] * self.meta["FwSize"], num=self.meta["FwSize"]), self.readRowTSVPart(fileName, "Fw"), self.readRowTSVPart(fileName, "Bk"))) elif (self.meta["State"] == "Columns"): raise NotImplementedError( "Because in this case meta is parsed not quite correctly") self.scaleData() del self.lines
def saveSettings(self, force=False): # We never want to save config if runing securely if globalVars.appArgs.secure: return # We save the configuration, # in case the user would not have checked the "Save configuration on exit" # checkbox in General settings or force is is True if not force\ and not config.conf['general']['saveConfigurationOnExit']: return if self.addonConfig is None: return val = Validator() try: self.addonConfig.validate(val, copy=True) except VdtTypeError: # error in configuration file log.warning("saveSettings: validator error: %s" % self.addonConfig.errors) return try: self.addonConfig.write() log.warning("add-on configuration saved") except: # noqa:E722 log.warning( "Could not save add-on configuration - probably read only file system" ) # noqa:E501
def save(): global vocalizerConfig if not vocalizerConfig: raise RuntimeError("Vocalizer config is not loaded.") val = Validator() vocalizerConfig.validate(val, copy=True) vocalizerConfig.write()
def __init__(self, input, translatedInput=None): """ Constructs an L{AddonManifest} instance from manifest string data @param input: data to read the manifest information @type input: a fie-like object. @param translatedInput: translated manifest input @type translatedInput: file-like object """ super(AddonManifest, self).__init__(input, configspec=self.configspec, encoding='utf-8', default_encoding='utf-8') self._errors = None val = Validator({"apiVersion": validate_apiVersionString}) result = self.validate(val, copy=True, preserve_errors=True) if result != True: self._errors = result elif True != self._validateApiVersionRange(): self._errors = "Constraint not met: minimumNVDAVersion ({}) <= lastTestedNVDAVersion ({})".format( self.get("minimumNVDAVersion"), self.get("lastTestedNVDAVersion")) self._translatedConfig = None if translatedInput is not None: self._translatedConfig = ConfigObj(translatedInput, encoding='utf-8', default_encoding='utf-8') for k in ('summary', 'description'): val = self._translatedConfig.get(k) if val: self[k] = val
def __init__(self): self.spec = confspec #: All loaded profiles by name. self._profileCache = {} #: The active profiles. self.profiles = [] #: Whether profile triggers are enabled (read-only). #: @type: bool self.profileTriggersEnabled = True self.validator = Validator() self.rootSection = None self._shouldHandleProfileSwitch = True self._pendingHandleProfileSwitch = False self._suspendedTriggers = None # Never save the config if running securely or if running from the launcher. # When running from the launcher we don't save settings because the user may decide not to # install this version, and these settings may not be compatible with the already # installed version. See #7688 self._shouldWriteProfile = not (globalVars.appArgs.secure or globalVars.appArgs.launcher) self._initBaseConf() #: Maps triggers to profiles. self.triggersToProfiles = None self._loadProfileTriggers() #: The names of all profiles that have been modified since they were last saved. self._dirtyProfiles = set()
def save(): global numberConfig if not numberConfig: raise RuntimeError("Config is not loaded.") val = Validator() numberConfig.validate(val, copy=True) numberConfig.write()
def __init__(self): self.config_file = str(Path(config_path(f"{app.name}.ini"))) self.spec = ConfigObj(config_spec, encoding="UTF8", list_values=False, _inspec=True) self.validator = Validator() self.validate_and_write()
def __init__(self, vlcSettings): self.addon = _curAddon self.vlcSettings = vlcSettings self._conf = None self._configFileError = None self._val = Validator() self._importOldSettings() self._load() self._updateResumeFiles()
def __init__(self): self._profile_path = Path(config_path()) / "voice_profiles" self.profiles = {} self.active_profile = None self.validator = Validator() if not self._profile_path.exists(): self._profile_path.mkdir(parents=True) self._add_builtin_voice_profiles() self.refresh_voice_profiles()
def test_get_extra_values(conf): conf.validate(Validator(), preserve_errors=True) extra_values = get_extra_values(conf) expected = sorted([ ((), 'extra'), ((), 'extra-section'), (('section', 'sub-section'), 'extra'), (('section', ), 'extra-sub-section'), ]) assert sorted(extra_values) == expected
def test_validate_preserve_errors(conf): validator = Validator() result = conf.validate(validator, preserve_errors=True) assert not result['value'] assert not result['missing-section'] section = result['section'] assert not section['value'] assert not section['sub-section']['value'] assert not section['missing-subsection']
def load(): global vocalizerConfig if not vocalizerConfig: path = os.path.join(globalVars.appArgs.configPath, VOCALIZER_CONFIG_FILENAME) vocalizerConfig = configobj.ConfigObj(path, configspec=StringIO(_configSpec), encoding="utf-8") vocalizerConfig.newlines = "\r\n" vocalizerConfig.stringify = True val = Validator() ret = vocalizerConfig.validate(val, preserve_errors=True, copy=True) if ret != True: log.warning("Vocalizer configuration is invalid: %s", ret)
def load(): global Access8MathConfig if not Access8MathConfig: path = os.path.join(globalVars.appArgs.configPath, CONFIG_FILENAME) Access8MathConfig = configobj.ConfigObj(path, configspec=StringIO(_configSpec), encoding="utf-8") Access8MathConfig.newlines = "\r\n" Access8MathConfig.stringify = True val = Validator() ret = Access8MathConfig.validate(val, preserve_errors=True, copy=True) if ret != True: log.warning("Access8Math configuration is invalid: %s", ret)
def saveSettings(self): #We never want to save config if runing securely if globalVars.appArgs.secure: return if self.addonConfig is None: return try: val = Validator() self.addonConfig.validate(val, copy = True) self.addonConfig.write() except: log.warning("Could not save configuration - probably read only file system")
def __init__(self,input ) : """ Constructs an L{AddonConfiguration} instance from manifest string data @param input: data to read the addon configuration information @type input: a fie-like object. """ super(BaseAddonConfiguration, self).__init__(input, configspec=self.configspec, encoding='utf-8', default_encoding='utf-8') self.newlines = "\r\n" self._errors = [] val = Validator() result = self.validate(val, copy=True, preserve_errors=True) if result != True: self._errors = result
def _validate(self, obj): validator = Validator() results = obj.validate(validator) if results != True: for (section_list, key, _) in flatten_errors(obj, results): if key is not None: raise Exception( 'The "%s" key in the section "%s" failed validation' % (key, ', '.join(section_list))) else: raise Exception('The following section was missing:%s ' % ', '.join(section_list))
def getTextAnalysisConfig(): path = getTextAnalysisIniFilePath() conf = ConfigObj( path, configspec=StringIO(""), encoding="utf-8", list_values=False) conf.newlines = "\r\n" val = Validator() ret = conf.validate(val, preserve_errors=True, copy=True) if not ret: log.warning("KeyboardKeys configuration file is invalid: %s", ret) return conf
def loadFileConfig(file): try: conf = ConfigObj( file, configspec=_confSpec, indent_type="\t", encoding="UTF-8") except ConfigObjErrore: return None conf.newlines = "\r\n" val = Validator() result = conf.validate(val) if not result: return None conf.newlines = "\r\n" return conf
def load(): global numberConfig if not numberConfig: path = os.path.join(globalVars.appArgs.configPath, NUMBER_CONFIG_FILENAME) numberConfig = configobj.ConfigObj(path, configspec=StringIO(_configSpec), encoding="utf-8") numberConfig.newlines = "\r\n" numberConfig.stringify = True val = Validator() ret = numberConfig.validate(val, preserve_errors=True, copy=True) if not ret: log.warning("Configuration is invalid: %s", ret)
def getKeyboardIniConfig(): global _conf if _conf is not None: return _conf path = getKeyboardKeysIniFilePath() conf = ConfigObj(path, configspec=StringIO(_configSpec), encoding="utf-8", list_values=False) conf.newlines = "\r\n" val = Validator() ret = conf.validate(val, preserve_errors=True, copy=True) if not ret: log.warning("KeyboardKeys configuration file is invalid: %s", ret) _conf = conf return conf
def saveSettings(self, force=False): # We never want to save config if runing securely if globalVars.appArgs.secure: return # We save the configuration, in case the user # would not have checked the "Save configuration on exit # " checkbox in General settings or force is is True if not force and not config.conf['general']['saveConfigurationOnExit']: return if self.addonConfig is None: return try: val = Validator() self.addonConfig.validate(val, copy=True, preserve_errors=True) self.addonConfig.write() log.warning("%s: configuration saved" % _addonName) except Exception: log.warning("%s: Could not save configuration - probably read only file system" % _addonName)
def __init__(self, input, translatedInput=None): """ Constructs an L{AddonManifest} instance from manifest string data @param input: data to read the manifest informatinon @type input: a fie-like object. @param translatedInput: translated manifest input @type translatedInput: file-like object """ super(AddonManifest, self).__init__(input, configspec=self.configspec, encoding='utf-8', default_encoding='utf-8') self._errors = [] val = Validator() result = self.validate(val, copy=True, preserve_errors=True) if result != True: self._errors = result self._translatedConfig = None if translatedInput is not None: self._translatedConfig = ConfigObj(translatedInput, encoding='utf-8', default_encoding='utf-8') for k in ('summary','description'): val=self._translatedConfig.get(k) if val: self[k]=val
def test_reloading_with_an_actual_file(self, request, reloadable_cfg_content, cfg_contents): with NamedTemporaryFile(delete=False, mode='wb') as cfg_file: cfg_file.write(reloadable_cfg_content.encode('utf-8')) request.addfinalizer(lambda : os.unlink(cfg_file.name)) configspec = cfg_contents(""" test1= integer(30,50) test2= string test3=integer test4=float(4.5) [section] test1=integer(30,50) test2=string test3=integer test4=float(4.5) [[sub section]] test1=integer(30,50) test2=string test3=integer test4=float(4.5) [section2] test1=integer(30,50) test2=string test3=integer test4=float(4.5) """) cfg = ConfigObj(cfg_file.name, configspec=configspec) cfg.configspec['test1'] = 'integer(50,60)' backup = ConfigObj(cfg_file.name) del cfg['section'] del cfg['test1'] cfg['extra'] = '3' cfg['section2']['extra'] = '3' cfg.reload() assert cfg == backup assert cfg.validate(Validator())
def main(): parser = ArgumentParser() parser.add_argument('-i', dest='src', default=None, action='store', type=str, required=True, help='input file') parser.add_argument('-o', dest='dst', default=None, action='store', type=str, required=True, help='output file') ns = parser.parse_args() if exists(ns.dst): return print('{} already exists, do nothing.'.format(ns.dst)) with open(ns.src, mode='rt') as f: configdata = ConfigObj(f, configspec=defaults_to_configspec()) validator = Validator() configdata.validate(validator, preserve_errors=True) for k, v in configdata['keybindings'].items(): if v == 'None': configdata['keybindings'][k] = '' with open(ns.dst, mode='wt') as f: json.dump(configdata, f, indent=4, separators=(',', ':'), sort_keys=True) return print('convert {} to {}'.format(ns.src, ns.dst))
class ConfigBase(Borg): """Class to provide access to our user configuration""" loaded = None whined = None sections = None global_config = None profiles = None keybindings = None plugins = None layouts = None command_line_options = None def __init__(self): """Class initialiser""" Borg.__init__(self, self.__class__.__name__) self.prepare_attributes() self.load() def prepare_attributes(self): """Set up our borg environment""" if self.loaded is None: self.loaded = False if self.whined is None: self.whined = False if self.sections is None: self.sections = [ 'global_config', 'keybindings', 'profiles', 'layouts', 'plugins' ] if self.global_config is None: self.global_config = copy(DEFAULTS['global_config']) if self.profiles is None: self.profiles = {} self.profiles['default'] = copy(DEFAULTS['profiles']['default']) if self.keybindings is None: self.keybindings = copy(DEFAULTS['keybindings']) if self.plugins is None: self.plugins = {} if self.layouts is None: self.layouts = {} for layout in DEFAULTS['layouts']: self.layouts[layout] = copy(DEFAULTS['layouts'][layout]) def defaults_to_configspec(self): """Convert our tree of default values into a ConfigObj validation specification""" configspecdata = {} keymap = { 'int': 'integer', 'str': 'string', 'bool': 'boolean', } section = {} for key in DEFAULTS['global_config']: keytype = DEFAULTS['global_config'][key].__class__.__name__ value = DEFAULTS['global_config'][key] if keytype in keymap: keytype = keymap[keytype] elif keytype == 'list': value = 'list(%s)' % ','.join(value) keytype = '%s(default=%s)' % (keytype, value) section[key] = keytype configspecdata['global_config'] = section section = {} for key in DEFAULTS['keybindings']: value = DEFAULTS['keybindings'][key] if value is None or value == '': continue section[key] = 'string(default=%s)' % value configspecdata['keybindings'] = section section = {} for key in DEFAULTS['profiles']['default']: keytype = DEFAULTS['profiles']['default'][key].__class__.__name__ value = DEFAULTS['profiles']['default'][key] if keytype in keymap: keytype = keymap[keytype] elif keytype == 'list': value = 'list(%s)' % ','.join(value) if keytype == 'string': value = '"%s"' % value keytype = '%s(default=%s)' % (keytype, value) section[key] = keytype configspecdata['profiles'] = {} configspecdata['profiles']['__many__'] = section section = {} section['type'] = 'string' section['parent'] = 'string' section['profile'] = 'string(default=default)' section['command'] = 'string(default="")' section['position'] = 'string(default="")' section['size'] = 'list(default=list(-1,-1))' configspecdata['layouts'] = {} configspecdata['layouts']['__many__'] = {} configspecdata['layouts']['__many__']['__many__'] = section configspecdata['plugins'] = {} configspec = ConfigObj(configspecdata) if DEBUG == True: configspec.write(open('/tmp/terminator_configspec_debug.txt', 'w')) return (configspec) def load(self): """Load configuration data from our various sources""" if self.loaded is True: dbg('ConfigBase::load: config already loaded') return filename = os.path.join(get_config_dir(), 'config') dbg('looking for config file: %s' % filename) try: configfile = open(filename, 'r') except Exception, ex: if not self.whined: err('ConfigBase::load: Unable to open %s (%s)' % (filename, ex)) self.whined = True return # If we have successfully loaded a config, allow future whining self.whined = False try: configspec = self.defaults_to_configspec() parser = ConfigObj(configfile, configspec=configspec) validator = Validator() result = parser.validate(validator, preserve_errors=True) except Exception, ex: err('Unable to load configuration: %s' % ex) return
class ConfigBase(Borg): """Class to provide access to our user configuration""" loaded = None whined = None sections = None global_config = None profiles = None keybindings = None plugins = None layouts = None command_line_options = None _curlayoutname = 'default' _dirty = None _nosave = None _building = None def __init__(self): """Class initialiser""" Borg.__init__(self, self.__class__.__name__) self.prepare_attributes() import optionparse self.command_line_options = optionparse.options self.load() self._dirty = False self._nosave = False def prepare_attributes(self): """Set up our borg environment""" if self.loaded is None: self.loaded = False if self.whined is None: self.whined = False if self.sections is None: self.sections = [ 'global_config', 'keybindings', 'profiles', 'layouts', 'plugins' ] if self.global_config is None: self.global_config = copy(DEFAULTS['global_config']) if self.profiles is None: self.profiles = {} self.profiles['default'] = copy(DEFAULTS['profiles']['default']) if self.keybindings is None: self.keybindings = copy(DEFAULTS['keybindings']) if self.plugins is None: self.plugins = {} if self.layouts is None: self.layouts = {} for layout in DEFAULTS['layouts']: self.layouts[layout] = copy(DEFAULTS['layouts'][layout]) # XXX prefseditor Cancel feature preparation def get_undo_tree(self): r = {} for k in self.sections: r[k] = getattr(self, k) return r # FIXME this all configspec thing needs to be purged off. No mere user # run terminal under terminal to get a chance to read what this 'validator' # whines about. Here already is DEFAULTS dict to get sane value from if the # conf file lacks a key. An user who can do vim ~/.config/terminator/config # will know what to do if she'd do a typo there. Mortals have prefseditor. # This mess will stay for a while due to plugins and time. (ohir) def defaults_to_configspec(self): """Convert our tree of default values into a ConfigObj validation specification""" configspecdata = {} keymap = { 'int': 'integer', 'str': 'string', 'bool': 'boolean', } section = {} for key in DEFAULTS['global_config']: keytype = DEFAULTS['global_config'][key].__class__.__name__ value = DEFAULTS['global_config'][key] if keytype in keymap: keytype = keymap[keytype] elif keytype == 'list': value = 'list(%s)' % ','.join(value) keytype = '%s(default=%s)' % (keytype, value) if key == 'custom_url_handler': keytype = 'string(default="")' section[key] = keytype configspecdata['global_config'] = section section = {} for key in DEFAULTS['keybindings']: value = DEFAULTS['keybindings'][key] if value is None or value == '': continue section[key] = 'string(default=%s)' % value configspecdata['keybindings'] = section section = {} for key in DEFAULTS['profiles']['default']: keytype = DEFAULTS['profiles']['default'][key].__class__.__name__ value = DEFAULTS['profiles']['default'][key] if keytype in keymap: keytype = keymap[keytype] elif keytype == 'list': value = 'list(%s)' % ','.join(value) if keytype == 'string': value = '"%s"' % value keytype = '%s(default=%s)' % (keytype, value) section[key] = keytype configspecdata['profiles'] = {} configspecdata['profiles']['__many__'] = section section = {} section['type'] = 'string' section['parent'] = 'string' section['profile'] = 'string(default=default)' section['position'] = 'string(default="")' #section['size'] = 'list(default=list(-1,-1))' #section['size'] = 'list' configspecdata['layouts'] = {} configspecdata['layouts']['__many__'] = {} configspecdata['layouts']['__many__']['__many__'] = section configspecdata['plugins'] = {} configspec = ConfigObj(configspecdata) if DEBUG == True: configspec.write(open('/tmp/terminator_configspec_debug.txt', 'w')) return (configspec) def load(self): """Load configuration data from our various sources""" if self.loaded is True: dbg('ConfigBase::load: config already loaded') return if self.command_line_options: if not self.command_line_options.config: self.command_line_options.config = os.path.join( get_config_dir(), 'config92') filename = self.command_line_options.config else: #filename = os.path.join(get_config_dir(), 'config') filename = os.path.join(get_config_dir(), 'config92') dbg('looking for config file: %s' % filename) try: configfile = open(filename, 'r') except Exception, ex: if not self.whined: err('ConfigBase::load: Unable to open %s (%s)' % (filename, ex)) self.whined = True return # If we have successfully loaded a config, allow future whining self.whined = False try: configspec = self.defaults_to_configspec() parser = ConfigObj(configfile, configspec=configspec) validator = Validator() result = parser.validate(validator, preserve_errors=True) except Exception, ex: err('Unable to load configuration: %s' % ex) return
def val(): return Validator()
def test_validate_no_valid_entries(conf): validator = Validator() result = conf.validate(validator) assert not result
def test_validate_extra_values(conf): conf.validate(Validator(), preserve_errors=True) assert conf.extra_values == ['extra', 'extra-section'] assert conf['section'].extra_values == ['extra-sub-section'] assert conf['section']['sub-section'].extra_values == ['extra']
def is_logging_level(value): """ Coerces a string to an integer logging level which maps to a standard python logging level """ try: level = format_loglevel(value) except: raise validate.VdtTypeError(value) return level def is_cmdline(value): """ Parse command line input """ try: return shlex.split(value) except: raise validate.VdtTypeError(value) VALIDATOR = Validator({ "octal": is_octal, "logging_level": is_logging_level, "coerced_list": is_coerced_list, "cmd_args": is_cmdline, })