11 def validate_keyword_conf_file(file_path:str):
13 Validate whether input file in keyword.conf format or not
14 :param: file_path : str File path to validate
15 :return: bool True if file in correct format. Else False
16 :return: str Validation message(True)/Error message(False)
19 with open(file_path,
'r')
as file:
20 lines = file.readlines()
22 return False,
"File is empty"
24 non_commented_lines = []
26 stripped_line = line.strip()
27 if stripped_line.startswith(
'#')
or not stripped_line:
28 commented_lines.append(stripped_line)
30 non_commented_lines.append(stripped_line)
32 if not non_commented_lines:
33 return False,
"File has no keyword to search for"
36 for line
in non_commented_lines:
37 if not re.search(
r'keyword=',line):
40 matches = re.findall(
r'__.*?__', line)
42 if not re.match(
r'__\w+__', match):
43 return False, f
"Invalid '__keyword__' format in line: {line}"
46 return False,
"File must contain at least one 'keyword=' line"
48 return True,
"Valid keyword.conf file"
50 except FileNotFoundError:
51 return False,
"File not found"
52 except Exception
as e:
55 def copy_keyword_file_to_destination(source_path:str, destination_path:str) ->
None:
57 Make destination path and copy keyword file to destination
58 :param: source_path:str Source file path
59 :param: destination_path:str Destination file path
63 os.makedirs(os.path.dirname(destination_path),exist_ok=
True)
64 shutil.copyfile(source_path,destination_path)
65 print(f
"Keyword configuration file copied to {destination_path}")
67 except Exception
as e:
68 print(f
"Unable to copy the file to {destination_path}: {e}")