Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/setup/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/setup/config.py')
-rw-r--r--src/setup/config.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/setup/config.py b/src/setup/config.py
new file mode 100644
index 0000000..696c039
--- /dev/null
+++ b/src/setup/config.py
@@ -0,0 +1,122 @@
+'''
+
+'''
+
+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(path)
+ levels.append(level)
+ return files
+
+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(file)
+ profiles.append(profile)
+ return profiles
+
+# -----------------------------------------------------------------------------
+# 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 dir in folders:
+ contents = os.listdir(dir)
+ 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
+
+# -----------------------------------------------------------------------------
+# 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):
+ ''' '''
+ # TODO fill in this stub
+ pass