Exemplo n.º 1
0
def check_default_values(section, key, validator=None):
    """Raise an MissingDefaultError if a value in section does not have a default values

    :param section: the section of a configspec
    :type section: section
    :param key: a key of the section
    :type key: str
    :param validator: a Validator object to get the default values
    :type validator: Validator
    :returns: None
    :raises: MissingDefaultError

    Use this in conjunction with the walk method of a ConfigObj.
    The ConfigObj should be the configspec!
    When you want to use a custom validator, try::

      configinstance.walk(check_default_values, validator=validatorinstance)

    """
    if validator is None:
        validator = Validator()
    try:
        validator.get_default_value(section[key])
    except KeyError:
        #dv = set(section.default_values.keys())  # set of all defined default values
        #scalars = set(section.scalars)  # set of all keys
        #if dv != scalars:
        parents = get_section_path(section)
        msg = 'The Key %s in the section %s is missing a default: %s' % (key, parents, section[key])
        log.debug(msg)
        raise ConfigError(msg)
Exemplo n.º 2
0
 def validate(self):
             
     #def walk_nested_dict(d):
         #for key1, value1 in d.items():
             #if isinstance(value1, dict):
                 #for key2, value2 in walk_nested_dict(value1):
                     #yield [key1, key2], value2
             #else:
                 #yield [key1,], value1
     
     for key1, value1 in self.entrydict.items():
         if not isinstance(value1, dict): # shouldn't happen
             if key1.find('Password') == -1:
                 self.settings[key1] = value1.getvalue()
             else:
                 self.settings[key1] = myutils.password_obfuscate(value1.getvalue())
         else:
             for key2, value2 in value1.items():
                 if not isinstance(value2, dict):
                     if key2.find('Password') == -1:
                         self.settings[key1][key2] = value2.getvalue()
                     else:
                         self.settings[key1][key2] = myutils.password_obfuscate(value2.getvalue())
                 else:
                     for key3, value3 in value2.items():
                         if not isinstance(value3, dict):
                             if key3.find('Password') == -1:
                                 self.settings[key1][key2][key3] = value3.getvalue()
                             else:
                                 self.settings[key1][key2][key3] = myutils.password_obfuscate(value3.getvalue())
                         else:
                             pass # shouldn't happen
             
     errortext=["Some of your input contains errors. "
                 "Detailed error output below.",]
     
     val = Validator()
     val.functions['log_filename_check'] = myutils.validate_log_filename
     val.functions['image_filename_check'] = myutils.validate_image_filename
     valresult=self.settings.validate(val, preserve_errors=True)
     if valresult != True:
         for section_list, key, error in flatten_errors(self.settings, 
                                                             valresult):
             if key is not None:
                 section_list.append(key)
             else:
                 section_list.append('[missing section]')
             section_string = ','.join(section_list)
             if error == False:
                 error = 'Missing value or section.'
             errortext.append('%s: %s' % (section_string, error))
         tkMessageBox.showerror("Erroneous input. Please try again.", 
                     '\n\n'.join(errortext), parent=self.dialog.interior())
         self.settings = self.read_settings()
         return False
     else:
         return True
Exemplo n.º 3
0
def validate(name, X, Y, normal_class):
    anomaly = [x for x, y in zip(X, Y) if y != normal_class]
    y_anomaly = [y for y in Y if y != normal_class]
    normal = [x for x, y in zip(X, Y) if y == normal_class]
    y_normal = [y for y in Y if y == normal_class]

    nu_range = np.logspace(-3, 0, 30) * 0.2
    gamma_range = np.logspace(-5, 4, 10)
    validator = Validator(X, Y, normal, y_normal, anomaly, y_anomaly)

    """
    hypersphere_valid = validator.hyperspherical_predictor(name, nu_range, gamma_range)
    print hypersphere_valid[:-3]
    one_class_svm_valid = validator.one_class_svm(name, nu_range, gamma_range)
    print one_class_svm_valid[:-3]

    plt.plot(hypersphere_valid[-3], hypersphere_valid[-2], 'b')
    plt.plot(hypersphere_valid[-3], hypersphere_valid[-1], 'b--')
    plt.plot(one_class_svm_valid[-3], one_class_svm_valid[-2], 'g')
    plt.plot(one_class_svm_valid[-3], one_class_svm_valid[-1], 'g--')
    #plt.show()
    plt.savefig('graphs/' + name + '.png')
    """
    print validator.svc_biclass(name, nu_range, gamma_range)
    print validator.multiclass_hyperspherical_predictor(name, nu_range, gamma_range)
    print validator.multiclass_one_class_svm(name, nu_range, gamma_range)
    print validator.svc(name, nu_range, gamma_range)
Exemplo n.º 4
0
    def run_custom(self, input_path_list, output_path):

        # aggregate
        print('-' * 20)
        outfiles = [output_path + 'aggregated.xml']
        from aggregate import Aggregator
        options = input_path_list + ['-o', outfiles[-1]]
        Aggregator.run(options)

        # convert shorthands
        print('-' * 20)
        outfiles += [output_path + 'converted.xml']
        from convert import Converter
        options = [outfiles[-2]] + ['-o', outfiles[-1]]
        Converter.run(options)

        # validate conversion
        print('-' * 20)
        from validate import Validator
        options = [outfiles[-1]] + ['-o', output_path + 'validation.log']
        Validator.run(options)

        if self.convert_only:
            return

        # tokenise
        print('-' * 20)
        outfiles += [output_path + 'tokenised.xml']
        from tokenise import TEITokeniser
        options = [outfiles[-2]] + ['-o', outfiles[-1]]
        TEITokeniser.run(options)

        # kwic.xml
        print('-' * 20)
        outfiles += [output_path + 'kwic.xml']
        from kwic import KWICList
        options = [outfiles[-2]] + ['-o', outfiles[-1]]
        if self.para_string:
            options += ['-r', self.para_string]
        KWICList.run(options)

        # kwic.html
        print('-' * 20)
        outfiles += [output_path + 'kwic.html']
        from kwic_html import KwicHtml
        options = [outfiles[-2]] + ['-o', outfiles[-1]]
        KwicHtml.run(options)
Exemplo n.º 5
0
def rewrite_entries(config, path, specpath, sec=None, sort=False):
    file = open(path, 'w')
    file.write(NOTE % specpath)

    if sec is None:
        sec = config
    if sort:
        sec.scalars.sort()
    for entry in sec.scalars:
        v = Validator()
        #config.validate(v)
        #print config[entry]
        #etype = re.sub('\(.*\)','', config[entry])
        ##if etype == 'option':
        etype, eargs, ekwargs, default = v._parse_check(sec[entry])
        if default is not None:
            default = config._quote(default)

        if etype == 'gpg_key_hint':
            etype = 'string'
        description = '\n.. _%s:\n' % entry.replace('_', '-')
        description += '\n.. describe:: %s\n\n' % entry
        comments = [sec.inline_comments[entry]] + sec.comments[entry]
        for c in comments:
            if c:
                description += ' '*4 + re.sub('^\s*#', '', c)
                description = description.rstrip(' ') + '\n'
        if etype == 'option':
            description += '\n    :type: option, one of %s\n' % eargs
        else:
            if etype == 'force_list':
                etype = 'string list'
            description += '\n    :type: %s\n' % etype

        if default is not None:
            default = default.replace('*','\\*')
            if etype in ['string', 'string_list', 'gpg_key_hint'] and default != 'None':
                description += '    :default: "%s"\n\n' % (default)
            else:
                description += '    :default: %s\n\n' % (default)
        file.write(description)
    file.close()
Exemplo n.º 6
0
def readGPCConfig(confF):
    """Uses ConfigObj to read and validate the specified config file"""
    cval = False
    p,f = path.split(confF)
    r,e = path.splitext(f)
    if r.startswith('Behaelter'):
        fspec = r.split('_')[0]+'.spec'
    else:
        fspec = r+'.spec'
    specF = path.join(p,fspec)
    if path.exists(specF):
        cspec = ConfigObj(specF,list_values=False)
        c = ConfigObj(confF, configspec=cspec)
        vtor = Validator()
        vtor.functions['string_set'] = is_string_set
        cval = c.validate(vtor)
    else:
        c = ConfigObj(confF)
        cval = None
    return c,cval
Exemplo n.º 7
0
def rewrite_entries(config, path, specpath, sec=None, sort=False):
    file = open(path, "w")
    file.write(NOTE % specpath)

    if sec == None:
        sec = config
    if sort:
        sec.scalars.sort()
    for entry in sec.scalars:
        v = Validator()
        # config.validate(v)
        # print config[entry]
        # etype = re.sub('\(.*\)','', config[entry])
        ##if etype == 'option':
        etype, eargs, ekwargs, default = v._parse_check(sec[entry])
        if default is not None:
            default = config._quote(default)

        if etype == "gpg_key_hint":
            etype = "string"
        description = "\n.. _%s:\n" % entry.replace("_", "-")
        description += "\n.. describe:: %s\n\n" % entry
        comments = [sec.inline_comments[entry]] + sec.comments[entry]
        for c in comments:
            if c:
                description += " " * 4 + re.sub("^\s*#", "", c) + "\n"
        if etype == "option":
            description += "\n    :type: option, one of %s\n" % eargs
        else:
            if etype == "force_list":
                etype = "string list"
            description += "\n    :type: %s\n" % etype

        if default != None:
            default = default.replace("*", "\\*")
            if etype in ["string", "string_list", "gpg_key_hint"] and default != "None":
                description += '    :default: "%s"\n\n' % (default)
            else:
                description += "    :default: %s\n\n" % (default)
        file.write(description)
    file.close()
Exemplo n.º 8
0
    def __init__(self, conf, parent=None):
        """Constructs a new config obj model

        :param conf: the ConfigObj with its spec already set!.
        :type conf: :class:`configobj.ConfigObj`
        :param parent: the parent object
        :type parent: :class:`PySide.QtCore.QObject`
        :returns: None
        :rtype: None
        :raises: None
        """
        super(ConfigObjModel, self).__init__(parent)
        self._conf = conf
        self._vld = Validator()
        if self._conf.configspec is not None:
            self._conf.validate(self._vld)

        # validation colors
        self._valid_col = QtGui.QColor(105, 205, 105)
        self._invalid_col = QtGui.QColor(250, 135, 135)
Exemplo n.º 9
0
    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 and self.command_line_options.config:
            filename = self.command_line_options.config
        else:
            filename = os.path.join(get_config_dir(), 'config')
            if not os.path.exists(filename):
                filename = os.path.join(get_system_config_dir(), 'config')
        dbg('looking for config file: %s' % filename)
        try:
            configfile = open(filename, 'r')
        except Exception as 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 as ex:
            err('Unable to load configuration: %s' % ex)
            return

        if result != True:
            err('ConfigBase::load: config format is not valid')
            for (section_list, key, _other) in flatten_errors(parser, result):
                if key is not None:
                    err('[%s]: %s is invalid' % (','.join(section_list), key))
                else:
                    err('[%s] missing' % ','.join(section_list))
        else:
            dbg('config validated successfully')

        for section_name in self.sections:
            dbg('ConfigBase::load: Processing section: %s' % section_name)
            section = getattr(self, section_name)
            if section_name == 'profiles':
                for profile in parser[section_name]:
                    dbg('ConfigBase::load: Processing profile: %s' % profile)
                    if section_name not in section:
                        # FIXME: Should this be outside the loop?
                        section[profile] = copy(
                            DEFAULTS['profiles']['default'])
                    section[profile].update(parser[section_name][profile])
            elif section_name == 'plugins':
                if section_name not in parser:
                    continue
                for part in parser[section_name]:
                    dbg('ConfigBase::load: Processing %s: %s' %
                        (section_name, part))
                    section[part] = parser[section_name][part]
            elif section_name == 'layouts':
                for layout in parser[section_name]:
                    dbg('ConfigBase::load: Processing %s: %s' %
                        (section_name, layout))
                    if layout == 'default' and \
                       parser[section_name][layout] == {}:
                        continue
                    section[layout] = parser[section_name][layout]
            elif section_name == 'keybindings':
                if section_name not in parser:
                    continue
                for part in parser[section_name]:
                    dbg('ConfigBase::load: Processing %s: %s' %
                        (section_name, part))
                    if parser[section_name][part] == 'None':
                        section[part] = None
                    else:
                        section[part] = parser[section_name][part]
            else:
                try:
                    section.update(parser[section_name])
                except KeyError as ex:
                    dbg('ConfigBase::load: skipping missing section %s' %
                        section_name)

        self.loaded = True
Exemplo n.º 10
0
def get_config(config_base, custom_file=None, configspec=None):
    """
	Loads a configuration file from multiple locations, and merge the results into one.

	This function will load configuration files from a number of locations in sequence,
	and will overwrite values in the previous level if they are redefined in the current.

	The levels are in sequence:

	1. Distribution level configuration in the program directory called $config_base.config.
	2. System-wide level configuration in /etc/$config_base.config
	3. User level configuration in ~/.$config_base.config
	4. An optionally specified $custom_file

	Parameters
	----------
	config_base: string
		Basename of the configuration file, typically the same as the name of the program.
	custom_file: string
		Absolute path to a custom configuration file.
	configspec: ConfigObj
		Used to sanitize the values in the resulting ConfigObj. Validation errors are currently
		not exposed to the caller.
	"""

    logger = logging.getLogger(__name__)

    logger.debug("Expanding variables")
    home = os.path.expanduser("~")
    loc = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))

    logger.debug("Create empty config")
    config = ConfigObj()

    # Merge in config file in program dir
    if os.path.isfile(os.path.join(loc, "%s.config" % config_base)):
        logger.debug("Loading config from workingdir")
        cfg = ConfigObj(os.path.join(loc, "%s.config" % config_base),
                        configspec=configspec)
        if configspec:
            cfg.validate(Validator())
        config.merge(cfg)

    # Merge in system-wide config (Unix specific)
    if os.path.isfile("/etc/%s.config" % config_base):
        logger.debug("Loading config from /etc")
        cfg = ConfigObj("/etc/%s.config" % config_base, configspec=configspec)
        if configspec:
            cfg.validate(Validator())
        config.merge(cfg)

    # Merge in user specific config
    if os.path.isfile(os.path.join(home, ".%s.config" % config_base)):
        logger.debug("Loading config from homedir")
        cfg = ConfigObj(os.path.join(home, ".%s.config" % config_base),
                        configspec=configspec)
        if configspec:
            cfg.validate(Validator())
        config.merge(cfg)

    # Config file provided on command line has preference
    if custom_file:
        logger.debug("Loading custom config file")
        cfg = ConfigObj(custom_file, configspec=configspec)
        if configspec:
            cfg.validate(Validator())
        config.merge(cfg)

    return config
Exemplo n.º 11
0
import json
from datetime import datetime
from validate import Validator
from validate import VdtTypeError

comp_vtor = Validator()

def check_cassette_details(details):
    '''
    Validates the "details"
    of the cassette component.
    '''
    # First check ids
    detail_valid_keys = [
            "sequenceid",
            "id",
            "reactor"]
    for k1,v1 in details.iteritems():
        if k1 in detail_valid_keys:
            comp_vtor.check("integer(min=1)",v1)
    # Check strings
    detail_valid_keys = [
        "note",
        "componenttype",
        "type",
        ]
    for k1,v1 in details.iteritems():
        if k1 in detail_valid_keys:
            comp_vtor.check("string",v1)

    # Check list of integers
Exemplo n.º 12
0
class ConfigObjModel(QtCore.QAbstractItemModel):
    """Model for ConfigObj

    A ConfigObj is a tree structured ordered dictionary.
    It represents ini-Files. There can also be a config specification.
    You can validate your ConfigObj against that specification.
    The model holds the configobj and can extract data like
    keys, values, specification.
    It should be used with a tree view because you can nest sections
    inside your ini.

    The data is validated life. So invalid or valid values get different forground roles.

    The internal pointers of the indices are the sections of the ConfigObj.
    The three columns are key, value, spec.
    """

    def __init__(self, conf, parent=None):
        """Constructs a new config obj model

        :param conf: the ConfigObj with its spec already set!.
        :type conf: :class:`configobj.ConfigObj`
        :param parent: the parent object
        :type parent: :class:`PySide.QtCore.QObject`
        :returns: None
        :rtype: None
        :raises: None
        """
        super(ConfigObjModel, self).__init__(parent)
        self._conf = conf
        self._vld = Validator()
        if self._conf.configspec is not None:
            self._conf.validate(self._vld)

        # validation colors
        self._valid_col = QtGui.QColor(105, 205, 105)
        self._invalid_col = QtGui.QColor(250, 135, 135)

    def rowCount(self, parent):
        """Reimplemented from QtCore.QAbstractItemModel"""
        if not parent.isValid():
            v = self._conf
        else:
            v = self.get_value(parent)
        if isinstance(v, Section):
            return len(v.keys())
        else:
            return 0

    def columnCount(self, parent):
        """Reimplemented from QtCore.QAbstractItemModel

        3 Columns: Key, Value, Spec
        """
        return 3

    def data(self, index, role = QtCore.Qt.DisplayRole):
        """Reimplemented from QtCore.QAbstractItemModel

        The value gets validated and is red if validation fails
        and green if it passes.
        """
        if not index.isValid():
            return None
        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            if index.column() == 0:
                p = index.internalPointer()
                k = self.get_key(p, index.row())
                return k
            if index.column() == 1:
                v = self.get_value(index)
                if not isinstance(v, Section):
                    return self._val_to_str(v)
            if index.column() == 2:
                return self.get_configspec_str(index)
        if role == QtCore.Qt.ForegroundRole:
            if index.column() == 1:
                v = self.get_value(index)
                if not isinstance(v, Section):
                    spec = self.get_configspec_str(index)
                    if spec is None or isinstance(spec, Section):
                        return
                    try:
                        self._vld.check(spec, v)
                    except ValidateError:
                        return QtGui.QBrush(self._invalid_col)
                    else:
                        return QtGui.QBrush(self._valid_col)

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        """Reimplemented from QtCore.QAbstractItemModel

        You can only set the value.

        :param index: the index to edit, column should be 1.
        :type index: :class:`PySide.QtCore.QModelIndex`
        :param value: the new value for the configobj
        :type value: object
        :param role: Optional - the ItemDataRole. Default is QtCore.Qt.EditRole
        :type role: QtCore.Qt.ItemDataRole
        :returns: True if index was edited, False if index could not be edited.
        :rtype: bool
        :raises: None

        """
        if index.isValid():
            if role == QtCore.Qt.EditRole:
                if index.column() == 1:
                    p = index.internalPointer()
                    k = self.get_key(p, index.row())
                    # we could just set the value
                    # BUT for listvalues etc it will not work
                    strval = self._val_to_str(value)
                    # _handle_value will parse it correctly
                    # comments gets lost
                    (parsedval, comment) = self._conf._handle_value(strval)
                    p[k] = parsedval
                    self.dataChanged.emit(index, index)
                    return True
        return False

    def restore_default(self, index):
        """Set the value of the given index row to its default

        :param index:
        :type index:
        :returns:
        :rtype:
        :raises:
        """
        spec = self.get_configspec_str(index)
        if spec is None or isinstance(spec, Section):
            return
        try:
            default = self._vld.get_default_value(spec)
            defaultstr = self._val_to_str(default)
            self.setData(index, defaultstr)
        except KeyError:
            raise ConfigError("Missing Default Value in spec: \"%s\"" % spec)

    def headerData(self, section, orientation, role):
        """Reimplemented from QtCore.QAbstractItemModel

        Just the header text for the 3 columns.
        Rows do not have headers.
        """
        dispheaders = ('Key', 'Value', 'Spec')
        if orientation == QtCore.Qt.Horizontal:
            if role == QtCore.Qt.DisplayRole:
                try:
                    return dispheaders[section]
                except IndexError:
                    return None

    def flags(self, index):
        """Reimplemented from QtCore.QAbstractItemModel

        Only the value is editable
        """
        if index.column() == 1:
            return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
        else:
            return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

    def parent(self, index):
        """Reimplemented from QtCore.QAbstractItemModel"""
        if not index.isValid():
            return QtCore.QModelIndex()
        p = index.internalPointer()
        if p is self._conf:
            return QtCore.QModelIndex()
        pp = p.parent
        pk = p.name
        row = (pp.scalars + pp.sections).index(pk)
        return self.createIndex(row, 0, pp)

    def index(self, row, column, parent):
        """Reimplemented from QtCore.QAbstractItemModel

        The internal pointer is the section.
        The row determines the key in the scalars then sections of the configobj.
        So for a given index, use row to retrieve the key::

          key = self.get_key(index.internalPointer(), index.row())

        To use the key on the section to get the value OR
        use get_value(index) / get_configspec_str

        """
        if not parent.isValid():
            s = self._conf
        else:
            p = parent.internalPointer()
            k = self.get_key(p, parent.row())
            s = p[k]
        return self.createIndex(row, column, s)

    def get_value(self, index):
        """ Return the value of the given index

        The index stores the section as internal pointer.
        The row of the index determines the key.
        The key is used on the section to return the value

        :param index: The QModelIndex
        :type index: QModelIndex
        :returns: The value for the given index
        """
        p = index.internalPointer()
        k = self.get_key(p, index.row())
        return p[k]

    def get_configspec_str(self, index):
        """ Return the config spec string of the given index

        The index stores the section as internal pointer.
        The row of the index determines the key.
        The section stores the spec in its configspec attribute
        The key is used on the configspec attribute to return the spec

        :param index: The QModelIndex
        :type index: QModelIndex
        :returns: The spec for the given index or None
        """
        p = index.internalPointer()
        if p is None:
            return
        spec = p.configspec
        if spec is None:
            return None
        k = self.get_key(p, index.row())
        try:
            return spec[k]
        except KeyError:
            return None

    def get_key(self, section, row):
        """ Return the key for the given section and row

        A sections stores scalars and sections.
        The row is the index for the combination of scalars and sections.

        :param section: A ConfigObj section
        :type section: Section
        :param row: the index for (section.scalars + section.sections)
        :type row: int
        :returns: the key
        :rtype: str
        :raises: IndexError
        """
        return (section.scalars + section.sections)[row]

    def set_valid_col(self, color):
        """Set the forgroundrole color for values if they are valid

        :param color: this color will be returned from data() when the value is valid and role is QtCore.Qt.ForegroundRole
        :type color: QtGui.QColor
        :returns: None
        :rtype: None
        :raises: None

        default is: QtGui.QColor(105, 205, 105)
        """
        self._valid_col = color

    def set_invalid_col(self, color):
        """Set the forgroundrole color for values if they are valid

        :param color: this color will be returned from data() when the value is valid and role is QtCore.Qt.ForegroundRole
        :type color: QtGui.QColor
        :returns: None
        :rtype: None
        :raises: None

        default is: QtGui.QColor(250, 135, 135)
        """
        self._invalid_col = color

    def _val_to_str(self, value):
        """Converts the value to a string that will be handled correctly by the confobj

        :param value: the value to parse
        :type value: something configobj supports
        :returns: str
        :rtype: str
        :raises: None

        When the value is a list, it will be converted to a string that can be parsed to
        the same list again.
        """
        # might be a list value
        # then represent it 'nicer' so that when we edit it, the same value will return
        if isinstance(value, list):
            # so we have a list value. the default str(v) would produce something like: ['a', 'b']
            # handling such a value is not possible. it should be: 'a', 'b'
            # so we have to convert it to a string but we have to make sure, we do not loose quotes
            # even when values are integers, they get quoted. thats alright. the config obj will parse them correctly
            return ', '.join("'%s'" % str(i) for i in value)
        return str(value)  # so we always have a line editor. validate can convert from string
Exemplo n.º 13
0
_args = _parser.parse_args()

_filepath = _args.config

if _args.daemonise:
    if not os.path.isabs(_filepath):
        _log.critical("In daemon mode config path must be absolute. Exiting.")
        exit(1)
    os.chdir("/")

_workingdir = os.getcwd()
_configpath = os.path.join(_workingdir, _filepath)

_configspec = ConfigObj(_inspec=True, infile={})
_config = ConfigObj(infile=_configpath, file_error=True, configspec=_configspec)
_validator = Validator()
_reload_handlers = []

_config["daemonise"] = _args.daemonise


def _validate_path(value):
    """
    Check that supplied value is a path, normalise it
    and make absolute (relative to working dir)
    """
    if value is None:
        return None
    if not isinstance(value, basestring):
        raise VdtTypeError(value)
    return os.path.normpath(os.path.join(_workingdir, value))
Exemplo n.º 14
0
    def load_config(self):
        """Load Config File"""

        logger.info(self.filename)

        if os.path.isfile(self.filename):
            try:
                # file exists, read & validate it
                self.var_dict = ConfigObj(self.filename,
                                          configspec=CONFIG_SPEC)
                _vdt = Validator()
                result = self.var_dict.validate(_vdt, preserve_errors=True)
                validate_errors = flatten_errors(self.var_dict, result)

                if validate_errors:
                    logger.error(self.tr("errors reading %s:") % self.filename)

                for entry in validate_errors:
                    section_list, key, error = entry
                    if key is not None:
                        section_list.append(key)
                    else:
                        section_list.append('[missing section]')
                    section_string = ', '.join(section_list)
                    if not error:
                        error = self.tr('Missing value or section.')
                    logger.error(section_string + ' = ' + error)

                if validate_errors:
                    raise BadConfigFileError("syntax errors in config file")

                # check config file version against internal version
                if CONFIG_VERSION:
                    fileversion = self.var_dict['Version'][
                        'config_version']  # this could raise KeyError

                    if fileversion != CONFIG_VERSION:
                        raise VersionMismatchError(fileversion, CONFIG_VERSION)

            except VersionMismatchError:
                #raise VersionMismatchError(fileversion, CONFIG_VERSION)
                # version mismatch flag, it will be used to display an error.
                self.version_mismatch = self.tr(
                    "The configuration file version ({0}) doesn't match the software expected version ({1}).\n\nYou have to delete (or carefully edit) the configuration file \"{2}\" to solve the problem."
                ).format(fileversion, CONFIG_VERSION, self.filename)

            except Exception as inst:
                logger.error(inst)
                (base, ext) = os.path.splitext(self.filename)
                badfilename = base + c.BAD_CONFIG_EXTENSION
                logger.debug(
                    self.tr("trying to rename bad cfg %s to %s") %
                    (self.filename, badfilename))
                try:
                    os.replace(self.filename, badfilename)
                except OSError as e:
                    logger.error(
                        self.tr("rename(%s,%s) failed: %s") %
                        (self.filename, badfilename, e.strerror))
                    raise
                else:
                    logger.debug(
                        self.tr("renamed bad varspace %s to '%s'") %
                        (self.filename, badfilename))
                    self.create_default_config()
                    self.default_config = True
                    logger.debug(
                        self.tr("created default varspace '%s'") %
                        self.filename)
            else:
                self.default_config = False
                # logger.debug(self.dir())
                # logger.debug(self.tr("created default varspace '%s'") % self.filename)
                # logger.debug(self.tr("read existing varspace '%s'") % self.filename)
        else:
            self.create_default_config()
            self.default_config = True
            logger.debug(
                self.tr("created default varspace '%s'") % self.filename)

        self.var_dict.main.interpolation = False  # avoid ConfigObj getting too clever
reload(gspr)
reload(gsro)

#subroutine for printing progress to the command line
def print_progress(n_complete, n_total):
    print str(n_complete) + ' of ' + str(n_total) + ' complete: (' + str(100.0*n_complete/float(n_total)) + '%)'

#set up command line argument parser to read in name of config file to use
parser = argparse.ArgumentParser()
parser.add_argument('config', help='configuration file for GMTB SCM analysis', nargs=1)

args = parser.parse_args()

#read in the configuration file specified on the command line (check against the configspec.ini file for validation)
config = ConfigObj(args.config[0],configspec="configspec.ini")
validator = Validator()
results = config.validate(validator)

#standardized output for config file validation errors (quit if error)
if results != True:
    for (section_list, key, _) in flatten_errors(config, results):
        if key is not None:
            print 'The "%s" key in the section "%s" failed validation' % (key, ', '.join(section_list))
        else:
            print 'The following section was missing:%s ' % ', '.join(section_list)
    print 'Since GMTB SCM analysis configuration file did not pass validation, the program will quit.'
    quit()

#get the config file variables
gmtb_scm_datasets = config['gmtb_scm_datasets']
gmtb_scm_datasets_labels = config['gmtb_scm_datasets_labels']
Exemplo n.º 16
0
configspec['composereportd'][
    'lockfile'] = "string(default='/tmp/composereportd.lock')"
configspec['composereportd']['user'] = "******"
configspec['composereportd']['group'] = "string(default=None)"
configspec['composereportd']['exchange'] = "string(default='rmap')"

config = ConfigObj('/etc/rmap/rmap-site.cfg',
                   file_error=False,
                   configspec=configspec)

usrconfig = ConfigObj(os.path.expanduser('~/.rmap.cfg'), file_error=False)
config.merge(usrconfig)
usrconfig = ConfigObj('rmap.cfg', file_error=False)
config.merge(usrconfig)

val = Validator()
test = config.validate(val, preserve_errors=True)
for entry in flatten_errors(config, test):
    # each entry is a tuple
    section_list, key, error = entry
    if key is not None:
        section_list.append(key)
    else:
        section_list.append('[missing section]')
    section_string = ', '.join(section_list)
    if error == False:
        error = 'Missing value or section.'
    print section_string, ' = ', error
    raise error

# section django
Exemplo n.º 17
0
Spec.VswitchName = voption('string')
Spec.Policy.Security.MacChanges = voption('boolean(default=None)')
Spec.VlanId = voption('integer')
Spec.Policy.Security.AllowPromiscuous = voption('boolean(default=None)')
Spec.Policy.Security.ForgedTransmits = voption('boolean(default=None)')
Spec.Name = voption('string')
_vnic._type.faultToleranceLogging = voption('boolean(default=False)')
_vnic._type.vmotion = voption('boolean(default=False)')
_vnic._type.management = voption('boolean(default=False)')
_vnic.Spec.Mtu = voption('integer(default=None)')
_vnic.Spec.TsoEnabled = voption('boolean(default=False)')
_vnic.Spec.Ip.Dhcp = voption('boolean(default=False)')

"""

validator = Validator()


def voption(value, options):
    if value == "None":
        return None
    return validator.check(options, value)


validator.functions["voption"] = voption


def load_config(config_file, config_mode="r"):
    """ Read the config file from disk using ConfigObj. """

    create_empty = config_mode == "w"
Exemplo n.º 18
0
    max_iterations=integer(min=1, default=60)
    [General]
    rep_max=integer(min=1, default=2000)
    max_bit_errors=integer(min=1, default=3000)
    unpacked_parameters=string_list(default=list('SNR'))
    """.split("\n")

    conf_file_parser = ConfigObj(config_file_name,
                                 list_values=True,
                                 configspec=spec)

    #conf_file_parser.write()

    # Dictionary with custom validation functions
    fdict = {'real_numpy_array': real_numpy_array_check}
    validator = Validator(fdict)

    # The 'copy' argument indicates that if we save the ConfigObj object to
    # a file after validating, the default values will also be written to
    # the file.
    result = conf_file_parser.validate(validator,
                                       preserve_errors=True,
                                       copy=True)

    # Note that if there was no parsing errors, then "result" will be True.
    # It there was an error, then result will be a dictionary with each
    # parameter as a key. The value of each key will be either 'True' if
    # that parameter was parsed without error or a "validate.something"
    # object (since we set preserve_errors to True) describing the error.

    # if result != True:
Exemplo n.º 19
0
def read_options_file(KOLIBRI_HOME, ini_filename="options.ini"):

    logger = get_logger(KOLIBRI_HOME)

    ini_path = os.path.join(KOLIBRI_HOME, ini_filename)

    conf = ConfigObj(ini_path, configspec=get_configspec())

    # validate once up front to ensure section structure is in place
    conf.validate(Validator())

    # keep track of which options were overridden using environment variables, to support error reporting
    using_env_vars = {}

    # override any values from their environment variables (if set)
    for section, opts in option_spec.items():
        for optname, attrs in opts.items():
            for envvar in attrs.get("envvars", []):
                if os.environ.get(envvar):
                    logger.info(
                        "Option {optname} in section [{section}] being overridden by environment variable {envvar}"
                        .format(optname=optname,
                                section=section,
                                envvar=envvar))
                    conf[section][optname] = os.environ[envvar]
                    using_env_vars[optname] = envvar
                    break

    conf = clean_conf(conf)

    validation = conf.validate(Validator(), preserve_errors=True)

    # loop over and display any errors with config values, and then bail
    if validation is not True:
        for section_list, optname, error in flatten_errors(conf, validation):
            section = section_list[0]
            if optname in using_env_vars:
                logger.error(
                    "Error processing environment variable option {envvar}: {error}"
                    .format(envvar=using_env_vars[optname], error=error))
            else:
                logger.error(
                    "Error processing {file} under section [{section}] for option {option}: {error}"
                    .format(file=ini_path,
                            section=section,
                            option=optname,
                            error=error))
        logger.critical(
            "Aborting: Could not process options config (see errors above for more details)"
        )
        raise SystemExit(1)

    # loop over any extraneous options and warn the user that we're ignoring them
    for sections, name in get_extra_values(conf):

        # this code gets the extra values themselves
        the_section = conf
        for section in sections:
            the_section = the_section[section]

        # the_value may be a section or a value
        the_value = the_section.pop(name)

        # determine whether the extra item is a section (dict) or value
        kind = "section" if isinstance(the_value, dict) else "option"

        logger.warn(
            "Ignoring unknown {kind} in options file {file} under {section}: {name}."
            .format(
                kind=kind,
                file=ini_path,
                section=sections[0] if sections else "top level",
                name=name,
            ))

    # run validation once again to fill in any default values for options we deleted due to issues
    conf.validate(Validator())

    # ensure all arguments under section "Paths" are fully resolved and expanded, relative to KOLIBRI_HOME
    _expand_paths(KOLIBRI_HOME, conf.get("Paths", {}))

    return conf
Exemplo n.º 20
0
    def __init__(self, config_filename):
        # load config
        self.config = ConfigObj(config_filename, configspec='config/default.ini')

        validator = Validator()
        self.config.validate(validator)