home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / prefs / prefsdata.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  5.5 KB  |  183 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import os.path as os
  6. import sys
  7. import util
  8. from util import dictadd, is_all
  9. from util.observe import observable_dict
  10. import syck
  11. from logging import getLogger
  12. log = getLogger('PrefsData')
  13. info = log.info
  14. warning = log.warning
  15.  
  16. def flatten(mapping, parent_name = ''):
  17.     if not mapping:
  18.         return []
  19.     
  20.     result_list = []
  21.     for key, value in mapping.iteritems():
  22.         path = parent_name + key
  23.         if isinstance(value, dict) or hasattr(key, 'iteritems'):
  24.             result_list.extend(flatten(value, path + '.'))
  25.             continue
  26.         result_list.append((path, value))
  27.     
  28.     return result_list
  29.  
  30.  
  31. def inflate(mappings_list, root = None, dict_factory = dict):
  32.     if not root:
  33.         pass
  34.     root_map = dict_factory()
  35.     for path, value in mappings_list:
  36.         path = path.split('.')
  37.         parent = root_map
  38.         for name in path:
  39.             if path[-1] == name:
  40.                 parent[name] = value
  41.             else:
  42.                 parent.setdefault(name, dict_factory())
  43.             parent = parent[name]
  44.         
  45.     
  46.     return root_map
  47.  
  48. nice_type_names = {
  49.     unicode: 'unicode',
  50.     str: 'str',
  51.     bool: 'boolean',
  52.     int: 'integer',
  53.     list: 'list',
  54.     float: 'float',
  55.     dict: 'dict',
  56.     type(None): 'none' }
  57. from util import dictreverse
  58. nice_name_types = dictreverse(nice_type_names)
  59. __localprefs = None
  60.  
  61. class localprefprop(object):
  62.     
  63.     def __init__(self, key, default):
  64.         if isinstance(key, basestring):
  65.             
  66.             key = lambda s: key
  67.         
  68.         self.key = key
  69.         self.default = default
  70.  
  71.     
  72.     def __get__(self, obj, objtype = None):
  73.         
  74.         try:
  75.             return localprefs()[self.key(obj)]
  76.         except KeyError:
  77.             return self.default
  78.  
  79.  
  80.     
  81.     def __set__(self, obj, val):
  82.         localprefs()[self.key(obj)] = val
  83.  
  84.  
  85.  
  86. def defaultprefs():
  87.     import prefs
  88.     import stdpaths
  89.     import config
  90.     the_prefs = util.Storage()
  91.     resdir = os.path.join(util.program_dir(), 'res')
  92.     filenames = [
  93.         os.path.join(resdir, 'defaults.yaml'),
  94.         os.path.join(resdir, config.platformName, 'defaults.yaml'),
  95.         stdpaths.config / 'prefs.yaml',
  96.         stdpaths.userdata / 'prefs.yaml']
  97.     for filename in filenames:
  98.         info('loading prefs from %r', filename)
  99.         
  100.         try:
  101.             
  102.             try:
  103.                 f = _[2]
  104.                 prefdict = syck.load(f)
  105.             finally:
  106.                 pass
  107.  
  108.         except Exception:
  109.             e = None
  110.             log.info('Error loading prefs from %r: %r', filename, e)
  111.             continue
  112.  
  113.         if not sys.DEV:
  114.             prefdict.pop('debug', None)
  115.         
  116.         prefdict = prefs.flatten(prefdict)
  117.         the_prefs.update(prefdict)
  118.     
  119.     return the_prefs
  120.  
  121.  
  122. def localprefs():
  123.     global __localprefs
  124.     import common
  125.     if __localprefs is not None and __localprefs.name == common.profile.name:
  126.         return __localprefs
  127.     
  128.     local_settings = local_settings
  129.     import gui.toolbox
  130.     SavingDictBase = ObservableDict
  131.     import util.observe
  132.     ls = local_settings()
  133.     
  134.     _section = lambda : 'localprefs %s' % common.profile.name
  135.     section = _section()
  136.     if not ls.has_section(section):
  137.         ls.add_section(section)
  138.     
  139.     
  140.     class SavingDict('SavingDict', (SavingDictBase,)):
  141.         import localdefaults
  142.         name = common.profile.name
  143.         
  144.         def __init__(self, save_func, *a, **k):
  145.             self.save = save_func
  146.             SavingDictBase.__init__(self, *a, **k)
  147.  
  148.         
  149.         def __setitem__(self, key, val):
  150.             SavingDictBase.__setitem__(self, key, val)
  151.             self.save(self)
  152.  
  153.         
  154.         def __delitem__(self, key):
  155.             SavingDictBase.__delitem__(self, key)
  156.             self.save(self)
  157.  
  158.         
  159.         def __getitem__(self, key):
  160.             
  161.             try:
  162.                 return SavingDictBase.__getitem__(self, key)
  163.             except KeyError:
  164.                 e = None
  165.                 
  166.                 try:
  167.                     v = getattr(self.localdefaults, key.replace('.', '_'))
  168.                 except AttributeError:
  169.                     raise e
  170.  
  171.                 return None if callable(v) else v
  172.  
  173.  
  174.  
  175.     
  176.     def save(d):
  177.         ls._sections[_section()] = d.copy()
  178.         ls.save()
  179.  
  180.     __localprefs = SavingDict(save, ls.iteritems(section))
  181.     return __localprefs
  182.  
  183.