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):
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):

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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
################################################################################

View file

@ -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:

View file

@ -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

View file

@ -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()

View file

@ -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('')