root/src/fileio.py @ 616

Revision 616, 3.3 KB (checked in by jelinson, 2 years ago)

renamed scene image directory

Line 
1'''
2Created on Feb 13, 2012
3
4Contains paths to specific image directories
5
6@author: TechTrek
7'''
8
9import os
10import sys
11import cPickle
12import logging
13
14import pygame
15
16__all__ = ["resourceDir", "imageDir", "mapDir", "landscapeDir", "sequenceDir",
17           "iconDir", "dataDir", "sceneDir", "saveDir", "saveExt",
18           "buildingDir", "buildExt", "arrowsDir", "buttonDir", "imgExt",
19           "mapDataDir", "specDir", "specExt", "tutorialDir",
20           "heurDir", "heurExt", "txtExt"]
21
22
23root = os.path.normpath(os.path.abspath(__file__) + os.sep + ".." + \
24                        os.sep + "..")
25
26resourceDir = root + os.sep + "resources" + os.sep
27
28imageDir = resourceDir + "images" + os.sep
29dataDir = resourceDir + "data" + os.sep
30
31arrowsDir = imageDir + "arrows" + os.sep
32buttonDir = imageDir + "new_layout" + os.sep
33sequenceDir = imageDir + "scenes" + os.sep
34iconDir = imageDir + "menu_icons" + os.sep
35
36mapDir = imageDir + "map" + os.sep
37landscapeDir = mapDir + "landscapes" + os.sep
38
39mapDataDir = dataDir + "landscapes" + os.sep
40sceneDir = dataDir + "scenes" + os.sep
41saveDir = dataDir + "saves" + os.sep
42buildingDir = dataDir + "buildings" + os.sep
43tutorialDir = dataDir + "tutorial" + os.sep
44fontDir = dataDir + "font" + os.sep
45
46analysisDir = dataDir + "analysis" + os.sep
47specDir = analysisDir + "spec" + os.sep
48heurDir = analysisDir + "state" + os.sep
49
50saveExt = ".trek"
51buildExt = ".bld"
52imgExt = ".png"
53specExt = ".spec"
54heurExt = ".heur"
55txtExt = ".txt"
56
57# initialize logger
58log = logging.getLogger("TechTrek")
59log.setLevel(logging.ERROR)
60fileHandler = logging.FileHandler("TechTrek.log")
61output = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
62format = logging.Formatter(output)
63fileHandler.setFormatter(format)
64fileHandler.setLevel(logging.ERROR)
65log.addHandler(fileHandler)
66
67
68def save(object, fileName, benign=True):
69    try:
70        file = open(fileName, "wb")
71        log.error("opening %s" % fileName)
72    except IOError:
73        if benign:
74            log.error("Could not save file %s; proceeding" % fileName)
75            return False
76        else:
77            log.error("Could not save file %s; quiting" % fileName)
78            pygame.quit()
79            sys.exit()
80    else:
81        cPickle.dump(object, file)
82        file.close()
83        return True
84
85
86def load(fileName, benign=False):
87    """
88        generic object loader
89
90        input type: str, str, str
91        return type: object
92    """
93    try:
94        data = open(fileName)
95    except IOError:
96        if benign:
97            log.error("Could not load file %s; proceeding" % fileName)
98            return None
99        else:
100            log.error("Could not load file %s; quiting" % fileName)
101            pygame.quit()
102            sys.exit()
103    else:
104        object = cPickle.load(data)
105        data.close()
106        return object
107
108
109def loadText(fileName, benign=True):
110    """
111        generic text file loader; note that it is the caller's
112        responsibility to close the file once it has been read
113
114        input type: str
115        return type: object
116    """
117    try:
118        data = open(fileName)
119    except IOError:
120        if not benign:
121            log.error("Could not open %s; quitting" % fileName)
122            pygame.quit()
123            sys.exit()
124        else:
125            log.error("Could not open %s; proceeding anyway" % fileName)
126            return None
127    else:
128        return data
Note: See TracBrowser for help on using the browser.