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)
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)
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
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
def test_get_schema_path(self): r = get_schema_path() self.assertEqual(r, root_path + '/share/canopsis/schema')