def list_config(): fd = Config.folder mkdirs(fd) res = [] for fn in os.listdir(fd): if fn.endswith('.json'): res.append(fn[:-5]) return res
def export_config(ctx, param, path): if path is None or ctx.resilient_parsing: return result = extract_settings('conf') if path.endswith('-'): newtd = TableDisplay({"[]": "fC"}) click.echo( newtd(title=">>>[ Config Parameters ]<<<", text=json.dumps(result, indent=2).strip('{}'))) else: mkdirs(path) filepath = os.path.join(path, 'OK_config.json') with open(filepath, 'wt') as f: json.dump(result, f, indent=2) click.echo( td(text=f' Config data Saved! \n File saved to:\n [{filepath}]')) ctx.exit()
def delete_by_pattern(path, pattern): regex = pattern.pattern movedFiles = 0 movedFolders = 0 tempfolder = PurePath(path) / '!DELETE SCRIPT TEMP FOLDER' filepath = tempfolder / 'Deleted files' folderpath = tempfolder / 'Deleted folders' mkdirs(tempfolder, filepath, folderpath) f = open(tempfolder / '!Deletion Log.txt', 'a') f.write( f'Start cleaning {path} on {datetime.now().strftime("%Y/%m/%d %H:%M")}\n' ) f.write(f"Cleaning pattern: >{regex}<\n") f.write('=' * 50 + '\n') for root, dirs, files in os.walk(path): if str(tempfolder) in root: # avoid looking into temp path. continue relative = PurePath(root).relative_to(path) if pattern.search(root): # if the root folder is matched. click.echo(f'Move [FOLDER] <{relative}>') f.write(f'Move [FOLDER] <{relative}>\n') shutil.move(root, folderpath / relative) movedFolders += 1 continue for file in files: if pattern.search(file): click.echo(f'Move [ FILE ] <{file}>') f.write(f'Move [ FILE ] <{os.path.join(relative, file)}>\n') os.makedirs(filepath / relative, exist_ok=True) shutil.move(os.path.join(root, file), filepath / relative / file) movedFiles += 1 #.append(oas.path.join(root, file)) f.write('=' * 50 + '\n') f.write( f'Cleaned {movedFiles} files, {movedFolders} folders, finished on {datetime.now().strftime("%Y/%m/%d %H:%M")}\n\n' ) f.close() return tempfolder, movedFiles, movedFolders
import click import os from mymodule import mkdirs plugin_folder = os.path.join(os.path.dirname(__file__), 'plugins') mkdirs(plugin_folder) def list_plugins(rv=[]): for filename in os.listdir(plugin_folder): if filename.endswith('.py'): temp = filename[:-3] if temp not in rv: rv.append(temp) return rv class MyCLI(click.MultiCommand): """ lazy load of sub commands from commands folder. """ commandList = {} def list_commands(self, ctx): rv = list(self.commandList.keys()) rv = list_plugins(rv) rv.sort() return rv def get_command(self, ctx, name): if name in self.commandList:
def create(self): mkdirs(self.folder) if not os.path.isfile(self.path): _ = open(self.path, 'wt') _.write("{}") _.close()