-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathfile_output.py
118 lines (101 loc) · 3.98 KB
/
file_output.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import shutil
def _clean_filename(filename):
"""
Remove invalid characters from filename and maximize it to 200 characters
:param filename: Input filename
:return: sanitized filename
"""
return filename.replace('/', '').replace('\\', '').replace(':', '')[:200]
def _clean_filepath(filename):
"""
Remove invalid characters from filename and maximize it to 200 characters
:param filename: Input filename including path
:return: sanitized filename
"""
fixed_filename = filename.replace(':', '')
if os.sep == '/':
fixed_filename = fixed_filename.replace('\\', '')
elif os.sep == '\\':
fixed_filename = fixed_filename.replace('/', '')
return fixed_filename[:200]
def write_file(filename, overwrite_mode, content):
"""
Writes content to a file and ensures if the file already exists it won't be overwritten by appending a number
as suffix.
:param filename: filename
:param overwrite_mode: defines whether we want to force overwriting existing file
:param content: the content of the file that needs to be written to the file
:return:
"""
if os.sep in filename:
output_filename = _clean_filepath(filename)
else:
output_filename = 'output/%s' % _clean_filename(filename)
if not overwrite_mode:
output_filename = get_non_existing_filename(output_filename, 'json')
else:
output_filename = use_existing_filename(output_filename, 'json')
try:
with open(output_filename, 'w') as f:
f.write(content)
print('File written: ' + output_filename)
except Exception as e:
print('[!] Error while writing layer file: %s' % str(e))
def backup_file(filename):
"""
Create a backup of the provided file
:param filename: existing YAML filename
:return:
"""
suffix = 1
backup_filename = filename.replace('.yaml', '_backup_' + str(suffix) + '.yaml')
while os.path.exists(backup_filename):
backup_filename = backup_filename.replace('_backup_' + str(suffix) + '.yaml', '_backup_' + str(suffix + 1) + '.yaml')
suffix += 1
shutil.copy2(filename, backup_filename)
print('Written backup file: ' + backup_filename + '\n')
def create_output_filename(filename_prefix, filename):
"""
Creates a filename using pre determined convention.
:param filename_prefix: prefix part of the filename
:param filename: filename
:return:
"""
return '%s_%s' % (filename_prefix, normalize_name_to_filename(filename))
def get_non_existing_filename(filename, extension):
"""
Generates a filename that doesn't exist based on the given filename by appending a number as suffix.
:param filename: input filename
:param extension: input extension
:return: unique filename
"""
if filename.endswith('.' + extension):
filename = filename.replace('.' + extension, '')
if os.path.exists('%s.%s' % (filename, extension)):
suffix = 1
while os.path.exists('%s_%s.%s' % (filename, suffix, extension)):
suffix += 1
output_filename = '%s_%s.%s' % (filename, suffix, extension)
else:
output_filename = '%s.%s' % (filename, extension)
return output_filename
def use_existing_filename(filename, extension):
"""
Generates a filename that preserves the file extension if present.
If no extension is present, adds the provided extension.
:param filename: input filename
:param extension: input extension
:return: filename and extension, without duplicating extensions
"""
if filename.endswith('.' + extension):
filename = filename.replace('.' + extension, '')
output_filename = '%s.%s' % (filename, extension)
return output_filename
def normalize_name_to_filename(name):
"""
Normalize the input filename to a lowercase filename and replace spaces with dashes.
:param name: input filename
:return: normalized filename
"""
return name.lower().replace(' ', '-').replace('/', '-')