Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/setup/config.py
blob: c844781536a3380ce0e23dd16421db1a13f161d2 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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