MacPrefs.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Tools » IDE » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Tools » IDE » MacPrefs.py
import macfs
import marshal
import types

from MACFS import kOnSystemDisk

class PrefObject:
  
  def __init__(self, dict = None):
    if dict == None:
      self._prefsdict = {}
    else:
      self._prefsdict = dict
  
  def __len__(self):
    return len(self._prefsdict)
  
  def __delattr__(self, attr):
    if self._prefsdict.has_key(attr):
      del self._prefsdict[attr]
    else:
      raise AttributeError, 'delete non-existing instance attribute'
  
  def __getattr__(self, attr):
    if attr == '__members__':
      keys = self._prefsdict.keys()
      keys.sort()
      return keys
    try:
      return self._prefsdict[attr]
    except KeyError:
      raise AttributeError, attr
  
  def __setattr__(self, attr, value):
    if attr[0] <> '_':
      self._prefsdict[attr] = value
    else:
      self.__dict__[attr] = value
  
  def getprefsdict(self):
    return self._prefsdict


class PrefFile(PrefObject):
  
  def __init__(self, path, creator = 'Pyth'):
    # Find the preferences folder and our prefs file, create if needed.
    self.__path = path
    self.__creator = creator
    self._prefsdict = {}
    try:
      prefdict = marshal.load(open(self.__path, 'rb'))
    except (IOError, ValueError):
      # file not found, or currupt marshal data
      pass
    else:
      for key, value in prefdict.items():
        if type(value) == types.DictType:
          self._prefsdict[key] = PrefObject(value)
        else:
          self._prefsdict[key] = value
  
  def save(self):
    prefdict = {}
    for key, value in self._prefsdict.items():
      if type(value) == types.InstanceType:
        prefdict[key] = value.getprefsdict()
        if not prefdict[key]:
          del prefdict[key]
      else:
        prefdict[key] = value
    marshal.dump(prefdict, open(self.__path, 'wb'))
    fss = macfs.FSSpec(self.__path)
    fss.SetCreatorType(self.__creator, 'pref')
  
  def __getattr__(self, attr):
    if attr == '__members__':
      keys = self._prefsdict.keys()
      keys.sort()
      return keys
    try:
      return self._prefsdict[attr]
    except KeyError:
      if attr[0] <> '_':
        self._prefsdict[attr] = PrefObject()
        return self._prefsdict[attr]
      else:
        raise AttributeError, attr


_prefscache = {}

def GetPrefs(prefname, creator = 'Pyth'):
  import macostools, os
  if _prefscache.has_key(prefname):
    return _prefscache[prefname]
  # Find the preferences folder and our prefs file, create if needed.
  vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0)
  prefsfolder_fss = macfs.FSSpec((vrefnum, dirid, ''))
  prefsfolder = prefsfolder_fss.as_pathname()
  path = os.path.join(prefsfolder, prefname)
  head, tail = os.path.split(path)
  # make sure the folder(s) exist
  macostools.mkdirs(head)
  
  preffile = PrefFile(path, creator)
  _prefscache[prefname] = preffile
  return preffile
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.