Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/setup/config.py
blob: 696c0397d113c04a9c7349b9579ff07e8b10c81a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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