def argparse_to_dict(args): text = args.text if text is not None: text = clipext.resolve(text) elif STDIN_MODE == 'pipe': text = clipext.resolve('!i') if hasattr(args, 'content_args') and args.content_args is not None: content_args = argparse_to_dict(args.content_args) else: content_args = None return { 'yes_all': args.yes_all, 'yes_any': args.yes_any, 'not_all': args.not_all, 'not_any': args.not_any, 'case_sensitive': args.case_sensitive, 'content_args': content_args, 'do_expression': args.do_expression, 'do_glob': args.do_glob, 'do_regex': args.do_regex, 'local_only': args.local_only, 'line_numbers': args.line_numbers, 'text': text, }
def argparse_to_dict(args): text = args.text if text is not None: text = clipext.resolve(text) elif STDIN_MODE == 'pipe': text = clipext.resolve('!i') if hasattr(args, 'content_args') and args.content_args is not None: content_args = argparse_to_dict(args.content_args) else: content_args = None return { 'yes_all': args.yes_all_1 + args.yes_all_2, 'yes_any': args.yes_any, 'not_all': args.not_all, 'not_any': args.not_any, 'case_sensitive': args.case_sensitive, 'content_args': content_args, 'do_expression': args.do_expression, 'do_glob': args.do_glob, 'do_regex': args.do_regex, 'do_strip': args.do_strip, 'local_only': args.local_only, 'line_numbers': args.line_numbers, 'only_dirs': args.only_dirs, 'only_files': args.only_files, 'text': text, }
def init_argparse(args): if isinstance(args.piece_size, str): piece_size = bytestring.parsebytes(args.piece_size) else: piece_size = args.piece_size url = clipext.resolve(args.url) init(url, localname=args.localname, piece_size=piece_size)
def getcrx_argparse(args): extension_ids = [] if len(args.extension_ids) == 1: extension_ids.extend( clipext.resolve(args.extension_ids[0], split_lines=True)) elif args.extension_ids: extension_ids.extend(args.extension_ids) if args.file: with open(args.file, 'r') as handle: lines = handle.readlines() extension_ids.extend(lines) extension_ids = [x.split('/')[-1].strip() for x in extension_ids] if args.overwrite and not args.dont_overwrite: auto_overwrite = True elif args.dont_overwrite and not args.overwrite: auto_overwrite = False else: auto_overwrite = None for extension_id in extension_ids: try: getcrx(extension_id, auto_overwrite=auto_overwrite) except Exception: if args.fail_early: raise else: traceback.print_exc() print('Resuming...')
def getcrx_argparse(args): extension_ids = [] if len(args.extension_ids) == 1: extension_ids.extend(clipext.resolve(args.extension_ids[0], split_lines=True)) elif args.extension_ids: extension_ids.extend(args.extension_ids) if args.file: with open(args.file, 'r') as handle: lines = handle.readlines() extension_ids.extend(lines) extension_ids = [x.split('/')[-1].strip() for x in extension_ids] if args.overwrite and not args.dont_overwrite: auto_overwrite = True elif args.dont_overwrite and not args.overwrite: auto_overwrite = False else: auto_overwrite = None for extension_id in extension_ids: try: getcrx(extension_id, auto_overwrite=auto_overwrite) except Exception: if args.fail_early: raise else: traceback.print_exc() print('Resuming...')
def threaded_dl_argparse(args): if os.path.isfile(args.url_file): f = open(args.url_file, 'r') with f: urls = f.read() else: urls = clipext.resolve(args.url_file) urls = urls.replace('\r', '').split('\n') urls = [u.split(' ', 1) if ' ' in u else u for u in urls] headers = args.headers if headers is not None: if len(headers) == 1 and headers[0].startswith('{'): headers = ast.literal_eval(headers[0]) else: keys = headers[::2] vals = headers[1::2] headers = {key: val for (key, val) in zip(keys, vals)} bytespersecond = args.bytespersecond if bytespersecond is not None: bytespersecond = bytestring.parsebytes(bytespersecond) threaded_dl( urls, bytespersecond=bytespersecond, filename_format=args.filename_format, headers=headers, thread_count=args.thread_count, timeout=args.timeout, )
def main(argv): filename = argv[0] if os.path.isfile(filename): f = open(filename, 'r') with f: urls = f.read() else: urls = clipext.resolve(filename) urls = urls.replace('\r', '').split('\n') thread_count = int(listget(argv, 1, 4)) filename_format = listget(argv, 2, None) threaded_dl(urls, thread_count=thread_count, filename_format=filename_format)
def download_argparse(args): url = args.url url = clipext.resolve(url) callback = { None: Progress1, '1': Progress1, '2': Progress2, }.get(args.callback, args.callback) bytespersecond = args.bytespersecond if bytespersecond is not None: bytespersecond = bytestring.parsebytes(bytespersecond) headers = {} if args.range is not None: headers['range'] = 'bytes=%s' % args.range retry = args.retry if not retry: retry = 1 while retry != 0: # Negative numbers permit infinite retries. try: download_file( url=url, localname=args.localname, bytespersecond=bytespersecond, callback_progress=callback, do_head=args.no_head is False, headers=headers, overwrite=args.overwrite, timeout=args.timeout, verbose=True, ) except (NotEnoughBytes, requests.exceptions.ConnectionError): retry -= 1 if retry == 0: raise else: break
def repeat_argparse(args): text = clipext.resolve(args.text) if args.times == 'inf': try: while True: print(text) except KeyboardInterrupt: return 0 else: try: times = int(args.times) except ValueError: pipeable.stderr('times should be an integer >= 1.') return 1 if times < 1: pipeable.stderr('times should be >= 1.') return 1 try: for t in range(times): print(text) except KeyboardInterrupt: return 1
import os import sys from voussoirkit import clipext text = sys.argv[1] command = sys.argv[2:] command = ['"%s"' % x if (' ' in x or x == '%x') else x for x in command] command = ' '.join(command) text = clipext.resolve(text) for line in text.splitlines(): thiscomm = command.replace('%x', line) os.system(thiscomm)
''' Keep the unique lines coming from stdin and print them. ''' from voussoirkit import clipext import sys if len(sys.argv) > 1: source = sys.argv[1] else: source = '!input' lines = clipext.resolve(source, split_lines=True) new_text = [] seen = set() for line in lines: if line not in seen: #new_text.append(line) seen.add(line) print(line)
import sys from voussoirkit import clipext arg = clipext.resolve(sys.argv[1]) arg = ''.join(reversed(arg)) print(arg)
''' Great for applying Python post-processing to the output of some other command. Provide an input string (!i for stdin) and an eval string using `x` as the variable name of the input. ''' from voussoirkit import clipext import math import os import random import string import sys import time if '--lines' in sys.argv: by_lines = True sys.argv.remove('--lines') else: by_lines = False text = clipext.resolve(sys.argv[1], split_lines=by_lines) transformation = ' '.join(sys.argv[2:]) if by_lines: for line in text: x = line result = eval(transformation) print(result) else: x = text result = eval(transformation) print(result)
''' Dump the clipboard to stdout. I use this for redirecting to files. ''' import pyperclip import sys if len(sys.argv) > 1: from voussoirkit import clipext stuff = clipext.resolve(sys.argv[1]) pyperclip.copy(stuff) else: text = pyperclip.paste() text = text.replace('\r', '') print(text)
''' Create the file, or update the last modified timestamp. ''' import glob import os import sys from voussoirkit import clipext from voussoirkit import safeprint def touch(glob_pattern): filenames = glob.glob(glob_pattern) if len(filenames) == 0: safeprint.safeprint(glob_pattern) open(glob_pattern, 'a').close() else: for filename in filenames: safeprint.safeprint(filename) os.utime(filename) if __name__ == '__main__': glob_patterns = [clipext.resolve(x).strip() for x in sys.argv[1:]] for glob_pattern in glob_patterns: touch(glob_pattern)
def reverse_argparse(args): lines = clipext.resolve(args.lines) lines = lines.splitlines() lines = reversed(lines) print('\n'.join(lines))
''' Sort the lines coming from stdin and print them. ''' from voussoirkit import clipext import sys if len(sys.argv) > 1: text = clipext.resolve(sys.argv[1]) else: text = clipext.resolve('!input') text = text.split('\n') if '-l' in sys.argv: text.sort(key=lambda x: x.lower()) else: text.sort() new_text = '\n'.join(text) print(new_text)
''' Perform a HEAD request and print the results. ''' import sys import json import requests from voussoirkit import clipext urls = clipext.resolve(sys.argv[1], split_lines=True) for url in urls: page = requests.head(url) headers = dict(page.headers) headers = json.dumps(headers, indent=4, sort_keys=True) print(page) print(headers)