diff options
Diffstat (limited to 'src/setup/config.py')
-rw-r--r-- | src/setup/config.py | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/setup/config.py b/src/setup/config.py new file mode 100644 index 0000000..c844781 --- /dev/null +++ b/src/setup/config.py @@ -0,0 +1,148 @@ +''' + +''' + +import os +from lxml import etree # using lxml for XML processing +from utils import strs # string constants +from utils import utils # some XML utilities +from profile import Profile +from level import Level + +# setup logging +import logging +logging.basicConfig(filename=strs.LoggingFile,level=logging.DEBUG) +log = logging.getLogger('setup.config') + + +# ----------------------------------------------------------------------------- +# Some Getters +# ----------------------------------------------------------------------------- + +# TODO refactor to remove duplicated code logic: should have the two getXXX() going to findFiles() -> findFolders() -> find() + +def getLevelDescriptors(): + '''Returns a list of Levels representing all known levels''' + # get all the level descriptor files + files = findLevelFiles() + + # convert them all into objects + levels = [] + for file in files: + level = Level(file) + levels.append(level) + return levels + +def getProfiles(): + '''Returns a list of Profiles representing all known profiles''' + # get all the profile files + files = findProfileFiles() + + # convert the files into Profile objects + profiles = [] + for file in files: + profile = Profile(path=file) + profiles.append(profile) + return profiles + +def getProfile(name): + '''Returns the player profile with the given name. If such a profile doesn't exist, one is created''' + + # search through the know profiles for the one with the given name + profiles = getProfiles() + for profile in profiles: + log.debug('Looking for a profile named: ' + name + ' and found: ' + profile.getName()) + if profile.getName() == name: + return profile + + # the wanted profile doesn't exist, so create a new one + return createProfile(name) + +# ----------------------------------------------------------------------------- +# File related methods +# ----------------------------------------------------------------------------- + +def getLastProfileName(): + '''Returns the name of the last loaded profile''' + name = find(strs.XP_LastProfileName)[0] + return name + +def findLevelFiles(): + '''Returns the list of found level descriptor files''' + folders = findLevelFolders() + files = findFiles(folders, strs.Ext_Level) + log.debug('Found ' + str(len(files)) + ' level files') + return files + +def findProfileFiles(): + '''Returns the list of found profile files''' + folders = findProfileFolders() + files = findFiles(folders, strs.Ext_Profile) + log.debug('Found ' + str(len(files)) + ' profile files') + return files + +def findFiles(folders, extension): + '''Returns a list of all files with the given extension directly inside the given folders''' + files = [] + + #for each given folder, + for dir in folders: + contents = os.listdir(dir) + + #for each directory in the folder, + for item in contents: + item = dir + '/' + item # must convert the file/folder name into its complete path + + # only want files with the given extension + if not os.path.isfile(item): + continue + if not item.endswith(extension): + continue + + files.append(item) + return files + +# ----------------------------------------------------------------------------- +# +# ----------------------------------------------------------------------------- + +def createProfile(profileName): + '''Creates and returns a new profile with the given profileName. This profile has not yet been saved to the disk''' + + log.debug('Creating a Profile with the name: ' + profileName) + return Profile(name=profileName) + +# ----------------------------------------------------------------------------- +# Folder related methods +# ----------------------------------------------------------------------------- + +def findLevelFolders(): + '''Returns the list of known level folders''' + folders = find(strs.XP_LevelFolderLocation) + log.debug('Found ' + str(len(folders)) + ' level folders') + return folders + pass + +def findProfileFolders(): + '''Returns the list of known profile folders''' + folders = find(strs.XP_ProfileFolderLocation) + log.debug('Found ' + str(len(folders)) + ' profile folders') + return folders + +# ----------------------------------------------------------------------------- +# General methods +# ----------------------------------------------------------------------------- + +def find(xpath): + '''Returns the result of applying the given xpath onto the config file''' + config = utils.getXMLRoot(strs.ConfigFile) + return config.xpath(xpath) + +# ----------------------------------------------------------------------------- +# Some Setters +# ----------------------------------------------------------------------------- + +def setLastProfileName(name): + '''Stores the name of the last used profile. This is so on next startup, the last used profile can start as the default player''' + # TODO fill in this stub + pass |