|
From: Bruno C. <bru...@us...> - 2009-12-05 06:20:00
|
This is an automated email from the git, the tree hashes are:
via 9bf3725e8aab7ebc9785d91f94b750d733a98a4e (commit)
from 5764f65142d71ba87f867018cd5f9e0986284d0d (commit)
- Log -----------------------------------------------------------------
commit 9bf3725e8aab7ebc9785d91f94b750d733a98a4e
Author: Bruno Cardoso Lopes <bruno@keyser.(none)>
Date: Sat Dec 5 04:18:46 2009 -0200
Shrink down code to gather preferences and auto generate everything! ;)
diff --git a/code/preferences.py b/code/preferences.py
index 453fac5..8527df6 100644
--- a/code/preferences.py
+++ b/code/preferences.py
@@ -11,10 +11,10 @@
# ----------------------------------------------------------------------
import ConfigParser
-import os
+import os, re
import shutil
-class Preferences:
+class Preferences(object):
def __init__(self, filename, default_filename):
self.conf = ConfigParser.ConfigParser()
self.default_conf = ConfigParser.ConfigParser()
@@ -35,6 +35,9 @@ class Preferences:
print "Warning: Not creating preferences backup file"
self.copy_default(default_filename, filename)
+ # autogenerate the attributes for each item on the config file.
+ self.autogen_attributes()
+
def copy_default(self, default_filename, filename):
try:
shutil.copy(default_filename, filename)
@@ -42,164 +45,38 @@ class Preferences:
except IOError:
print "Warning: preferences file couldn't be created"
- def get(self, section, option):
- if not self.conf.has_section(section) or \
- not self.conf.has_option(section, option):
- return self.default_conf.get(section, option)
- return self.conf.get(section, option)
-
- def get_screen_resolution(self):
- res = self.get('screen', 'resolution')
- res = res .lower()
- res = res.split("x")
- return [ int(res[0]), int(res[1])]
-
- def get_screen_fullscreen(self):
- return self.get('screen', 'fullscreen')
-
- def get_keyboard_up(self):
- return int(self.get('keyboard', 'up'))
-
- def get_keyboard_down(self):
- return int(self.get('keyboard', 'down'))
-
- def get_keyboard_right(self):
- return int(self.get('keyboard', 'right'))
-
- def get_keyboard_left(self):
- return int(self.get('keyboard', 'left'))
-
- def get_keyboard_fire(self):
- return int(self.get('keyboard', 'fire'))
-
- def get_keyboard_secondary_fire(self):
- return int(self.get('keyboard', 'secondary_fire'))
-
- def get_keyboard_rot_clock(self):
- return int(self.get('keyboard', 'rot_clock'))
-
- def get_keyboard_rot_anti_clock(self):
- return int(self.get('keyboard', 'rot_anti_clock'))
-
- def get_keyboard_player_play(self):
- return int(self.get('keyboard', 'player_play'))
-
- def get_keyboard_player_stop(self):
- return int(self.get('keyboard', 'player_stop'))
-
- def get_keyboard_player_next_track(self):
- return int(self.get('keyboard', 'player_next_track'))
-
- def get_keyboard_prev_secondary_weapon(self):
- return int(self.get('keyboard', 'prev_secondary_weapon'))
-
- def get_keyboard_next_secondary_weapon(self):
- return int(self.get('keyboard', 'next_secondary_weapon'))
-
- def get_keyboard_toogle_fullscreen(self):
- return int(self.get('keyboard', 'toogle_fullscreen'))
-
- def get_mouse_sensitivity(self):
- return float(self.get('mouse', 'sensitivity'))
-
- def get_mouse_fire(self):
- return int(self.get('mouse', 'fire'))
-
- def get_mouse_secondary_fire(self):
- return int(self.get('mouse', 'secondary_fire'))
-
- def get_mouse_prev_secondary_weapon(self):
- return int(self.get('mouse', 'prev_secondary_weapon'))
-
- def get_mouse_next_secondary_weapon(self):
- return int(self.get('mouse', 'next_secondary_weapon'))
-
- def get_joystick_axis_x(self):
- return int(self.get('joystick', 'axis_x'))
-
- def get_joystick_axis_y(self):
- return int(self.get('joystick', 'axis_y'))
-
- def get_joystick_axis_z(self):
- return int(self.get('joystick', 'axis_z'))
-
- def get_joystick_fire(self):
- return int(self.get('joystick', 'fire'))
-
- def get_joystick_secondary_fire(self):
- return int(self.get('joystick', 'secondary_fire'))
-
- def get_joystick_rot_clock(self):
- return int(self.get('joystick', 'rot_clock'))
-
- def get_joystick_rot_anti_clock(self):
- return int(self.get('joystick', 'rot_anti_clock'))
-
- def get_joystick_player_play(self):
- return int(self.get('joystick', 'player_play'))
-
- def get_joystick_player_stop(self):
- return int(self.get('joystick', 'player_stop'))
-
- def get_joystick_player_next_track(self):
- return int(self.get('joystick', 'player_next_track'))
-
- def get_joystick_prev_secondary_weapon(self):
- return int(self.get('joystick', 'prev_secondary_weapon'))
-
- def get_joystick_next_secondary_weapon(self):
- return int(self.get('joystick', 'next_secondary_weapon'))
-
- def get_joystick_sensitivity(self):
- return float(self.get('joystick', 'sensitivity'))
-
- def get_joystick_deadzone(self):
- return float(self.get('joystick', 'deadzone'))
-
- def get_joystick_id(self):
- return int(self.get('joystick', 'id'))
-
- def get_general_input(self):
- return self.get('general', 'input')
-
- def get_general_music_volume(self):
- return float(self.get('general', 'music_volume'))
+ def parse_config_item(self, value):
+ """
+ Gather the type we're dealing with and return the value
+ on its real type.
+ """
+ resolution_type = lambda s: [ int(s.split("x")[0]),
+ int(s.split("x")[1]) ]
+
+ patterns = [ [resolution_type, re.compile("[0-9]+x[0-9]+")],
+ [float, re.compile("[0-9]+\.[0-9]+")],
+ [int, re.compile("[0-9]+")],
+ [str, re.compile(".*")] ]
+
+ for ty, regex in patterns:
+ if regex.match(value):
+ return ty(value)
+
+ def autogen_attributes(self):
+ """
+ Get tuples from ConfigParser and autogenerate attributes
+ based on the section and preference name, example:
+ [keyboard]
+ up = 273
+ down = ...
+ This should turn into:
+ self.keyboard_up = 273
+ self.keyboard_down = ...
+ """
+ for section in self.conf.sections():
+ for item, value in self.conf.items(section):
+ attr_name = "%s_%s" % (section, item)
+ attr_value = self.parse_config_item(value)
+ self.__setattr__(attr_name, attr_value)
+ print section, item, attr_value
- screen_fullscreen = property(get_screen_fullscreen)
- screen_resolution = property(get_screen_resolution)
- keyboard_up = property(get_keyboard_up)
- keyboard_down = property(get_keyboard_down)
- keyboard_right = property(get_keyboard_right)
- keyboard_left = property(get_keyboard_left)
- keyboard_fire = property(get_keyboard_fire)
- keyboard_secondary_fire = property(get_keyboard_secondary_fire)
- keyboard_rot_clock = property(get_keyboard_rot_clock)
- keyboard_rot_anti_clock = property(get_keyboard_rot_anti_clock)
- keyboard_player_play = property(get_keyboard_player_play)
- keyboard_player_stop = property(get_keyboard_player_stop)
- keyboard_player_next_track = property(get_keyboard_player_next_track)
- keyboard_prev_secondary_weapon = property(get_keyboard_prev_secondary_weapon)
- keyboard_next_secondary_weapon = property(get_keyboard_next_secondary_weapon)
- keyboard_toogle_fullscreen = property(get_keyboard_toogle_fullscreen)
- mouse_sensitivity = property(get_mouse_sensitivity)
- mouse_fire = property(get_mouse_fire)
- mouse_secondary_fire = property(get_mouse_secondary_fire)
- mouse_prev_secondary_weapon = property(get_mouse_prev_secondary_weapon)
- mouse_next_secondary_weapon = property(get_mouse_next_secondary_weapon)
- joystick_axis_x = property(get_joystick_axis_x)
- joystick_axis_y = property(get_joystick_axis_y)
- joystick_axis_z = property(get_joystick_axis_z)
- joystick_fire = property(get_joystick_fire)
- joystick_secondary_fire = property(get_joystick_secondary_fire)
- joystick_rot_clock = property(get_joystick_rot_clock)
- joystick_rot_anti_clock = property(get_joystick_rot_anti_clock)
- joystick_player_play = property(get_joystick_player_play)
- joystick_player_stop = property(get_joystick_player_stop)
- joystick_player_next_track = property(get_joystick_player_next_track)
- joystick_prev_secondary_weapon = property(get_joystick_prev_secondary_weapon)
- joystick_next_secondary_weapon = property(get_joystick_next_secondary_weapon)
- joystick_sensitivity = property(get_joystick_sensitivity)
- joystick_deadzone = property(get_joystick_deadzone)
- joystick_id = property(get_joystick_id)
- general_input = property(get_general_input)
- general_music_volume = property(get_general_music_volume)
-----------------------------------------------------------------------
Summary of changes:
code/preferences.py | 201 ++++++++++-----------------------------------------
1 files changed, 39 insertions(+), 162 deletions(-)
--
Dead Channel is a sci-fi shoot 'em up game developed using pygame.
|