"""Common formatters used between multiple entity types.""" import re from switchboard_consumer.constants.cline import ( INVALID_CHAR_SEQUENCES as INVALID_CLINE_SEQUENCES) from switchboard_consumer.constants.pline import ( INVALID_CHAR_SEQUENCES as INVALID_PLINE_SEQUENCES) def format_cline(cline): """Format cLine.""" formatted_cline = '' try: cline_text = cline.get('cLineText') formatted_cline = re.sub(r'|'.join(map(re.escape, [*INVALID_CLINE_SEQUENCES, *INVALID_PLINE_SEQUENCES])), '', cline_text, 1).strip() except (AttributeError, TypeError): pass return formatted_cline def format_pline(pline): """Format pLine.""" formatted_pline = '' try: pline_text = pline.get('pLineText') formatted_pline = re.sub(r'|'.join(map(re.escape, [*INVALID_CLINE_SEQUENCES, *INVALID_PLINE_SEQUENCES])), '', pline_text, 1).strip() except (AttributeError, TypeError): pass return formatted_pline def format_title_fields(data): """Format title fields.""" formal_title = data.get('formalTitle') formal_title_text = None formal_title_subtitle = None if formal_title: formal_title_text = formal_title.get('titleText') formal_title_subtitle = formal_title.get('subTitle') return formal_title_text, formal_title_subtitle