Major code cleanup

This commit is contained in:
punkrockguy318 2008-06-17 15:46:41 +00:00
parent e6cc29b7d2
commit cff7eb225a
1 changed files with 207 additions and 202 deletions

321
gfceu
View File

@ -31,67 +31,30 @@ import shutil
from optparse import OptionParser from optparse import OptionParser
from subprocess import Popen from subprocess import Popen
# # # # # # # #
# Messaging Functions
def gfceu_print(text, use_gtk=False):
"""
gfceu_print()
This function prints messages to the user. This is generally used for status
messages. If a GTK message_box is requried, the use_gtk flag can be enabled.
"""
print text
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(text)
msgbox.run()
msgbox.destroy()
def gfceu_error(message, code, use_gtk=True, fatal=True):
"""
gfceu_error()
Presents the user with an error message and optionally quits the program.
"""
print title + ' error code '+str(code)+': ' + message
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(title + ' ERROR Code '+str(code)+':\n'+message)
msgbox.run()
msgbox.destroy()
if fatal:
sys.exit(code)
# # # # # # # #
# Import libraries
try:
import pytgtk
pygtk.require("2.12")
except:
pass
try: try:
import pygtk
pygtk.require("2.0")
import gtk import gtk
except ImportError: except ImportError:
gfceu_error('The PyGTK libraries cannot be found.\n\ print "The PyGTK libraries cannot be found.\n\
Ensure that PyGTK (>=2.12) is installed on this system.\n\ Ensure that PyGTK (>=2.12) is installed on this system.\n\
On Debian based systems (like Ubuntu), try this command:\n\ On Debian based systems (like Ubuntu), try this command:\n\
sudo apt-get install python-gtk2 libgtk2.0-0', 1, False) sudo apt-get install python-gtk2 libgtk2.0-0"
try: try:
import gnomevfs import gnomevfs
have_gnomevfs = True have_gnomevfs = True
except ImportError: except ImportError:
gfceu_error('The gnomevfs libraries cannot be found.\n\ print "The gnomevfs libraries cannot be found.\n\
To enable ROM loading over the network, ensure that gnomevfs is installed on\ To enable ROM loading over the network, ensure that gnomevfs is installed on\
this system.\n\ this system.\n\
On Debian based systems (like Ubuntu), try this command:\n\ On Debian based systems (like Ubuntu), try this command:\n\
sudo apt-get install python-gtk2 libgnomevfs2-0', 5, False, False) sudo apt-get install python-gtk2 libgnomevfs2-0"
have_gnomevfs = False have_gnomevfs = False
# # # # # # # # # # # # # # # #
# GFCEU Functions # GFCEU Functions
@ -145,7 +108,7 @@ def give_widgets():
This function takes data from the options struct and relays it to This function takes data from the options struct and relays it to
the GTK window the GTK window
""" """
global xml, options global options, widgets
try: try:
widgets.get_object("rom_entry").set_text(options.romfile) widgets.get_object("rom_entry").set_text(options.romfile)
@ -173,27 +136,11 @@ def give_widgets():
widgets.get_object("host_port").set_value(float(options.host_port)) widgets.get_object("host_port").set_value(float(options.host_port))
widgets.get_object("host_pass").set_text(options.host_pass) widgets.get_object("host_pass").set_text(options.host_pass)
except: except AttributeError:
# When new widgets are added, old pickle files might break. # When new widgets are added, old pickle files might break.
options = game_options() options = game_options()
give_widgets() give_widgets()
def setup_environment ():
"""
Configures the environment if this is the first time the application
has been run. For instance, it checks for the options file and creates
it if it doesn't exist. It also converts between the old version and
the new version of this application, which stores the options file in
a separate directory.
"""
global appconfigdir, optionsfile
if not os.path.exists(appconfigdir):
# this is the first time the application is run.
# create the directory
gfceu_print("Creating application settings directory")
os.mkdir(appconfigdir)
def set_options(): def set_options():
""" """
@ -202,7 +149,6 @@ def set_options():
This function grabs all of the data from the GTK widgets This function grabs all of the data from the GTK widgets
and stores it in the options object. and stores it in the options object.
""" """
global xml
options.romfile = widgets.get_object("rom_entry").get_text() options.romfile = widgets.get_object("rom_entry").get_text()
# sound # sound
@ -229,8 +175,140 @@ def set_options():
def launch(rom_name, local=False):
global xml, options, fceu_server_binary, fceux_binary
def find_binary(file):
path = os.getenv('PATH')
directories= []
directory = ''
# check for '$' so last entry is processed
for x in path + '$':
if x != ':' and x != '$':
directory = directory + x
else:
directories.append(directory)
directory = ''
for x in directories:
if os.path.isfile(os.path.join(x, file)):
return os.path.join(x,file)
if os.path.isfile(os.path.join(os.curdir,file)):
return os.path.join(os.curdir, file)
return None
##############################################################################
# Globals
options = None
appconfigdir = os.getenv('HOME') + '/.'+ title
optionsfile = appconfigdir + 'gfceu_options.dat'
widgets = None
class GfceuApp:
def __init__(self):
self.fceux_binary = self.find_fceu()
self.load_ui()
self.setup_environment()
options = game_options()
load_options()
give_widgets()
try:
gtk.main()
except KeyboardInterrupt:
sys.exit(0)
def msg(self, text, use_gtk=False):
"""
GfceuApp.msg()
This function prints messages to the user. This is generally used for status
messages. If a GTK message_box is requried, the use_gtk flag can be enabled.
"""
print text
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(text)
msgbox.run()
msgbox.destroy()
def print_error(self, message, code, use_gtk=True, fatal=True):
"""
GfceuApp.error()
Presents the user with an error message and optionally quits the program.
"""
print title + ' error code '+str(code)+': ' + message
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(title + ' ERROR Code '+str(code)+':\n'+message)
msgbox.run()
msgbox.destroy()
if fatal:
sys.exit(code)
def setup_environment (self):
"""
Configures the environment if this is the first time the application
has been run. For instance, it checks for the options file and creates
it if it doesn't exist. It also converts between the old version and
the new version of this application, which stores the options file in
a separate directory.
"""
global appconfigdir, optionsfile
if not os.path.exists(appconfigdir):
# this is the first time the application is run.
# create the directory
self.msg("Creating application settings directory")
os.mkdir(appconfigdir)
def find_fceu(self):
bin = find_binary('fceux')
if bin == None:
gfceu_error('Could not find the fceu binary.\n\
Ensure that FCE Ultra is installed and in the $PATH.\n\
On Debian based systems (like Ubuntu), try the following command:\n\
sudo apt-get install fceu', 4, True)
else:
self.msg('Using: ' + bin)
return bin
def load_ui(self):
global widgets
""" Search for the glade XML file and load it """
# Check first in the directory of this script.
if os.path.isfile('gfceu.xml'):
glade_file = 'gfceu.xml'
# Then check to see if its installed on a *nix system
elif os.path.isfile(os.path.join(os.path.dirname(sys.argv[0]), '../share/gfceu/gfceu.xml')):
glade_file = os.path.join(os.path.dirname(sys.argv[0]), '../share/gfceu/gfceu.xml')
else:
print 'ERROR.'
print 'Could not find the glade interface file.'
print 'Try reinstalling the application.'
sys.exit(1)
try:
print "Using: " + glade_file
widgets = gtk.Builder()
widgets.add_from_file(glade_file)
widgets.connect_signals(self)
except:
self.print_error("Couldn't load the glade UI file", 24)
widgets.get_object("main_window").show_all()
def launch(self, rom_name, local=False):
global options
set_options() set_options()
sound_options = '' sound_options = ''
@ -292,9 +370,9 @@ def launch(rom_name, local=False):
""" """
network = '' network = ''
command = fceux_binary + ' ' + sound_options + video_options +\ command = self.fceux_binary + ' ' + sound_options + video_options +\
network + options.extra_entry + ' '+ rom_name network + options.extra_entry + ' '+ rom_name
gfceu_print('Command: ' + command) self.msg('Command: ' + command)
# more code to disable because netplay is fucked # more code to disable because netplay is fucked
""" """
@ -303,9 +381,9 @@ def launch(rom_name, local=False):
if xterm_binary == None: if xterm_binary == None:
gfceu_error("Cannot find xterm on this system. You will not \n\ gfceu_error("Cannot find xterm on this system. You will not \n\
be informed of server output.", 102, True, False) be informed of server output.", 102, True, False)
args = [fceu_server_binary] args = [self.server_binary]
else: else:
args = [xterm_binary, "-e", fceu_server_binary] args = [xterm_binary, "-e", self.server_binary]
args.append('--port') args.append('--port')
args.append(str(options.host_port)) args.append(str(options.host_port))
if options.host_pass: if options.host_pass:
@ -329,67 +407,13 @@ def launch(rom_name, local=False):
os.kill(pid, 9) os.kill(pid, 9)
""" """
### Callbacks
def find_binary(file):
path = os.getenv('PATH')
directories= []
directory = ''
# check for '$' so last entry is processed
for x in path + '$':
if x != ':' and x != '$':
directory = directory + x
else:
directories.append(directory)
directory = ''
for x in directories:
if os.path.isfile(os.path.join(x, file)):
return os.path.join(x,file)
if os.path.isfile(os.path.join(os.curdir,file)):
return os.path.join(os.curdir, file)
return None
##############################################################################
# Globals
options = None
appconfigdir = os.getenv('HOME') + '/.'+ title
optionsfile = appconfigdir + 'gfceu_options.dat'
fceux_binary = None
fceu_server_binary = None
class WidgetsWrapper:
def __init__(self):
# Search for the glade file
# Check first in the directory of this script.
global widgets
if os.path.isfile('gfceu.xml'):
glade_file = 'gfceu.xml'
# Then check to see if its installed on a *nix system
elif os.path.isfile(os.path.join(os.path.dirname(sys.argv[0]), '../share/gfceu/gfceu.xml')):
glade_file = os.path.join(os.path.dirname(sys.argv[0]), '../share/gfceu/gfceu.xml')
else:
print 'ERROR.'
print 'Could not find the glade interface file.'
print 'Try reinstalling the application.'
sys.exit(1)
try:
print "Using: " + glade_file
widgets = gtk.Builder()
widgets.add_from_file(glade_file)
widgets.connect_signals(self)
except:
gfceu_error("Couldn't load the glade UI file", 24)
def launch_button_clicked(self, arg1): def launch_button_clicked(self, arg1):
global xml
global options global options
options.romfile = widgets.get_object("rom_entry").get_text() options.romfile = widgets.get_object("rom_entry").get_text()
if widgets.get_object("rom_entry").get_text() == '': if widgets.get_object("rom_entry").get_text() == '':
gfceu_print('Please specify a ROM to open in the main tab.', True) self.msg('Please specify a ROM to open in the main tab.', True)
return return
if options.network_rom: if options.network_rom:
try: try:
@ -418,16 +442,15 @@ class WidgetsWrapper:
else: else:
romfile = options.romfile romfile = options.romfile
launch('"'+romfile+'"') self.launch('"'+romfile+'"')
def about_button_clicked(self, menuitem, data=None): def about_button_clicked(self, menuitem, data=None):
global xml
widgets.get_object("about_dialog").set_name('GNOME FCE Ultra '+version) widgets.get_object("about_dialog").set_name('GNOME FCE Ultra '+version)
widgets.get_object("about_dialog").run() widgets.get_object("about_dialog").run()
widgets.get_object("about_dialog").hide() widgets.get_object("about_dialog").hide()
def browse_button_clicked(self, menuitem, data=None): def browse_button_clicked(self, menuitem, data=None):
global xml,options global options
set_options() set_options()
chooser = gtk.FileChooserDialog("Open...", None, chooser = gtk.FileChooserDialog("Open...", None,
gtk.FILE_CHOOSER_ACTION_OPEN, gtk.FILE_CHOOSER_ACTION_OPEN,
@ -506,10 +529,10 @@ class WidgetsWrapper:
options.no_network_radio = False options.no_network_radio = False
def host_radio_clicked(self, menuitem, data=None): def host_radio_clicked(self, menuitem, data=None):
global fceu_server_binary
if widgets.get_object("host_radio").get_active(): if widgets.get_object("host_radio").get_active():
fceu_server_binary = find_binary('fceu-server') self.server_binary = find_binary('fceu-server')
if fceu_server_binary == None:
if self.server_binary == None:
if os.name == 'nt': if os.name == 'nt':
gfceu_error("The fceu server software cannot be found. \n\ gfceu_error("The fceu server software cannot be found. \n\
Ensure that it is installed in the same directory as \n\ Ensure that it is installed in the same directory as \n\
@ -522,7 +545,6 @@ class WidgetsWrapper:
options.no_network_radio = True options.no_network_radio = True
return False return False
gfceu_print("Using: "+fceu_server_binary)
widgets.get_object("join_frame").set_sensitive(False) widgets.get_object("join_frame").set_sensitive(False)
widgets.get_object("host_frame").set_sensitive(True) widgets.get_object("host_frame").set_sensitive(True)
options.join_radio = False options.join_radio = False
@ -537,38 +559,21 @@ class WidgetsWrapper:
options.no_network_radio = True options.no_network_radio = True
def end(self, menuitem, data=None): def end(self, menuitem, data=None):
global xml, options, optionsfile global options, optionsfile
set_options() set_options()
save_options() save_options()
gtk.main_quit() gtk.main_quit()
widgets = None
# # # # # # # #
# main
if __name__ == '__main__': if __name__ == '__main__':
# Parse options
fceux_binary = find_binary('fceux')
parser = OptionParser(version='%prog '+ version) parser = OptionParser(version='%prog '+ version)
parser.add_option('-b', '--binary', action="store", type="string", dest="fceux_binary")
parser.parse_args() parser.parse_args()
if fceux_binary == None:
gfceu_error('Could not find the fceu binary.\n\
Ensure that FCE Ultra is installed and in the $PATH.\n\
On Debian based systems (like Ubuntu), try the following command:\n\
sudo apt-get install fceu', 4, True)
else:
gfceu_print('Using: '+fceux_binary)
wrap = WidgetsWrapper()
widgets.get_object("main_window").show_all()
setup_environment()
options = game_options() app = GfceuApp()
load_options()
give_widgets()
try:
gtk.main()
except KeyboardInterrupt:
sys.exit(0)