def delete_section_property(self, section_name, property_name):
        """
        Args:
            section_name (str): the name of the section, case insensitive
            property_name (str): the property name in the section
        """
        section_name = JSONSchema.format_section_name(section_name).lower()
        property_name = JSONSchema.format_property_name(property_name)
        rebuild_data = False
        if section_name in self._sections and \
            'properties' in self._sections[section_name] and \
                property_name in self._sections[section_name]['properties']:
            del self._sections[section_name]['properties'][property_name]
            rebuild_data = True

        if rebuild_data:
            contents = ''
            properties = self._sections[section_name]['properties']
            lastIndex = len(properties) - 1
            for i, (key, value) in enumerate(properties.items()):
                contents += '{}{}{}'.format(key,
                                            InputParser._PROPVALUE_SEPARATOR,
                                            value)
                if i < lastIndex:
                    contents += '\n'

            self._sections[section_name]['data'] = contents
예제 #2
0
    def is_pluggable_section(section_name):
        section_name = JSONSchema.format_section_name(section_name)
        for pluggable_type in local_pluggables_types():
            if section_name == pluggable_type.value:
                return True

        return False
    def section_is_text(self, section_name):
        section_name = JSONSchema.format_section_name(section_name).lower()
        types = self.get_section_types(section_name)
        if len(types) > 0:
            return 'string' in types

        return False
    def check_if_substitution_key(self, section_name, property_names):
        result = [(property_name, False) for property_name in property_names]
        if not self.is_substitution_allowed():
            return result

        section_name = JSONSchema.format_section_name(section_name).lower()
        property_names = [
            JSONSchema.format_property_name(property_name)
            for property_name in property_names
        ]
        section_property_name = self.get_property_default_value(
            section_name, JSONSchema.NAME)
        section_property_name = self.get_section_property(
            section_name, JSONSchema.NAME, section_property_name)
        for key in self._substitutions.keys():
            key_items = key.split('.')
            if len(key_items) == 3 and \
                    key_items[0] == section_name and \
                    key_items[1] == section_property_name and \
                    key_items[2] in property_names:
                result[property_names.index(key_items[2])] = (key_items[2],
                                                              True)
                continue

        return result
 def delete_section_properties(self, section_name):
     """
     Args:
         section_name (str): the name of the section, case insensitive
     """
     section_name = JSONSchema.format_section_name(section_name).lower()
     if section_name in self._sections:
         del self._sections[section_name]
 def delete_section_data(self, section_name):
     """
     Deletes a section data.
     Args:
         section_name (str): the name of the section, case insensitive
     """
     section_name = JSONSchema.format_section_name(section_name).lower()
     if section_name in self._sections:
         self._sections[section_name]['data'] = ''
         self._sections[section_name]['properties'] = OrderedDict()
    def set_section_property(self, section_name, property_name, value):
        section_name = JSONSchema.format_section_name(section_name).lower()
        property_name = JSONSchema.format_property_name(property_name)
        value = self._json_schema.check_property_value(section_name,
                                                       property_name, value)
        types = self.get_property_types(section_name, property_name)

        parser_temp = copy.deepcopy(self)
        InputParser._set_section_property(parser_temp._sections, section_name,
                                          property_name, value, types)
        msg = self._json_schema.validate_property(parser_temp.to_JSON(),
                                                  section_name, property_name)
        if msg is not None:
            raise AquaChemistryError("{}.{}: Value '{}': '{}'".format(
                section_name, property_name, value, msg))

        InputParser._set_section_property(self._sections, section_name,
                                          property_name, value, types)
        if property_name == JSONSchema.NAME:
            if InputParser.OPERATOR == section_name:
                self._update_operator_input_schema()
                # remove properties that are not valid for this section
                default_properties = self.get_section_default_properties(
                    section_name)
                if isinstance(default_properties, dict):
                    properties = self.get_section_properties(section_name)
                    for property_name in list(properties.keys()):
                        if property_name != JSONSchema.NAME and property_name not in default_properties:
                            self.delete_section_property(
                                section_name, property_name)
            elif JSONSchema.PROBLEM == section_name:
                self._update_algorithm_problem()
                self._update_operator_problem()
            elif InputParser.is_pluggable_section(section_name):
                self._json_schema.update_pluggable_input_schemas(self)
                # remove properties that are not valid for this section
                default_properties = self.get_section_default_properties(
                    section_name)
                if isinstance(default_properties, dict):
                    properties = self.get_section_properties(section_name)
                    for property_name in list(properties.keys()):
                        if property_name != JSONSchema.NAME and property_name not in default_properties:
                            self.delete_section_property(
                                section_name, property_name)

                if section_name == JSONSchema.ALGORITHM:
                    self._update_dependency_sections()
            elif value is not None:
                value = str(value).lower().strip()
                if len(value) > 0 and self.section_is_driver(value):
                    self._update_driver_input_schemas()
                    self._update_driver_sections()

        self._sections = self._order_sections(self._sections)
 def set_section(self, section_name):
     """
     Args:
         section_name (str): the name of the section, case insensitive
     """
     section_name = JSONSchema.format_section_name(section_name).lower()
     if section_name not in self._sections:
         self._sections[section_name] = OrderedDict([(JSONSchema.NAME,
                                                      section_name)])
         self._sections[section_name]['properties'] = OrderedDict()
         self._sections[section_name]['data'] = ''
         self._sections = self._order_sections(self._sections)
 def get_section(self, section_name):
     """Return a Section by name.
     Args:
         section_name (str): the name of the section, case insensitive
     Returns:
         Section: The section with this name
     Raises:
         AquaChemistryError: if the section does not exist.
     """
     section_name = JSONSchema.format_section_name(section_name).lower()
     try:
         return self._sections[section_name]
     except KeyError:
         raise AquaChemistryError('No section "{0}"'.format(section_name))
    def delete_section(self, section_name):
        """
        Args:
            section_name (str): the name of the section, case insensitive
        """
        section_name = JSONSchema.format_section_name(section_name).lower()
        if section_name not in self._sections:
            return

        del self._sections[section_name]

        # update schema
        self._json_schema.rollback_changes()
        self._json_schema.update_pluggable_input_schemas(self)
        self._update_driver_input_schemas()
        self._update_operator_input_schema()
    def get_section_data(self, section_name, default_value=None):
        """
        Return a section data.
        Args:
            section_name (str): the name of the section, case insensitive
            default_value : default value in case it is not found
        Returns:
            Value: data value
        """
        section_name = JSONSchema.format_section_name(section_name).lower()
        if section_name in self._sections:
            section = self._sections[section_name]
            if 'data' in section:
                return section['data']

        return default_value
예제 #12
0
    def get_section_property(self, section_name, property_name, default_value=None):
        """Return a property by name.
        Args:
            section_name (str): the name of the section, case insensitive
            property_name (str): the property name in the section
            default_value : default value in case it is not found
        Returns:
            Value: The property value
        """
        section_name = JSONSchema.format_section_name(section_name).lower()
        property_name = JSONSchema.format_property_name(property_name)
        if section_name in self._sections:
            section = self._sections[section_name]
            if 'properties' in section and property_name in section['properties']:
                return section['properties'][property_name]

        return default_value
    def set_section_data(self, section_name, value):
        """
        Sets a section data.
        Args:
            section_name (str): the name of the section, case insensitive
            value : value to set
        """
        section_name = JSONSchema.format_section_name(section_name).lower()
        value = self._json_schema.check_section_value(section_name, value)
        self._sections[section_name] = OrderedDict([(JSONSchema.NAME,
                                                     section_name)])
        self._sections[section_name]['data'] = value
        properties = OrderedDict()
        if value is not None:
            lines = str(value).splitlines()
            for line in lines:
                k, v = self._get_key_value(line)
                if k is not None and v is not None:
                    properties[k] = v

        self._sections[section_name]['properties'] = properties
    def _set_section_property(sections, section_name, property_name, value,
                              types):
        """
        Args:
            section_name (str): the name of the section, case insensitive
            property_name (str): the property name in the section
            value : property value
            types : schema valid types
        """
        section_name = JSONSchema.format_section_name(section_name).lower()
        property_name = JSONSchema.format_property_name(property_name)
        value = JSONSchema.get_value(value, types)

        if section_name not in sections:
            sections[section_name] = OrderedDict([(JSONSchema.NAME,
                                                   section_name)])

        if 'properties' not in sections[section_name]:
            sections[section_name]['properties'] = OrderedDict()

        # name should come first
        if JSONSchema.NAME == property_name and property_name not in sections[
                section_name]['properties']:
            new_dict = OrderedDict([(property_name, value)])
            new_dict.update(sections[section_name]['properties'])
            sections[section_name]['properties'] = new_dict
        else:
            sections[section_name]['properties'][property_name] = value

        # rebuild data
        contents = ''
        properties = sections[section_name]['properties']
        lastIndex = len(properties) - 1
        for i, (key, value) in enumerate(properties.items()):
            contents += '{}{}{}'.format(key, InputParser._PROPVALUE_SEPARATOR,
                                        value)
            if i < lastIndex:
                contents += '\n'

        sections[section_name]['data'] = contents
    def _load_parser_from_dict(self):
        self._sections = OrderedDict()
        for section_name, value in self._inputdict.items():
            section_name = JSONSchema.format_section_name(section_name).lower()
            self._sections[section_name] = OrderedDict()
            self._sections[section_name]['properties'] = OrderedDict()
            self._sections[section_name]['data'] = ''
            if isinstance(value, dict):
                for k, v in value.items():
                    self._sections[section_name]['properties'][k] = v
                contents = ''
                properties = self._sections[section_name]['properties']
                lastIndex = len(properties) - 1
                for i, (k, v) in enumerate(properties.items()):
                    contents += '{}{}{}'.format(
                        k, InputParser._PROPVALUE_SEPARATOR, v)
                    if i < lastIndex:
                        contents += '\n'
                self._sections[section_name]['data'] = contents
            elif isinstance(value, list) or isinstance(value, str):
                lines = []
                if isinstance(value, list):
                    lines = value
                    self._sections[section_name]['data'] = '\n'.join(
                        str(e) for e in value)
                else:
                    lines = value.splitlines()
                    self._sections[section_name]['data'] = value

                for line in lines:
                    k, v = self._get_key_value(line)
                    if k is not None and v is not None:
                        self._sections[section_name]['properties'][k] = v
            else:
                raise AquaChemistryError(
                    "Invalid parser input type for section {}".format(
                        section_name))
 def section_is_driver(self, section_name):
     section_name = JSONSchema.format_section_name(section_name).lower()
     InputParser._load_driver_names()
     return section_name in InputParser._DRIVER_NAMES
 def is_pluggable_section(section_name):
     return JSONSchema.format_section_name(
         section_name).lower() in local_pluggables_types()