示例#1
0
 def is_valid(self):
     """ Check if XML is valid with XSD file """
     if self.xsd_path is not None:
         xsd_file = XMLSchema(self.xsd_path)
         xml_file = ElementTree.parse(self.get_xml_path())
         return xsd_file.is_valid(xml_file)
     return False
示例#2
0
 def _read_xml(self, filename, xsd):
     schema = XMLSchema(os.path.join(cwd, "xsd", xsd))
     filepath = os.path.join(self.filepath, filename)
     (data, errors) = schema.to_dict(filepath, validation="lax")
     for error in errors:
         self.logger.error(error)
     return data
示例#3
0
def buildProject(projectFilePath: str, projectSchema: str):

    logger.debug("Building new project object.")
    if projectFilePath is None or projectSchema is None:
        logger.warning("No file and/or no schema file is given.")
        return None
    if not os.path.exists(projectFilePath):
        logger.error("Path of the project file does not exist"\
                " '{}'".format(projectFilePath))
        return None
    if not os.path.exists(projectSchema):
        logger.error("Path of the schema file does not exist"\
                " '{}'".format(projectSchema))
        return None

    schema = XMLSchema(projectSchema)
    (projectDict, errors) = schema.to_dict(projectFilePath, validation="lax")
    errorList = [str(err) for err in errors]
    if len(errorList) > 0:
        logger.error(errorList)

    # can do that because the project file is valid and ProjectId is required
    # by the schema
    pId = UUID(projectDict["Project"]["@ProjectId"])
    pName = str()
    pExtensionSchema = str()
    if "Name" in projectDict["Project"]:
        pName = projectDict["Project"]["Name"]
    if "ExtensionSchema" in projectDict:
        pExtensionSchema = projectDict["ExtensionSchema"]

    p = Project(pId, pName, pExtensionSchema)
    logger.debug("New project object created '{}'".format(p))
    return p
示例#4
0
def getVersion(extrBcfPath: str, versionSchemaPath: str):
    """
    Tries to open `extrBcfPath`/bcf.version. If successful it parses it
    into a python dictonary and returns the content of the attribute
    `VersionId` of the element `Version`.

    If `bcf.version` was not found a ValueError is raised. If `bcf.version`
    does not parse against versionSchema then `None` is returned.
    """

    logger.debug("Retrieving version from the BCF project")
    versionFileName = "bcf.version"
    versionFilePath = os.path.join(extrBcfPath, versionFileName)
    if not os.path.exists(versionFilePath):
        raise ValueError("{} was not found in the extracted zip archive {}."\
                "Make sure that you opened a correct bcf zip archive.".format(
                    versionFileName,
                    os.path.basename(extrBcfPath)))

    versionSchema = XMLSchema(versionSchemaPath)
    if not versionSchema.is_valid(versionFilePath):
        return None

    versionDict = versionSchema.to_dict(versionFilePath)
    version = versionDict["@VersionId"]
    logger.debug("Version of the BCF project is {}".format(version))
    return version
示例#5
0
    def setUpClass(cls):
        cls.schema_class = XMLSchema
        cls.xsd_types = XMLSchema.builtin_types()
        cls.content_pattern = re.compile(r'(xs:sequence|xs:choice|xs:all)')

        cls.default_namespaces = {
            'ns': 'ns',
            'xsi': 'http://www.w3.org/2001/XMLSchema-instance'
        }

        cls.vh_dir = cls.abspath('cases/examples/vehicles')
        cls.vh_xsd_file = cls.abspath('cases/examples/vehicles/vehicles.xsd')
        cls.vh_xml_file = cls.abspath('cases/examples/vehicles/vehicles.xml')
        cls.vh_json_file = cls.abspath('cases/examples/vehicles/vehicles.json')
        cls.vh_schema = XMLSchema(cls.vh_xsd_file)
        cls.vh_namespaces = fetch_namespaces(cls.vh_xml_file)

        cls.col_dir = cls.abspath('cases/examples/collection')
        cls.col_xsd_file = cls.abspath(
            'cases/examples/collection/collection.xsd')
        cls.col_xml_file = cls.abspath(
            'cases/examples/collection/collection.xml')
        cls.col_json_file = cls.abspath(
            'cases/examples/collection/collection.json')
        cls.col_schema = XMLSchema(cls.col_xsd_file)
        cls.col_namespaces = fetch_namespaces(cls.col_xml_file)

        cls.st_xsd_file = cls.abspath(
            'cases/features/decoder/simple-types.xsd')
        cls.st_schema = xmlschema.XMLSchema(cls.st_xsd_file)

        cls.models_xsd_file = cls.abspath('cases/features/models/models.xsd')
        cls.models_schema = xmlschema.XMLSchema(cls.models_xsd_file)
示例#6
0
    def test_data_element_converter(self):
        col_schema = XMLSchema(self.col_xsd_filename,
                               converter=DataElementConverter)
        obj = col_schema.decode(self.col_xml_filename)

        self.assertIsInstance(obj, DataElement)
        self.assertEqual(obj.tag, self.col_xml_root.tag)
        self.assertEqual(obj.nsmap, self.col_nsmap)
示例#7
0
 def setUpClass(cls):
     cls.test_dir = os.path.dirname(__file__)
     cls.xs1 = XMLSchema(
         os.path.join(cls.test_dir, "cases/examples/vehicles/vehicles.xsd"))
     cls.xs2 = XMLSchema(
         os.path.join(cls.test_dir,
                      "cases/examples/collection/collection.xsd"))
     cls.cars = cls.xs1.elements['vehicles'].type.content_type[0]
     cls.bikes = cls.xs1.elements['vehicles'].type.content_type[1]
示例#8
0
    def test_strip_namespace_argument(self):
        # Test for issue #161
        converter = XMLSchemaConverter(strip_namespaces=True)
        col_xsd_filename = self.casepath('examples/collection/collection.xsd')
        col_xml_filename = self.casepath('examples/collection/collection.xml')

        col_schema = XMLSchema(col_xsd_filename, converter=converter)
        self.assertIn('@xmlns:', str(col_schema.decode(col_xml_filename, strip_namespaces=False)))
        self.assertNotIn('@xmlns:', str(col_schema.decode(col_xml_filename)))
class LibDocLib(object):
    def __init__(self, interpreter=None):
        self.interpreter = interpreter
        self.schema = XMLSchema(join(ROOT, 'doc', 'schema', 'libdoc.02.xsd'))

    @property
    def libdoc(self):
        return self.interpreter.libdoc

    @property
    def encoding(self):
        return SYSTEM_ENCODING \
                if not self.interpreter.is_ironpython else CONSOLE_ENCODING

    def run_libdoc(self, args):
        cmd = self.libdoc + self._split_args(args)
        cmd[-1] = cmd[-1].replace('/', os.sep)
        logger.info(' '.join(cmd))
        result = run(cmd,
                     cwd=join(ROOT, 'src'),
                     stdout=PIPE,
                     stderr=STDOUT,
                     encoding=self.encoding,
                     timeout=120,
                     universal_newlines=True)
        logger.info(result.stdout)
        return result.stdout

    def _split_args(self, args):
        lexer = shlex.shlex(args, posix=True)
        lexer.escape = ''
        lexer.whitespace_split = True
        return list(lexer)

    def get_libdoc_model_from_html(self, path):
        with open(path, encoding='UTF-8') as html_file:
            model_string = self._find_model(html_file)
        model = json.loads(model_string.replace('\\x3c/', '</'))
        logger.info(pprint.pformat(model))
        return model

    def _find_model(self, html_file):
        for line in html_file:
            if line.startswith('libdoc = '):
                return line.split('=', 1)[1].strip(' \n;')
        raise RuntimeError('No model found from HTML')

    def validate_spec(self, path):
        self.schema.validate(path)

    def relative_source(self, path, start):
        if not exists(path):
            return path
        try:
            return relpath(path, start)
        except ValueError:
            return normpath(path)
示例#10
0
    def parser(self, xml, xsd, pattern=None):
        """
        Validate xml to match with xsd. Calling parsers to get Model from xml. If provided pattern, filtering Model.
        :param xml: path to MOBILE_API.xml
        :param xsd: path to MOBILE_API.xsd
        :param pattern: regex-pattern from command-line arguments to filter element from initial Model
        :return: initial Model
        """
        self.logger.info(
            '''Validating XML and generating model with following parameters:
            Source xml      : %s
            Source xsd      : %s''', xml, xsd)

        try:
            schema = XMLSchema(xsd)
            if not schema.is_valid(xml):
                raise GenerateError(schema.validate(xml))
            interface = Parser().parse(xml)
        except (InterfaceError, XMLSchemaError, GenerateError) as message1:
            self.logger.critical('Invalid XML file content: %s, %s', xml,
                                 message1)
            sys.exit(1)

        enum_names = tuple(interface.enums.keys())
        struct_names = tuple(interface.structs.keys())

        if pattern:
            intermediate = OrderedDict()
            intermediate.update({'params': interface.params})
            for kind, content in vars(interface).items():
                if kind == 'params':
                    continue
                for name, item in content.items():
                    if re.match(pattern, item.name):
                        self.logger.info('%s/%s match with %s', kind,
                                         item.name, pattern)
                        if kind in intermediate:
                            intermediate[kind].update({name: item})
                        else:
                            intermediate.update({kind: {name: item}})
            interface = Interface(**intermediate)

        self.logger.debug({
            'enums':
            tuple(interface.enums.keys()),
            'structs':
            tuple(interface.structs.keys()),
            'functions':
            tuple(
                map(lambda i: i.function_id.name,
                    interface.functions.values())),
            'params':
            interface.params
        })
        return enum_names, struct_names, interface
示例#11
0
    def test_decode_encode_parker_converter(self):
        col_schema = XMLSchema(self.col_xsd_filename,
                               converter=ParkerConverter)

        obj1 = col_schema.decode(self.col_xml_filename)

        with self.assertRaises(XMLSchemaValidationError) as ec:
            col_schema.encode(obj1,
                              path='./col:collection',
                              namespaces=self.col_nsmap)
        self.assertIn("missing required attribute 'id'", str(ec.exception))
示例#12
0
def validate_single_file(file_to_test: ConnectorFile, path_to_file: Path,
                         xml_violations_buffer: List[str],
                         properties: ConnectorProperties) -> bool:
    """
    Arguments:
        file_to_test {ConnectorFile} -- path to a single file to test
        path_to_file {Path} -- path to the file
        xml_violations_buffer {list[str]} -- a list of strings that holds the xml violation messages
        properties {ConnectorProperties} -- an object contating properties that apply to the entire connector

    Returns:
        bool -- True if the xml file passes validation, false if it does not or there is an error
        Any xml violation messages will be appended to xml_violations_buffer
    """
    logger.debug("Validating " + str(path_to_file))

    if file_to_test.file_type == 'connection-dialog':
        properties.uses_tcd = True

    xsd_file = get_xsd_file(file_to_test)

    if not xsd_file:
        xml_violations_buffer.append("Error: No valid XSD for file type:" +
                                     file_to_test.file_type)
        return False

    manifest_schema = XMLSchema(str(PATH_TO_XSD_FILES / Path(xsd_file)))

    # If the file is too big, we shouldn't try and parse it, just log the violation and move on
    if path_to_file.stat().st_size > MAX_FILE_SIZE:
        xml_violations_buffer.append(file_to_test.file_name +
                                     " exceeds maximum size of " +
                                     str(int(MAX_FILE_SIZE / 1024)) + " KB")
        return False

    # Try to validate the xml. If the xml validation error is thrown, save the violation information to the buffer
    try:
        manifest_schema.validate(str(path_to_file))
    except Exception:
        saved_error_type = sys.exc_info()[0]
        saved_error = sys.exc_info()[1]
        xml_violations_buffer.append("File: " + file_to_test.file_name +
                                     " Error Type: " + str(saved_error_type) +
                                     "\n" + str(saved_error))
        logger.error("XML Validation failed for " + file_to_test.file_name)
        return False

    if not validate_file_specific_rules(file_to_test, path_to_file,
                                        xml_violations_buffer, properties):
        logger.error("XML Validation failed for " + file_to_test.file_name)
        return False

    return True
示例#13
0
    def test_decode_encode_unordered_converter_with_preserve_root(self):
        col_schema = XMLSchema(self.col_xsd_filename,
                               converter=UnorderedConverter)

        # Decode from XML file
        obj1 = col_schema.decode(self.col_xml_filename, preserve_root=True)
        self.assertIn("'col:collection'", repr(obj1))
        self.assertIn("'@xmlns:col'", repr(obj1))

        root = col_schema.encode(obj1,
                                 path='./col:collection',
                                 namespaces=self.col_nsmap,
                                 preserve_root=True)
        self.assertIsNone(
            etree_elements_assert_equal(self.col_xml_root, root, strict=False))

        root = col_schema.encode(obj1, preserve_root=True)
        self.assertIsNone(
            etree_elements_assert_equal(self.col_xml_root, root, strict=False))

        # Decode from lxml.etree.Element tree
        obj2 = col_schema.decode(self.col_lxml_root, preserve_root=True)
        self.assertIn("'col:collection'", repr(obj2))
        self.assertIn("'@xmlns:col'", repr(obj2))
        self.assertEqual(obj1, obj2)

        # Decode from ElementTree.Element tree providing namespaces
        obj2 = col_schema.decode(self.col_xml_root,
                                 namespaces=self.col_nsmap,
                                 preserve_root=True)
        self.assertIn("'col:collection'", repr(obj2))
        self.assertIn("'@xmlns:col'", repr(obj2))
        self.assertEqual(obj1, obj2)

        # Decode from ElementTree.Element tree without namespaces
        obj2 = col_schema.decode(self.col_xml_root, preserve_root=True)
        self.assertNotIn("'col:collection'", repr(obj2))
        self.assertNotIn("'@xmlns:col'", repr(obj2))
        self.assertNotEqual(obj1, obj2)

        root = col_schema.encode(obj2,
                                 path='./col:collection',
                                 namespaces=self.col_nsmap,
                                 preserve_root=True)
        self.assertIsNone(
            etree_elements_assert_equal(self.col_xml_root, root, strict=False))

        root = col_schema.encode(
            obj2, preserve_root=True)  # No namespace unmap is required
        self.assertIsNone(
            etree_elements_assert_equal(self.col_xml_root, root, strict=False))
示例#14
0
    def test_exception_repr(self):
        xs = XMLSchema(
            os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd'))

        error = XMLSchemaValidatorError(xs, 'unknown error')
        self.assertEqual(str(error), 'unknown error')
        self.assertEqual(error.msg, 'unknown error')

        error = XMLSchemaValidatorError(xs, 'unknown error', elem=xs.root)
        lines = str(error).split('\n')

        self.assertEqual(lines[0], 'unknown error:')
        self.assertEqual(lines[2], 'Schema:')
        self.assertTrue(lines[4].strip().startswith('<xs:schema '))
        self.assertEqual(lines[-2].strip(), '</xs:schema>')

        schema = XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:element name="root" type="xs:integer"/>
            </xs:schema>""")
        root = lxml.etree.XML('<root a="10"/>')

        with self.assertRaises(XMLSchemaValidationError) as ctx:
            schema.validate(root)

        lines = str(ctx.exception).split('\n')
        self.assertEqual(
            lines[0],
            "failed validating {'a': '10'} with XsdAttributeGroup():")
        self.assertEqual(lines[2],
                         "Reason: 'a' attribute not allowed for element.")
        self.assertEqual(lines[8], "Instance (line 1):")
        self.assertEqual(lines[12], "Path: /root")

        self.assertEqual(
            repr(ctx.exception), "XMLSchemaValidationError(reason=\"'a' "
            "attribute not allowed for element.\")")

        error = XMLSchemaValidationError(schema.elements['root'], root)
        self.assertIsNone(error.reason)
        self.assertNotIn("Reason:", str(error))
        self.assertIn("Schema:", str(error))

        error = XMLSchemaValidationError(schema, root)
        self.assertNotIn("Reason:", str(error))
        self.assertNotIn("Schema:", str(error))

        error = XMLSchemaValidationError(schema, 10)
        self.assertEqual(
            str(error), "failed validating 10 with XMLSchema10(namespace='')")
示例#15
0
    def test_columnar_converter(self):
        col_schema = XMLSchema(self.col_xsd_filename,
                               converter=ColumnarConverter)

        obj = col_schema.decode(self.col_xml_filename)
        self.assertIn("'authorid'", str(obj))
        self.assertNotIn("'author_id'", str(obj))
        self.assertNotIn("'author__id'", str(obj))

        obj = col_schema.decode(self.col_xml_filename, attr_prefix='_')
        self.assertNotIn("'authorid'", str(obj))
        self.assertIn("'author_id'", str(obj))
        self.assertNotIn("'author__id'", str(obj))

        obj = col_schema.decode(self.col_xml_filename, attr_prefix='__')
        self.assertNotIn("'authorid'", str(obj))
        self.assertNotIn("'author_id'", str(obj))
        self.assertIn("'author__id'", str(obj))

        col_schema = XMLSchema(self.col_xsd_filename)

        obj = col_schema.decode(self.col_xml_filename,
                                converter=ColumnarConverter,
                                attr_prefix='__')
        self.assertNotIn("'authorid'", str(obj))
        self.assertNotIn("'author_id'", str(obj))
        self.assertIn("'author__id'", str(obj))
    def test_parquet_converter(self):
        col_xsd_filename = self.casepath('examples/collection/collection.xsd')
        col_xml_filename = self.casepath('examples/collection/collection.xml')

        col_schema = XMLSchema(col_xsd_filename, converter=ColumnarConverter)

        obj = col_schema.decode(col_xml_filename)
        self.assertIn("'authorid'", str(obj))
        self.assertNotIn("'author_id'", str(obj))
        self.assertNotIn("'author__id'", str(obj))

        obj = col_schema.decode(col_xml_filename, attr_prefix='_')
        self.assertNotIn("'authorid'", str(obj))
        self.assertIn("'author_id'", str(obj))
        self.assertNotIn("'author__id'", str(obj))

        obj = col_schema.decode(col_xml_filename, attr_prefix='__')
        self.assertNotIn("'authorid'", str(obj))
        self.assertNotIn("'author_id'", str(obj))
        self.assertIn("'author__id'", str(obj))

        col_schema = XMLSchema(col_xsd_filename)

        obj = col_schema.decode(col_xml_filename,
                                converter=ColumnarConverter,
                                attr_prefix='__')
        self.assertNotIn("'authorid'", str(obj))
        self.assertNotIn("'author_id'", str(obj))
        self.assertIn("'author__id'", str(obj))
示例#17
0
def buildViewpoint(viewpointFilePath: str, viewpointSchemaPath: str):

    logger.debug("Building new Viewpoint object")
    vpSchema = XMLSchema(viewpointSchemaPath)
    vpSchema = modifyVisinfoSchema(vpSchema)
    (vpDict, errors) = vpSchema.to_dict(viewpointFilePath, validation="lax")
    errorList = [str(err) for err in errors]
    if len(errorList) > 0:
        logger.error(errorList)

    id = UUID(vpDict["@Guid"])
    componentsDict = getOptionalFromDict(vpDict, "Components", None)
    components = None
    if componentsDict:
        components = buildComponents(componentsDict)

    oCamDict = getOptionalFromDict(vpDict, "OrthogonalCamera", None)
    oCam = None
    if oCamDict:
        oCam = buildOrthogonalCamera(oCamDict)

    pCamDict = getOptionalFromDict(vpDict, "PerspectiveCamera", None)
    pCam = None
    if pCamDict:
        pCam = buildPerspectiveCamera(pCamDict)

    linesDict = getOptionalFromDict(vpDict, "Lines", None)
    lines = list()
    if linesDict:
        linesList = getOptionalFromDict(linesDict, "Line", list())
        lines = [buildLine(line) for line in linesList]

    clippingPlaneDict = getOptionalFromDict(vpDict, "ClippingPlanes", None)
    clippingPlanes = list()
    if clippingPlaneDict:
        clippingPlaneList = getOptionalFromDict(clippingPlaneDict,
                                                "ClippingPlane", list())
        clippingPlanes = [
            buildClippingPlane(clipDict) for clipDict in clippingPlaneList
        ]

    bitmapList = getOptionalFromDict(vpDict, "Bitmap", list())
    bitmaps = [buildBitmap(bmDict) for bmDict in bitmapList]

    viewpoint = Viewpoint(id, components, oCam, pCam, lines, clippingPlanes,
                          bitmaps)

    logger.debug("New Viewpoint object created")
    return viewpoint
示例#18
0
    def test_document_init(self):
        with self.assertRaises(XmlDocumentError) as context:
            XmlDocument()
        self.assertEqual(str(context.exception),
                         "missing schema for XML data!")

        schema = os.path.join(self.schemas_dir, 'qes.xsd')

        with self.assertRaises(XmlDocumentError) as context:
            XmlDocument(schema)
        self.assertEqual(str(context.exception), "source is an XSD schema")

        document = XmlDocument(schema=schema)
        self.assertIsInstance(document, XmlDocument)
        self.assertIsNone(document.root)
        self.assertIsNone(document.filename)
        self.assertIsNone(document.format)
        self.assertIsInstance(document.schema, XMLSchema)
        if platform.system() == 'Linux':
            self.assertTrue(document.schema.url.endswith(schema))
        else:
            self.assertTrue(
                document.schema.url.endswith('qeschema/schemas/qes.xsd'))

        document = XmlDocument(schema=XMLSchema(schema))
        self.assertIsInstance(document.schema, XMLSchema)
        if platform.system() == 'Linux':
            self.assertTrue(document.schema.url.endswith(schema))
        else:
            self.assertTrue(
                document.schema.url.endswith('qeschema/schemas/qes.xsd'))
示例#19
0
    def test_document_init(self):
        schema = os.path.join(self.schemas_dir, 'qes.xsd')

        document = XmlDocument(schema)
        self.assertIsInstance(document, XmlDocument)
        self.assertIsNone(document.root)
        self.assertIsNone(document.filename)
        self.assertIsNone(document.format)
        self.assertTrue(document.schema.url.endswith(schema))
        self.assertIsInstance(document.schema, XMLSchema)

        document = XmlDocument(schema=XMLSchema(schema))
        self.assertTrue(document.schema.url.endswith(schema))
        self.assertIsInstance(document.schema, XMLSchema)

        self.assertIsInstance(PwDocument(), PwDocument)
        self.assertIsInstance(PwDocument(schema=schema), PwDocument)

        schema = os.path.join(self.schemas_dir, 'ph_temp.xsd')
        self.assertIsInstance(PhononDocument(), PhononDocument)
        self.assertIsInstance(PhononDocument(schema=schema), PhononDocument)

        schema = os.path.join(self.schemas_dir, 'qes_neb.xsd')
        self.assertIsInstance(NebDocument(), NebDocument)
        self.assertIsInstance(NebDocument(schema=schema), NebDocument)

        schema = os.path.join(self.schemas_dir, 'tddfpt.xsd')
        self.assertIsInstance(TdDocument(), TdDocument)
        self.assertIsInstance(TdDocument(schema=schema), TdDocument)

        schema = os.path.join(self.schemas_dir, 'qes_spectrum.xsd')
        self.assertIsInstance(TdSpectrumDocument(), TdSpectrumDocument)
        self.assertIsInstance(TdSpectrumDocument(schema=schema),
                              TdSpectrumDocument)
示例#20
0
class EntriesParser:
    def __init__(self):
        self.db = Database()
        self.schema = XMLSchema(f"res{os.sep}schemas{os.sep}plugin_schema.xsd")

    def validatePluginSchema(self, instancePath):
        xml = open(instancePath, "r").read().strip()
        return self.schema.is_valid(xml)

    def getPluginFromXml(self, instancePath):
        xml = open(instancePath, "r").read().strip()
        tree = ET.fromstring(xml)
        plugin = PluginItem()
        plugin.name = tree[0].text
        plugin.description = tree[1].text
        plugin.types = ["All"] + [t.text for t in tree[2]]
        plugin.outputs = [o.text for o in tree[3]]
        for i in range(4, len(tree)):
            poi = {}
            poi['name'] = tree[i][0].text
            poi['type'] = tree[i][1].text
            poi['map'] = tree[i][2].text
            plugin.pois[tree[i][0].text] = poi
        self.db.updateEntry("plugin", plugin.asDictionary())

    def updateEntry(self, which, item):
        if which == "project":
            self.db.updateEntry("project", item.asDictionary())
        if which == "plugin":
            self.db.updateEntry("plugin", item.asDictionary())

    def getEntries(self, which):
        if which == "plugin":
            entries = {}
            for entry in self.db.getEntries('plugin'):
                item = PluginItem()
                item.id = entry['_id']
                item.name = entry['name']
                item.description = entry['description']
                item.outputs = entry['fields']
                item.pois = entry['pois']
                item.types = entry['types']
                entries[item.name] = item
            return entries
        if which == "project":
            entries = {}
            for entry in self.db.getEntries('project'):
                item = ProjectItem()
                item.id = entry['_id']
                item.name = entry['name']
                item.description = entry['description']
                item.binaryPath = entry['path']
                item.binaryProperties = entry['properties']
                item.results = entry['results']
                entries[item.name] = item
            return entries

    def deleteEntry(self, which, item):
        self.db.deleteEntry(which, item.id)
示例#21
0
class TestResources(XMLSchemaTestCase):
    xs1 = XMLSchema("examples/vehicles/vehicles.xsd")
    xs2 = XMLSchema("examples/collection/collection.xsd")
    cars = xs1.elements['vehicles'].type.content_type[0]
    bikes = xs1.elements['vehicles'].type.content_type[1]

    def test_absolute_path(self):
        url1 = "https://example.com/xsd/other_schema.xsd"
        self.assertTrue(
            normalize_url(url1, base_url="/path_my_schema/schema.xsd") == url1)

    def test_fetch_resource(self):
        self.assertRaises(XMLSchemaURLError, fetch_resource,
                          'examples/resources/issue017.txt')
        self.assertTrue(
            fetch_resource('examples/resources/issue 017.txt').endswith(
                'e%20017.txt'))
示例#22
0
    def parse(self, filename, xsd=None):
        filename = str(filename)
        if not xsd:
            if not Path(filename).exists():
                raise modelParseError('File not found: ' + filename)
            replace = filename.replace('.xml', '.xsd')
            if Path(replace).exists():
                xsd = replace
            else:
                raise modelParseError('File not found: ' + replace)

        try:
            schema = XMLSchema(xsd)
            if not schema.is_valid(filename):
                raise modelParseError('Invalid XML file content:\n' +
                                      schema.validate(filename))
            return super(Parser, self).parse(filename)
        except xmlParseError as error:
            raise modelParseError(error)
示例#23
0
    def test_cdata_mapping(self):
        schema = XMLSchema("""
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root">
                <xs:complexType mixed="true">
                    <xs:sequence>
                        <xs:element name="node" type="xs:string" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema> 
        """)

        self.assertEqual(
            schema.decode('<root>1<node/>2<node/>3</root>'), {'node': [None, None]}
        )
        self.assertEqual(
            schema.decode('<root>1<node/>2<node/>3</root>', cdata_prefix='#'),
            {'#1': '1', 'node': [None, None], '#2': '2', '#3': '3'}
        )
示例#24
0
    def __init__(self, filename: str, umlFrame: UmlClassDiagramsFrame):

        self.logger: Logger = getLogger(__name__)

        self._umlFrame: UmlClassDiagramsFrame = umlFrame

        self.schema: XMLSchema = XMLSchema(filename)

        self.position: Tuple[int, int] = self._positionGenerator()
        self._schemaTypeNames: List[str] = []
        self.classTree: Dict[str, ElementTreeData] = {}
示例#25
0
 def setUpClass(cls):
     cls.col_xsd_filename = cls.casepath(
         'examples/collection/collection.xsd')
     cls.col_xml_filename = cls.casepath(
         'examples/collection/collection.xml')
     cls.col_xml_root = ElementTree.parse(cls.col_xml_filename).getroot()
     cls.col_lxml_root = lxml_etree.parse(cls.col_xml_filename).getroot()
     cls.col_nsmap = fetch_namespaces(cls.col_xml_filename)
     cls.col_namespace = cls.col_nsmap['col']
     cls.col_schema = XMLSchema(cls.col_xsd_filename,
                                converter=cls.converter)
示例#26
0
    def test_exception_init(self):
        xs = XMLSchema(os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd'))

        with self.assertRaises(ValueError) as ctx:
            XMLSchemaValidatorError(xs, 'unknown error', elem='wrong')
        self.assertIn("'elem' attribute requires an Element", str(ctx.exception))

        error = XMLSchemaNotBuiltError(xs, 'schema not built!')
        self.assertEqual(error.message, 'schema not built!')

        schema = XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:group name="group1">
                <xs:choice>
                  <xs:element name="root" type="xs:integer"/>
                </xs:choice>
              </xs:group>
            </xs:schema>""")

        error = XMLSchemaModelDepthError(schema.groups['group1'])
        self.assertEqual("maximum model recursion depth exceeded", error.message[:38])
示例#27
0
def validateFile(validateFilePath: str, schemaPath: str, bcfFile: str):
    """ Validates `validateFileName` against the XSD file referenced by
    `schemaPath`.

    If successful an empty string is returned, else an error string is
    returned.
    """

    logger.debug("Validating file {} against {}".format(
        validateFilePath, schemaPath))
    schema = XMLSchema(schemaPath)
    try:
        schema.validate(validateFilePath)
    except Exception as e:
        # get parent directory of file, useful for the user if the file is a
        # markup.bcf file inside some topic
        parentDir = os.path.abspath(os.path.join(validateFilePath, os.pardir))
        return "{} file inside {} of {} could not be validated against"\
                " {}\nError:{}".format(validateFilePath, parentDir, bcfFile,
                    os.path.basename(schemaPath), str(e))

    return ""
示例#28
0
def create_app() -> 'Flask':
    """
    Create application

    :return: flask application object
    """
    app = Flask(__name__)
    app.config.from_object(environ.get('VOICE_CONFIG', 'config.ProductionConfig'))
    db.init_app(app)
    tasks.init_app(app)
    app.register_blueprint(api.bp)
    setattr(api.bp, 'entity_schema', XMLSchema(app.config['ENTITY_XSD']))
    return app
def validate_file(path: Path, schema: xmlschema.XMLSchema,
                  validators: List[ScenarioValidator]):
    scenarios = []
    paths = []
    if path.suffix == ".xosc":
        paths.append(path)
        scenarios.append(schema.to_dict(str(path)))
    else:
        output = Path("/tmp").joinpath("xosc")
        for converted_path in convert(path, output, verbose=False):
            paths.append(converted_path)
            scenarios.append(schema.to_dict(str(converted_path)))

    for scenario, path in zip(scenarios, paths):
        overall_result = True
        try:
            for validator in validators:
                if not validator(scenario):
                    overall_result = False
        except:
            overall_result = False
        print(str(path) + " : " + str(overall_result))
示例#30
0
def buildMarkup(markupFilePath: str, markupSchemaPath: str):

    logger.debug("Building new Markup object")
    markupSchema = XMLSchema(markupSchemaPath)
    (markupDict, errors) = markupSchema.to_dict(markupFilePath,
                                                validation="lax")
    errorList = [str(err) for err in errors]
    if len(errorList) > 0:
        logger.error(errorList)

    commentList = getOptionalFromDict(markupDict, "Comment", list())
    comments = [buildComment(comment) for comment in commentList]

    topicDict = markupDict["Topic"]
    topic = buildTopic(topicDict)

    headerDict = getOptionalFromDict(markupDict, "Header", None)
    header = None
    # there may be instances that define an empty header element
    if headerDict and len(headerDict) > 0:
        header = buildHeader(headerDict)

    viewpointList = getOptionalFromDict(markupDict, "Viewpoints", list())
    viewpoints = [buildViewpointReference(vpDict) for vpDict in viewpointList]

    markupDir = os.path.abspath(os.path.dirname(markupFilePath))
    snapshotList = buildSnapshotList(markupDir)
    markup = Markup(topic, header, comments, viewpoints, snapshotList)

    # Add the right viewpoint references to each comment
    for comment in comments:
        if comment.viewpoint:
            cViewpointRefGuid = comment.viewpoint.xmlId
            viewpointRef = markup.getViewpointRefByGuid(cViewpointRefGuid)
            comment.viewpoint = viewpointRef

    logger.debug("New Markup object created")
    return markup