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

Fix: Use with everywhere to open files #397

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
25 changes: 12 additions & 13 deletions doc/build_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@

sys.stdout = sys.__stdout__

f = open("command_reference.txt", "w")
f.write("=====================\n")
f.write("smt command reference\n")
f.write("=====================\n\n")

for mode in modes:
sio = usage[mode]
f.write(mode + '\n')
f.write('-'*len(mode) + '\n::\n\n ')
sio.seek(0)
f.write(" ".join(sio.readlines()) + '\n')
sio.close()
f.close()
with open("command_reference.txt", "w") as f:
f.write("=====================\n")
f.write("smt command reference\n")
f.write("=====================\n\n")

for mode in modes:
sio = usage[mode]
f.write(mode + '\n')
f.write('-'*len(mode) + '\n::\n\n ')
sio.seek(0)
f.write(" ".join(sio.readlines()) + '\n')
sio.close()
10 changes: 4 additions & 6 deletions sumatra/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,8 @@ def comment(argv):
args = parser.parse_args(argv)

if args.file:
f = open(args.comment, 'r')
comment = f.read()
f.close()
with open(args.comment, 'r') as f:
comment = f.read()
else:
comment = args.comment

Expand Down Expand Up @@ -686,9 +685,8 @@ def upgrade(argv):
project.record_store.clear()
filename = "%s/records_export.json" % backup_dir
if os.path.exists(filename):
f = open(filename)
project.record_store.import_(project.name, f.read())
f.close()
with open(filename) as f:
project.record_store.import_(project.name, f.read())
else:
print("Record file not found")
sys.exit(1)
Expand Down
16 changes: 7 additions & 9 deletions sumatra/datastore/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ def __init__(self, path, store, creation=None):
self.mimetype, self.encoding = mimetypes.guess_type(self.full_path)

def get_content(self, max_length=None):
f = open(self.full_path, 'rb')
if max_length:
content = f.read(max_length)
else:
content = f.read()
f.close()
with open(self.full_path, 'rb') as f:
if max_length:
content = f.read(max_length)
else:
content = f.read()
return content
content = property(fget=get_content)

Expand All @@ -52,9 +51,8 @@ def sorted_content(self):
cmd = "sort %s > %s" % (self.full_path, sorted_path)
job = Popen(cmd, shell=True)
job.wait()
f = open(sorted_path, 'rb')
content = f.read()
f.close()
with open(sorted_path, 'rb') as f:
content = f.read()
if len(content) != self.size: # sort adds a \n if the file does not end with one
assert len(content) == self.size + 1
content = content[:-1]
Expand Down
22 changes: 11 additions & 11 deletions sumatra/dependency_finder/matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def save_dependencies(cmd, filename):

def find_dependencies(filename, executable):
#ifile = os.path.join(os.getcwd(), 'depfun.data')
file_data = (open('depfun.data', 'r'))
content = file_data.read()
paths = re.split('1: ', content)[2:]
list_deps = []
for path in paths:
if os.name == 'posix':
list_data = path.split('/')
else:
list_data = path.split('\\')
list_deps.append(Dependency(list_data[-2], path.split('\n')[0]))
file_data.close() # TODO: find version of external toolboxes
with open('depfun.data', 'r') as file_data:
content = file_data.read()
paths = re.split('1: ', content)[2:]
list_deps = []
for path in paths:
if os.name == 'posix':
list_data = path.split('/')
else:
list_data = path.split('\\')
list_deps.append(Dependency(list_data[-2], path.split('\n')[0]))
# TODO: find version of external toolboxes
return list_deps
20 changes: 8 additions & 12 deletions sumatra/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ def save(self):
state[name][key] = value
else:
state[name] = attr
f = open(_get_project_file(self.path), 'w') # should check if file exists?
json.dump(state, f, indent=2)
f.close()
with open(_get_project_file(self.path), 'w') as f: # should check if file exists?
json.dump(state, f, indent=2)

def info(self):
"""Show some basic information about the project."""
Expand Down Expand Up @@ -389,9 +388,8 @@ def export(self):
# copy the project data
shutil.copy(".smt/project", ".smt/project_export.json")
# export the record data
f = open(".smt/records_export.json", 'w')
f.write(self.record_store.export(self.name))
f.close()
with open(".smt/records_export.json", 'w') as f:
f.write(self.record_store.export(self.name))

def repeat(self, original_label, new_label=None):
if original_label == 'last':
Expand Down Expand Up @@ -470,9 +468,8 @@ def remove_plugins(self, *plugins):


def _load_project_from_json(path):
f = open(_get_project_file(path), 'r')
data = json.load(f)
f.close()
with open(_get_project_file(path), 'r') as f:
data = json.load(f)
prj = Project.__new__(Project)
prj.path = path
for key, value in data.items():
Expand All @@ -498,9 +495,8 @@ def _load_project_from_json(path):

def _load_project_from_pickle(path):
# earlier versions of Sumatra saved Projects using pickle
f = open(_get_project_file(path), 'r')
prj = pickle.load(f)
f.close()
with open(_get_project_file(path), 'r') as f:
prj = pickle.load(f)
return prj


Expand Down
24 changes: 11 additions & 13 deletions tools/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,17 @@ def export_records(output_file):
store = load_recordstore()
if minor_version < 3:
patch_sumatra()
f = open(output_file, 'w')
if minor_version == 1:
json.dump([encode_record(record) for record in store.list(groups=None)],
f, indent=2)
else:
project_name = projects.load_project().name
if minor_version == 2:
json.dump([encode_record(record) for record in store.list(project_name)],
with open(output_file, 'w') as f:
if minor_version == 1:
json.dump([encode_record(record) for record in store.list(groups=None)],
f, indent=2)
else:
f.write(store.export(project_name))
f.close()
project_name = projects.load_project().name
if minor_version == 2:
json.dump([encode_record(record) for record in store.list(project_name)],
f, indent=2)
else:
f.write(store.export(project_name))


def patch_sumatra():
Expand Down Expand Up @@ -172,9 +171,8 @@ def export_project(output_file):
state['record_store']['db_file'] = ".smt/records" #prj.record_store._db_file
else:
state['record_store']['shelf_name'] = ".smt/records" #prj.record_store._shelf_name
f = open(output_file, 'w')
json.dump(state, f, indent=2)
f.close()
with open(output_file, 'w') as f:
json.dump(state, f, indent=2)


if __name__ == "__main__":
Expand Down