registry.py :  » GUI » PmwContribD » PmwContribD-r2_0_2 » 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 » GUI » PmwContribD 
PmwContribD » PmwContribD r2_0_2 » registry.py
#!/usr/bin/env python
#
# $Id: registry.py,v 1.1 2001/11/11 13:20:27 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
#                         All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""A class to wrap the annoying registry access calls in win32api

"""

__rcs_info__ = {
    #
    #  Creation Information
    #
    'module_name'  : '$RCSfile: registry.py,v $',
    'rcs_id'       : '$Id: registry.py,v 1.1 2001/11/11 13:20:27 doughellmann Exp $',
    'creator'      : 'Doug Hellmann <doug@hellfly.net>',
    'project'      : 'PmwContribD',
    'created'      : 'Sun, 02-Jan-2000 15:43:22 EST',

    #
    #  Current Information
    #
    'author'       : '$Author: doughellmann $',
    'version'      : '$Revision: 1.1 $',
    'date'         : '$Date: 2001/11/11 13:20:27 $',
}
try:
    __version__ = __rcs_info__['version'].split(' ')[1]
except:
    __version__ = '0.0'

#
# Import system modules
#
import string
import win32api
import win32con


#
# Import Local modules
#


#
# Module
#


#
# Ways to open keys
#
KeyRead = win32con.KEY_READ
KeyAll = win32con.KEY_ALL_ACCESS
KeyCreateSubKey = win32con.KEY_CREATE_SUB_KEY
KeyEnumSubKeys = win32con.KEY_ENUMERATE_SUB_KEYS
KeyExecute = win32con.KEY_EXECUTE
KeyNotify = win32con.KEY_NOTIFY
KeyQueryValue = win32con.KEY_QUERY_VALUE
KeySetValue = win32con.KEY_SET_VALUE
KeyWrite = win32con.KEY_WRITE

#
# Types of values
#
ValueBinary = win32con.REG_BINARY
ValueDWord = win32con.REG_DWORD
ValueDWordLittleEndian = win32con.REG_DWORD_LITTLE_ENDIAN
ValueDWordBigEndian = win32con.REG_DWORD_BIG_ENDIAN
ValueExpandString = win32con.REG_EXPAND_SZ
ValueLink = win32con.REG_LINK
ValueArrayStrings = win32con.REG_MULTI_SZ
ValueNone = win32con.REG_NONE
ValueResourceList = win32con.REG_RESOURCE_LIST
ValueString = win32con.REG_SZ


class RegistryKey:
     def __init__(self, name, openKey):
          "Create a wrapper object for win32api registry key access."
          self.name = name
          self.key = openKey
          return
     
     def valueQuery(self, valueName=None, defaultValue=None):
          "Ask for a value, return a default if given."
          try:
               val = win32api.RegQueryValueEx(self.key, valueName)
          except:
               val = (defaultValue, None)
          return val

     def subKeyNames(self):
          "Returns a list of all sub-key names."
          idx = 0
          subKeyList = []
          while 1:
               try:
                    keyName = win32api.RegEnumKey(self.key, idx)
               except:
                    break
               else:
                    subKeyList.append(keyName)
                    idx = idx + 1
          return subKeyList

     def valueNames(self):
          "Returns a list of all value names."
          idx = 0
          valueList = []
          while 1:
               try:
                    valName, valValue, valType = win32api.RegEnumValue(self.key, idx)
               except:
                    break
               else:
                    valueList.append(valName)
                    idx = idx + 1
          return valueList

     def valueSet(self, newValue, valueName=None, valueType=ValueString):
          "Set a value."
          return win32api.RegSetValueEx(self.key, valueName, None, valueType, newValue)


class Registry:

     specialKeys = {
          'HKEY_CLASSES_ROOT':win32con.HKEY_CLASSES_ROOT,
          'HKEY_CURRENT_USER':win32con.HKEY_CURRENT_USER,
          'HKEY_LOCAL_MACHINE':win32con.HKEY_LOCAL_MACHINE,
          'HKEY_USERS':win32con.HKEY_USERS
          }

     def __init__(self):
          self.__openKeys = {}
     
     def openKey(self, keyName, accessMode=KeyRead):
          key = self.__openKey__(keyName, accessMode)
          return RegistryKey(keyName, key)

     def __openKey__(self, keyName, accessMode):
          "Using a fully qualified key name, open a registry key and return it."
          try:
               #print 'Looking for %s:%s in open keys' % (keyName, repr(accessMode))
               key = self.__openKeys['%s:%s' % (keyName, repr(accessMode))]
          except KeyError:
               #print 'Not found'
               key = self.__newKey__(keyName, accessMode)
               self.__openKeys[keyName] = key
          return key

     def __newKey__(self, newName, accessMode):
          "Create a new key for the fully qualified name."
          #
          # Find the key separator character
          #
          keyList = string.splitfields(newName, '\\')
          if len(keyList) == 2:
               #
               # We have just a single key name, so
               # open it.
               #
               #print 'Using special root key for ', keyList[0]
               rootKey = self.specialKeys[keyList[0]]
          else:
               #
               # We have to open the root of the key first,
               # so we recurse.
               #
               rootKeyName = string.joinfields(keyList[:-1], '\\')
               #print 'Opening root key: ', rootKeyName
               rootKey = self.__openKey__(rootKeyName, accessMode)

          key = self.openKeyWithRoot(rootKey, keyList[-1], accessMode)
          #print 'Returning key: ', repr(key)
          return key

     def openKeyWithRoot(self, rootKey, subKeyName, accessMode):
          #print 'openKeyWithRoot: ', repr(rootKey), subKeyName
          key = win32api.RegCreateKey(rootKey, subKeyName)
          return key

def keyNameMake(nameParts):
     newKeyName = None
     if type(nameParts) in [ type(()), type([]) ]:
          for name in nameParts:
               if newKeyName:
                    newKeyName = '%s\\%s' % (newKeyName, name)
               else:
                    newKeyName = name
     elif type(nameParts) == type(''):
          newKeyName = nameParts
     return newKeyName

          

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.