Ejemplo n.º 1
0
def copy_fields_to_builder(
    sentence_group_builder: entities.StateSentenceGroup.Builder,
    proto: StateSentenceGroup,
    metadata: IngestMetadata,
) -> None:
    """Mutates the provided |sentence_group_builder| by converting an
    ingest_info proto StateSentenceGroup.

    Note: This will not copy children into the Builder!
    """
    new = sentence_group_builder

    # Enum mappings
    new.status = EnumParser(getattr(proto, "status"), StateSentenceStatus,
                            metadata.enum_overrides)
    new.status_raw_text = getattr(proto, "status")

    # 1-to-1 mappings
    state_sentence_group_id = getattr(proto, "state_sentence_group_id")
    new.external_id = (None
                       if common_utils.is_generated_id(state_sentence_group_id)
                       else state_sentence_group_id)
    new.date_imposed = getattr(proto, "date_imposed")
    new.state_code = metadata.region
    new.county_code = getattr(proto, "county_code")
    new.min_length_days = getattr(proto, "min_length")
    new.max_length_days = getattr(proto, "max_length")
    new.is_life = getattr(proto, "is_life")
Ejemplo n.º 2
0
def _proto_to_py(proto, py_type, id_name):
    py_obj = py_type()
    _copy_fields(proto, py_obj, proto.DESCRIPTOR)

    obj_id = getattr(proto, id_name)
    if not common_utils.is_generated_id(obj_id):
        setattr(py_obj, id_name, obj_id)

    return obj_id, py_obj
Ejemplo n.º 3
0
 def test_is_not_generated_id(self) -> None:
     id_str = "id_str"
     self.assertFalse(is_generated_id(id_str))
Ejemplo n.º 4
0
 def test_is_generated_id(self) -> None:
     id_str = "id_str_GENERATE"
     self.assertTrue(is_generated_id(id_str))
Ejemplo n.º 5
0
def parse_external_id(id_str):
    """If the supplied |id_str| is generated, returns None. Otherwise
    returns the normalized version of the provided |id_str|"""
    if common_utils.is_generated_id(id_str):
        return None
    return normalize(id_str)