def get_art(self, track_library, image_size: int, attempts: int = 5, worker_limit: int = 50, folder_mode=True): """ Search and batch download all the Album arts :param track_library: path to music folder (if folder mode enabled), list of path to each of your track (if folder mode disabled). :param image_size: the size of the image that will be downloaded. :param attempts: number of attempts allowed, if failed to found image, the script will re-attempt to search it. :param worker_limit: set the limit of thread spawning. :param folder_mode: If enabled artgetter will scan a music folder that passed in from the parameter 'track_library'. otherwise you have to pass a list of the path of each of your track. :return: None """ if folder_mode: folder = os.listdir(track_library) track_library = os_sorted(folder) else: track_library = os_sorted(track_library) self.track_list = track_library for attempt in range(attempts): with ThreadPoolExecutor(max_workers=worker_limit) as executor: for track in track_library: executor.submit( self._search, os.path.splitext(os.path.basename(track))[0], image_size, track)
def sub_items(lib_id, dir_id, items_per_page=36, page=1): with Database() as db: count = db.select( "select count(1) c from item where parent=%s and order_id=-1", (dir_id, ), dict_result=True)[0]['c'] ''' 很多文件命名混乱,此处重排 新增列order_id 从0开始排序到最大 逻辑如下: 1. 根据名称中的数字数量 没有数字的,按照字母顺序放在最前面 对于只有一个数字的,直接排序 多个数字的,分别处理,目前仅考虑最大有4个数字的 ''' if count == 0: offset = (page - 1) * items_per_page return db.select( "select id,name,item_type,cover,order_id,library_id,file_type from item where library_id=%s and parent=%s order by order_id limit %s offset %s", (lib_id, dir_id, items_per_page, offset), dict_result=True) # 文件夹排序必须在文件前面 items = [] dir_rows = db.select( "select id,name,order_id from item where library_id=%s and parent=%s and item_type='dir'", (lib_id, dir_id), dict_result=True) for row in dir_rows: items.append({'id': row['id'], 'name': row['name']}) items = natsort.os_sorted(items, key=lambda k: k['name']) rows = db.select( "select id,name,order_id from item where library_id=%s and parent=%s and item_type='file'", (lib_id, dir_id), dict_result=True) file_items = [] for row in rows: file_items.append({'id': row['id'], 'name': row['name']}) file_items = natsort.os_sorted(file_items, key=lambda k: k['name']) items.extend(file_items) for i, item in enumerate(items): db.execute('update item set order_id=%s where id=%s', (i, item['id'])) db.execute('commit') offset = (page - 1) * items_per_page return db.select( "select id,name,item_type,order_id,cover,library_id,file_type from item where library_id=%s and parent=%s order by order_id limit %s offset %s", (lib_id, dir_id, items_per_page, offset), dict_result=True)
def main(): args = parser.parse_args() tracked_files = get_tracked_files() with open("README.template.md", encoding="UTF-8") as file: output = file.read() output += "<table>\n" for filename in os_sorted(os.listdir("bucket")): path = f"bucket/{filename}" if path not in tracked_files: continue with open(path) as file: manifest = json.loads(file.read()) manifest_name = os.path.splitext(filename)[0] name = get_name(manifest_name, manifest) output += f"<tr><td><a href='{manifest['homepage']}'><b>{name}</b></a> — <a href='{path}'><code>{manifest_name}</code></a>" if "description" in manifest: output += f" <br> {manifest['description']} <br><br>" output += "</td></tr>\n" output += "</table>\n" print_diff(output) if not args.dry_run: with open(args.output, "w+", encoding="UTF-8") as file: file.write(output)
def __init__(self, img_dir, sampling_rate=1, start_idx=0, end_idx=-1): self.img_dir = img_dir self.sampling_rate = sampling_rate self.start_idx = start_idx self.end_idx = end_idx self.img_paths = os.listdir(self.img_dir) self.img_paths = os_sorted(self.img_paths) if self.end_idx == -1: self.end_idx = len(self.img_paths)
def sort(images, sorting_method=SortingMethod.LEXICOGRAPHICAL, func=None): if sorting_method == SortingMethod.LEXICOGRAPHICAL: return sorted(images, key=func) elif sorting_method == SortingMethod.NATURAL: return os_sorted(images, key=func) elif sorting_method == SortingMethod.PREDEFINED: return images elif sorting_method == SortingMethod.RANDOM: shuffle(images) return images else: raise NotImplementedError()
def load_images(self, input_dir: Path) -> Tuple[List[Path], List[np.ndarray]]: """Load images from the input directory.""" character_images = [] image_paths = [] for img_path in os_sorted(input_dir.glob("*.png")): image_paths.append(img_path) img = cv.imread(str(img_path)) img = cv.resize(img, (60, 70)) img = np.expand_dims(img, axis=0) character_images.append(img) return image_paths, character_images
def get_arts_path(self, return_sorted: bool = True): """ Get the path of each image :param return_sorted: return sorted list of image paths (Enabled by default) :return: list of image paths """ if return_sorted: return os_sorted(list(self.path_to_arts)) else: list(self.path_to_arts)
def __init__(self, dataDir, dataType, anchors_wh, batch_size): self.img_path = os.path.join(dataDir, 'images', dataType) self.ann_path = os.path.join(dataDir, 'annotations', '{}bboxes.json'.format(dataType)) self.batch_size = batch_size self.anchors_wh = anchors_wh self.num_classes = len(classes) self.classes = classes # ordered list of category names for training self.img_filenames = os_sorted(os.listdir(img_path)) self.img_ids = [os.path.splitext(f)[0] for f in img_filenames] # Create dictionary with all the bboxes with open(ann_path, 'r') as json_file: self.img_bboxes_json = json.load(json_file) json_file.close()
def test_os_sorted_compound(): given = [ "/p/Folder (10)/file.tar.gz", "/p/Folder (1)/file (1).tar.gz", "/p/Folder/file.x1.9.tar.gz", "/p/Folder (2)/file.tar.gz", "/p/Folder (1)/file.tar.gz", "/p/Folder/file.x1.10.tar.gz", ] expected = [ "/p/Folder/file.x1.9.tar.gz", "/p/Folder/file.x1.10.tar.gz", "/p/Folder (1)/file.tar.gz", "/p/Folder (1)/file (1).tar.gz", "/p/Folder (2)/file.tar.gz", "/p/Folder (10)/file.tar.gz", ] result = natsort.os_sorted(given) assert result == expected
def __init__(self, master, title, manga_path, **kwargs): super().__init__(**kwargs) self.master = master self.manga_path = resource_path(manga_path) self.manga_title = title self.effect_cls = "ScrollEffect" self.bar_width = "10dp" self.pos_hint = {"top": .9} self.padding = ( "20dp", "0dp", "20dp", "0dp" ) # padding: [padding_left, padding_top, padding_right, padding_bottom] # Get all the chapter directories and sort them alphanumerically self.chapter_dirs = os_sorted([ os.path.abspath(resource_path(dir)) for dir in glob(os.path.join(self.manga_path, "*/")) if os.path.isdir(dir) ]) self.grid = MDStackLayout(adaptive_height=True, padding=("30dp", "50dp", "30dp", "0dp"), spacing="20dp", pos_hint={"center_x": .5}) # Loop to create buttons for each chapter of a manga for chapter_dir in self.chapter_dirs: chapter_name = os.path.basename(resource_path(chapter_dir)) def reload_func(manga_title=self.manga_title, name=chapter_name, path=chapter_dir): self.master.create_manga_reader(manga_title, name, path) self.chapter_btn = MDRectangleFlatButton( text=chapter_name, pos_hint={ "center_x": .5, "center_y": .8 }, on_release=partial(kill_screen, "Manga Reader Carousel", reload_func), ) self.grid.add_widget(self.chapter_btn) self.add_widget(self.grid)
def openPath(self, path): global directory, previousDirectory, cwd directory = path if not os.path.exists(directory + "/hot"): os.makedirs(directory + "/hot") if not os.path.exists(directory + "/not"): os.makedirs(directory + "/not") # print("CREATED FOLDERS") # print(directory) cwd = glob(directory + "/*.png") cwd.extend(glob(directory + '/*.jpg')) cwd.extend(glob(directory + '/*.jpeg')) cwd.extend(glob(directory + '/*.gif')) cwd.extend(glob(directory + '/*.webm')) cwd.extend(glob(directory + '/*.mp4')) cwd = os_sorted(cwd) # print(cwd) self.changeButtonState(True) self.loadNextImage(directory) previousDirectory = os.path.dirname(directory)
def index_folder(folderPath): print("Indexing: " + folderPath) #Getting the content of the folder files = os_sorted(os.listdir(folderPath)) #If Root folder, correcting folder name root = folderPath if folderPath == '.': root = 'Root' indexText = indexTextStart.format(folderPath=root) for file in files: #Avoiding index.html files if file != 'index.html': indexText += '\t\t<li>\n\t\t\t<a href="' + Dir + "/" + file + '">' + file + "</a>\n\t\t</li>\n" #Recursive call to continue indexing if os.path.isdir(folderPath+'/'+file): index_folder(folderPath + '/' + file) indexText += indexTextEnd #Create or override previous index.html index = open("."+'/index.html', "w") #Save indexed content to file index.write(indexText)
def WelcheBPMGibtEs(Name): global dir_path if os.path.isdir( dir_path ): #os.path.exists(dirname): # wenn der Pfad überhaupt existiert files = os.listdir(dir_path) files = os_sorted(files) files.reverse() # print(files) debug bpmFiles = [] if Name == "": # alle Sensoren wurden gelöscht return bpmFiles # keine BPM anzeigen for aktuellesFile in files: if aktuellesFile.find( Name) == 0: # Wenn der Name im Dateinahme vorkommt if aktuellesFile.find("_V", len(Name)) == len( Name ): # Kommt nach dem Sensorname gleich die Nummerierung bpmFiles.append(aktuellesFile) #print(bpmFiles) # debug #print(len(bpmFiles)) # debug return bpmFiles
async def upload(event): if event.fwd_from: return await event.edit("`Processing...`") input_str = event.pattern_match.group(1) if os.path.exists(input_str): if os.path.isfile(input_str): c_time = time.time() start_time = datetime.now() file_name = os.path.basename(input_str) thumb = None attributes = [] with open(input_str, "rb") as f: result = await upload_file( client=event.client, file=f, name=file_name, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress(d, t, event, c_time, "[FILE - UPLOAD]", input_str) ), ) up_time = (datetime.now() - start_time).seconds if input_str.lower().endswith(("mp4", "mkv", "webm")): thumb = await get_video_thumb(input_str, "thumb_image.jpg") metadata = extractMetadata(createParser(input_str)) duration = 0 width = 0 height = 0 if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") attributes = [ DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=False, supports_streaming=True, ) ] elif input_str.lower().endswith(("mp3", "flac", "wav")): metadata = extractMetadata(createParser(input_str)) duration = 0 artist = "" title = "" if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("title"): title = metadata.get("title") if metadata.has("artist"): artist = metadata.get("artist") attributes = [ DocumentAttributeAudio( duration=duration, title=title, performer=artist, ) ] await event.client.send_file( event.chat_id, result, force_document=True, allow_cache=False, reply_to=event.message.id, attributes=attributes, ) if thumb is not None: os.remove(thumb) await event.edit(f"Uploaded successfully in `{up_time}` seconds.") elif os.path.isdir(input_str): start_time = datetime.now() lst_files = [] for root, _, files in os.walk(input_str): for file in files: lst_files.append(os.path.join(root, file)) if not lst_files: return await event.edit(f"`{input_str}` is empty.") await event.edit(f"Found `{len(lst_files)}` files. Now uploading...") for files in os_sorted(lst_files): file_name = os.path.basename(files) thumb = None attributes = [] msg = await event.reply(f"Uploading `{files}`") with open(files, "rb") as f: result = await upload_file( client=event.client, file=f, name=file_name, ) if file_name.lower().endswith(("mp4", "mkv", "webm")): thumb = await get_video_thumb(files, "thumb_image.jpg") metadata = extractMetadata(createParser(files)) duration = 0 width = 0 height = 0 if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") attributes = [ DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=False, supports_streaming=True, ) ] elif file_name.lower().endswith(("mp3", "flac", "wav")): metadata = extractMetadata(createParser(files)) duration = 0 title = "" artist = "" if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("title"): title = metadata.get("title") if metadata.has("artist"): artist = metadata.get("artist") attributes = [ DocumentAttributeAudio( duration=duration, title=title, performer=artist, ) ] await event.client.send_file( event.chat_id, result, force_document=True, allow_cache=False, attributes=attributes, ) await msg.delete() if thumb is not None: os.remove(thumb) await event.delete() up_time = (datetime.now() - start_time).seconds else: await event.edit("`404: File/Folder Not Found`")
async def lst(event): if event.fwd_from: return cat = event.pattern_match.group(1) path = cat if cat else os.getcwd() if not exists(path): await event.edit( f"Tidak ada direktori atau file dengan nama `{cat}` coba check lagi!" ) return if isdir(path): if cat: msg = "**Folder dan File di `{}`** :\n\n".format(path) else: msg = "**Folder dan File di Direktori Saat Ini** :\n\n" lists = os.listdir(path) files = "" folders = "" for contents in os_sorted(lists): catpath = path + "/" + contents if not isdir(catpath): size = os.stat(catpath).st_size if contents.endswith((".mp3", ".flac", ".wav", ".m4a")): files += "🎵 " elif contents.endswith((".opus")): files += "🎙 " elif contents.endswith( (".mkv", ".mp4", ".webm", ".avi", ".mov", ".flv") ): files += "🎞 " elif contents.endswith( (".zip", ".tar", ".tar.gz", ".rar", ".7z", ".xz") ): files += "🗜 " elif contents.endswith( (".jpg", ".jpeg", ".png", ".gif", ".bmp", ".ico", ".webp") ): files += "🖼 " elif contents.endswith((".exe", ".deb")): files += "⚙️ " elif contents.endswith((".iso", ".img")): files += "💿 " elif contents.endswith((".apk", ".xapk")): files += "📱 " elif contents.endswith((".py")): files += "🐍 " else: files += "📄 " files += f"`{contents}` (__{humanbytes(size)}__)\n" else: folders += f"📁 `{contents}`\n" msg = msg + folders + files if files or folders else msg + "__empty path__" else: size = os.stat(path).st_size msg = "Rincian file yang diberikan:\n\n" if path.endswith((".mp3", ".flac", ".wav", ".m4a")): mode = "🎵 " elif path.endswith((".opus")): mode = "🎙 " elif path.endswith((".mkv", ".mp4", ".webm", ".avi", ".mov", ".flv")): mode = "🎞 " elif path.endswith((".zip", ".tar", ".tar.gz", ".rar", ".7z", ".xz")): mode = "🗜 " elif path.endswith((".jpg", ".jpeg", ".png", ".gif", ".bmp", ".ico", ".webp")): mode = "🖼 " elif path.endswith((".exe", ".deb")): mode = "⚙️ " elif path.endswith((".iso", ".img")): mode = "💿 " elif path.endswith((".apk", ".xapk")): mode = "📱 " elif path.endswith((".py")): mode = "🐍 " else: mode = "📄 " time.ctime(os.path.getctime(path)) time2 = time.ctime(os.path.getmtime(path)) time3 = time.ctime(os.path.getatime(path)) msg += f"**Location :** `{path}`\n" msg += f"**Icon :** `{mode}`\n" msg += f"**Size :** `{humanbytes(size)}`\n" msg += f"**Last Modified Time:** `{time2}`\n" msg += f"**Last Accessed Time:** `{time3}`" if len(msg) > MAX_MESSAGE_SIZE_LIMIT: with io.BytesIO(str.encode(msg)) as out_file: out_file.name = "ls.txt" await event.client.send_file( event.chat_id, out_file, force_document=True, allow_cache=False, caption=path, ) await event.delete() else: await event.edit(msg)
import subprocess from pathlib import Path from natsort import os_sorted from colorama import Fore, Style, init as init_colorama # Directory of this file ( /serene/compiler/ ) here = Path(__file__).parent.resolve() init_colorama() existing_coverage = False paths = os_sorted([Path(x) for x in here.glob("./tests/t*.sn")]) for p in paths: print() print('Testing', p.name, 'now...') if not existing_coverage: serene_completed_process = subprocess.run(['coverage', 'run', 'serene', p, '-o', './temp/test_compiled'], cwd=here, capture_output=True, text=True) existing_coverage = True else: serene_completed_process = subprocess.run(['coverage', 'run', '--append', 'serene', p, '-o', './temp/test_compiled'], cwd=here, capture_output=True, text=True) if serene_completed_process.returncode == 0: print(f"{Fore.GREEN}> Success!{Style.RESET_ALL}") else: print(f"{Fore.YELLOW}{serene_completed_process.stderr}{Style.RESET_ALL}") print(f"{Fore.RED}{Style.BRIGHT}> Failed with error code {serene_completed_process.returncode}.{Style.RESET_ALL}")
def test_os_sorted_corpus(): result = natsort.os_sorted(given) print(result) assert result == expected
async def lst(event): if event.fwd_from: return cat = event.pattern_match.group(1) path = cat if cat else os.getcwd() if not exists(path): await event.edit( f"`Tidak ada direktori atau file seperti itu dengan nama` **{cat}**, `periksa lagi!" ) return if isdir(path): if cat: msg = "`Folder dan file di` **{}** :\n\n".format(path) else: msg = "`Folder dan file di direktori saat ini` :\n\n" lists = os.listdir(path) files = "" folders = "" for contents in os_sorted(lists): catpath = path + "/" + contents if not isdir(catpath): size = os.stat(catpath).st_size if contents.endswith((".mp3", ".flac", ".wav", ".m4a")): files += "🎵 " elif contents.endswith((".opus")): files += "🎙 " elif contents.endswith( (".mkv", ".mp4", ".webm", ".avi", ".mov", ".flv")): files += "🎞 " elif contents.endswith( (".zip", ".tar", ".tar.gz", ".rar", ".7z", ".xz")): files += "🗜 " elif contents.endswith((".jpg", ".jpeg", ".png", ".gif", ".bmp", ".ico", ".webp")): files += "🖼 " elif contents.endswith((".exe", ".deb")): files += "⚙️ " elif contents.endswith((".iso", ".img")): files += "💿 " elif contents.endswith((".apk", ".xapk")): files += "📱 " elif contents.endswith((".py")): files += "🐍 " else: files += "📄 " files += f"`{contents}` - __{humanbytes(size)}__\n" else: folders += f"📁 `{contents}`\n" msg = msg + folders + files if files or folders else msg + "__direktori kosong__" else: size = os.stat(path).st_size msg = "The details of given file :\n\n" if path.endswith((".mp3", ".flac", ".wav", ".m4a")): mode = "🎵 " elif path.endswith((".opus")): mode = "🎙 " elif path.endswith((".mkv", ".mp4", ".webm", ".avi", ".mov", ".flv")): mode = "🎞 " elif path.endswith((".zip", ".tar", ".tar.gz", ".rar", ".7z", ".xz")): mode = "🗜 " elif path.endswith( (".jpg", ".jpeg", ".png", ".gif", ".bmp", ".ico", ".webp")): mode = "🖼 " elif path.endswith((".exe", ".deb")): mode = "⚙️ " elif path.endswith((".iso", ".img")): mode = "💿 " elif path.endswith((".apk", ".xapk")): mode = "📱 " elif path.endswith((".py")): mode = "🐍 " else: mode = "📄 " time.ctime(os.path.getctime(path)) time2 = time.ctime(os.path.getmtime(path)) time3 = time.ctime(os.path.getatime(path)) msg += f"**Lokasi :** `{path}`\n" msg += f"**Ikon :** `{mode}`\n" msg += f"**Ukuran :** `{humanbytes(size)}`\n" msg += f"**Waktu terakhir diubah :** `{time2}`\n" msg += f"**Waktu terakhir diakses :** `{time3}`" if len(msg) > MAX_MESSAGE_SIZE_LIMIT: with io.BytesIO(str.encode(msg)) as out_file: out_file.name = "ls.txt" await event.client.send_file( event.chat_id, out_file, force_document=True, allow_cache=False, caption=path, ) await event.delete() else: await event.edit(msg)
import os import sys from natsort import os_sorted idx = 0 src = os.getcwd() def rename(s): if (s[-6:-4] == 'p0'): global idx idx = idx + 1 prefix = '' if idx >= 10 else '0' return prefix + str(idx) + s[s.find('_'):] print('renaming') script_name = sys.argv[0] filenames = os.listdir() filenames.remove(script_name) filenames = os_sorted(filenames) #natural sort filenames.sort(key=lambda x: int(x[:x.find('_')]), reverse=True) #Guaranteed to be stable new_names = list(map(rename, filenames)) for i in range(len(filenames)): os.rename(filenames[i], new_names[i])
def test_os_sorted_misc_no_fail(): natsort.os_sorted([9, 4.3, None, float("nan")])
def build_frames(input_file: str, width: int, height: int, pixel_size: int, palette_size: int, threads: int, ecc_bytes: int, video_codec: str, fps: int, output_file: str): """Encodes any file into images.""" # first frame will always be metadata file = open(input_file, "rb") filesize = os.path.getsize(input_file) if len(file.name) > 512: raise ValueError("File names longer than 512 characters are not supported!") # future-proofing for the next century, file sizes larger than that cannot be written down in the metadata frame if filesize > (256 ** 8) - 1: raise ValueError("Files bigger than %d bytes are not supported!" % ((256 ** 8) - 1)) blocks_per_frame = math.floor((width * height) / (pixel_size ** 2) / int(math.log(256, palette_size)) / BLOCK_SIZE) content_bytes_per_block = BLOCK_SIZE - ecc_bytes content_bytes_per_frame = blocks_per_frame * content_bytes_per_block needed_frames = int(math.ceil(filesize / content_bytes_per_frame)) + 1 print("Needed amount of frames are %d." % (needed_frames)) print("Video length is %d seconds." % (needed_frames / args.fps)) # metadata looks like this: bytes 0 and 1 are the encoding version, bytes 2-4 are for the palette size, byte 5 is the pixel size, bytes 6-13 is the file size, bytes 14-33 are the SHA1 checksum, byte 34 is the amount of ECC bytes, bytes 35-546 are the filename # the metadata frame always has 32 bytes of ECC, regardless of whether strong_ecc is True or False meta_bytes = bytearray() meta_ecc = RSCodec(32) meta_bytes += ENC_VERSION_NUM.to_bytes(2, byteorder="big") meta_bytes += palette_size.to_bytes(2, byteorder="big") meta_bytes += pixel_size.to_bytes(1, byteorder="big") meta_bytes += os.path.getsize(input_file).to_bytes(8, byteorder="big") meta_bytes += vb_common.sha1_file(input_file) meta_bytes += ecc_bytes.to_bytes(1, byteorder="big") meta_bytes += os.path.basename(file.name + (((512 - len(file.name)) * '\0'))).encode("utf-8") # fill the file name with nulls so we have fixed length metadata # the metadata frame is our first partial video, we use MPEG-2 TS as a container since concatenating with it requires little effort os.rename(build_frame(meta_ecc.encode(meta_bytes), width, height, 0, pixel_size, palette_size, fps, video_codec), os.path.join("tmp", "partial.ts")) print(f"Built metadata frame; 1/{needed_frames} ({(1/needed_frames):.2f} %).") ecc = RSCodec(ecc_bytes) # rather unoptimized multithreading, to be improved def work(num: int): # seek to the correct position as with multithreading this seems to fail quite a lot bytearray_to_encode = bytearray() file.seek((num - 1) * content_bytes_per_frame) all_bytes = file.read(content_bytes_per_frame) cur_bytes = bytearray() for i in range(0, blocks_per_frame): try: cur_bytes = all_bytes[(i * content_bytes_per_block):((i + 1) * content_bytes_per_block)] # if we reached the end of the file, we might have not read 256 bytes, append NULLs in this case except: cur_bytes = all_bytes[(i * content_bytes_per_block):] for i in range(0, len(cur_bytes) - content_bytes_per_block): cur_bytes.append(0x00) bytearray_to_encode += ecc.encode(cur_bytes) build_frame(bytearray_to_encode, width, height, num, pixel_size, palette_size, fps, video_codec) cur_frame = 1 while cur_frame < needed_frames: jobs = [] frames = [] for i in range(threads): # we need to check again because the loop might terminate too late if(cur_frame < needed_frames): frames.append("%d.ts" % cur_frame) p = multiprocessing.Process(target=work, args=(cur_frame,)) cur_frame += 1 jobs.append(p) p.start() # wait until the process actually started time.sleep(0.05) for job in jobs: job.join() frames = os_sorted(frames) frames.insert(0, "partial.ts") with open(os.path.join("tmp", "list.txt"), "w") as l: for f in frames: l.write("file '" + f + "'\n") #concat_files(frames, os.path.join("tmp", "new_partial.ts")) subprocess.run(["ffmpeg", "-y", "-f", "concat", "-r", str(fps), "-i", os.path.join("tmp", "list.txt"), "-c", "copy", os.path.join("tmp", "new_partial.ts")], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for f in frames: os.remove(os.path.join("tmp", f)) os.remove(os.path.join("tmp", "list.txt")) os.rename(os.path.join("tmp", "new_partial.ts"), os.path.join("tmp", "partial.ts")) percent = ((cur_frame)/needed_frames)*100 print(f"Built all frames up to {cur_frame}/{needed_frames} ({percent:.2f} %).") print(f"Converting into desired format...") subprocess.run(["ffmpeg", "-y", "-r", str(fps), "-i", os.path.join("tmp", "partial.ts"), "-c", "copy", output_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) file.close()
def natural_sort(item_to_natural_sort): return os_sorted(item_to_natural_sort)
def test_os_sorted_key(): given = ["foo0", "foo2", "goo1"] expected = ["foo0", "goo1", "foo2"] result = natsort.os_sorted(given, key=lambda x: x.replace("g", "f")) assert result == expected
def main(): root_path: str = input('请输入工作目录:') os.chdir(root_path) for sub_folder in (entry for entry in os.scandir(root_path) if entry.is_dir()): # print(sub_folder.path) # if not os.listdir(sub_folder.path): # with open('./{}.bat'.format("asd"), 'a') as f: # f.writelines('rd "{}"'.format(sub_folder.path)) # continue tmp_folder_path = os.path.join(sub_folder.path, 'tmp') pic_folder_path = os.path.join(sub_folder.path, 'pic') video_folder_path = os.path.join(sub_folder.path, 'video') others_folder_path = os.path.join(sub_folder.path, 'others') os.makedirs(tmp_folder_path, exist_ok=True) os.makedirs(pic_folder_path, exist_ok=True) os.makedirs(video_folder_path, exist_ok=True) os.makedirs(others_folder_path, exist_ok=True) move_file(sub_folder.path, tmp_folder_path) sorted_file_deque = deque(os_sorted(os.listdir(tmp_folder_path))) pic_num = 0 video_num = 0 while sorted_file_deque: file_name = sorted_file_deque.popleft() ext_name = os.path.splitext(file_name)[-1] if ext_name in ('.bmp', '.jpg', '.jpeg', '.gif', '.tif', '.png', '.pcx', '.tga', '.ico', '.BMP', '.JPG', '.JPEG', '.GIF', '.TIF', '.PNG', '.PCX', '.TGA', '.ICO'): shutil.move(f'{tmp_folder_path}/{file_name}', pic_folder_path) pic_num += 1 os.rename( f'{pic_folder_path}/{file_name}', f'{pic_folder_path}/{str(pic_num).zfill(3)}{ext_name}') elif ext_name in ('.mp4', '.MP4', '.avi', '.AVI', '.mov', '.MOV', '.mkv', '.MKV', '.wmv', '.WMV', '.m2t', '.M2T'): shutil.move(f'{tmp_folder_path}/{file_name}', f'{video_folder_path}/{file_name}') video_num += 1 os.rename( f'{video_folder_path}/{file_name}', f'{video_folder_path}/v{str(video_num).zfill(3)}{ext_name}' ) else: shutil.move(f'{tmp_folder_path}/{file_name}', f'{others_folder_path}/{file_name}') if pic_num != 0: move_file(pic_folder_path, sub_folder.path) else: move_file(video_folder_path, sub_folder.path) folder_list = [x for x in os.scandir(sub_folder.path) if x.is_dir()] for folder in folder_list: del_empty_folder(folder.path)
"\nExample U04-C07" "\nMAKE SURE TO USE CAPITAL LETTERS") choose = easygui.indexbox(msg="Which mode would you like?", title="Yeast Classifier", choices=('384 format with cluster of 4', '384 format each cell different')) loc = (re.findall('\d+', cell_loc)) row = (cell_loc[4]) col = int(loc[1]) plate = (int(loc[0]) - 1) # list will be zero indexed so minus 1 if (col > 12): print("Wrong value entered") exit(-2) imagePath = (list(paths.list_images(dir))) imagePath = os_sorted(imagePath) if (choose == 1): # for when 1-4 are on the same plate if (plate == 0 or plate == 1 or plate == 2 or plate == 3): newplate = 0 if (plate == 4 or plate == 5 or plate == 6 or plate == 7): newplate = 1 if (plate == 8 or plate == 9 or plate == 10 or plate == 11): newplate = 2 if (plate == 12 or plate == 13 or plate == 14 or plate == 15): newplate = 3 if (plate == 16 or plate == 17 or plate == 18 or plate == 19): newplate = 4 if (plate == 20): newplate = 5 if choose == 0: newplate = plate
def create_cmd( pls_obj: Playlist, from_: str, rel: bool, extended: bool, nat_sort: bool, is_here: bool, is_empty: bool, is_reversed: bool, ) -> None: """Creates playlist from folder or from scratch.""" pls_path: Path = pls_obj.path if is_empty: # Create an empty playlist file and exit. pls_path.write_text("") click.get_current_context().exit() dir: Path = Path(from_) playlist_as_text: str = "" try: if dir.is_dir(): valid_files: List[Path] = [] valid_files = get_supported_audios_in_folder(dir) if not valid_files: click.echo( f"Warning: No supported audio files in folder '{str(dir)}'." ) click.get_current_context().exit() sorted_rel: List[str] = [] sorted_abs: List[str] = [] rel_paths = [f.name for f in valid_files] abs_paths = [str(f.resolve()) for f in valid_files] if nat_sort: sorted_rel, sorted_abs = os_sorted(rel_paths), os_sorted( abs_paths) else: sorted_rel, sorted_abs = sorted(rel_paths), sorted(abs_paths) if is_reversed: sorted_rel.reverse() sorted_abs.reverse() zipped_paths = zip(sorted_rel, sorted_abs) playlist_as_text = generate_playlist_content_from_zipped( zipped_paths, extended, rel) target_file: Path = Path() if is_here: playlist_name = pls_path.name target_file = Path(dir, Path(playlist_name)) else: target_file = pls_path playlist.save_playlist_content(playlist_as_text, target_file) else: click.echo(f"Error: This directory '{str(dir)}' does NOT exist.") click.get_current_context().exit() except (OSError) as error: message = str(error) raise click.ClickException(message)
async def upload(event): if event.fwd_from: return await event.edit("`Sedang memproses...`") input_str = event.pattern_match.group(1) if os.path.exists(input_str): if os.path.isfile(input_str): c_time = time.time() start_time = datetime.now() file_name = os.path.basename(input_str) thumb = None attributes = [] with open(input_str, "rb") as f: result = await upload_file( client=event.client, file=f, name=file_name, progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, event, c_time, "[FILE - UNGGAH]", input_str)), ) up_time = (datetime.now() - start_time).seconds if input_str.lower().endswith(("mp4", "mkv", "webm")): thumb = await get_video_thumb(input_str, "thumb_image.jpg") metadata = extractMetadata(createParser(input_str)) duration = 0 width = 0 height = 0 if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") attributes = [ DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=False, supports_streaming=True, ) ] elif input_str.lower().endswith(("mp3", "flac", "wav")): metadata = extractMetadata(createParser(input_str)) duration = 0 artist = "" title = "" if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("title"): title = metadata.get("title") if metadata.has("artist"): artist = metadata.get("artist") attributes = [ DocumentAttributeAudio( duration=duration, title=title, performer=artist, ) ] await event.client.send_file( event.chat_id, result, thumb=thumb, caption=file_name, force_document=False, allow_cache=False, reply_to=event.message.id, attributes=attributes, ) if thumb is not None: os.remove(thumb) await event.edit(f"Berhasil diunggah dalam `{up_time}` detik.") elif os.path.isdir(input_str): start_time = datetime.now() lst_files = [] for root, dirs, files in os.walk(input_str): for file in files: lst_files.append(os.path.join(root, file)) if len(lst_files) == 0: return await event.edit(f"`{input_str}` kosong.") await event.edit( f"Ditemukan `{len(lst_files)}` file.\n`Sedang mengunggah...`") for files in os_sorted(lst_files): file_name = os.path.basename(files) thumb = None attributes = [] msg = await event.reply(f"Mengunggah `{files}`") with open(files, "rb") as f: result = await upload_file( client=event.client, file=f, name=file_name, ) if file_name.lower().endswith(("mp4", "mkv", "webm")): thumb = await get_video_thumb(files, "thumb_image.jpg") metadata = extractMetadata(createParser(files)) duration = 0 width = 0 height = 0 if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("width"): width = metadata.get("width") if metadata.has("height"): height = metadata.get("height") attributes = [ DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=False, supports_streaming=True, ) ] elif file_name.lower().endswith(("mp3", "flac", "wav")): metadata = extractMetadata(createParser(files)) duration = 0 title = "" artist = "" if metadata.has("duration"): duration = metadata.get("duration").seconds if metadata.has("title"): title = metadata.get("title") if metadata.has("artist"): artist = metadata.get("artist") attributes = [ DocumentAttributeAudio( duration=duration, title=title, performer=artist, ) ] await event.client.send_file( event.chat_id, result, thumb=thumb, caption=file_name, force_document=False, allow_cache=False, attributes=attributes, ) await msg.delete() if thumb is not None: os.remove(thumb) await event.delete() up_time = (datetime.now() - start_time).seconds await event.respond( f"Mengunggah `{len(lst_files)}` file ke folder `{input_str}` " f"dalam `{up_time}` detik.") else: await event.edit("**404** : File/Folder tidak ditemukan.")
cypher_transaction_block = [] record = True for line in lines: stripped_line = line.replace('\r', '').replace('\n', '').strip() if stripped_line == ":begin": record = True continue elif stripped_line == ":commit": record = False commit( isolate_single_statements_in_transaction_block( cypher_transaction_block)) cypher_transaction_block = [] continue if record: # ignore empty line. only record lines with content if stripped_line: cypher_transaction_block.append(stripped_line) # Delete existing/old source files, to prevent confusion with newer files clean_data_sources() # Download source files download_data() for filename in os_sorted(os.listdir(DATASOURCE_PATH)): if filename.endswith(".cypher"): print(f"Start processing '{filename}'") with CodeTimer(filename, unit="s"): parse_cypher_file(os.path.join(DATASOURCE_PATH, filename))
def __init__(self, master, manga_title, chapter_name, chapter_path, **kwargs): super().__init__(**kwargs) self.master = master self.manga_title = manga_title self.chapter_name = chapter_name self.chapter_path = resource_path(chapter_path) self.padding = ( "0dp", "100dp", "0dp", "20dp" ) # padding: [padding_left, padding_top, padding_right, padding_bottom] self.chapter_imgs = os_sorted([ resource_path(img) for img in glob(os.path.join(self.chapter_path, "*.jpg")) if os.path.isfile(resource_path(img)) ]) # Debug: if platform == "android": print(f"Chapter images of {self.manga_title}: {self.chapter_imgs}") self.swiping_direction = "left" if self.master.manga_swiping_direction == "Left to Right (Japanese style)" else "right" self.reading_direction = "bottom" if self.master.manga_reading_direction == "Scroll vertically" else self.swiping_direction #self.carousel = Carousel(direction=self.reading_direction) self.carousel = MangaCarousel(direction=self.reading_direction) for index, img in enumerate(self.chapter_imgs): self.scatter = ZoomableImage(image_src=resource_path(img)) self.inner_carousel_layout = MDRelativeLayout( ) #size=self.scatter.size) self.inner_carousel_layout.add_widget( MDLabel(text=f"Page {index + 1}/{len(self.chapter_imgs)}", pos_hint={"top": .6})) self.inner_carousel_layout.add_widget(self.scatter) self.carousel.add_widget(self.inner_carousel_layout) self.prev_btn = MDIconButton( icon="menu-left", user_font_size="200sp", on_release=lambda *x: self.carousel.load_previous(), pos_hint={ "center_x": .1, "center_y": .5 }) self.next_btn = MDIconButton( icon="menu-right", user_font_size="200sp", on_release=lambda *x: self.carousel.load_next(), pos_hint={ "center_x": .9, "center_y": .5 }) # Changes the way the arrows load the pages depending on the reading direction # True/left --> Left to right (left) JP ; False/right --> Right to left (right) EN if self.swiping_direction == "left" and self.reading_direction != "bottom": self.prev_btn = MDIconButton( icon="menu-left", user_font_size="200sp", on_release=lambda *x: self.carousel.load_next(), pos_hint={ "center_x": .1, "center_y": .5 }) self.next_btn = MDIconButton( icon="menu-right", user_font_size="200sp", on_release=lambda *x: self.carousel.load_previous(), pos_hint={ "center_x": .9, "center_y": .5 }) if platform != "android": self.inner_carousel_layout.add_widget(self.prev_btn) self.inner_carousel_layout.add_widget(self.next_btn) self.add_widget(self.carousel)