# Path: utils/settings/_settings.py
# _DEFAULT=object() # unique object to compare identity with
# class EmptyMapping(Mapping):
# class ImmutableMapping(Mapping):
# class EmptySection(EmptyMapping):
# class SettingsSection(ImmutableMapping):
# class Settings(object):
#   def __init__(self,default=None):
#   def __getitem__(self,key):
#   def __len__(self):
#   def __iter__(self):
#   def __contains__(self,key):
#   def get(self, key, default=_DEFAULT):
#   def __str__(self):
#   def __repr__(self):
#   def __init__(self,_dict,default=None):
#   def __getitem__(self,key):
#   def __len__(self):
#   def __iter__(self):
#   def __contains__(self,key):
#   def get(self, key, default=_DEFAULT):
#   def __str__(self):
#   def __repr__(self):
#   def __init__(self):
#   def sections(self):
#   def getsection(self,key,**kwargs):
#   def __eq__(self,other):
#   def __str__(self):
#   def __repr__(self):
#   def __init__(self,sections,values):
#   def sections(self):
#   def getsection(self,key,**kwargs):
#   def __getattr__(self,name):
#   def __eq__(self,other):
#   def __str__(self):
#   def __repr__(self):
# def _mergesettings(parent,sectiondict,section,value):
# def _loadsettings(filename,parent=None):
# def __print(section,indent=""):
#   def __new__(cls):
#   def system(self):
#   def user(self):
#   def get(self,user):


from importlib import import_module
from urllib.parse import quote,unquote
from utils.settings import settings
import os
import time
import random
import sys
import pwd
        import subprocess as sp
""" Module to download game servers and cache and share the downloads"""

def expandcustomuser(path,user):
    if path[0:2] == "~/":
         return pwd.getpwnam(user).pw_dir + path[1:]
    return path


# NONE OF THESE PATHS SHOULD BE ON A NFS!
# If they are there may be race conditions and database corruption


RAW_USER = settings.system.downloader.get('user')
USER_SET = RAW_USER != None
USER=RAW_USER if USER_SET else pwd.getpwuid(os.getuid()).pw_name
DB_PATH=expandcustomuser(settings.get(USER_SET).downloader.get('db_path') or os.path.join(settings.get(USER_SET).getsection("core").get("alphagsm_path","~/.alphagsm"),"downloads/downloads.txt"),USER)
TARGET_PATH=expandcustomuser(settings.system.downloader.get('target_path') or os.path.join(settings.get(USER_SET).getsection("core").get("alphagsm_path","~/.alphagsm"),"downloads/downloads"),USER)

DOWNLOADERS_PACKAGE = settings.system.downloader.get('downloaders_package',"downloadermodules.")
UPDATE_SUFFIX=".new"
LOCK_SUFFIX=".lock"

LOCK_PATH = DB_PATH+LOCK_SUFFIX
UPDATE_PATH = DB_PATH+UPDATE_SUFFIX


# Path: utils/settings/_settings.py
# _DEFAULT=object() # unique object to compare identity with
# class EmptyMapping(Mapping):
# class ImmutableMapping(Mapping):
# class EmptySection(EmptyMapping):
# class SettingsSection(ImmutableMapping):
# class Settings(object):
#   def __init__(self,default=None):
#   def __getitem__(self,key):
#   def __len__(self):
#   def __iter__(self):
#   def __contains__(self,key):
#   def get(self, key, default=_DEFAULT):
#   def __str__(self):
#   def __repr__(self):
#   def __init__(self,_dict,default=None):
#   def __getitem__(self,key):
#   def __len__(self):
#   def __iter__(self):
#   def __contains__(self,key):
#   def get(self, key, default=_DEFAULT):
#   def __str__(self):
#   def __repr__(self):
#   def __init__(self):
#   def sections(self):
#   def getsection(self,key,**kwargs):
#   def __eq__(self,other):
#   def __str__(self):
#   def __repr__(self):
#   def __init__(self,sections,values):
#   def sections(self):
#   def getsection(self,key,**kwargs):
#   def __getattr__(self,name):
#   def __eq__(self,other):
#   def __str__(self):
#   def __repr__(self):
# def _mergesettings(parent,sectiondict,section,value):
# def _loadsettings(filename,parent=None):
# def __print(section,indent=""):
#   def __new__(cls):
#   def system(self):
#   def user(self):
#   def get(self,user):


# Path: downloader/downloader.py
from importlib import import_module
from urllib.parse import quote,unquote
from utils.settings import settings
import os
import time
import random
import sys
import pwd
        import subprocess as sp

""" Module to download game servers and cache and share the downloads"""

def expandcustomuser(path,user):
    if path[0:2] == "~/":
         return pwd.getpwnam(user).pw_dir + path[1:]
    return path


# NONE OF THESE PATHS SHOULD BE ON A NFS!
# If they are there may be race conditions and database corruption


RAW_USER = settings.system.downloader.get('user')
USER_SET = RAW_USER != None
USER=RAW_USER if USER_SET else pwd.getpwuid(os.getuid()).pw_name
DB_PATH=expandcustomuser(settings.get(USER_SET).downloader.get('db_path') or os.path.join(settings.get(USER_SET).getsection("core").get("alphagsm_path","~/.alphagsm"),"downloads/downloads.txt"),USER)
TARGET_PATH=expandcustomuser(settings.system.downloader.get('target_path') or os.path.join(settings.get(USER_SET).getsection("core").get("alphagsm_path","~/.alphagsm"),"downloads/downloads"),USER)

DOWNLOADERS_PACKAGE = settings.system.downloader.get('downloaders_package',"downloadermodules.")
UPDATE_SUFFIX=".new"
LOCK_SUFFIX=".lock"

LOCK_PATH = DB_PATH+LOCK_SUFFIX
UPDATE_PATH = DB_PATH+UPDATE_SUFFIX


PARENTLEN=settings.user.downloader.getsection('pathgen').get('parentlen',1)