def retrieve(audio_file_input, key_file): """Method responsible to manage the files, it will convert the audio formats if necessary before starting the decoding of the data Parameters: audio_file_input: Location of the audio file key_file: Location of the key file """ # get the extension of the file, to check if conversion is necessary output_input_extension = utils.get_file_extension(audio_file_input) # if is flac file, convert to wav to perform the operations if output_input_extension == 'flac': # first save with wav extension and after the file was been save, convert the file to flac new_file = utils.replace_file_extension(audio_file_input, 'wav') wav.convert_audio_file(audio_file_input, new_file) audio_file_input = new_file # if is wav file, do nothing elif output_input_extension == 'wav': pass # if not, then it was given an incorrect extension of a file else: assert 'The output file can only be .wav or .flac types' sequence_retrieve(audio_file_input, key_file) # if the input was a flac file, then delete the temporary file if output_input_extension == 'flac': os.remove(audio_file_input)
def _get_folder_files(self, folder, exclude_dotted=True): """ Given a folder, recursively retrieves contained files returns a list of lists ([file extension, file path]). NB : Shall not be used independtly. folder String : path to retrieve files from. """ listed_dirs = [] for root, dirs, files in os.walk(folder): # If bool param is True, exclude dotted # dirs from computing. if exclude_dotted: dirs = utils.remove_dotted_path_elements(dirs) files = utils.remove_dotted_path_elements(files) for name in files: file_path = os.path.join(root, name) file_extension = utils.get_file_extension(file_path) # Stamper should not apply a header on file with # no language extension if self.license.is_valid_file_extension(file_extension): listed_dirs.append([file_extension, file_path]) return listed_dirs
def _get_path_elements(self, path, exclude_dotted=True): """ Recursively retrieves a given path files. returns a list of tuples (file extension, file path). If given path is a folder, returns get_folder_files result, else uses the given path file. path String : path to retrieve files from. exclude_dotted Boolean : Wheter to ignore paths beggining with a dot while walking dirs. Ex : .git .svn .emacs """ listed_elems = [] if os.path.isdir(path): listed_elems = self._get_folder_files(path, exclude_dotted) else: file_path = utils.remove_dotted_path_elements(path) if exclude_dotted \ else path if file_path: file_extension = utils.get_file_extension(file_path) if self.license.is_valid_file_extension(file_extension): listed_elems = [[file_extension, file_path]] return(listed_elems)
def upload_resource(): if 'file' not in request.files: abort(400, 'no file has been uploaded.') file = request.files['file'] if file.filename == '': abort(400, 'no file has been uploaded') file_extension = get_file_extension(file.filename) if file and allowed_file(file_extension, current_app.config['ALLOWED_EXTENSIONS']): ret = upload_file_to_qiniu(current_app.config['QINIU_ACCESS_KEY'], current_app.config['QINIU_SECRET_KEY'], current_app.config['QINIU_BUCKET_NAME'], file.filename, file.read()) if not ret['ret']: abort(500, ret['msg']) else: abort(400, 'not allowed file type.') url = f"http://{current_app.config['QINIU_BUCKET_URL']}/{ret['key']}" file_type = get_file_type(file_extension) sessionFactory = getSessionFactory() session = sessionFactory.get_session() new_resource = Resources(0, file.filename, file_type, url) session.add(new_resource) session.commit() session.close() return jsonify({'msg': 'ok'})
def load_player(path): global is_audio_loaded, player is_audio_loaded = True extension = get_file_extension(path) player = PlayerFactory.create(extension) player.load(path)
def read_word_embedding(replace=False): unknown_dir = "embedding/unknown.npy" vectors_dir = "embedding/vectors.npy" words_dir = "embedding/words.json" word_embeddings_dir = "embedding/word_embeddings.npy" word2idx_dir = "embedding/word2idx.json" print("read word embedding") if replace or not os.path.exists( word_embeddings_dir) or not os.path.exists(word2idx_dir): vectors = np.load(vectors_dir) unknown = np.load(unknown_dir) extension = utils.get_file_extension(words_dir)[1:] assert extension in ["json", "pl"] if extension == "json": words = utils.json_load(words_dir) else: words = utils.pkl_load(words_dir) word2idx = {"UNKNOWN": 1, **{w: i + 2 for i, w in enumerate(words)}} vectors = [ unknown, *list(vectors), ] np.save(word_embeddings_dir, vectors) utils.json_dump(word2idx, word2idx_dir) else: word2idx = utils.json_load(word2idx_dir) print("vocab: %d words" % (len(word2idx) - 1)) return word2idx
def explore_file(file_href): """ Extrai conteúdo de arquivo Retorna nome, linhas, bytes e extensão do arquivo recebido """ file_content = pull_file_content(file_href) lines, bytes_ = get_lines_and_bytes(file_content) filename = get_folder_or_file_name(file_href) return filename, lines, bytes_, get_file_extension(filename)
def fetch_images_habble(name_folder, collection_name): habble_collections_api_url = 'http://hubblesite.org/api/v3/images' habble_image_ids = get_habble_image_ids(habble_collections_api_url, collection_name) for link_number, link in enumerate(habble_image_ids): habble_api_url = 'http://hubblesite.org/api/v3/image/{}'.format(link) image_last_link = get_image_last_link(habble_api_url) file_extension = utils.get_file_extension(image_last_link) habble_image_name = f'habble-{link_number}{file_extension}' utils.save_image(image_last_link, habble_image_name, name_folder)
def handle_file_extension(self, file_header): file_description = get_file_extension(file_header) blackbox_name = 'blackbox' if file_description['file_extension']: blackbox_name += '.%s' % file_description['file_extension'] else: raven.captureMessage( 'This is file type not supported, mime type: %s' % file_description['mime_type']) return blackbox_name
def load(self, path: str): extension = utils.get_file_extension(path) if extension == "xml": self._load_xml(path=path) elif extension == "json": self._load_json(path=path) else: raise Exception( "Error when loading file {}: extension {} not supported.". format(path, extension))
def organise(filelist, directory): # moves files in folders accordind to their extensions for my_file in filelist: file_category = getExtensionCategory(get_file_extension(my_file)) loc = directory + "\\" if file_category != None: loc += file_category else: loc += "Misc" # not in the file formats Path(loc).mkdir(exist_ok=True) shutil.move(my_file, loc) print(f"moved {my_file} to {loc}")
def load(self, file): extension = get_file_extension(file) if extension != 'mp3': raise MP3WrongTypeError try: pg.mixer.music.load(file) except Exception as e: raise FileNotFoundError self._file = file
def _add_attachments(safebox, extension_filter, localpath): logger = logging.getLogger("SSFilePollerRotatingLog") attachments = safebox.attachments safebox.attachments = [] files_to_delete = [] for a in attachments: if not _is_attachment_allowed(extension_filter, get_file_extension(a['file_path'])): raise RuntimeError('File type "' + get_file_extension(a['file_path']) + '" is not allowed by enterprise settings') path, filename = split(a['file_path']) file_size = getsize(a['file_path']) attachment = Attachment({ 'source': open(a['file_path'], 'rb'), 'content_type': str(a['content_type']), 'filename': str(filename), 'size': file_size }) safebox.attachments.append(attachment) if 'delete' not in a.keys() or a['delete']: files_to_delete.append(a['file_path']) return files_to_delete
def export(app, file_path, params, xsi_toolkit): imp.reload(prim_xform) imp.reload(prim_mesh) imp.reload(prim_camera) imp.reload(prim_light) imp.reload(prim_hair) imp.reload(prim_model) imp.reload(prim_pointcloud) imp.reload(materials) imp.reload(utils) progress_bar = xsi_toolkit.ProgressBar progress_bar.CancelEnabled = False progress_bar.Visible = True stage = Usd.Stage.CreateNew(file_path) path_head, path_tail = os.path.split(file_path) path_for_objects = path_head + "\\" + utils.get_file_name(path_tail) + "_objects\\" utils.add_stage_metadata(stage, params) opts = params.get("options", None) if opts is not None: opts["extension"] = utils.get_file_extension(file_path) is_materials = True mats = params.get("materials", None) if mats is not None: is_materials = mats.get("is_materials", True) root_path = "" materials_opt = {} # some parameters of the exported materials material_asset_path = None if is_materials: materials_path = path_head + "\\" + utils.get_file_name(path_tail) + "_materials." + opts["extension"] material_asset_path = materials.export_materials(app, params, stage, materials_path, progress_bar) materials_opt["asset_path"] = material_asset_path exported_objects = [] # store here ObjectID of exported objects if len(params["objects_list"]) == 1 and params["objects_list"][0].ObjectID == app.ActiveProject2.ActiveScene.Root.ObjectID: for obj in app.ActiveProject2.ActiveScene.Root.Children: export_step(app, params, path_for_objects, stage, obj, exported_objects, materials_opt, root_path, progress_bar) else: for obj in params["objects_list"]: export_step(app, params, path_for_objects, stage, obj, exported_objects, materials_opt, root_path, progress_bar) stage.Save() progress_bar.Visible = False
def update_attack_graph(graph_data: str, _: int, filename: str, graph_type: str, n_exploits: int) -> str: if graph_data is None: # The user wants to generate an attack graph generator = Generator(n_exploits=n_exploits) if graph_type == "state": attack_graph = generator.generate_state_attack_graph() else: attack_graph = generator.generate_dependency_attack_graph() else: # The user wants to load an existing attack graph decoded_string = b64decode(graph_data.split(",")[1]) extension = utils.get_file_extension(filename) attack_graph = get_attack_graph_from_string(decoded_string, extension) if attack_graph is None: return "" else: return attack_graph.write()
def _create_img_path(self): img_file_ext = utils.get_file_extension(self._loaded_img_src) img_name = utils.create_filename('{}-'.format(self._img_name_prefix), img_file_ext) return utils.create_filepath(self._IMG_FOLDER, img_name)
def get_input_format(input_files_objects): sample_input_file = input_files_objects[0].get_input_file_path() input_format = get_file_extension(sample_input_file) return input_format
import utils # import docx print(utils.get_random_int(10)) print(utils.get_file_extension("test.txt")) # docx.api.Document()
def test_get_file_extensions(): assert get_file_extension('.gitignore') == '<outros>' assert get_file_extension('Makefile') == '<outros>' assert get_file_extension('file.txt') == 'txt' assert get_file_extension('file.txt~') == 'txt~' assert get_file_extension('.file.yml') == 'yml'
def hide(audio_file_input, audio_file_output, message_file, will_shuffle=False, dict_index=None): """Method responsible to manage the files, it will convert the audio formats if necessary before starting the coding of the data Parameters: audio_file_input: Location of the audio file audio_file_output: Location to save the modified audio file message_file: Location of the file to hide will_shuffle: if true then the shuffle method will be used dict_index: dictionary containing the index lists """ input_input_extension = utils.get_file_extension(audio_file_input) # if the file extension is not wav then convert it to wav extension if input_input_extension != 'wav': if utils.check_ffmpeg() is False: print( 'FFmpeg must be installed on the system to use a different format then wav' ) sys.exit() new_file = utils.replace_file_extension(audio_file_input, 'wav') wav.convert_audio_file(audio_file_input, new_file) audio_file_input = new_file # get the extension of the output file output_input_extension = utils.get_file_extension(audio_file_output) # if is flac file, convert to flac if output_input_extension == 'flac': if utils.check_ffmpeg() is False: print( 'FFmpeg must be installed on the system to use a different format then wav' ) sys.exit() # first save with wav extension and after the file was been save, convert the file to flac audio_file_output = utils.replace_file_extension( audio_file_output, 'wav') # if is wav file, do nothing elif output_input_extension == 'wav': pass # if not, then it was given an incorrect extension of a file else: print('The output file can only be .wav or .flac format') sys.exit() # begin the encoding sequence_hide(audio_file_input, audio_file_output, message_file, will_shuffle, dict_index) # if the user wanted an flac file, lets convert it if output_input_extension == 'flac': new_file = utils.replace_file_extension(audio_file_output, 'flac') wav.convert_audio_file(audio_file_output, new_file, True) # check if the original file was a wav file, if note remove it as is a temporary file if input_input_extension != 'wav': os.remove(audio_file_input)
def main(argv: list): # argument check if len(argv) == 1: print("NO VIDEO FILE", flush=True) exit(0) # set videopath video = argv[1] # get file extension extension = utils.get_file_extension(video).lower() # extension check if extension not in SUPPORTED_EXTENSIONS: print("NOT SUPPORTED EXTENSION") exit(0) rotating_angle = 0 # set rotating angle if second parameter given if len(argv) > 2: rotating_angle = int(argv[2]) # construct VideoCapture object cap = cv2.VideoCapture(video) # initializing base white image image_to_save = np.zeros([200, 200, 3], dtype=np.uint8) image_to_save.fill(255) # 4-character code of codec for writing video fourcc = cv2.VideoWriter_fourcc(*'mp4v') datetime_stamp = datetime.datetime.now()\ .strftime("%Y_%m%d_%H%M%S") # temporary video file temp_video = "temp_video.mp4" filename = 'enarvideo' + datetime_stamp + '.mp4' fps = cap.get(cv2.CAP_PROP_FPS) # construct VideoWriter object # needed to make video from cropped enarnumbers out = cv2.VideoWriter(temp_video, fourcc, float(fps), (200, 200)) # initialize array for enardicts dict_array = [] # processing the video while True: # get one frame from VideoCapture object ret, frame = cap.read() # if no more frame, stop processing if not ret: break # getting current_time in sec current_time = int(cap.get(cv2.CAP_PROP_POS_MSEC) / 1000) # this print needed for the wrapper app print(str( round( cap.get(cv2.CAP_PROP_POS_FRAMES) / cap.get(cv2.CAP_PROP_FRAME_COUNT), 3)), flush=True) # append new dict if len(dict_array) == current_time: dict_array.append(dict()) # rotating frame frame = rotate(frame, angle=rotating_angle) # converting image to HSV colorspace hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # creating mask for filtering mask = cv2.inRange(hsv, lower, upper) # filtering frame with mask res = cv2.bitwise_and(frame, frame, mask=mask) # converting to grayscale gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) # searching connected components output = cv2.connectedComponentsWithStats(gray, 8) # number of labels nlabels = output[0] # stats of the components (width,height etc.) stats = output[2] modframe = frame.copy() # getting the bounding boxes bboxes = utils.get_bounding_boxes(nlabels, stats) # iterating over the bounding boxes for bbox in bboxes: # drawing rectangle to displayed frame cv2.rectangle(modframe, (bbox[0][0], bbox[0][1]), (bbox[1][0], bbox[1][1]), (0, 0, 255), 1) # cropping image crop_img = utils.crop_image(frame, bbox) # getting string from image ocr_string = utils.image_to_string(crop_img, "digits", 1, 6) # check if (ocr_string): # saving cropped image to write out later image_to_save = cv2.resize(crop_img, (200, 200)) # splitting str splitted_str = ocr_string.split("\n") # ocr_string = 'Recognized:\n' + ocr_string + '\nLength: ' + str(len(ocr_string)) + ' \n END' # iterating over the lines for _str in splitted_str: # if only one word in the line and longer then 3 if len(_str.split(' ')) == 1 and len(_str) > 3: # adding to dict if _str[:4] not in dict_array[current_time]: dict_array[current_time][_str[:4]] = 1 else: dict_array[current_time][ _str[:4]] = dict_array[current_time].get( _str[:4]) + 1 # if only one word in the line and longer then 4 if len(_str.split(' ')) == 1 and len(_str) > 4: # adding to dict if _str[:5] not in dict_array[current_time]: dict_array[current_time][_str[:5]] = 1 else: dict_array[current_time][ _str[:5]] = dict_array[current_time].get( _str[:5]) + 1 out.write(image_to_save) # if current_time > 3: # md = utils.merge_dicts(dict_array[current_time], dict_array[current_time - 1], dict_array[current_time - 2], # dict_array[current_time - 3], dict_array[current_time - 4]) # enar5 = utils.get_enar_from_dict(md) # # if enar5 != "": # cv2.putText(modframe, "Enar: " + enar5, (40, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, # (200, 200, 255)) cv2.imshow('video', resize(modframe, 1200, 1200)) k = cv2.waitKey(5) & 0xFF if k == 27: break cv2.destroyAllWindows() cap.release() out.release() # writing to file # checking size if len(dict_array) > 1: # opening file to write with open("enar" + datetime_stamp + ".txt", "w") as f: i = 0 # iterating over the array for _ in dict_array: i = i + 1 # merging dicts # and getting enarnumber if i == 1: m = utils.merge_dicts(dict_array[i], dict_array[i - 1]) #print(m) enar5 = utils.get_enar_from_dict(m) #print(enar4, enar5) elif i == len(dict_array): m = utils.merge_dicts(dict_array[i - 1], dict_array[i - 2]) #print(m) enar5 = utils.get_enar_from_dict(m) #print(enar4, enar5) else: m = utils.merge_dicts(dict_array[i], dict_array[i - 1], dict_array[i - 2]) #print(m) enar5 = utils.get_enar_from_dict(m) #print(enar4, enar5) # writing time and recognized enar to file f.write(str(i) + " " + (enar5 or "") + "\n") # converting video with ffmpeg command = "ffmpeg -i {} {}\n".format(temp_video, filename) try: # running command output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError as e: print('FAIL:\ncmd:{}\noutput:{}'.format(e.cmd, e.output)) # and deleting temporary video file delete_command = "del" if platform.system() == "Windows" else "rm" subprocess.call("{} {}".format(delete_command, temp_video), shell=True)
def get_file_extension(self, doc): mtype = utils.get_buffer_type(doc) return utils.get_file_extension(mtype)
def main(args_dictionary): """Method that will invoke the functions according to the user input Parameters: args_dictionary: Dictionary containing the user arguments """ try: if 'input_file' not in args_dictionary: print('Missing input file destination!') sys.exit() # encode option if args_dictionary['operation'] == 'encode': # validate the arguments if 'message_file' not in args_dictionary: print('Missing message file destination!') sys.exit() # if no method specified then use the basic method if 'operation_method' not in args_dictionary: print('No operation method defined, using basic method!') args_dictionary['operation_method'] = 'basic' if 'output_file' not in args_dictionary: output_file = utils.replace_file_extension( args_dictionary['input_file'], 'wav') file_directory = path.dirname(output_file) file_name = path.basename(output_file) if file_directory != '': final_name = file_directory + '/hidden_' + file_name else: final_name = 'hidden_' + file_name args_dictionary['output_file'] = final_name # dictionary containing the indexes list dict_index = None # if the user input a key file then he wants to use a defined index lists if 'key_file' in args_dictionary: dict_index = utils.read_key_index(args_dictionary['key_file']) will_shuffle = True if args_dictionary[ 'operation_method'] == 'shuffle' else False input_file = args_dictionary['input_file'] output_file = args_dictionary['output_file'] message_file = args_dictionary['message_file'] methods.hide(input_file, output_file, message_file, will_shuffle, dict_index) # decode option elif args_dictionary['operation'] == 'decode': # validate the arguments if 'key_file' not in args_dictionary: print('Missing key file destination!') sys.exit() input_file = args_dictionary['input_file'] key_file = args_dictionary['key_file'] methods.retrieve(input_file, key_file) # information option elif args_dictionary['operation'] == 'info': data, rate = wav.read_wav_file(args_dictionary['input_file']) wav.show_information(data, rate) # show spectrogram elif args_dictionary['operation'] == 'plot': input_file = args_dictionary['input_file'] if utils.get_file_extension(input_file) != 'wav': print('The spectrogram can only be shown to wav files') return data, rate = wav.read_wav_file(input_file) channel = 0 if 'channel' not in args_dictionary: print( 'Channel not specified. Will be used the channel 1 as default' ) else: channel = int(args_dictionary['channel']) - 1 # get channel data channel_data = wav.channel_bytes(data, channel) # represent the plot plot.show_plot(channel_data, rate) except KeyError: print('Bad arguments, use -help.')
def getFilesList(self): """Get the list of files from a room and prepare the information of each file """ wait = ui.WebDriverWait(self.driver, 3) # Wait for the list of files and get them try: files = wait.until( lambda driver: driver.find_elements_by_css_selector( "#file_list .filelist_file")) except TimeoutException: self.logger.error("Couldn't find the list of files, aborting...") return (Result.ERROR, None) # Get all files information files_list_output = [] for file_elem in files: file_left_part = file_elem.find_element_by_class_name( "file_left_part") file_right_part = file_elem.find_element_by_class_name( "file_right_part") url = file_left_part.get_attribute("href") file_tag = file_left_part.find_element_by_class_name( "file_tag").get_attribute("innerHTML") file_size_expiration = file_right_part.get_attribute("innerHTML") size_expiration_pattern = re.compile(r"^(.*?)<.*>(.*)<\/span>") size_expiration_info = size_expiration_pattern.findall( file_size_expiration) file_size = size_expiration_info[0][0] file_expiration = size_expiration_info[0][1] file_id, real_file_name = get_file_id_and_name(url) file_name_without_extension, extension = get_file_extension( real_file_name) files_list_output.append({ "id": file_id, "url": url, "name": sanitize_file_name(file_name_without_extension), "extension": extension, "tag": file_tag, "size": humanfriendly.parse_size(file_size), "expiration": file_expiration }) if config.download_oldest_first: files_list_output = files_list_output[::-1] return (Result.SUCCESS, files_list_output)