-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_path.py
75 lines (54 loc) · 2.33 KB
/
file_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Parse and manage file_path related functions"""
from os import path
from language import Language
from error import Error
class FilePath:
@staticmethod
def validate_file_path(file_path):
"""Checks if file_path is valid"""
# Check if file_path is a string
if not isinstance(file_path, str):
return "Parameter file_path must be a string"
# Check if file exists
if not path.isfile(file_path):
return "File - {} Does not exist".format(file_path)
# Get extension from file_path
response = Language.recognize(file_path)
# Check if error encountered
if isinstance(response, tuple) and not response[0]:
# Parse error
Error.parse(response)
# Save error response
extension = response[1]
else:
# Else save response
extension = response
# Check if is supported language source code
if extension.startswith('Unsupported file extension '):
return "Unsupported file type"
@staticmethod
def validate_file_name(file_path, input_language):
"""Checks if file_name matches specified language"""
# Check if file_name and input_language are strings
if not (isinstance(file_path, str) and isinstance(input_language, str)):
return "Parameters file_path and input_language must be a string"
# Extract file name from file path
file_name = path.split(file_path)[1]
# Get file extension
response = Language.recognize(file_name)
# Check if error encountered
if isinstance(response, list) and not response[0]:
# Parse error
Error.parse(response)
# Check if file_name is a valid
special_characters = "\\/:*?\"<>|"
for character in special_characters:
if file_name.find(character) != -1:
return "File name must not contain '%s'" % special_characters
# Else save response
recognized_language = response
# Check if file_name extension matches language
error_message = "Extension and language don't match"
# Return validation result
if input_language != recognized_language:
return error_message