From 61a48bd8b6c6952ade89b03fbdeb0bf0c18d8d6c Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Wed, 11 Mar 2020 18:16:55 -0400 Subject: [PATCH] add conf_dir function --- IzzyLib/misc.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/IzzyLib/misc.py b/IzzyLib/misc.py index 2b2c50d..2f97d49 100644 --- a/IzzyLib/misc.py +++ b/IzzyLib/misc.py @@ -1,7 +1,9 @@ '''Miscellaneous functions''' -import random, string +import random, string, sys, os +from os import environ as env from datetime import datetime +from os.path import abspath, dirname, basename, isdir, isfile from . import logging @@ -41,6 +43,26 @@ def formatUTC(timestamp=None): return date.strftime('%a, %d %b %Y %H:%M:%S GMT') +def config_dir(modpath=None): + if env.get('CONFDIR'): + '''set the storage path to the environment variable if it exists''' + stor_path = abspath(env['CONFDIR']) + + else: + stor_path = f'{os.getcwd()}' + + if modpath and not env.get('CONFDIR'): + modname = basename(dirname(modpath)) + + if isdir(f'{stor_path}/{modname}'): + '''set the storage path to CWD/data if the module or script is in the working dir''' + stor_path += '/data' + + if not isdir (stor_path): + os.makedirs(stor_path, exist_ok=True) + + return stor_path + def merp(): log = logging.getLogger('merp-heck', {'level': 'merp', 'date': False}) log.merp('heck')