def loadFile(file=DEFAULT_FILE_PATH): from appData import io_formats format = io_formats.get_format_from_ext(os.path.splitext(file)[1]) if format is None: print("Format non supporté !") exit(CmdApp.NOT_SUPPORTED_EXIT_CODE) contacts = format.import_data(file) return contacts
def import_menu(self, *args): file_path = input("Entrez un chemin pour le fichier: ") menu = EasyMenu("Importation de fichiers") menu.set_prompt("Votre choix (a,b,c): ") menu.set_multiple(True) files_to_process = [] if os.path.exists(file_path): if os.path.isdir(file_path): filtered_files = DataManipulation.filter_file( DataManipulation.get_files(file_path), tuple(io_formats.get_ext())) for filtered in filtered_files: files_to_process.append(os.path.join(file_path, filtered)) menu.add_entry(filtered, None) menu.show_menu() chx = menu.wait_for_choise() sel = DataManipulation.get_selected_from_index( files_to_process, chx) for file in sel: ext = os.path.splitext(file)[1] print(ext) l = io_formats.get_format_from_ext(ext).import_data( file_path + '/' + file) self.contacts = self.contacts + l self.make_menu() return else: if file_path.endswith(tuple(io_formats.get_ext())): ext = os.path.splitext(file_path)[1] l = io_formats.get_format_from_ext(ext).import_data( file_path) self.contacts = self.contacts + l self.make_menu() return print("Le chemin/fichier n'est pas valide/dans un format reconu") self.import_menu()
def __init__(self, base=None): self.contacts = [] self.loaded = False self.file_path = "" if base is not None: self.contacts = io_formats.get_format_from_ext( os.path.splitext(base)[1]).import_data(base) if len(self.contacts) > 0: self.loaded = True self.file_path = base self.make_menu()
def convert_file(info): # print(info) source_format, choosed_format, chosed_path = info temp_import = io_formats.get_format_from_ext( source_format).import_data(chosed_path) # print(temp_import) io_formats.get_format(choosed_format).export_data( temp_import, chosed_path.replace(source_format, io_formats.get_format(choosed_format).EXT)) print( "Le nouveau fichier sera écrit a côté du précédant sous le nom :" + chosed_path.replace(source_format, io_formats.get_format(choosed_format).EXT))
def save(content, file=DEFAULT_FILE_PATH, zip=False, db=False): tmp_folder = "./tmp/" + os.path.splitext(os.path.basename(file))[0] if os.path.exists(tmp_folder): shutil.rmtree(tmp_folder) os.makedirs(tmp_folder) from appData import io_formats format = io_formats.get_format_from_ext(os.path.splitext(file)[1]) export = format.export_data( content, file if not zip else tmp_folder + os.path.basename(file)) if zip: export = io_formats.get_format("zip").export_data(export, file) return export
def import_data(file_path): from appData import io_formats tmp_folder = "./tmp/" + os.path.splitext(os.path.basename(file_path))[0] if os.path.exists(tmp_folder): shutil.rmtree(tmp_folder) os.makedirs(tmp_folder) with ZipFile(file_path, 'r') as file: file.extractall(tmp_folder) files = DataManipulation.filter_file(DataManipulation.get_files(tmp_folder+'/tmp/'), tuple(io_formats.get_ext())) print(files) contacts = [] for f in files: contacts += io_formats.get_format_from_ext(os.path.splitext(f)[1]).import_data(tmp_folder+'/tmp/'+f) return contacts
def imports(self, **kwargs): value = cherrypy.request.params print(value) if value != {}: try: files = value['file'] if type(files) is not list: files = [files] for file in files: import_format = io_formats.get_format_from_ext( os.path.splitext(file.filename)[1]) upload_path = os.path.normpath('./webData/tmp') upload_file = os.path.join(upload_path, file.filename) with open(upload_file, 'wb') as out: while True: data = file.file.read(8192) if not data: break out.write(data) i = import_format.import_data(upload_file) print(i) self.app.contacts += i return self.get_page_content("imports", importok=True, addnumber=len(i), totalfiles=len(files)) except KeyError: return self.get_page_content("imports", emptyquery=True) except NotImplementedError: return self.get_page_content("imports", wrongtype=True, formats=io_formats.get_formats()) return self.get_page_content("imports")
def index(self, **kwargs): values = cherrypy.request.params print(values) if values != {}: try: format_chosen = values['format'] filename = values['filename'] if filename == "": return self.get_page_content( "index", formats=io_formats.get_formats(), complete=False) files = values['importfile'] if type(files) is not list: files = [files] for file in files: import_format = io_formats.get_format_from_ext( os.path.splitext(file.filename)[1]) upload_path = os.path.normpath(self.app.TMP_FOLDER) upload_file = os.path.join(upload_path, file.filename) with open(upload_file, 'wb') as out: while True: data = file.file.read(8192) if not data: break out.write(data) i = import_format.import_data(upload_file) print(i) original_dest = self.app.TMP_FOLDER + filename dest = io_formats.get_format(format_chosen).export_data( i, original_dest) if type(dest) is list: zipfilename = os.path.splitext( original_dest)[0] + ".zip" with ZipFile(zipfilename, 'w') as myzip: for d in dest: myzip.write(os.path.abspath(d), os.path.normcase(d)) return serve_file(os.path.abspath(zipfilename), "application/x-download", os.path.basename(zipfilename)) else: return serve_file(os.path.abspath(dest), "application/x-download", filename) except KeyError: return self.get_page_content("index", formats=io_formats.get_formats(), complete=False) except NotImplementedError: return self.get_page_content("index", wrongtype=True, formats=io_formats.get_formats()) return self.get_page_content('index', formats=io_formats.get_formats(), complete=True)