Exemple #1
0
 def process_included(
     self,
     included: Union[Import, Include, Redefine, Override],
     package: str,
     target_namespace: Optional[str],
 ) -> List[Class]:
     """Prepare the given included schema location and send it for
     processing."""
     classes = []
     if not included.location:
         logger.warning(
             "%s: %s unresolved schema location..",
             included.class_name,
             included.schema_location,
         )
     elif included.location in self.processed:
         logger.debug(
             "%s: %s already included skipping..",
             included.class_name,
             included.schema_location,
         )
     else:
         package = self.adjust_package(package, included.schema_location)
         classes = self.process_schema(included.location, package,
                                       target_namespace)
     return classes
Exemple #2
0
    def load_resource(self, uri: str) -> Optional[bytes]:
        """Read and return the contents of the given uri."""
        if uri not in self.processed:
            try:
                self.processed.append(uri)
                return self.preloaded.pop(uri, None) or urlopen(uri).read()  # nosec
            except OSError:
                logger.warning("Resource not found %s", uri)
        else:
            logger.debug("Skipping already processed: %s", os.path.basename(uri))

        return None
Exemple #3
0
    def process_type(self, target: Class, attr: Attr, attr_type: AttrType):
        """
        Process attribute type, split process for xml schema and user defined
        types.

        Ignore forward references to inner classes.
        """
        if attr_type.native:
            self.process_native_type(attr, attr_type)
        elif attr_type.forward:
            logger.debug("Skipping attribute type that points to inner class.")
        else:
            self.process_dependency_type(target, attr, attr_type)
Exemple #4
0
    def process_complex_extension(cls, source: Class, target: Class, ext: Extension):
        """
        Complex flatten extension handler for primary classes eg ComplexType,
        Element.

        Compare source and target classes and either remove the
        extension completely, copy all source attributes to the target
        class or leave the extension alone.
        """
        if cls.should_remove_extension(source, target):
            target.extensions.remove(ext)
        elif cls.should_flatten_extension(source, target):
            ClassUtils.copy_attributes(source, target, ext)
        else:
            ext.type.reference = id(source)
            logger.debug("Ignore extension: %s", ext.type.name)
Exemple #5
0
    def process_complex_extension(cls, source: Class, target: Class,
                                  ext: Extension):
        """
        Complex flatten extension handler for primary classes eg ComplexType,
        Element.

        Compare source and target classes and either remove the
        extension completely, copy all source attributes to the target
        class or leave the extension alone.
        """
        res = cls.compare_attributes(source, target)
        if res == cls.REMOVE_EXTENSION:
            target.extensions.remove(ext)
        elif res == cls.FLATTEN_EXTENSION:
            ClassUtils.copy_attributes(source, target, ext)
        else:
            logger.debug("Ignore extension: %s", ext.type.name)
Exemple #6
0
    def process_schema(self, uri: str, namespace: Optional[str] = None):
        """Recursively parse the given schema uri and all the imports and
        generate a class map indexed with the schema uri."""
        if uri in self.processed:
            logger.debug("Already processed skipping: %s", uri)
            return

        logger.info("Parsing schema %s", os.path.basename(uri))
        self.processed.append(uri)
        schema = self.parse_schema(uri, namespace)
        if schema is None:
            return

        for sub in schema.included():
            if sub.location:
                self.process_schema(sub.location, schema.target_namespace)

        self.class_map[uri] = self.generate_classes(schema)
Exemple #7
0
    def process_schema(self, uri: str, namespace: Optional[str] = None):
        """
        Parse and convert schema to codegen models.

        Avoid processing the same uri twice and fail silently if
        anything goes wrong with fetching and parsing the schema
        document.
        """
        if uri in self.processed:
            logger.debug("Skipping already processed: %s",
                         os.path.basename(uri))
        else:
            logger.info("Parsing schema %s", os.path.basename(uri))
            self.processed.append(uri)

            schema = self.parse_schema(uri, namespace)
            if schema:
                self.convert_schema(schema)
Exemple #8
0
    def process_schema(
        self,
        schema_path: Path,
        package: str,
        target_namespace: Optional[str] = None,
    ) -> List[Class]:
        """Recursively parse the given schema and all it's included schemas and
        generate a list of classes."""
        classes = []
        if schema_path not in self.processed:
            self.processed.append(schema_path)
            logger.info("Parsing schema...")
            schema = self.parse_schema(schema_path, target_namespace)
            target_namespace = schema.target_namespace
            for sub in schema.included():
                included_classes = self.process_included(
                    sub, package, target_namespace)
                classes.extend(included_classes)

            classes.extend(self.generate_classes(schema, package))
        else:
            logger.debug("Already processed skipping: %s", schema_path.name)
        return classes