diff --git a/contentreplace.py b/contentreplace.py index 9ddf558..e883b81 100644 --- a/contentreplace.py +++ b/contentreplace.py @@ -14,8 +14,7 @@ log = vlogging.getLogger(__name__, 'contentreplace') def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False): file = pathclass.Path(file) - with file.open('r', encoding='utf-8') as f: - content = f.read() + content = file.read('r', encoding='utf-8') if do_regex: occurances = len(re.findall(replace_from, content, flags=re.MULTILINE)) @@ -34,8 +33,7 @@ def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False else: content = content.replace(replace_from, replace_to) - with file.open('w', encoding='utf-8') as f: - f.write(content) + file.write('w', content, encoding='utf-8') @pipeable.ctrlc_return1 def contentreplace_argparse(args): diff --git a/crc32.py b/crc32.py index 343be76..b96a9be 100644 --- a/crc32.py +++ b/crc32.py @@ -16,10 +16,9 @@ def crc32_argparse(args): for file in files: try: - with open(file, 'rb') as handle: - crc = zlib.crc32(handle.read()) + crc = zlib.crc32(file.read('rb')) crc = hex(crc)[2:].rjust(8, '0') - pipeable.stdout(f'{crc} {file}') + pipeable.stdout(f'{crc} {file.absolute_path}') except Exception as e: log.error('%s %s', file, e) return_status = 1 diff --git a/crlf.py b/crlf.py index 211d02e..2c2028d 100644 --- a/crlf.py +++ b/crlf.py @@ -12,8 +12,7 @@ LF = b'\x0A' CRLF = CR + LF def crlf(file): - with file.open('rb') as handle: - content = handle.read() + content = file.read('rb') original = content content = content.replace(CRLF, LF) @@ -21,15 +20,14 @@ def crlf(file): if content == original: return - with file.open('wb') as handle: - handle.write(content) + file.write('wb', content) def crlf_argparse(args): patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True) files = pathclass.glob_many(patterns) for file in files: crlf(file) - pipeable.stdout(file) + pipeable.stdout(file.absolute_path) return 0 diff --git a/filetimelapse.py b/filetimelapse.py index 05b05af..0db8953 100644 --- a/filetimelapse.py +++ b/filetimelapse.py @@ -25,9 +25,6 @@ def hash_file_md5(filepath): return hash_file(filepath, hasher=hashlib.md5()) def read_filebytes(filepath, chunk_size=bytestring.MIBIBYTE): - ''' - Yield chunks of bytes from the file between the endpoints. - ''' filepath = pathclass.Path(filepath) if not filepath.is_file: raise FileNotFoundError(filepath) diff --git a/gitcheckup.py b/gitcheckup.py index ebb406e..53fed9c 100644 --- a/gitcheckup.py +++ b/gitcheckup.py @@ -125,13 +125,10 @@ def read_directories_file(): directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt') try: - handle = directories_file.open('r', encoding='utf-8') + directories = directories_file.readlines('r', encoding='utf-8') except FileNotFoundError as exc: raise NoConfigFile(exc.filename) from exc - with handle: - directories = handle.readlines() - directories = [line.strip() for line in directories] directories = [line for line in directories if line] directories = [pathclass.Path(line) for line in directories] @@ -149,10 +146,7 @@ def write_directories_file(directories): directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt') - handle = directories_file.open('w', encoding='utf-8') - - with handle: - handle.write('\n'.join(directories)) + directories_file.write('w', '\n'.join(directories), encoding='utf-8') # GIT FUNCTIONS ################################################################################ diff --git a/lint_main_returns.py b/lint_main_returns.py index 49cd747..7844adc 100644 --- a/lint_main_returns.py +++ b/lint_main_returns.py @@ -22,7 +22,7 @@ def main(argv): for file in files: no_py = file.replace_extension('').basename - text = file.open('r', encoding='utf-8').read() + text = file.read('r', encoding='utf-8') try: tree = ast.parse(text) except Exception: diff --git a/nosmartquotes.py b/nosmartquotes.py index a96bf56..175c5be 100644 --- a/nosmartquotes.py +++ b/nosmartquotes.py @@ -7,14 +7,18 @@ Replace smart quotes and smart apostrophes with regular ASCII values. Just say no to smart quotes! ''' import argparse +import os import sys from voussoirkit import betterhelp +from voussoirkit import pathclass from voussoirkit import spinal from voussoirkit import vlogging log = vlogging.getLogger(__name__, 'nosmartquotes') +THIS_FILE = os.path.abspath(__file__) + def replace_smartquotes(text): text = text.replace('“', '"') text = text.replace('”', '"') @@ -25,13 +29,12 @@ def replace_smartquotes(text): def nosmartquotes_argparse(args): files = spinal.walk( glob_filenames=args.filename_glob, + exclude_filenames={THIS_FILE}, recurse=args.recurse, ) for file in files: - handle = file.open('r', encoding='utf-8') - text = handle.read() - handle.close() + text = file.read('r', encoding='utf-8') original_text = text text = replace_smartquotes(text) @@ -39,9 +42,7 @@ def nosmartquotes_argparse(args): if text == original_text: continue - handle = file.open('w', encoding='utf-8') - handle.write(text) - handle.close() + file.write('w', text, encoding='utf-8') print(file.absolute_path) return 0 diff --git a/search.py b/search.py index bd01b99..96ff50d 100644 --- a/search.py +++ b/search.py @@ -49,12 +49,10 @@ def all_terms_match(search_text, terms, match_function): def search_contents_generic(filepath, content_args): try: - with filepath.open('r') as handle: - text = handle.read() + text = filepath.read('r') except UnicodeDecodeError: try: - with filepath.open('r', encoding='utf-8') as handle: - text = handle.read() + text = filepath.read('r', encoding='utf-8') except UnicodeDecodeError: #safeprint.safeprint(filepath.absolute_path) #traceback.print_exc() diff --git a/watchforlinks.py b/watchforlinks.py index db307d5..7037736 100644 --- a/watchforlinks.py +++ b/watchforlinks.py @@ -27,9 +27,7 @@ def loop_once(extension, regex=None): path = pathclass.Path(passwordy.urandom_hex(12)).add_extension(extension) pyperclip.copy('') print(path.basename, text) - h = path.open('w', encoding='utf-8') - h.write(text) - h.close() + path.write('w', text, encoding='utf-8') def loop_forever(extension, regex): pyperclip.copy('')