Skip to content

Commit a521d96

Browse files
committed
calculate paths relative to main.py
Avoid requiring that PWD be set to repository root. Instead, determine own directory, and calculate resource file paths relative to it.
1 parent 896ad89 commit a521d96

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

data/types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from enum import IntEnum
22
from json import load
33

4+
import paths
5+
46

57
# The possible serialisation types the game uses and their binary value (as int)
68
class Types(IntEnum):
@@ -39,19 +41,19 @@ class Types(IntEnum):
3941
# they are not complete, considering I mostly was in it for the items and perks and so on
4042
# But still they may contain spoilers considering quite a few dialogues and so on are in
4143
# them so read it at your own risk
42-
with open("./data/locals.json", encoding="utf8") as f:
44+
with open(paths.locals_json, encoding="utf8") as f:
4345
id_to_name = load(f)
4446

4547
# Generic game information
46-
with open("./data/html/items.json", encoding="utf8") as f:
48+
with open(paths.items_json, encoding="utf8") as f:
4749
gamedata = load(f)
4850

49-
with open("./data/data.json", encoding="utf8") as f:
51+
with open(paths.data_json, encoding="utf8") as f:
5052
jsongamedata = load(f)
5153

5254
# Load a list with default items, if those items have special attributes which would otherwise not work
5355
# (using the default item)
54-
with open("./data/new_item_data.json", encoding="utf8") as f:
56+
with open(paths.new_item_data_json, encoding="utf8") as f:
5557
item_fallback_data = load(f)
5658

5759
# A example item in the case of the inventory being empty and people wanting to add items to it

main.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
import psutil
2222
import pkg_resources
2323
from packaging import version
24+
import paths
25+
26+
own_dir = os.path.abspath(os.path.dirname(__file__))
2427

2528

2629
# Set up global variables and the used classes
2730
options = {}
28-
hashes = Hashlist("./data/hashes")
31+
hashes = Hashlist(paths.hashes)
2932
decoder = Decoder(hashes)
3033
encoder = Encoder()
3134
savefiles = {}
@@ -35,21 +38,21 @@
3538
loaded_items = {}
3639

3740
# Load the version number of this application and already create a newversion variable for a later check for updates
38-
with open("./data/version") as f:
41+
with open(paths.version) as f:
3942
currentversion = f.read()
4043
newversion = currentversion
4144

4245

4346
# Load the information about the item version making it possible to fix bugs without having to make a new release
4447
# (when no code was changed)
45-
with open("./data/itemversion") as f:
48+
with open(paths.item_version) as f:
4649
currentiversion = f.read()
4750
newiversion = currentiversion
4851

4952

5053
# Load the settings of the settings file
5154
def load_settings():
52-
with open("./data/settings") as f:
55+
with open(paths.settings) as f:
5356
global options, newversion, newiversion, web_app_options
5457
options = json.load(f)
5558

@@ -357,7 +360,7 @@ def save_json_savefile(data, shash):
357360
# If we want to dump it as html file we do so here
358361
if file.endswith(".html"):
359362

360-
with open('./data/html/dumpskeleton.html', 'r') as placeholderfile:
363+
with open(paths.dump_skeleton_html) as placeholderfile:
361364
placeholder = placeholderfile.read()
362365

363366
print("Creating HTML file at " + file)
@@ -1035,7 +1038,7 @@ def set_settings(settings):
10351038
if "path" in options:
10361039
options["path"] = os.path.expandvars(options["path"])
10371040

1038-
with open("./data/settings", "w") as f:
1041+
with open(paths.settings, "w") as f:
10391042
json.dump(settings, f)
10401043
return True
10411044

paths.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
3+
own_dir = os.path.abspath(os.path.dirname(__file__))
4+
data_dir = os.path.join(own_dir, 'data')
5+
html_dir = os.path.join(data_dir, 'html')
6+
7+
version = os.path.join(data_dir, 'version')
8+
item_version = os.path.join(data_dir, 'itemversion')
9+
hashes = os.path.join(data_dir, 'hashes')
10+
settings = os.path.join(data_dir, 'settings')
11+
locals_json = os.path.join(data_dir, 'locals.json')
12+
data_json = os.path.join(data_dir, 'data.json')
13+
new_item_data_json = os.path.join(data_dir, 'new_item_data.json')
14+
15+
dump_skeleton_html = os.path.join(html_dir, 'dumpskeleton.html')
16+
items_json = os.path.join(html_dir, 'items.json')

0 commit comments

Comments
 (0)