add conf_dir function

This commit is contained in:
Izalia Mae 2020-03-11 18:16:55 -04:00
parent 1314693ab0
commit 61a48bd8b6

View file

@ -1,7 +1,9 @@
'''Miscellaneous functions''' '''Miscellaneous functions'''
import random, string import random, string, sys, os
from os import environ as env
from datetime import datetime from datetime import datetime
from os.path import abspath, dirname, basename, isdir, isfile
from . import logging from . import logging
@ -41,6 +43,26 @@ def formatUTC(timestamp=None):
return date.strftime('%a, %d %b %Y %H:%M:%S GMT') 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(): def merp():
log = logging.getLogger('merp-heck', {'level': 'merp', 'date': False}) log = logging.getLogger('merp-heck', {'level': 'merp', 'date': False})
log.merp('heck') log.merp('heck')