Use pathclass.read, write.

This commit is contained in:
voussoir 2021-10-04 17:21:14 -07:00
parent 7c3ef856e8
commit 701cf3e0d3
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
9 changed files with 20 additions and 37 deletions

View file

@ -14,8 +14,7 @@ log = vlogging.getLogger(__name__, 'contentreplace')
def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False): def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False):
file = pathclass.Path(file) file = pathclass.Path(file)
with file.open('r', encoding='utf-8') as f: content = file.read('r', encoding='utf-8')
content = f.read()
if do_regex: if do_regex:
occurances = len(re.findall(replace_from, content, flags=re.MULTILINE)) 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: else:
content = content.replace(replace_from, replace_to) content = content.replace(replace_from, replace_to)
with file.open('w', encoding='utf-8') as f: file.write('w', content, encoding='utf-8')
f.write(content)
@pipeable.ctrlc_return1 @pipeable.ctrlc_return1
def contentreplace_argparse(args): def contentreplace_argparse(args):

View file

@ -16,10 +16,9 @@ def crc32_argparse(args):
for file in files: for file in files:
try: try:
with open(file, 'rb') as handle: crc = zlib.crc32(file.read('rb'))
crc = zlib.crc32(handle.read())
crc = hex(crc)[2:].rjust(8, '0') crc = hex(crc)[2:].rjust(8, '0')
pipeable.stdout(f'{crc} {file}') pipeable.stdout(f'{crc} {file.absolute_path}')
except Exception as e: except Exception as e:
log.error('%s %s', file, e) log.error('%s %s', file, e)
return_status = 1 return_status = 1

View file

@ -12,8 +12,7 @@ LF = b'\x0A'
CRLF = CR + LF CRLF = CR + LF
def crlf(file): def crlf(file):
with file.open('rb') as handle: content = file.read('rb')
content = handle.read()
original = content original = content
content = content.replace(CRLF, LF) content = content.replace(CRLF, LF)
@ -21,15 +20,14 @@ def crlf(file):
if content == original: if content == original:
return return
with file.open('wb') as handle: file.write('wb', content)
handle.write(content)
def crlf_argparse(args): def crlf_argparse(args):
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True) patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
files = pathclass.glob_many(patterns) files = pathclass.glob_many(patterns)
for file in files: for file in files:
crlf(file) crlf(file)
pipeable.stdout(file) pipeable.stdout(file.absolute_path)
return 0 return 0

View file

@ -25,9 +25,6 @@ def hash_file_md5(filepath):
return hash_file(filepath, hasher=hashlib.md5()) return hash_file(filepath, hasher=hashlib.md5())
def read_filebytes(filepath, chunk_size=bytestring.MIBIBYTE): def read_filebytes(filepath, chunk_size=bytestring.MIBIBYTE):
'''
Yield chunks of bytes from the file between the endpoints.
'''
filepath = pathclass.Path(filepath) filepath = pathclass.Path(filepath)
if not filepath.is_file: if not filepath.is_file:
raise FileNotFoundError(filepath) raise FileNotFoundError(filepath)

View file

@ -125,13 +125,10 @@ def read_directories_file():
directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt') directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt')
try: try:
handle = directories_file.open('r', encoding='utf-8') directories = directories_file.readlines('r', encoding='utf-8')
except FileNotFoundError as exc: except FileNotFoundError as exc:
raise NoConfigFile(exc.filename) from exc raise NoConfigFile(exc.filename) from exc
with handle:
directories = handle.readlines()
directories = [line.strip() for line in directories] directories = [line.strip() for line in directories]
directories = [line for line in directories if line] directories = [line for line in directories if line]
directories = [pathclass.Path(line) for line in directories] 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') directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt')
handle = directories_file.open('w', encoding='utf-8') directories_file.write('w', '\n'.join(directories), encoding='utf-8')
with handle:
handle.write('\n'.join(directories))
# GIT FUNCTIONS # GIT FUNCTIONS
################################################################################ ################################################################################

View file

@ -22,7 +22,7 @@ def main(argv):
for file in files: for file in files:
no_py = file.replace_extension('').basename no_py = file.replace_extension('').basename
text = file.open('r', encoding='utf-8').read() text = file.read('r', encoding='utf-8')
try: try:
tree = ast.parse(text) tree = ast.parse(text)
except Exception: except Exception:

View file

@ -7,14 +7,18 @@ Replace smart quotes and smart apostrophes with regular ASCII values.
Just say no to smart quotes! Just say no to smart quotes!
''' '''
import argparse import argparse
import os
import sys import sys
from voussoirkit import betterhelp from voussoirkit import betterhelp
from voussoirkit import pathclass
from voussoirkit import spinal from voussoirkit import spinal
from voussoirkit import vlogging from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'nosmartquotes') log = vlogging.getLogger(__name__, 'nosmartquotes')
THIS_FILE = os.path.abspath(__file__)
def replace_smartquotes(text): def replace_smartquotes(text):
text = text.replace('', '"') text = text.replace('', '"')
text = text.replace('', '"') text = text.replace('', '"')
@ -25,13 +29,12 @@ def replace_smartquotes(text):
def nosmartquotes_argparse(args): def nosmartquotes_argparse(args):
files = spinal.walk( files = spinal.walk(
glob_filenames=args.filename_glob, glob_filenames=args.filename_glob,
exclude_filenames={THIS_FILE},
recurse=args.recurse, recurse=args.recurse,
) )
for file in files: for file in files:
handle = file.open('r', encoding='utf-8') text = file.read('r', encoding='utf-8')
text = handle.read()
handle.close()
original_text = text original_text = text
text = replace_smartquotes(text) text = replace_smartquotes(text)
@ -39,9 +42,7 @@ def nosmartquotes_argparse(args):
if text == original_text: if text == original_text:
continue continue
handle = file.open('w', encoding='utf-8') file.write('w', text, encoding='utf-8')
handle.write(text)
handle.close()
print(file.absolute_path) print(file.absolute_path)
return 0 return 0

View file

@ -49,12 +49,10 @@ def all_terms_match(search_text, terms, match_function):
def search_contents_generic(filepath, content_args): def search_contents_generic(filepath, content_args):
try: try:
with filepath.open('r') as handle: text = filepath.read('r')
text = handle.read()
except UnicodeDecodeError: except UnicodeDecodeError:
try: try:
with filepath.open('r', encoding='utf-8') as handle: text = filepath.read('r', encoding='utf-8')
text = handle.read()
except UnicodeDecodeError: except UnicodeDecodeError:
#safeprint.safeprint(filepath.absolute_path) #safeprint.safeprint(filepath.absolute_path)
#traceback.print_exc() #traceback.print_exc()

View file

@ -27,9 +27,7 @@ def loop_once(extension, regex=None):
path = pathclass.Path(passwordy.urandom_hex(12)).add_extension(extension) path = pathclass.Path(passwordy.urandom_hex(12)).add_extension(extension)
pyperclip.copy('') pyperclip.copy('')
print(path.basename, text) print(path.basename, text)
h = path.open('w', encoding='utf-8') path.write('w', text, encoding='utf-8')
h.write(text)
h.close()
def loop_forever(extension, regex): def loop_forever(extension, regex):
pyperclip.copy('') pyperclip.copy('')