def breplace_argparse(args): replace_from = ' '.join(pipeable.input(args.replace_from)) replace_to = ' '.join(pipeable.input(args.replace_to)) if args.regex: command = f're.sub(r"{replace_from}", r"{replace_to}", x)' else: command = f'x.replace("{replace_from}", "{replace_to}")' brename.brename(command, autoyes=args.autoyes, recurse=args.recurse)
def shuffle_argparse(args): lines = pipeable.input(args.source, read_files=True, skip_blank=True, strip=True) lines = list(lines) random.shuffle(lines) for line in lines: pipeable.stdout(line)
def unique_argparse(args): lines = pipeable.input(args.source, read_files=True, skip_blank=True) seen = set() for line in lines: if line not in seen: pipeable.output(line) seen.add(line)
def moveall_argparse(args): files = (pathclass.Path(file) for pattern in pipeable.input(args.source) for file in winglob.glob(pattern)) destination = pathclass.Path(args.destination) if not destination.is_dir: pipeable.stderr('destination must be a directory.') return 1 pairs = [] fail = False for file in files: new_path = destination.with_child(file.basename) if new_path.exists: pipeable.stderr(f'{file.basename} cannot be moved.') fail = True continue pairs.append((file, new_path)) if fail: return 1 for (file, new_path) in pairs: pipeable.output(new_path.absolute_path) shutil.move(file.absolute_path, new_path.absolute_path)
def init_argparse(args): if isinstance(args.piece_size, str): piece_size = bytestring.parsebytes(args.piece_size) else: piece_size = args.piece_size url = pipeable.input(args.url, split_lines=False) init(url, localname=args.localname, piece_size=piece_size)
def linenumbers_argparse(args): lines = pipeable.input(args.source, read_files=True) if args.lazy: form = '{no} | {line}' else: lines = list(lines) digits = len(str(len(lines))) form = '{no:>0%d} | {line}' % digits for (index, line) in enumerate(lines): print(form.format(no=index + 1, line=line))
def crc32_argparse(args): files = (file for arg in args.source for pattern in pipeable.input(arg) for file in winglob.glob(pattern)) for file in files: try: with open(file, 'rb') as handle: crc = zlib.crc32(handle.read()) print(hex(crc)[2:].rjust(8, '0'), file) except Exception as e: print(file, e)
def groupsof_argparse(args): lines = pipeable.input(args.source, read_files=True, strip=True, skip_blank=True) chunks = gentools.chunk_generator(lines, args.chunk_size) for chunk in chunks: chunk = args.separator.join(chunk) pipeable.output(chunk)
def sorted_argparse(args): lines = pipeable.input(args.source, read_files=True, skip_blank=True, strip=True) lines = list(lines) if args.nocase: lines.sort(key=lambda x: x.lower()) else: lines.sort() for line in lines: pipeable.output(line)
def tempeditor_argparse(args): initial_text = '\n'.join(pipeable.input( args.initial_text)) if args.initial_text else None try: text = tempeditor(initial_text=initial_text) pipeable.output(text) return 0 except NoEditor as exc: pipeable.stderr(exc) return 1 except BadStatus as exc: pipeable.stderr( f'Command {exc.args[0]} returned status {exc.args[1]}.') return 1
def shuffle_argparse(args): if args.count < 1: pipeable.stderr('count must be >= 1.') return 1 lines = pipeable.input(args.source, read_files=True, skip_blank=True, strip=True) lines = list(lines) lines = lines[:args.count] for line in lines: pipeable.stdout(line)
def touch_argparse(args): patterns = [ pattern for arg in args.patterns for pattern in pipeable.input(arg) ] for pattern in patterns: filenames = winglob.glob(pattern) if len(filenames) == 0 and not winglob.is_glob(pattern): open(pattern, 'a').close() print(pattern) for filename in filenames: os.utime(filename) print(filename)
def printstdout_argparse(args): for text in args.texts: text = pipeable.input(text) for line in text: pipeable.stdout(line)
import sys from voussoirkit import pipeable lines = pipeable.input(sys.argv[1]) replace_from = sys.argv[2] replace_to = sys.argv[3] for line in lines: pipeable.output(line.replace(replace_from, replace_to))