Esempio n. 1
0
    def __init__(self, spec, resource):
        assert 'content' in resource
        self.spec = spec
        self.definition = resource
        self.url = resource.get('url')
        if self.url in self.spec.settings.enum_namemap:
            self.name = self.spec.settings.enum_namemap[self.url]
        else:
            self.name = self.spec.safe_enum_name(resource.get('name'),
                                                 ucfirst=True)
        self.codes = None
        self.generate_enum = False
        concepts = self.definition.get('concept', [])

        if resource.get('experimental'):
            return
        self.generate_enum = 'complete' == resource['content']
        if not self.generate_enum:
            logger.debug(
                "Will not generate enum for CodeSystem \"{}\" whose content is {}"
                .format(self.url, resource['content']))
            return

        assert concepts, "Expecting at least one code for \"complete\" CodeSystem"
        if len(concepts) > 200:
            self.generate_enum = False
            logger.info(
                "Will not generate enum for CodeSystem \"{}\" because it has > 200 ({}) concepts"
                .format(self.url, len(concepts)))
            return

        self.codes = self.parsed_codes(concepts)
Esempio n. 2
0
    def read_profiles(self):
        """ Find all (JSON) profiles and instantiate into FHIRStructureDefinition.
        """
        resources = []
        for filename in ['profiles-types.json', 'profiles-resources.json'
                         ]:  #, 'profiles-others.json']:
            bundle_res = self.read_bundle_resources(filename)
            for resource in bundle_res:
                if 'StructureDefinition' == resource['resourceType']:
                    resources.append(resource)
                else:
                    logger.debug('Not handling resource of type {}'.format(
                        resource['resourceType']))

        # create profile instances
        for resource in resources:
            profile = FHIRStructureDefinition(self, resource,
                                              self.settings.sort_resources)
            for pattern in skip_because_unsupported:
                if re.search(pattern, profile.url) is not None:
                    logger.info('Skipping "{}"'.format(resource['url']))
                    profile = None
                    break

            if profile is not None and self.found_profile(profile):
                profile.process_profile()
Esempio n. 3
0
    def found_profile(self, profile):
        if not profile or not profile.name:
            raise Exception("No name for profile {}".format(profile))
        if profile.name.lower() in self.profiles:
            logger.debug('Already have profile "{}", discarding'.format(
                profile.name))
            return False

        self.profiles[profile.name.lower()] = profile
        return True
Esempio n. 4
0
    def create_class(self, module=None, sort_resources: bool = True):
        """ Creates a FHIRClass instance from the receiver, returning the
        created class as the first and all inline defined subclasses as the
        second item in the tuple.
        """
        assert self._did_resolve_dependencies
        if not self.represents_class:
            return None, None

        class_name = self.name_if_class()
        subs = []
        cls, did_create = fhirclass.FHIRClass.for_element(self)
        if did_create:  # manual_profiles
            if module is None:
                if self.profile.manual_module is not None:
                    module = self.profile.manual_module
                elif self.is_main_profile_element:
                    module = self.profile.spec.as_module_name(cls.name)
            cls.module = module
            logger.debug('Created class "{}", module {}'.format(
                cls.name, module))

        # child classes
        if self.children is not None:
            for child in self.children:
                properties = child.as_properties()
                if properties is not None:

                    # collect subclasses
                    sub, subsubs = child.create_class(
                        module, sort_resources=sort_resources)
                    if subsubs:
                        subs.extend(subsubs)
                    if sub is not None:
                        subs.insert(0, sub)

                    # add properties to class
                    if did_create:
                        for prop in properties:
                            cls.add_property(prop, sort_resources)

        return cls, subs
Esempio n. 5
0
    def resolve_dependencies(self):
        # update the definition from a reference, if there is one
        if self.content_reference is not None:
            if '#' != self.content_reference[:1]:
                raise Exception(
                    "Only relative 'contentReference' element definitions are supported right now"
                )
            elem = self.element.profile.element_with_id(
                self.content_reference[1:])
            if elem is None:
                raise Exception(
                    "There is no element definiton with id \"{}\", as referenced by {} in {}"
                    .format(self.content_reference, self.path,
                            self.profile.url))
            self._content_referenced = elem.definition

        # resolve bindings
        if self.binding is not None and self.binding.is_required and (
                self.binding.valueSet is not None or self.binding.legacy_uri
                is not None or self.binding.legacy_canonical is not None):
            uri = self.binding.valueSet or self.binding.legacy_canonical or self.binding.legacy_uri
            if 'http://hl7.org/fhir' != uri[:19]:
                logger.debug("Ignoring foreign ValueSet \"{}\"".format(uri))
                return

# remove version from canonical URI, if present, e.g. "http://hl7.org/fhir/ValueSet/name-use|4.0.0"
            if '|' in uri:
                uri = uri.split('|')[0]

            valueset = self.element.profile.spec.valueset_with_uri(uri)
            if valueset is None:
                logger.error(
                    "There is no ValueSet for required binding \"{}\" on {} in {}"
                    .format(uri, self.name or self.prop_name,
                            self.element.profile.name))
            else:
                self.element.valueset = valueset
                self.element.enum = valueset.enum