X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Futils.py;h=19364c2b013c46279e94d6eed8b3282002e8a45d;hb=dd276546e6cb28189c9e2516624ca600b906b082;hp=6be4469ecf8b404fc8032cb3f2bddb8dcacf0250;hpb=ed02eb3de3b7de8b9555ae1a0956d349e4f15628;p=tabakrolletjie.git diff --git a/tabakrolletjie/utils.py b/tabakrolletjie/utils.py index 6be4469..19364c2 100644 --- a/tabakrolletjie/utils.py +++ b/tabakrolletjie/utils.py @@ -4,6 +4,9 @@ import functools import time import pygame.surface import pygame.locals as pgl +import os +import sys +import json from .constants import DEBUG from .loader import loader @@ -69,3 +72,33 @@ def shadowed_text(text, font_name, size, offset=4): base.blit(background, (offset, offset), None) base.blit(foreground, (0, 0), None) return base + + +def save_location(): + """Return the directory for the save location.""" + app = "tabakrolletjie" + if sys.platform.startswith('win'): + if 'APPDATA' in os.environ: + return os.path.join(os.environ['APPDATA'], app) + return os.path.join(os.path.expanduser('~'), '.' + app) + elif 'XDG_DATA_HOME' in os.environ: + return os.path.join(os.environ['XDG_DATA_HOME'], app) + return os.path.join(os.path.expanduser('~'), '.local', 'share', app) + + +def get_save_file_name(): + return os.path.join(save_location(), 'savegame.json') + + +def save_file_exists(): + savefile = get_save_file_name() + return os.path.isfile(savefile) + + +def write_save_file(json_data): + save_dir = save_location() + if not os.path.exists(save_dir): + os.makedirs(save_dir) + savefile = get_save_file_name() + with open(savefile, 'wb') as f: + json.dump(json_data, f, indent=3)