def main(): args = parser.parse_args() if args.dump: config = Config() config.from_pythontidy_namespace() config.write(file=stdout) exit() if args.config: config = Config(file=args.config) config.to_pythontidy_namespace() tidy_up(args.input, args.output)
def prettifyFile(source, bak=True): """格式化py代码文件""" if not os.path.exists(source): return print source if os.path.exists(source + '.bak'): os.remove(source + '.bak') if bak: tidy_up(source, source + '.pre') os.rename(source, source + '.bak') os.rename(source + '.pre', source) else: tidy_up(source, source)
def formatCode(path): list_dirs = os.listdir(path) for dir in list_dirs: new_path = os.path.join(path, dir) if os.path.isdir(new_path): formatCode(new_path) if os.path.isfile(new_path): re_object = re.compile("\w*py$") if re_object.search(new_path): new_path_back = new_path + ".bak" tidy_up(new_path, new_path_back) cmd = "mv " + new_path_back + " " + new_path print cmd subprocess.call(cmd, shell=True)
def tidy_recursively(orig_dir, tidied_dir, filter_pattern): """Tidy code in src_path recursively using PythonTidy and write results accordingly in dst_path. The filter_pattern is a Unix shell-style wildcards pattern for files to tidy. """ tidied_path_list = [] for root, _, files in os.walk(orig_dir): tidied_root = root.replace(orig_dir, tidied_dir) # Make sure tidied parent dir exists try: os.makedirs(tidied_root) except OSError: pass for name in fnmatch.filter(files, filter_pattern): orig_path = os.path.join(root, name) tidied_path = os.path.join(tidied_root, name) try: logging.info("tidy %s into %s" % (orig_path, tidied_path)) tidy_up(orig_path, tidied_path) tidied_path_list.append(tidied_path) except StandardError, tidy_err: logging.error("tidy failed for %s: %s" % (orig_path, tidy_err))