/
init.py
142 lines (119 loc) · 3.96 KB
/
init.py
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
import os, sys, pygame, menu, db, joystick, shutil
from pygame.locals import *
pygame.display.init()
pygame.font.init()
pygame.mouse.set_visible(False)
installDir = os.path.dirname(os.path.abspath(__file__))
def requireFile(fileName):
"""look in the current dir first, then the home dir
"""
try:
f = open(os.path.join( installDir, fileName), 'r')
except:
print "no local file ", fileName
pass
try:
os.chdir(os.path.expanduser("~/.nesmenu"))
except:
try:
os.makedirs(os.path.expanduser("~/.nesmenu"))
except:
print "Could not create directory", os.path.expanduser("~/.nesmenu"), "aborting..."
sys.exit()
finally:
try:
f = open(os.path.expanduser("~/.nesmenu/") + fileName, 'r')
except:
try:
shutil.copy(os.path.join(installDir,fileName), os.path.expanduser("~/.nesmenu/"))
except:
print "Could not create file", os.path.expanduser("~/.nesmenu/") + fileName, "aborting..."
sys.exit()
else:
print "Copied default file", fileName, "to", os.path.expanduser("~/.nesmenu")
f = open(os.path.expanduser("~/.nesmenu/") + fileName, 'r')
finally:
return f
def readConfig():
localConfig = {}
configFile = requireFile("config")
configLines = configFile.readlines()
for x in configLines:
splitted = x.split(": ")
data = eval(splitted[1])
localConfig[splitted[0]] = data
return localConfig
def getMenu():
#print db.list
menuFile = requireFile("menu.conf")
menuLines = menuFile.readlines()
menu.Menu.path.append(menuParse(menuLines))
def menuParse(lines):
line = lines[0].split(": ")
del(lines[0])
index = 0
if not line[0].strip() == "menu":
print "Error parsing menu file. Aborting..."
sys.exit()
returnMenu = menu.Menu(line[-1].strip())
line = lines[0].split(": ")
while line[0].strip() != ".menu" and len(lines):
if line[0].strip() == "menu":
returnMenu[index] = menuParse(lines)
while lines[0].strip() != ".menu":
del(lines[0])
if not len(lines):
return returnMenu
elif line[0].strip() == "<db>":
for x in db.list:
returnMenu[index] = x
index += 1
del(lines[0])
line = lines[0].split(": ")
continue
elif line[0].strip() == "func":
returnMenu[index] = menu.Func(line[1].strip(), line[2].strip())
elif line[0].strip() == "exec":
returnMenu[index] = menu.Exe(line[1].strip(), line[2].strip())
del(lines[0])
line = lines[0].split(": ")
index += 1
return returnMenu
def exit(message = ""):
print message
db.save()
sys.exit()
def mergeFlags(c):
for x in sys.argv:
if (x.find('--', 0, 2) == -1):
continue
k,junk,v = x.replace('--', '').partition('=')
c[k] = v
def checkConfig(c):
return 1
try:
if (c['warnMissingJs'] == 1 and not "js0" in os.listdir("/dev/input")):
print "Warning: No joystick connected"
except KeyError:
pass
def startDisplay():
global config
displayFlags = 0
if 'useFullscreen' not in config or config['useFullscreen'] == 0:
displayFlags = 0
displaySize = (640, 480)
else:
displayFlags |= pygame.FULLSCREEN
displaySize = (0, 0)
config['screen'] = pygame.display.set_mode( displaySize , displayFlags )
config = readConfig()
mergeFlags(config)
checkConfig(config)
os.chdir(installDir)
startDisplay()
config['itemsPerPage'] = int( ( config['screen'].get_size()[1] - 3.5 * config['textSize'] ) / ( config['textSize'] + config['linespace'] ) ) - 1
config['font'] = pygame.font.Font(config['font'], config['textSize'])
joystick.display("Initializing...")
db.get()
getMenu()
menu.Menu.path[-1].enter()