Skip to content

Commit

Permalink
calculate paths relative to main.py
Browse files Browse the repository at this point in the history
Avoid requiring that PWD be set to repository root. Instead, determine
own directory, and calculate resource file paths relative to it.
  • Loading branch information
dr1fter committed Mar 30, 2022
1 parent 896ad89 commit 0cff4ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
10 changes: 6 additions & 4 deletions data/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from enum import IntEnum
from json import load

import paths


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

# Generic game information
with open("./data/html/items.json", encoding="utf8") as f:
with open(paths.items_json, encoding="utf8") as f:
gamedata = load(f)

with open("./data/data.json", encoding="utf8") as f:
with open(paths.data_json, encoding="utf8") as f:
jsongamedata = load(f)

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

# A example item in the case of the inventory being empty and people wanting to add items to it
Expand Down
13 changes: 7 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import psutil
import pkg_resources
from packaging import version
import paths


# Set up global variables and the used classes
options = {}
hashes = Hashlist("./data/hashes")
hashes = Hashlist(paths.hashes)
decoder = Decoder(hashes)
encoder = Encoder()
savefiles = {}
Expand All @@ -35,21 +36,21 @@
loaded_items = {}

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


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


# Load the settings of the settings file
def load_settings():
with open("./data/settings") as f:
with open(paths.settings) as f:
global options, newversion, newiversion, web_app_options
options = json.load(f)

Expand Down Expand Up @@ -357,7 +358,7 @@ def save_json_savefile(data, shash):
# If we want to dump it as html file we do so here
if file.endswith(".html"):

with open('./data/html/dumpskeleton.html', 'r') as placeholderfile:
with open(paths.dump_skeleton_html) as placeholderfile:
placeholder = placeholderfile.read()

print("Creating HTML file at " + file)
Expand Down Expand Up @@ -1035,7 +1036,7 @@ def set_settings(settings):
if "path" in options:
options["path"] = os.path.expandvars(options["path"])

with open("./data/settings", "w") as f:
with open(paths.settings, "w") as f:
json.dump(settings, f)
return True

Expand Down
16 changes: 16 additions & 0 deletions paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

own_dir = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(own_dir, 'data')
html_dir = os.path.join(data_dir, 'html')

version = os.path.join(data_dir, 'version')
item_version = os.path.join(data_dir, 'itemversion')
hashes = os.path.join(data_dir, 'hashes')
settings = os.path.join(data_dir, 'settings')
locals_json = os.path.join(data_dir, 'locals.json')
data_json = os.path.join(data_dir, 'data.json')
new_item_data_json = os.path.join(data_dir, 'new_item_data.json')

dump_skeleton_html = os.path.join(html_dir, 'dumpskeleton.html')
items_json = os.path.join(html_dir, 'items.json')

0 comments on commit 0cff4ae

Please sign in to comment.