commit 5fda43d1e5860a4dd908cf68416b5e1f391b8edb
parent 357af45a114e815aab6deac73ca83902dd0ce1d5
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Fri, 27 Nov 2020 18:04:08 -0600
Implement context manager for file opening
Diffstat:
6 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/src/constants.py b/src/constants.py
@@ -53,7 +53,7 @@ AFFINITY_RELATIONS = {
}
# Item constants
-ITEMBASE = json.load(open(os.path.join(JSON_PATH, "items.json")))
+with open(os.path.join(JSON_PATH, "items.json")) as f: ITEMBASE = json.load(f)
WEAPON_TYPE_SOURCES = {
"Dagger" : "ATK",
"Sword" : "ATK",
diff --git a/src/images.py b/src/images.py
@@ -51,7 +51,7 @@ class SheetManager(manager.Manager):
"""
Load sheets from the given definition JSON file.
"""
- self.sheets_def = json.load(open(os.path.join(JSON_PATH, sheet_json)))
+ with open(os.path.join(JSON_PATH, sheet_json)) as df: self.sheets_def = json.load(df)
for j in self.sheets_def:
self.loaded_sheets[j] = Sheet(self, self.sheets_def[j]["filename"], j, tuple(self.sheets_def[j]["dimensions"]), self.sheets_def[j]["total_sprites"])
@@ -59,7 +59,7 @@ class SheetManager(manager.Manager):
"""
Load animations from the given definition JSON file.
"""
- self.animations = json.load(open(os.path.join(JSON_PATH, anim_json)))
+ with open(os.path.join(JSON_PATH, anim_json)) as anf: self.animations = json.load(anf)
# Make sure the sheet subimage coords are in tuple form
for c in self.animations:
diff --git a/src/menu.py b/src/menu.py
@@ -82,7 +82,7 @@ class Menu(object):
self.menu_file = menufile
self.button_group = pygame.sprite.LayeredDirty()
self.entities_group = pygame.sprite.LayeredDirty()
- self.definition = json.load(open(os.path.join(MENU_JSON_PATH, menufile)))
+ with open(os.path.join(MENU_JSON_PATH, menufile)) as df: self.definition = json.load(df)
# Load/setup menu
self.load_menu()
diff --git a/src/piece.py b/src/piece.py
@@ -99,7 +99,7 @@ class PieceManager(manager.Manager):
foldname = filename[::-4]
# Next, load up the JSON file
- jsondef = json.load(open(os.path.join(BOARD_PATH, foldname, filename)))
+ with open(os.path.join(BOARD_PATH, foldname, filename)) as jf: jsondef = json.load(jf)
# Now, create the full definition given the JSON and the character database
for p in jsondef:
diff --git a/src/scene.py b/src/scene.py
@@ -129,7 +129,7 @@ class StillScene(object):
self.continue_prompt.set_position(self.continue_prompt_pos)
# Load from scene definition JSON
- scenedef = json.load(open(os.path.join(SCENE_JSON_PATH, scenefile)))
+ with open(os.path.join(SCENE_JSON_PATH, scenefile)) as df: scenedef = json.load(df)
self.name = scenedef["name"]
self.background = entity.Entity(self.manager.bus.fetch("sheet_manager", "sheets")[scenedef["bg_sheet"]], tuple(scenedef["bg_sprite"]))
diff --git a/src/sound.py b/src/sound.py
@@ -41,8 +41,7 @@ class SoundManager(manager.Manager):
Load up sounds from a definition JSON
file.
"""
- with open(os.path.join(JSON_PATH, soundjson)) as sj:
- j = json.load(sj)
+ with open(os.path.join(JSON_PATH, soundjson)) as sj: j = json.load(sj)
for s in j["sounds"]:
self.sounds[s["name"]] = pygame.mixer.Sound(os.path.join(SOUND_PATH, s["filename"]))