def check_for_file_encoding(comparable: Compare): with open(comparable.file_name, newline='', encoding=comparable.encoding) as file: try: file.readline() except UnicodeDecodeError: raise AppErrorHandler(AppErrorHandler.unknown_encoding)
def check_for_type_string(comparator): """Check for parameter value with type string to be string""" for key_val in comparator.default_schema: is_key_available = all([key_val[Field.prm_name.value] in comparator.parameters, key_val[Field.prm_type.value] is str]) if is_key_available: if not isinstance(comparator.parameters[key_val[Field.prm_name.value]], str): raise AppErrorHandler(AppErrorHandler.invalid_value_type)
def check_for_required_key(comparator): """Check for required parameter key to exist""" for key_val in comparator.default_schema: is_required = key_val[Field.prm_required.value] is_missing = key_val[Field.prm_name.value] not in comparator.parameters if all([is_required, is_missing]): raise AppErrorHandler( AppErrorHandler.missing_required_key.format( key_val[Field.prm_name.value]))
def check_for_default_schema(comparator): """Check for submitted parameter keys to exist in default schema""" submitted_keys = list(comparator.parameters.keys()) default_schema_keys = [ key_val[Field.prm_name.value] for key_val in comparator.default_schema ] is_any_key_missing = not all(user_submitted_key in default_schema_keys for user_submitted_key in submitted_keys) if is_any_key_missing: raise AppErrorHandler(AppErrorHandler.no_parameter)
def set_original_header(comparable: Compare): with open(comparable.file_name, encoding=comparable.encoding) as csv_file: reader = csv.DictReader(csv_file) skipped_line = 1 while skipped_line < comparable.header_row: try: next(csv_file) except StopIteration: raise AppErrorHandler(AppErrorHandler.header_row_out_of_range) skipped_line += 1 comparable.original_header = reader.fieldnames
def check_for_empty_value(comparator): """ Check for parameters that can not have empty value""" for key_val in comparator.default_schema: is_key_available = all([ key_val[Field.prm_required.value], key_val[Field.prm_name.value] in comparator.parameters, key_val[Field.prm_default_value.value] is None ]) if is_key_available: is_value_blank = comparator.parameters[key_val[Field.prm_name.value]] == \ Field.empty_string.value if is_value_blank: raise AppErrorHandler(AppErrorHandler.invalid_value)
def check_for_parameter_type(comparator): for key in comparator.default_schema: is_key_available = key[Field.prm_name.value] in comparator.parameters if is_key_available: is_value_not_empty = comparator.parameters[key[ Field.prm_name.value]] != Field.empty_string.value is_value_wrong_type = not isinstance( comparator.parameters[key[Field.prm_name.value]], key[Field.prm_type.value]) if is_value_not_empty and is_value_wrong_type: raise AppErrorHandler(AppErrorHandler.invalid_value_type)
def set_file_encoding(comparable: Compare): with open(comparable.file_name, 'rb') as file: detector = UniversalDetector() for line in file.readlines(): detector.feed(line) if detector.done: break detector.close() if detector.result["confidence"] < 0.5 or \ detector.result["encoding"] is None: raise AppErrorHandler(AppErrorHandler.unknown_encoding) else: comparable.encoding = detector.result['encoding'].lower()
def check_for_special_character(comparator): forbidden_characters = ['*', '?', ':', '"', '<', '>', '|', ']', '[', "'"] for key in comparator.default_schema: is_key_available = all([ key[Field.prm_name.value] in comparator.parameters, Field.prm_can_have_special_char.value in key ]) if is_key_available: submitted_value = comparator.parameters[key[Field.prm_name.value]] is_submitted_value_str = isinstance(submitted_value, str) if is_submitted_value_str: is_containing_forbidden_char = any([ x in forbidden_characters for x in list( comparator.parameters[key[Field.prm_name.value]]) ]) if is_containing_forbidden_char: raise AppErrorHandler(AppErrorHandler.invalid_value)
def check_for_type_dictionary(comparator): if not isinstance(comparator.parameters, dict): raise AppErrorHandler(AppErrorHandler.invalid_dictionary)
def check_for_input_file_existence(comparable: Compare): is_file_available = os.path.isfile(comparable.file_name) if not is_file_available: raise AppErrorHandler(AppErrorHandler.no_file)
def check_for_key_to_be_string_type(comparator): for key_val in comparator.parameters: if not isinstance(key_val, str): raise AppErrorHandler(AppErrorHandler.invalid_key_type)
def check_for_negative_header_row(comparable: Compare): if comparable.header_row <= 0: raise AppErrorHandler(AppErrorHandler.header_row_out_of_range)
def check_for_file_write_access(comparable: Compare): is_file_writeable = os.access(comparable.file_name, os.W_OK) if not is_file_writeable: raise AppErrorHandler(AppErrorHandler.write_access)
def check_for_index_column(comparable: Compare): for index_col in comparable.index_column_name: if not h.is_header(comparable, index_col): raise AppErrorHandler(AppErrorHandler.missing_index_column)
def check_for_file_size(comparable: Compare): if os.path.getsize(comparable.file_name) == 0: raise AppErrorHandler(AppErrorHandler.empty_file)
def check_for_file_read_access(comparable: Compare): is_file_readable = os.access(comparable.file_name, os.R_OK) if not is_file_readable: raise AppErrorHandler(AppErrorHandler.read_access)
def check_for_empty_dictionary(comparator): if not comparator.parameters: raise AppErrorHandler(AppErrorHandler.empty_list)
def check_for_header_row_location(comparable: Compare): """Check for header row location in the file to not exceed number of lines in the file""" num_lines = sum( 1 for _ in open(comparable.file_name, encoding=comparable.encoding)) if num_lines < comparable.header_row: raise AppErrorHandler(AppErrorHandler.header_row_out_of_range)