def save(self): """ Stores the inmates charges if they are new or if they have been changes Charges: charges come on two lines. The first line is a citation and the # second is an optional description of the charges. """ try: charges = strip_the_lines(self._inmate_details.charges().splitlines()) if just_empty_lines(charges): return # Capture Charges and Citations if specified parsed_charges_citation = charges[0] parsed_charges = charges[1] if len(charges) > 1 else '' create_new_charge = True if len(self._inmate.charges_history.all()) != 0: inmate_latest_charge = self._inmate.charges_history.latest('date_seen') # last known charge # if the last known charge is different than the current info then create a new charge if inmate_latest_charge.charges == parsed_charges and \ inmate_latest_charge.charges_citation == parsed_charges_citation: create_new_charge = False if create_new_charge: new_charge = self._inmate.charges_history.create(charges=parsed_charges, charges_citation=parsed_charges_citation) new_charge.date_seen = yesterday() new_charge.save() except DatabaseError as e: self._debug("Could not save charges '%s' and citation '%s'\nException is %s" % (parsed_charges, parsed_charges_citation, str(e))) except Exception, e: self._debug("Unknown exception for inmate '%s'\nException is %s" % (self._inmate.jail_id, str(e)))
def _parse_court_location(self): """ Takes a location string of the form: "Criminal C\nCriminal Courts Building, Room:506\n2650 South California Avenue Room: 506\nChicago, IL 60608" The lines can contain spurious white-space at their beginning and end, multiple newline characters, and annoying ASCII characters; this is all normalized, no matter what. We return two values: a cleaned up version of the input string, and a dict of the following form... { 'location_name': 'Criminal C', 'branch_name': 'Criminal Courts Building', 'room_number': 506, 'address': '2650 South California Avenue', 'city': 'Chicago', 'state': 'IL', 'zip_code': 60608, } If location string is something other than 4 lines long, or doesn't match our current parsing expectations (mostly based around where the characters "Room:" appear in the string), then the normalized location string is returned, as well an empty dict. Note that room_number and zip_code are stored as ints, not strings. """ location_string = self._inmate_details.court_house_location() if location_string == "": return "", {} # Normalize whitespace, newlines (and weird unicode character). location_string = location_string.replace(u'\xa0', u' ') lines = strip_the_lines(location_string.splitlines()) if len(lines) == 4: try: # First line is the shortened form of the branch name, usually. location_name = lines[0] # Second line must be split into room number and branch name. branch_line = lines[1].split(', Room:') branch_name = branch_line[0].strip() room_number = convert_to_int(branch_line[1], 0) # Remove room number and store the address. address = lines[2].split('Room:')[0].strip() # Fourth line has city, state and zip separated by spaces. city_state_zip = lines[3].split(' ') city = " ".join(city_state_zip[0:-2]).replace(',', '').strip() state = city_state_zip[-2].strip() zip_code = convert_to_int(city_state_zip[-1], 60639) d = { 'location_name': location_name, 'branch_name': branch_name, 'room_number': room_number, 'address': address, 'city': city, 'state': state, 'zip_code': zip_code, } return "\n".join(lines), d except IndexError: self._debug("Following Court location has unknown format: %s" % location_string) return "\n".join(lines), {} else: self._debug( "Following Court location doesn't have right number of lines: %s" % location_string) return "\n".join(lines), {}
def _parse_court_location(self): """ Takes a location string of the form: "Criminal C\nCriminal Courts Building, Room:506\n2650 South California Avenue Room: 506\nChicago, IL 60608" The lines can contain spurious white-space at their beginning and end, multiple newline characters, and annoying ASCII characters; this is all normalized, no matter what. We return two values: a cleaned up version of the input string, and a dict of the following form... { 'location_name': 'Criminal C', 'branch_name': 'Criminal Courts Building', 'room_number': 506, 'address': '2650 South California Avenue', 'city': 'Chicago', 'state': 'IL', 'zip_code': 60608, } If location string is something other than 4 lines long, or doesn't match our current parsing expectations (mostly based around where the characters "Room:" appear in the string), then the normalized location string is returned, as well an empty dict. Note that room_number and zip_code are stored as ints, not strings. """ location_string = self._inmate_details.court_house_location() if location_string == "": return "", {} # Normalize whitespace, newlines (and weird unicode character). location_string = location_string.replace(u'\xa0', u' ') lines = strip_the_lines(location_string.splitlines()) if len(lines) == 4: try: # First line is the shortened form of the branch name, usually. location_name = lines[0] # Second line must be split into room number and branch name. branch_line = lines[1].split(', Room:') branch_name = branch_line[0].strip() room_number = convert_to_int(branch_line[1], 0) # Remove room number and store the address. address = lines[2].split('Room:')[0].strip() # Fourth line has city, state and zip separated by spaces. city_state_zip = lines[3].split(' ') city = " ".join(city_state_zip[0:-2]).replace(',', '').strip() state = city_state_zip[-2].strip() zip_code = convert_to_int(city_state_zip[-1], 60639) d = { 'location_name': location_name, 'branch_name': branch_name, 'room_number': room_number, 'address': address, 'city': city, 'state': state, 'zip_code': zip_code, } return "\n".join(lines), d except IndexError: self._debug("Following Court location has unknown format: %s" % location_string) return "\n".join(lines), {} else: self._debug("Following Court location doesn't have right number of lines: %s" % location_string) return "\n".join(lines), {}
def parse_court_location(location_string): """ Takes a location string of the form: "Criminal C\nCriminal Courts Building, Room:506\n2650 South California Avenue Room: 506\nChicago, IL 60608" The lines can contain spurious white-space at the beginning and end of the lines, these are stripped and returns two values, cleaned up version the input string and a dict of the form: { 'location_name': 'Criminal C', 'branch_name': 'Criminal Courts Building', 'room_number': 506, 'address': '2650 South California Avenue', 'city': 'Chicago', 'state': 'IL', 'zip_code': 60608, } If location is malformed, then original location string is returned with an empty dict """ lines = strip_the_lines(location_string.splitlines()) if len(lines) == 4: try: # The first line is the location_name location_name = lines[0] # Second line must be split into room number and branch name branch_line = lines[1].split(', Room:') branch_name = branch_line[0].strip() room_number = convert_to_int(branch_line[1], 0) # Third line has address - remove room number and store address = lines[2].split('Room:')[0].strip() # Fourth line has city, state and zip separated by spaces, # or a weird unicode space character city_state_zip = lines[3].replace(u'\xa0', u' ').split(' ') city = " ".join(city_state_zip[0:-2]).replace(',', '').strip() state = city_state_zip[-2].strip() zip_code = convert_to_int(city_state_zip[-1], 60639) d = { 'location_name': location_name, 'branch_name': branch_name, 'room_number': room_number, 'address': address, 'city': city, 'state': state, 'zip_code': zip_code, } return "\n".join(lines), d except IndexError: log.debug("Following Court location has unknown format: %s" % location_string) return location_string, {} else: log.debug("Following Court location doesn't have right number of lines: %s" % location_string) return location_string, {}
def parse_court_location(location_string): """ Takes a location string of the form: "Criminal C\nCriminal Courts Building, Room:506\n2650 South California Avenue Room: 506\nChicago, IL 60608" The lines can contain spurious white-space at the beginning and end of the lines, these are stripped and returns two values, cleaned up version the input string and a dict of the form: { 'location_name': 'Criminal C', 'branch_name': 'Criminal Courts Building', 'room_number': 506, 'address': '2650 South California Avenue', 'city': 'Chicago', 'state': 'IL', 'zip_code': 60608, } If location is malformed, then original location string is returned with an empty dict """ lines = strip_the_lines(location_string.splitlines()) if len(lines) == 4: try: # The first line is the location_name location_name = lines[0] # Second line must be split into room number and branch name branch_line = lines[1].split(', Room:') branch_name = branch_line[0].strip() room_number = convert_to_int(branch_line[1], 0) # Third line has address - remove room number and store address = lines[2].split('Room:')[0].strip() # Fourth line has city, state and zip separated by spaces, # or a weird unicode space character city_state_zip = lines[3].replace(u'\xa0', u' ').split(' ') city = " ".join(city_state_zip[0:-2]).replace(',', '').strip() state = city_state_zip[-2].strip() zip_code = convert_to_int(city_state_zip[-1], 60639) d = { 'location_name': location_name, 'branch_name': branch_name, 'room_number': room_number, 'address': address, 'city': city, 'state': state, 'zip_code': zip_code, } return "\n".join(lines), d except IndexError: log.debug("Following Court location has unknown format: %s" % location_string) return location_string, {} else: log.debug( "Following Court location doesn't have right number of lines: %s" % location_string) return location_string, {}