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 Jan 6, 2022
1 parent 896ad89 commit 25d385f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import pkg_resources
from packaging import version

own_dir = os.path.abspath(os.path.dirname(__file__))


# Set up global variables and the used classes
options = {}
hashes = Hashlist("./data/hashes")
hashes = Hashlist(os.path.join(own_dir, "data/hashes"))
decoder = Decoder(hashes)
encoder = Encoder()
savefiles = {}
Expand All @@ -35,21 +37,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(os.path.join(own_dir, "data/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(os.path.join(own_dir, "data/itemversion")) 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(os.path.join("data/settings")) as f:
global options, newversion, newiversion, web_app_options
options = json.load(f)

Expand Down Expand Up @@ -357,7 +359,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(os.path.join(own_dir, 'data/html/dumpskeleton.html'), 'r') as placeholderfile:
placeholder = placeholderfile.read()

print("Creating HTML file at " + file)
Expand Down Expand Up @@ -1035,7 +1037,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(os.path.join(own_dir, "data/settings"), "w") as f:
json.dump(settings, f)
return True

Expand Down

0 comments on commit 25d385f

Please sign in to comment.