Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use context manager to open files #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions grc
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ if cfile == "":
for conffile in conffilenames:
# test if conffile exists, it can be also a pipe
if os.path.exists(conffile) and not os.path.isdir(conffile):
f = open(conffile, "r")
while 1:
l = f.readline()
if l == "":
break
if l[0] == "#" or l[0] == '\012':
continue
regexp = l.strip()
if re.search(regexp, ' '.join(args)):
cfile = f.readline().strip()
break
with open(conffile, "r", encoding='UTF-8') as f:
while 1:
l = f.readline()
if l == "":
break
if l[0] == "#" or l[0] == '\012':
continue
regexp = l.strip()
if re.search(regexp, ' '.join(args)):
cfile = f.readline().strip()
break


signal.signal(signal.SIGINT, catch_signal)
Expand Down
49 changes: 25 additions & 24 deletions grcat
Original file line number Diff line number Diff line change
Expand Up @@ -147,36 +147,37 @@ if not conffile:

regexplist = []

f = open(conffile, "r")
is_last = 0
split = str.split
lower = str.lower
letters = string.ascii_letters
while not is_last:
ll = {'count':"more"}
while 1:
l = f.readline()
if l == "":
is_last = 1
break
if l[0] == "#" or l[0] == '\012':
continue
if not l[0] in letters:
break
fields = split(l.rstrip('\r\n'), "=", 1)
if len(fields) != 2:
sys.stderr.write('Error in configuration, I expect keyword=value line\n')
sys.stderr.write('But I got instead:\n')
sys.stderr.write(repr(l))
sys.stderr.write('\n')
sys.exit(1)
keyword, value = fields
keyword = lower(keyword)
if keyword in ('colors', 'colour', 'color'):
keyword = 'colours'
if not keyword in ["regexp", "colours", "count", "command", "skip", "replace", "concat"]:
raise ValueError("Invalid keyword")
ll[keyword] = value

with open(conffile, "r", encoding='UTF-8') as f:
while 1:
l = f.readline()
if l == "":
is_last = 1
break
if l[0] == "#" or l[0] == '\012':
continue
if not l[0] in letters:
break
fields = split(l.rstrip('\r\n'), "=", 1)
if len(fields) != 2:
sys.stderr.write('Error in configuration, I expect keyword=value line\n')
sys.stderr.write('But I got instead:\n')
sys.stderr.write(repr(l))
sys.stderr.write('\n')
sys.exit(1)
keyword, value = fields
keyword = lower(keyword)
if keyword in ('colors', 'colour', 'color'):
keyword = 'colours'
if not keyword in ["regexp", "colours", "count", "command", "skip", "replace", "concat"]:
raise ValueError("Invalid keyword")
ll[keyword] = value

# Split string into one string per regex group
# e.g. split "brown bold, red" into "brown bold" and
Expand Down