Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def find_child_reference(self, name):
name = name.upper()
if isinstance(self.structure_by_name, MutableMapping):
element = self.structure_by_name.get(name) or self.structure_by_longname.get(name)
else:
element = None
if element is None: # not found in self.structure
if _valid_z_segment_name(name):
element = {'cls': Segment, 'name': name, 'ref': ('sequence', ())}
else:
element = find_reference(name, self.child_classes.values(), self.version)
if Validator.is_strict(self.validation_level): # cannot be created if validation is strict
raise ChildNotValid(name, self)
return element
def __init__(self, value, max_length=None, validation_level=None):
if validation_level is None:
validation_level = get_default_validation_level()
self.validation_level = validation_level
self.max_length = max_length
if Validator.is_strict(self.validation_level):
if self.max_length is not None and len('{0}'.format(value)) > self.max_length:
raise MaxLengthReached(value, self.max_length)
self.value = value
def find_child_reference(self, name):
name = name.upper()
if isinstance(self.structure_by_name, MutableMapping):
element = self.structure_by_name.get(name) or self.structure_by_longname.get(name)
else:
element = None
if element is None: # not found in self.structure
element = find_reference(name, self.child_classes.values(), self.version)
if Validator.is_strict(self.validation_level): # cannot be created if validation is strict
raise ChildNotValid(name, self)
return element
factories['DTM'] = datetime_factory
if 'NM' in factories:
factories['NM'] = numeric_factory
if 'SI' in factories:
factories['SI'] = sequence_id_factory
try:
factory = factories[datatype]
if isinstance(factory, FunctionType):
return factory(value, base_datatypes[datatype], validation_level=validation_level)
return factory(value, validation_level=validation_level)
except KeyError:
raise InvalidDataType(datatype)
except ValueError as e:
print(e)
if Validator.is_strict(validation_level):
raise e
# TODO: Do we really want this? In that case the parent's datatype must be changed accordingly
return factories['ST'](value)
def _can_add_child(self, child):
if self.element._is_valid_child(child):
if child.parent != self.element and child.traversal_parent != self.element: # avoid infinite recursion
child.parent = self.element
else:
# if validation is strict, check the child cardinality
if Validator.is_strict(self.element.validation_level):
min_rep, max_rep = self.element.repetitions.get(child.name, (0, -1))
if len(self.indexes.get(child.name, [])) + 1 > int(max_rep) and max_rep > -1:
raise MaxChildLimitReached(self.element, child, max_rep)
if self.element.validation_level != child.validation_level:
raise OperationNotAllowed('Cannot add a child with a different validation_level')
if self.element.version != child.version:
raise OperationNotAllowed('Cannot add a child with a different HL7 version')
return True
else:
raise ChildNotValid(child, self.element)
return False
def _set_datatype(self, datatype):
if Validator.is_strict(self.validation_level) and self.datatype and \
datatype != self.datatype:
raise OperationNotAllowed("Cannot change datatype using STRICT validation")
# This will change the structure of the Field/Component so it is done only if the structure
# is really changed. That's because the first time the datatype is set by the Element._find_structure method
if not is_base_datatype(datatype, self.version) and \
datatype not in ('varies', None, self.datatype) and self.datatype is not None:
reference = load_reference(datatype, 'Datatypes_Structs', self.version)
new_ref = [ref_item for ref_item in self.reference]
new_ref[1] = reference
new_ref[2] = datatype
structure = ElementFinder.get_structure(self, new_ref)
for k, v in iteritems(structure):
if k != 'datatype': # avoid maximum recursion
setattr(self, k, v)
def _get_children(self, trailing=False):
if Validator.is_strict(self.validation_level):
children = self.children.get_ordered_children()
else:
children = self.children.get_children()
if not trailing:
children = _remove_trailing(children)
return children
def __init__(self, name=None, parent=None, reference=None, version=None,
validation_level=None, traversal_parent=None):
# self.child_classes = (Segment, Group)
self.child_classes = {"SEG": Segment, "GRP": Group}
super(Group, self).__init__(name, parent, reference, version, validation_level, traversal_parent)
if self.name is None and Validator.is_strict(self.validation_level):
raise OperationNotAllowed("Cannot instantiate an unknown Element with strict validation")
>>> print(cx_4)
>>> print(cx_4.to_er7())
GATEWAY&1.3.6.1.4.1.21367.2011.2.5.17
>>> print(parse_component(component))
"""
version = _get_version(version)
encoding_chars = _get_encoding_chars(encoding_chars, version)
validation_level = _get_validation_level(validation_level)
try:
component = Component(name, datatype, version=version, validation_level=validation_level,
reference=reference)
except InvalidName as e:
if Validator.is_strict(validation_level):
raise e
component = Component(datatype, version=version, validation_level=validation_level,
reference=reference)
children = parse_subcomponents(text, component.datatype, version, encoding_chars, validation_level)
if Validator.is_tolerant(component.validation_level) and is_base_datatype(component.datatype, version) and \
len(children) > 1:
component.datatype = None
component.children = children
return component