def split_file_name(cls, file_name, expected_components): """Splits the file name to a dictionary keyed by components names. Args: file_name: File name to split. expected_components: A list of the expected file name parts. Returns: A dictionary of the file name components names (keys) and the given file name parts (values). """ basic_split = file_name.split(constants.FILE_NAME_DELIMITER) if len(basic_split) != len(constants.FILE_NAME_COMPONENTS) - 2: raise error.FileNameValidationFailure( file_name, 'bad name structure, expected format: %s.' % constants.FILE_NAME_FORMAT) xofy = basic_split[-2] message_created_time_ext = basic_split[-1] file_name_parts = basic_split[:-2] xofy = xofy.split('of') message_created_time_ext = message_created_time_ext.split('.', 1) file_name_parts.extend(xofy) file_name_parts.extend(message_created_time_ext) if len(file_name_parts) != len(constants.FILE_NAME_COMPONENTS): raise error.FileNameValidationFailure( file_name, 'bad name structure, expected format: %s.' % constants.FILE_NAME_FORMAT) file_name_dict = { component_name: value for component_name, value in zip(expected_components, file_name_parts) } return file_name_dict
def validate_message_notification_period(cls, mnp, file_name): if not constants.MESSAGE_NOTIFICATION_PERIOD_PATTERN.match(mnp): raise error.FileNameValidationFailure( file_name, 'Message Notification Period "%s" is invalid, should be ' 'ISO 8601:2004 period format.' % mnp) return mnp
def validate_suffix(cls, suffix, file_name): if suffix not in constants.SUPPORTED_FILE_EXTENSIONS: raise error.FileNameValidationFailure( file_name, 'Suffix "%s" is not valid, supported suffixes: %s.' % (suffix, constants.SUPPORTED_FILE_EXTENSIONS)) return suffix
def validate_value(self, file_name): """Validates that a filename consists of the expected components. Args: file_name: File name to validate. Returns: A dictionary of {component_name = component_value} (eg. {'ServiceDescription': 'AdSupport'}). """ warnings = set() file_name_dict = self.split_file_name(file_name, self.expected_components) try: self.validate_xofy(file_name_dict['x'], file_name_dict['y'], file_name) self.validate_prefix( file_name_dict['DSR'], file_name, ) self.validate_suffix(file_name_dict['ext'], file_name) self.validate_message_notification_period( file_name_dict['MessageNotificationPeriod'], file_name) self.validate_territory_of_use_or_sale( file_name_dict['TerritoryOfUseOrSale'], file_name) self.validate_message_created_datetime( file_name_dict['MessageCreatedDateTime'], file_name) except KeyError: raise error.FileNameValidationFailure( file_name, 'bad name structure, expected format: %s.' % constants.FILE_NAME_FORMAT) except error.FileNameValidationWarning as e: warnings.add(e) return file_name_dict, warnings
def validate_message_created_datetime(cls, mcdt, file_name): if not constants.MESSAGE_CREATED_DATETIME_PATTERN.match(mcdt): raise error.FileNameValidationFailure( file_name, 'MessageCreated-DateTime "%s" is invalid, should be ' 'yyyyymmddThhmmss.' % mcdt) return mcdt
def validate_xofy(cls, x, y, file_name): try: if int(x) <= int(y): return x, y except ValueError: pass raise error.FileNameValidationFailure( file_name, 'File number is not an integer or does not exist.')
def _raise_filename_validation_error(file_name, row_number, row_type, cell_name, cell_value, filename_part, filename_value, file_number): raise error.FileNameValidationFailure( file_name, '[%s: row %s]: The cell "%s" with the value "%s" does ' 'not match the file name part "%s" with the value "%s" in file ' 'number %s.' % (row_number, row_type, cell_name, cell_value, filename_part, filename_value, file_number))
def _validate_party_filename(file_name, file_name_dict, party_cells, party_type): """Message Sender/Recipient values in the filename must match the HEAD. Args: file_name: Original name of the file. file_name_dict: Components extracted from the filename. party_cells: The Party ID & name from the HEAD for the Sender|Receiver. party_type: Either 'Sender' or 'Recipient' """ if (party_cells and file_name_dict['Message' + party_type] not in list( party_cells.values())): raise error.FileNameValidationFailure( file_name, 'The Message%(party_type)s value in the filename ' '("%(filename_value)s") did not match either of the ' '%(party_type)sPartyId ("%(party_id)s") or the %(party_type)sName ' '("%(party_name)s") in the HEAD row' % { 'party_type': party_type, 'filename_value': file_name_dict['Message' + party_type], 'party_id': party_cells[party_type + 'PartyId'], 'party_name': party_cells[party_type + 'Name'] })
def _raise_mismatch_parts(cls, part_name, expected_name, actual_name, file_name): raise error.FileNameValidationFailure( file_name, 'The %s value "%s" is expected to match the other files' ' in the report and be "%s".' % (part_name, actual_name, expected_name))
def validate_prefix(cls, prefix, file_name): if prefix != constants.FILE_NAME_PREFIX: raise error.FileNameValidationFailure( file_name, 'File name should start with %s.' % constants.FILE_NAME_PREFIX) return prefix