Changeset 14

Show
Ignore:
Timestamp:
02/13/06 19:53:52 (3 years ago)
Author:
travis
Message:

Started on new config API based on ConfigParser?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/audio_control.py

    r12 r14  
    2929_ = i18n._ 
    3030 
    31 from common import mesk 
     31import mesk 
    3232 
    3333class AudioControl: 
  • trunk/src/common/__init__.py

    r13 r14  
    2424 
    2525GLADE_XML = 'mesk_gui.glade' 
    26  
    27 import mesk 
  • trunk/src/common/config.py

    r6 r14  
    11################################################################################ 
    2 #  Copyright (C) 2003-2006 Yann Le Boulanger <asterix@lagaule.org> 
    3 #                          Vincent Hanquez <tab@snarc.org> 
    42#  Copyright (C) 2006  Travis Shirk <travis@pobox.com> 
    53# 
     
    2624_ = i18n._ 
    2725 
    28 OPT_TYPE = 0 
    29 OPT_VAL = 1 
    30 OPT_DESC = 2 
     26from program_info import APP_NAME 
     27from program_info import APP_VERSION 
     28from program_info import APP_MAINTAINER 
     29import ConfigParser 
    3130 
    32 opt_int = [ 'integer', 0 ] 
    33 opt_str = [ 'string', 0 ] 
    34 opt_bool = [ 'boolean', 0 ] 
    35 opt_color = [ 'color', '^(#[0-9a-fA-F]{6})|()$' ] 
     31class Config(ConfigParser.SafeConfigParser): 
     32    GST_ALSA = 'alsa' 
     33    GST_OSS  = 'oss' 
     34    GST_ESD  = 'esd' 
    3635 
     36    (OPT_TYPE, 
     37     OPT_DEFAULT, 
     38     OPT_VALUES, 
     39     OPT_HELP) = range(4) 
    3740 
    38 class Config: 
    39     GST_ALSA = 'alsa' 
    40     GST_OSS = 'oss' 
    41     GST_ESD = 'esd' 
     41    # Interpolation values 
     42    __defaults = {} 
    4243 
    4344    __options = { 
    44         # name: [ type, default_value, i18n_help_string ] 
    45         'gst_sink': [opt_str, GST_ALSA, 
     45        # name: [ type, default_value, str_choices, i18n_help_string ] 
     46        'version': [str, APP_VERSION, None, _('%s version') % APP_NAME], 
     47        'gst_sink': [str, GST_ALSA, None, 
    4648                     _("GStreamer output sink. May be '%s', '%s', '%s'") % \ 
    4749                       (GST_ALSA, GST_OSS, GST_ESD)], 
    48         'gst_delay': [opt_int, '1000', _('Number of milliseconds to pause ' 
    49                                          'between tracks')], 
    50     } 
    51  
    52     __options_per_key = { 
    53         'libraries': ({'name': [ opt_str, '' ], 
    54                       }, {}) 
     50        'gst_delay': [int, 1000, None, 
     51                      _('Number of milliseconds to pause between tracks')], 
    5552    } 
    5653 
    5754    def __init__(self): 
    58             return 
     55        ConfigParser.SafeConfigParser.__init__(self, self.__defaults) 
    5956 
    60     def foreach(self, cb, data = None): 
     57        self.add_section('DEFAULT') 
    6158        for opt in self.__options: 
    62             cb(data, opt, None, self.__options[opt]) 
    63         for opt in self.__options_per_key: 
    64             cb(data, opt, None, None) 
    65             dict = self.__options_per_key[opt][1] 
    66             for opt2 in dict.keys(): 
    67                 cb(data, opt2, [opt], None) 
    68                 for opt3 in dict[opt2]: 
    69                     cb(data, opt3, [opt, opt2], dict[opt2][opt3]) 
     59            opt_type = self.__options[opt][self.OPT_TYPE] 
     60            if opt_type is unicode: 
     61                opt_val = self.__options[opt][self.OPT_DEFAULT].encode('utf-8') 
     62            else: 
     63                opt_val = str(self.__options[opt][self.OPT_DEFAULT]) 
     64            self.set('DEFAULT', opt, opt_val) 
    7065 
    71     def is_valid_int(self, val): 
    72         try: 
    73             ival = int(val) 
    74         except: 
    75             return None 
    76         return ival 
     66    def get(self, opt): 
     67        '''Lookup in the DEFAULT section''' 
     68        return ConfigParser.SafeConfigParser.get(self, 'DEFAULT', opt) 
    7769 
    78     def is_valid_bool(self, val): 
    79         if val == 'True': 
    80             return True 
    81         elif val == 'False': 
    82             return False 
    83         else: 
    84             ival = self.is_valid_int(val) 
    85             if ival: 
    86                     return True 
    87             elif ival is None: 
    88                     return None 
    89             return False 
    90         return None 
    91  
    92     def is_valid_string(self, val): 
    93         return val 
    94  
    95     def is_valid(self, type, val): 
    96         if not type: 
    97             return None 
    98         if type[0] == 'boolean': 
    99             return self.is_valid_bool(val) 
    100         elif type[0] == 'integer': 
    101             return self.is_valid_int(val) 
    102         elif type[0] == 'string': 
    103             return self.is_valid_string(val) 
    104         else: 
    105             if sre.match(type[1], val): 
    106                 return val 
    107             else: 
    108                 return None 
    109  
    110     def set(self, optname, value): 
    111         if not self.__options.has_key(optname): 
    112             raise RuntimeError, 'option %s does not exist' % optname 
    113         opt = self.__options[optname] 
    114         value = self.is_valid(opt[OPT_TYPE], value) 
    115         if value is None: 
    116             raise RuntimeError, 'value of %s cannot be None' % optname 
    117  
    118         opt[OPT_VAL] = value 
    119  
    120     def get(self, optname = None): 
    121         if not optname: 
    122             return self.__options.keys() 
    123         if not self.__options.has_key(optname): 
    124             return None 
    125         return self.__options[optname][OPT_VAL] 
    126              
    127     def get_desc(self, optname): 
    128         if not self.__options.has_key(optname): 
    129             return None 
    130         if len(self.__options[optname]) > OPT_DESC: 
    131             return self.__options[optname][OPT_DESC] 
    132  
    133     def add_per(self, typename, name): # per_group_of_option 
    134         if not self.__options_per_key.has_key(typename): 
    135             raise RuntimeError, 'option %s does not exist' % typename 
    136              
    137         opt = self.__options_per_key[typename] 
    138         if opt[1].has_key(name): 
    139             # we already have added group name before 
    140             return 'you already have added %s before' % name 
    141         opt[1][name] = copy.deepcopy(opt[0]) 
    142  
    143     def del_per(self, typename, name, subname = None): # per_group_of_option 
    144         if not self.__options_per_key.has_key(typename): 
    145             raise RuntimeError, 'option %s does not exist' % typename 
    146  
    147         opt = self.__options_per_key[typename] 
    148         if subname is None: 
    149             del opt[1][name] 
    150         # if subname is specified, delete the item in the group.         
    151         elif opt[1][name].has_key(subname): 
    152             del opt[1][name][subname] 
    153  
    154     def set_per(self, optname, key, subname, value): # per_group_of_option 
    155         if not self.__options_per_key.has_key(optname): 
    156             raise RuntimeError, 'option %s does not exist' % optname 
    157         dict = self.__options_per_key[optname][1] 
    158         if not dict.has_key(key): 
    159             raise RuntimeError, '%s is not a key of %s' % (key, dict) 
    160         obj = dict[key] 
    161         if not obj.has_key(subname): 
    162             raise RuntimeError, '%s is not a key of %s' % (subname, obj) 
    163         subobj = obj[subname] 
    164         value = self.is_valid(subobj[OPT_TYPE], value) 
    165         if value is None: 
    166             raise RuntimeError, '%s of %s cannot be None' % optname 
    167         subobj[OPT_VAL] = value 
    168  
    169     def get_per(self, optname, key = None, subname = None): # per_group_of_option 
    170         if not self.__options_per_key.has_key(optname): 
    171             return None 
    172         dict = self.__options_per_key[optname][1] 
    173         if not key: 
    174             return dict.keys() 
    175         if not dict.has_key(key): 
    176             return None 
    177         obj = dict[key] 
    178         if not subname: 
    179             return obj 
    180         if not obj.has_key(subname): 
    181             return None 
    182         return obj[subname][OPT_VAL] 
    183  
  • trunk/src/common/i18n.py

    r6 r14  
    2121#  $Id$ 
    2222################################################################################ 
     23 
     24### NOTE: Thanks to the Gajim <http://gajim.org/> project for this code 
    2325 
    2426import locale 
  • trunk/src/mesk.py

    r6 r14  
    2222import gtk, gtk.glade 
    2323 
    24 from common import mesk 
     24import mesk 
    2525from main_window import MainWindow 
    2626