Exemple #1
0
    def push_schema(self, name, schema):
        """
        Store a schema on server and cache it

        :param str name: name of schema file
        :param str schema: xml content in a string
        :raises ValueError: if schema name already exists
        :raises SyntaxError: if xml cannot be read by lxml
        :raises AttributeError: if unique_key does not exist
        :raises ValueError: if unique_key exists in an other schema
        """

        if is_name_available(name):
            raise ValueError("Provided schema name : '{}' already exists".format(name))

        if self.validate_schema(schema)[0] is False:
            raise SyntaxError("Provided xml can neither be interpreted as " "XMLSchema nor as XSLT")

        xschema = parse(StringIO(schema))
        unique_key = get_unique_key(xschema)
        if unique_key is None:
            raise AttributeError("Unique key (targetNamespace attribute of " "root element) does not exist")

        if self.is_unique_key_existing(unique_key):
            raise ValueError("Unique key ('{}') already exists".format(unique_key))

        # if no exception has been raised until here, we can write the
        # schema and add it to cache
        self.cache_schema(xschema)
        with open(get_schema_path(name), "w") as schema_file:
            schema_file.write(schema)
Exemple #2
0
    def push_schema(self, name, schema):
        """
        Store a schema on server and cache it

        :param str name: name of schema file
        :param str schema: xml content in a string
        :raises ValueError: if schema name already exists
        :raises SyntaxError: if xml cannot be read by lxml
        :raises AttributeError: if unique_key does not exist
        :raises ValueError: if unique_key exists in an other schema
        """

        if is_name_available(name):
            raise ValueError(
                'Provided schema name : \'{}\' already exists'.format(name))

        if self.validate_schema(schema)[0] is False:
            raise SyntaxError('Provided xml can neither be interpreted as '
                              'XMLSchema nor as XSLT')

        xschema = parse(StringIO(schema))
        unique_key = get_unique_key(xschema)
        if unique_key is None:
            raise AttributeError('Unique key (targetNamespace attribute of '
                                 'root element) does not exist')

        if self.is_unique_key_existing(unique_key):
            raise ValueError(
                'Unique key (\'{}\') already exists'.format(unique_key))

        # if no exception has been raised until here, we can write the
        # schema and add it to cache
        self.cache_schema(xschema)
        with open(get_schema_path(name), 'w') as schema_file:
            schema_file.write(schema)
Exemple #3
0
    def test_get_unique_key(self):
        supposed_schemas = [
            ('profile.xsd', 'profile:1.0'),
            ('profile_to_name.xsl', 'profile>name:1.0'),
            ('keyless_profile.xsd', None),
        ]

        for schema_file, supposed_key in supposed_schemas:
            xschema = parse(mock_get_schema_path(schema_file))
            returned_key = get_unique_key(xschema)

            self.assertEqual(supposed_key, returned_key)
Exemple #4
0
    def cache_schema(self, xschema):
        """
        (Over)write a cache entry for supplied schema

        :param _ElementTree xschema: schema to cache
        :return: False is xschema has no unique_key, True otherwise
        """
        unique_key = get_unique_key(xschema)
        if unique_key is None:
            return False
        else:
            self.cache[unique_key] = xschema
            return True
Exemple #5
0
    def cache_schema(self, xschema):
        """
        (Over)write a cache entry for supplied schema

        :param _ElementTree xschema: schema to cache
        :return: False is xschema has no unique_key, True otherwise
        """
        unique_key = get_unique_key(xschema)
        if unique_key is None:
            return False
        else:
            self.cache[unique_key] = xschema
            return True
Exemple #6
0
    def load_cache(self):
        """
        Put each available schema in cache
        """

        # For the moment only one location is allowed for schemas : in
        # root_path/share/canopsis/schema/xml
        for schema_file in listdir(get_schema_path()):
            try:
                xschema = parse(get_schema_path(schema_file))
            except Exception:
                # If we don't manage to parse it, it may be a non-xml
                # file that we ignore
                continue

            unique_key = get_unique_key(xschema)
            # unique_key is None when an xml parsable ressource does not
            # fit the unique_key requirement
            if unique_key is not None and unique_key not in self.cache:
                self.cache[unique_key] = xschema
Exemple #7
0
    def load_cache(self):
        """
        Put each available schema in cache
        """

        self.cache = {}

        # For the moment only one location is allowed for schemas : in
        # root_path/share/canopsis/schema/xml
        for schema_file in listdir(get_schema_path()):
            try:
                xschema = parse(get_schema_path(schema_file))
            except Exception:
                # If we don't manage to parse it, it may be a non-xml
                # file that we ignore
                continue

            unique_key = get_unique_key(xschema)
            # unique_key is None when an xml parsable ressource does not
            # fit the unique_key requirement
            if unique_key is not None and unique_key not in self.cache:
                self.cache[unique_key] = xschema