Exemple #1
0
def copy_fields_to_builder(state_person_builder: entities.StatePerson.Builder,
                           proto: StatePerson,
                           metadata: IngestMetadata):
    """Mutates the provided |state_person_builder| by converting an
    ingest_info proto StatePerson.

    Note: This will not copy children into the Builder!
    """
    enum_fields = {
        'gender': Gender,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    new = state_person_builder

    # Enum mappings
    new.gender = enum_mappings.get(Gender)
    new.gender_raw_text = fn(normalize, 'gender', proto)

    # 1-to-1 mappings
    new.full_name = parse_name(proto)
    new.birthdate, new.birthdate_inferred_from_age = parse_birthdate(
        proto, 'birthdate', 'age')
    new.current_address = fn(normalize, 'current_address', proto)
    new.residency_status = fn(
        parse_residency_status, 'current_address', proto)
    new.state_code = parse_region_code_with_override(
        proto, 'state_code', metadata)
    def _convert_booking(self, ingest_booking):
        """Converts an ingest_info proto Booking to a persistence entity."""
        booking_builder = entities.Booking.builder()

        booking.copy_fields_to_builder(booking_builder, ingest_booking,
                                       self.metadata)

        booking_builder.arrest = \
            fn(lambda i: arrest.convert(self.arrests[i]),
               'arrest_id', ingest_booking)

        converted_holds = [
            hold.convert(self.holds[hold_id], self.metadata)
            for hold_id in ingest_booking.hold_ids
        ]
        booking_builder.holds = list(
            more_itertools.unique_everseen(converted_holds))

        ingest_charges = [self.charges[c] for c in ingest_booking.charge_ids]
        charges = self._convert_charges(ingest_charges)
        booking_builder.charges = charges

        bond_info_tuple = fn(parse_bond_amount_type_and_status,
                             'total_bond_amount', ingest_booking)
        if bond_info_tuple is not None:
            bond_amount, bond_type, bond_status = bond_info_tuple
            booking_builder.charges = \
                _charges_pointing_to_total_bond(
                    bond_amount, bond_type, bond_status, charges)

        return booking_builder.build()
Exemple #3
0
def convert(
    proto: StateSupervisionViolationResponseDecisionEntry,
    metadata: IngestMetadata
) -> entities.StateSupervisionViolationResponseDecisionEntry:
    """Converts an ingest_info proto
    StateSupervisionViolationResponseDecisionEntry to a persistence entity.
    """
    new = entities.StateSupervisionViolationResponseDecisionEntry.builder()

    enum_fields = {
        "decision": StateSupervisionViolationResponseDecision,
        "revocation_type": StateSupervisionViolationResponseRevocationType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # Enum mappings
    new.decision = enum_mappings.get(StateSupervisionViolationResponseDecision)
    new.decision_raw_text = fn(normalize, "decision", proto)

    new.revocation_type = enum_mappings.get(
        StateSupervisionViolationResponseRevocationType)
    new.revocation_type_raw_text = fn(normalize, "revocation_type", proto)

    # 1-to-1 mappings
    new.state_code = parse_region_code_with_override(proto, "state_code",
                                                     metadata)

    return new.build()
Exemple #4
0
def copy_fields_to_builder(
    fine_builder: entities.StateFine.Builder, proto: StateFine, metadata: IngestMetadata
) -> None:
    """Mutates the provided |fine_builder| by converting an ingest_info proto
    StateFine.

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

    enum_fields = {
        "status": StateFineStatus,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.status = enum_mappings.get(
        StateFineStatus, default=StateFineStatus.PRESENT_WITHOUT_INFO
    )
    new.status_raw_text = fn(normalize, "status", proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, "state_fine_id", proto)
    new.date_paid = fn(parse_date, "date_paid", proto)
    new.state_code = parse_region_code_with_override(proto, "state_code", metadata)
    new.county_code = fn(normalize, "county_code", proto)
    new.fine_dollars = fn(parse_dollars, "fine_dollars", proto)
def convert(
        proto: StateIncarcerationIncidentOutcome, metadata: IngestMetadata
) -> entities.StateIncarcerationIncidentOutcome:
    """Converts an ingest_info proto StateIncarcerationIncidentOutcome to a
    persistence entity.
    """
    new = entities.StateIncarcerationIncidentOutcome.builder()

    enum_fields = {
        'outcome_type': StateIncarcerationIncidentOutcomeType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.outcome_type = enum_mappings.get(StateIncarcerationIncidentOutcomeType)
    new.outcome_type_raw_text = fn(normalize, 'outcome_type', proto)

    # 1-to-1 mappings
    new.external_id = \
        fn(parse_external_id, 'state_incarceration_incident_outcome_id', proto)
    new.date_effective = fn(parse_date, 'date_effective', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.outcome_description = fn(normalize, 'outcome_description', proto)
    new.punishment_length_days = fn(parse_int, 'punishment_length_days', proto)

    return new.build()
Exemple #6
0
def copy_fields_to_builder(
        supervision_violation_builder:
        entities.StateSupervisionViolation.Builder,
        proto: StateSupervisionViolation,
        metadata: IngestMetadata) -> None:
    """Converts an ingest_info proto StateSupervisionViolation to a
    persistence entity."""
    new = supervision_violation_builder

    enum_fields = {
        'violation_type': StateSupervisionViolationType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.violation_type = enum_mappings.get(StateSupervisionViolationType)
    new.violation_type_raw_text = fn(normalize, 'violation_type', proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id,
                         'state_supervision_violation_id',
                         proto)
    new.violation_date = fn(parse_date, 'violation_date', proto)
    new.state_code = parse_region_code_with_override(
        proto, 'state_code', metadata)
    new.is_violent = fn(parse_bool, 'is_violent', proto)
    new.violated_conditions = fn(normalize, 'violated_conditions', proto)
Exemple #7
0
def convert(proto, metadata: IngestMetadata) -> entities.Bond:
    """Converts an ingest_info proto Bond to a persistence entity."""
    new = entities.Bond.builder()

    enum_fields = {
        'status': BondStatus,
        'bond_type': BondType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.bond_type = enum_mappings.get(BondType)
    new.bond_type_raw_text = fn(normalize, 'bond_type', proto)
    new.status = enum_mappings.get(BondStatus)
    new.status_raw_text = fn(normalize, 'status', proto)

    # parsed values
    new.external_id = fn(parse_external_id, 'bond_id', proto)
    new.bond_agent = fn(normalize, 'bond_agent', proto)
    new.amount_dollars, new.bond_type, new.status = \
        converter_utils.parse_bond_amount_type_and_status(
            fn(normalize, 'amount', proto),
            provided_bond_type=cast(Optional[BondType], new.bond_type),
            provided_status=cast(Optional[BondStatus], new.status))

    return new.build()
Exemple #8
0
def parse_name(proto) -> Optional[str]:
    """Parses name into a single string."""
    names = Names(full_name=fn(normalize, 'full_name', proto),
                  given_names=fn(normalize, 'given_names', proto),
                  middle_names=fn(normalize, 'middle_names', proto),
                  surname=fn(normalize, 'surname', proto),
                  name_suffix=fn(normalize, 'name_suffix', proto))
    return names.combine()
Exemple #9
0
def parse_name(proto) -> Optional[str]:
    """Parses name into a single string."""
    names = Names(
        full_name=fn(normalize, "full_name", proto),
        given_names=fn(normalize, "given_names", proto),
        middle_names=fn(normalize, "middle_names", proto),
        surname=fn(normalize, "surname", proto),
        name_suffix=fn(normalize, "name_suffix", proto),
    )
    return names.combine()
def copy_fields_to_builder(
        supervision_period_builder: entities.StateSupervisionPeriod.Builder,
        proto: StateSupervisionPeriod, metadata: IngestMetadata) -> None:
    """Mutates the provided |supervision_period_builder| by converting an ingest_info proto StateSupervisionPeriod.

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

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, 'state_supervision_period_id',
                         proto)

    new.start_date = fn(parse_date, 'start_date', proto)
    new.termination_date = fn(parse_date, 'termination_date', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.county_code = fn(normalize, 'county_code', proto)
    new.supervision_site = fn(normalize, 'supervision_site', proto)
    if proto.conditions:
        new.conditions = create_comma_separated_list(proto, 'conditions')

    enum_fields = {
        'status': StateSupervisionPeriodStatus,
        'supervision_type': StateSupervisionType,
        'admission_reason': StateSupervisionPeriodAdmissionReason,
        'termination_reason': StateSupervisionPeriodTerminationReason,
        'supervision_level': StateSupervisionLevel
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    # Status default based on presence of admission/release dates
    if new.termination_date:
        status_default = StateSupervisionPeriodStatus.TERMINATED
    elif new.start_date and not new.termination_date:
        status_default = StateSupervisionPeriodStatus.UNDER_SUPERVISION
    else:
        status_default = StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO
    new.status = enum_mappings.get(StateSupervisionPeriodStatus,
                                   default=status_default)

    new.status_raw_text = fn(normalize, 'status', proto)
    new.supervision_type = enum_mappings.get(StateSupervisionType)
    new.supervision_type_raw_text = fn(normalize, 'supervision_type', proto)
    new.admission_reason = enum_mappings.get(
        StateSupervisionPeriodAdmissionReason)
    new.admission_reason_raw_text = fn(normalize, 'admission_reason', proto)
    new.termination_reason = enum_mappings.get(
        StateSupervisionPeriodTerminationReason)
    new.termination_reason_raw_text = fn(normalize, 'termination_reason',
                                         proto)
    new.supervision_level = enum_mappings.get(StateSupervisionLevel)
    new.supervision_level_raw_text = fn(normalize, 'supervision_level', proto)
def convert(proto: StatePersonExternalId,
            metadata: IngestMetadata) -> entities.StatePersonExternalId:
    """Converts an ingest_info proto Hold to a persistence entity."""
    new = entities.StatePersonExternalId.builder()

    new.external_id = \
        fn(_parse_state_external_id, 'state_person_external_id_id', proto)
    new.id_type = fn(normalize, 'id_type', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)

    return new.build()
Exemple #12
0
def copy_fields_to_builder(court_case_builder: entities.StateCourtCase.Builder,
                           proto: StateCourtCase,
                           metadata: IngestMetadata) -> None:
    """Mutates the provided |court_case_builder| by converting an ingest_info
    proto StateCourtCase.

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

    enum_fields = {
        'status': StateCourtCaseStatus,
        'court_type': StateCourtType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.status = enum_mappings.get(
        StateCourtCaseStatus,
        default=StateCourtCaseStatus.PRESENT_WITHOUT_INFO)
    new.status_raw_text = fn(normalize, 'status', proto)

    new.court_type = enum_mappings.get(
        StateCourtType, default=StateCourtType.PRESENT_WITHOUT_INFO)
    new.court_type_raw_text = fn(normalize, 'court_type', proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, 'state_court_case_id', proto)
    new.date_convicted = fn(parse_date, 'date_convicted', proto)
    new.next_court_date = fn(parse_date, 'next_court_date', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.county_code = fn(normalize, 'county_code', proto)
    new.judicial_district_code = fn(normalize, 'judicial_district_code', proto)
    new.court_fee_dollars = fn(parse_dollars, 'court_fee_dollars', proto)
Exemple #13
0
    def __init__(self, proto, enum_fields: Mapping[str, EntityEnumMeta], overrides: EnumOverrides):
        """
        Initializes a mapping from enum fields within a single Entity to enum
        values. If enum fields map to enum values in a different entity, those
        values will be ignored. EnumMappings prefers values that are the same
        type as the field they are from; for example, if both ChargeClass and
        ChargeStatus fields map to a ChargeClass, we will return the ChargeClass
        from the charge_class field. However, if multiple fields map to
        different values of the another enum type (for example, if ChargeDegree
        and ChargeStatus fields map to two ChargeClasses), an error will be
        raised on calling |get|. If the given enum fields dict contains multiple
        fields that map to the same enum type, both will be retrievable by their
        field name in the `get` method below.
        Args:
            proto: the proto to read fields from, e.g. Person, Booking, etc.
            enum_fields: a mapping from field names (e.g. custody_status) to
                         parsing functions (e.g. CustodyStatus.parse).
            overrides: the enum overrides mapping.
        """
        self.parsed_enums_from_original_field: Dict[EntityEnumMeta, Dict[str, EntityEnum]] = dict()
        self.all_parsed_enums: Set[EntityEnum] = set()

        for field_name, from_enum in enum_fields.items():
            value = fn(from_enum.parse, field_name, proto, overrides)
            if not value:
                continue
            if isinstance(value, from_enum):
                default_mapping: Dict[str, EntityEnum] = {}
                mappings_by_field = self.parsed_enums_from_original_field.get(from_enum, default_mapping)
                mappings_by_field[field_name] = value
                self.parsed_enums_from_original_field[from_enum] = mappings_by_field
            else:
                self.all_parsed_enums.add(value)
Exemple #14
0
    def _convert_incarceration_incident(
        self, ingest_incident: StateIncarcerationIncident
    ) -> entities.StateIncarcerationIncident:
        """Converts an ingest_info proto StateIncarcerationIncident to a
        persistence entity."""
        incident_builder = entities.StateIncarcerationIncident.builder()

        state_incarceration_incident.copy_fields_to_builder(
            incident_builder, ingest_incident, self.metadata)

        incident_builder.responding_officer = fn(
            lambda i: state_agent.convert(self.agents[i], self.metadata),
            "responding_officer_id",
            ingest_incident,
        )

        converted_outcomes = [
            state_incarceration_incident_outcome.convert(
                self.incarceration_incident_outcomes[outcome_id],
                self.metadata) for outcome_id in
            ingest_incident.state_incarceration_incident_outcome_ids
        ]

        incident_builder.incarceration_incident_outcomes = converted_outcomes
        return incident_builder.build()
    def _convert_charge(self, ingest_charge):
        """Converts an ingest_info proto Charge to a persistence entity."""
        charge_builder = entities.Charge.builder()

        charge.copy_fields_to_builder(charge_builder, ingest_charge,
                                      self.metadata)

        charge_builder.bond = \
            fn(lambda i: bond.convert(self.bonds[i], self.metadata),
               'bond_id',
               ingest_charge)
        charge_builder.sentence = \
            fn(lambda i: self._convert_sentence(self.sentences[i]),
               'sentence_id', ingest_charge)

        return charge_builder.build()
Exemple #16
0
def convert(proto, metadata: IngestMetadata) -> entities.Hold:
    """Converts an ingest_info proto Hold to a persistence entity."""
    new = entities.Hold.builder()

    enum_fields = {
        "status": HoldStatus,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    new.status = enum_mappings.get(HoldStatus,
                                   default=HoldStatus.PRESENT_WITHOUT_INFO)
    new.status_raw_text = fn(normalize, "status", proto)

    new.external_id = fn(parse_external_id, "hold_id", proto)
    new.jurisdiction_name = fn(normalize, "jurisdiction_name", proto)

    return new.build()
Exemple #17
0
def copy_fields_to_builder(
        early_discharge_builder: entities.StateEarlyDischarge.Builder,
        proto: StateEarlyDischarge, metadata: IngestMetadata) -> None:
    """Converts an ingest_info proto StateEarlyDischarge to a early discharge entity."""
    new = early_discharge_builder

    enum_fields = {
        'decision': StateEarlyDischargeDecision,
        'deciding_body_type': StateActingBodyType,
        'requesting_body_type': StateActingBodyType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.decision = enum_mappings.get(StateEarlyDischargeDecision)
    new.decision_raw_text = fn(normalize, 'decision', proto)
    new.deciding_body_type = enum_mappings.get(StateActingBodyType,
                                               field_name='deciding_body_type')
    new.deciding_body_type_raw_text = fn(normalize, 'deciding_body_type',
                                         proto)
    new.requesting_body_type = enum_mappings.get(
        StateActingBodyType, field_name='requesting_body_type')
    new.requesting_body_type_raw_text = fn(normalize, 'requesting_body_type',
                                           proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, 'state_early_discharge_id', proto)
    new.request_date = fn(parse_date, 'request_date', proto)
    new.decision_date = fn(parse_date, 'decision_date', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.county_code = fn(normalize, 'county_code', proto)
def copy_fields_to_builder(
        state_parole_decision_builder: entities.StateParoleDecision.Builder,
        proto: StateParoleDecision, metadata: IngestMetadata) -> None:
    """Mutates the provided |state_parole_decision_builder| by converting an
    ingest_info proto StateParoleDecision.

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

    enum_fields = {
        'decision_outcome': StateParoleDecisionOutcome,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # Enum mappings
    new.decision_outcome = enum_mappings.get(StateParoleDecisionOutcome)
    new.decision_outcome_raw_text = fn(normalize, 'decision_outcome', proto)

    new.external_id = fn(parse_external_id, 'state_parole_decision_id', proto)
    new.decision_date = fn(parse_date, 'decision_date', proto)
    new.corrective_action_deadline = fn(parse_date,
                                        'corrective_action_deadline', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.county_code = fn(normalize, 'county_code', proto)
    new.decision_reasoning = fn(normalize, 'decision_reasoning', proto)
    new.corrective_action = fn(normalize, 'corrective_action', proto)
Exemple #19
0
def copy_fields_to_builder(
        state_assessment_builder: entities.StateAssessment.Builder,
        proto: StateAssessment, metadata: IngestMetadata) -> None:
    """Mutates the provided |state_assessment_builder| by converting an
    ingest_info proto StateAssessment.

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

    new = state_assessment_builder

    enum_fields = {
        'assessment_class': StateAssessmentClass,
        'assessment_type': StateAssessmentType,
        'assessment_level': StateAssessmentLevel,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # Enum mappings
    new.assessment_class = enum_mappings.get(StateAssessmentClass)
    new.assessment_class_raw_text = fn(normalize, 'assessment_class', proto)
    new.assessment_type = enum_mappings.get(StateAssessmentType)
    new.assessment_type_raw_text = fn(normalize, 'assessment_type', proto)
    new.assessment_level = enum_mappings.get(StateAssessmentLevel)
    new.assessment_level_raw_text = fn(normalize, 'assessment_level', proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, 'state_assessment_id', proto)
    new.assessment_date = fn(parse_date, 'assessment_date', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.assessment_score = fn(parse_int, 'assessment_score', proto)
    new.assessment_metadata = fn(normalize, 'assessment_metadata', proto)
Exemple #20
0
def convert(proto: StateBond, metadata: IngestMetadata) -> entities.StateBond:
    """Converts an ingest_info proto StateBond to a persistence entity."""
    new = entities.StateBond.builder()

    enum_fields = {
        "status": BondStatus,
        "bond_type": BondType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.status = enum_mappings.get(BondStatus)
    new.status_raw_text = fn(normalize, "status", proto)
    new.bond_type = enum_mappings.get(BondType)
    new.bond_type_raw_text = fn(normalize, "bond_type", proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, "state_bond_id", proto)
    new.date_paid = fn(parse_date, "date_paid", proto)
    new.state_code = parse_region_code_with_override(proto, "state_code",
                                                     metadata)
    new.county_code = fn(normalize, "county_code", proto)
    new.bond_agent = fn(normalize, "bond_agent", proto)

    (
        new.amount_dollars,
        new.bond_type,
        new.status,
    ) = converter_utils.parse_bond_amount_type_and_status(
        fn(normalize, "amount", proto),
        provided_bond_type=cast(Optional[BondType], new.bond_type),
        provided_status=cast(Optional[BondStatus], new.status),
    )

    return new.build()
Exemple #21
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_fields = {
        'status': StateSentenceStatus,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # Enum mappings
    new.status = enum_mappings.get(
        StateSentenceStatus,
        default=StateSentenceStatus.PRESENT_WITHOUT_INFO)
    new.status_raw_text = fn(normalize, 'status', proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, 'state_sentence_group_id', proto)
    new.date_imposed = fn(parse_date, 'date_imposed', proto)
    new.state_code = parse_region_code_with_override(
        proto, 'state_code', metadata)
    new.county_code = fn(normalize, 'county_code', proto)
    new.min_length_days = fn(parse_days, 'min_length', proto)
    new.max_length_days = fn(parse_days, 'max_length', proto)
    new.is_life = fn(parse_bool, 'is_life', proto)
Exemple #22
0
    def _convert_charge(self, ingest_charge: StateCharge) \
            -> entities.StateCharge:
        """Converts an ingest_info proto StateCharge to a persistence entity."""
        charge_builder = entities.StateCharge.builder()

        state_charge.copy_fields_to_builder(charge_builder, ingest_charge,
                                            self.metadata)

        charge_builder.bond = \
            fn(lambda i: state_bond.convert(self.bonds[i], self.metadata),
               'state_bond_id',
               ingest_charge)

        charge_builder.court_case = \
            fn(lambda i: self._convert_court_case(self.court_cases[i]),
               'state_court_case_id',
               ingest_charge)

        return charge_builder.build()
Exemple #23
0
def _parse_admission(proto, metadata):
    admission_date = fn(parse_date, 'admission_date', proto)

    if admission_date is None:
        admission_date = metadata.ingest_time.date()
        admission_date_inferred = True
    else:
        admission_date_inferred = False

    return admission_date, admission_date_inferred
Exemple #24
0
def copy_fields_to_builder(booking_builder, proto, metadata):
    """Mutates the provided |booking_builder| by converting an ingest_info proto
    Booking.

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

    enum_fields = {
        "admission_reason": AdmissionReason,
        "release_reason": ReleaseReason,
        "custody_status": CustodyStatus,
        "classification": Classification,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # Enum mappings
    new.admission_reason = enum_mappings.get(AdmissionReason)
    new.admission_reason_raw_text = fn(normalize, "admission_reason", proto)
    new.release_reason = enum_mappings.get(ReleaseReason)
    new.release_reason_raw_text = fn(normalize, "release_reason", proto)
    new.custody_status = enum_mappings.get(
        CustodyStatus, default=CustodyStatus.PRESENT_WITHOUT_INFO)
    new.custody_status_raw_text = fn(normalize, "custody_status", proto)
    new.classification = enum_mappings.get(Classification)
    new.classification_raw_text = fn(normalize, "classification", proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, "booking_id", proto)
    new.projected_release_date = fn(parse_date, "projected_release_date",
                                    proto)
    new.facility = fn(normalize, "facility", proto)
    new.facility_id = fn(validate_fid,
                         "facility_id",
                         proto,
                         default=metadata.facility_id)

    # Inferred attributes
    new.admission_date, new.admission_date_inferred = _parse_admission(
        proto, metadata)
    (
        new.release_date,
        new.projected_release_date,
        new.release_date_inferred,
    ) = _parse_release_date(proto, metadata)
    _set_custody_status_if_needed(new)

    # Metadata
    new.last_seen_time = metadata.ingest_time
    # Will be overwritten by first_seen_time value in database if one is already
    # present
    new.first_seen_time = metadata.ingest_time
Exemple #25
0
def _parse_release_date(
    proto, metadata: IngestMetadata
) -> Tuple[Optional[date], Optional[date], Optional[bool]]:
    """Reads release_date and projected_release_date from |proto|.

    If release_date is present on proto, sets release_date_inferred to (False).

    If release_date is in the future relative to scrape time, will be treated
    as projected_release_date instead.
    """
    release_date = fn(parse_date, "release_date", proto)
    projected_release_date = fn(parse_date, "projected_release_date", proto)

    if release_date and release_date > metadata.ingest_time.date():
        projected_release_date = release_date
        release_date = None

    release_date_inferred = None if release_date is None else False

    return release_date, projected_release_date, release_date_inferred
Exemple #26
0
    def _convert_supervision_period(
            self, ingest_supervision_period: StateSupervisionPeriod) \
            -> entities.StateSupervisionPeriod:
        """Converts an ingest_info proto StateSupervisionPeriod to a
        persistence entity."""
        supervision_period_builder = \
            entities.StateSupervisionPeriod.builder()

        state_supervision_period.copy_fields_to_builder(
            supervision_period_builder, ingest_supervision_period,
            self.metadata)

        supervision_period_builder.supervising_officer = \
            fn(lambda i: state_agent.convert(self.agents[i], self.metadata),
               'supervising_officer_id',
               ingest_supervision_period)

        converted_violations = [
            self._convert_supervision_violation(
                self.supervision_violations[violation_id]) for violation_id in
            ingest_supervision_period.state_supervision_violation_entry_ids
        ]
        supervision_period_builder.supervision_violation_entries = \
            converted_violations

        converted_assessments = [
            self._convert_assessment(self.assessments[assessment_id])
            for assessment_id in ingest_supervision_period.state_assessment_ids
        ]
        supervision_period_builder.assessments = converted_assessments

        converted_program_assignments = [
            self._convert_program_assignment(
                self.program_assignments[assignment_id]) for assignment_id in
            ingest_supervision_period.state_program_assignment_ids
        ]
        supervision_period_builder.program_assignments = \
            converted_program_assignments
        converted_case_types = [
            state_supervision_case_type_entry.convert(
                self.supervision_case_type_entries[case_type_id],
                self.metadata) for case_type_id in
            ingest_supervision_period.state_supervision_case_type_entry_ids
        ]
        supervision_period_builder.case_type_entries = converted_case_types

        converted_contacts = [
            self._convert_supervision_contact(
                self.supervision_contacts[contact_id]) for contact_id in
            ingest_supervision_period.state_supervision_contact_ids
        ]
        supervision_period_builder.supervision_contacts = converted_contacts

        return supervision_period_builder.build()
Exemple #27
0
def convert(proto: StateAgent,
            metadata: IngestMetadata) -> entities.StateAgent:
    """Converts an ingest_info proto StateAgent to a persistence entity."""
    new = entities.StateAgent.builder()

    enum_fields = {
        'agent_type': StateAgentType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.agent_type = enum_mappings.get(StateAgentType)
    new.agent_type_raw_text = fn(normalize, 'agent_type', proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, 'state_agent_id', proto)
    new.state_code = parse_region_code_with_override(proto, 'state_code',
                                                     metadata)
    new.full_name = parse_name(proto)

    return new.build()
def copy_fields_to_builder(
    state_incarceration_incident_builder: entities.StateIncarcerationIncident.
    Builder,
    proto: StateIncarcerationIncident,
    metadata: IngestMetadata,
) -> None:
    """Mutates the provided |state_incarceration_incident_builder| by converting
    an ingest_info proto StateIncarcerationIncident.

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

    enum_fields = {
        "incident_type": StateIncarcerationIncidentType,
    }
    enum_mappings = EnumMappings(proto, enum_fields, metadata.enum_overrides)

    # enum values
    new.incident_type = enum_mappings.get(StateIncarcerationIncidentType)
    new.incident_type_raw_text = fn(normalize, "incident_type", proto)

    # 1-to-1 mappings
    new.external_id = fn(parse_external_id, "state_incarceration_incident_id",
                         proto)
    new.incident_date = fn(parse_date, "incident_date", proto)
    new.state_code = parse_region_code_with_override(proto, "state_code",
                                                     metadata)
    new.facility = fn(normalize, "facility", proto)
    new.location_within_facility = fn(normalize, "location_within_facility",
                                      proto)
    new.incident_details = fn(normalize, "incident_details", proto)
Exemple #29
0
    def _convert_court_case(self, ingest_court_case: StateCourtCase):
        court_case_builder = entities.StateCourtCase.builder()

        state_court_case.copy_fields_to_builder(court_case_builder,
                                                ingest_court_case,
                                                self.metadata)

        court_case_builder.judge = \
            fn(lambda i: state_agent.convert(self.agents[i], self.metadata),
               'judge_id',
               ingest_court_case)

        return court_case_builder.build()
def convert(
    proto: StateSupervisionViolatedConditionEntry, metadata: IngestMetadata
) -> entities.StateSupervisionViolatedConditionEntry:
    """Converts an ingest_info proto StateSupervisionViolatedConditionEntry to a
    persistence entity."""
    new = entities.StateSupervisionViolatedConditionEntry.builder()

    # 1-to-1 mappings
    new.condition = fn(normalize, "condition", proto)
    new.state_code = parse_region_code_with_override(proto, "state_code",
                                                     metadata)

    return new.build()