mirror of
https://github.com/ail-project/ail-framework.git
synced 2024-11-10 08:38:28 +00:00
fix: [import_dir] remove special characters
This commit is contained in:
parent
72fe8a2461
commit
bdf2fce332
1 changed files with 14 additions and 4 deletions
|
@ -10,6 +10,7 @@ import argparse
|
||||||
import os
|
import os
|
||||||
import time, datetime
|
import time, datetime
|
||||||
import magic
|
import magic
|
||||||
|
import re
|
||||||
|
|
||||||
'''
|
'''
|
||||||
'
|
'
|
||||||
|
@ -50,6 +51,9 @@ def is_hierachy_valid(path):
|
||||||
correctDate = False
|
correctDate = False
|
||||||
return correctDate
|
return correctDate
|
||||||
|
|
||||||
|
def sanitize_str(str_var, invalid_char_regex):
|
||||||
|
res = re.sub(invalid_char_regex, "-", str_var)
|
||||||
|
return res.replace(' ', '_')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description='Take files from a directory and push them into a 0MQ feed.')
|
parser = argparse.ArgumentParser(description='Take files from a directory and push them into a 0MQ feed.')
|
||||||
|
@ -67,6 +71,9 @@ if __name__ == "__main__":
|
||||||
socket.bind("tcp://*:{}".format(args.port))
|
socket.bind("tcp://*:{}".format(args.port))
|
||||||
time.sleep(1) #Important, avoid loosing the 1 message
|
time.sleep(1) #Important, avoid loosing the 1 message
|
||||||
|
|
||||||
|
invalid_char = r'[\\/*?&%=:"<>|#\\\']'
|
||||||
|
invalid_char_dir = r'[\\*?&%=:"<>|#\\\']'
|
||||||
|
|
||||||
for dirname, dirnames, filenames in os.walk(args.directory):
|
for dirname, dirnames, filenames in os.walk(args.directory):
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
complete_path = os.path.join(dirname, filename)
|
complete_path = os.path.join(dirname, filename)
|
||||||
|
@ -85,17 +92,20 @@ if __name__ == "__main__":
|
||||||
if not is_hierachy_valid(complete_path):
|
if not is_hierachy_valid(complete_path):
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
paste_name = complete_path.split('/')[-1]
|
paste_name = complete_path.split('/')[-1]
|
||||||
|
paste_name = sanitize_str(paste_name, invalid_char)
|
||||||
directory = complete_path.split('/')[-2]
|
directory = complete_path.split('/')[-2]
|
||||||
|
directory = sanitize_str(directory, invalid_char_dir)
|
||||||
wanted_path = os.path.join(directory, now.strftime("%Y"), now.strftime("%m"), now.strftime("%d"), paste_name)
|
wanted_path = os.path.join(directory, now.strftime("%Y"), now.strftime("%m"), now.strftime("%d"), paste_name)
|
||||||
|
wanted_path = os.path.relpath(wanted_path)
|
||||||
else:
|
else:
|
||||||
#take wanted path of the file
|
#take wanted path of the file
|
||||||
wanted_path = os.path.realpath(complete_path)
|
wanted_path = os.path.relpath(complete_path)
|
||||||
wanted_path = wanted_path.split('/')
|
wanted_path = wanted_path.split('/')
|
||||||
wanted_path = '/'.join(wanted_path[-(4+args.hierarchy):])
|
wanted_path = '/'.join(wanted_path[-(4+args.hierarchy):])
|
||||||
|
wanted_path = sanitize_str(wanted_path, invalid_char_dir)
|
||||||
|
|
||||||
# remove whitespace
|
# sanitize feeder_name
|
||||||
wanted_path = wanted_path.replace(' ', '')
|
feeder_name = os.path.relpath(sanitize_str(args.name, invalid_char))
|
||||||
feeder_name = args.name.replace(' ', '')
|
|
||||||
|
|
||||||
path_to_send = 'import_dir/' + feeder_name + '>>' + wanted_path
|
path_to_send = 'import_dir/' + feeder_name + '>>' + wanted_path
|
||||||
s = b' '.join( [ args.channel.encode(), path_to_send.encode(), base64.b64encode(messagedata) ] )
|
s = b' '.join( [ args.channel.encode(), path_to_send.encode(), base64.b64encode(messagedata) ] )
|
||||||
|
|
Loading…
Reference in a new issue