Changeset 1453
- Timestamp:
- 06/06/08 10:46:11 (7 months ago)
- Files:
-
- sandbox/astormont/RegParser.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/astormont/RegParser.py
r1452 r1453 1 1 # astormont: I've subclassed the python ConfigParser and tweaked it to read .reg files 2 2 3 import ConfigParser 3 import re 4 from ConfigParser import ConfigParser, ParsingError, DEFAULTSECT 4 5 5 DEFAULTSECT = "DEFAULT" 6 ( REG_PLAIN, REG_STR, REG_HEX, REG_WORD, REG_DWORD ) = range( 5 ) 6 7 7 class RegParser( ConfigParser.ConfigParser ): 8 class RegParser( ConfigParser ): 9 10 header = "REGEDIT 4" 11 12 # Fixed regex for .regs. Handles quotes better than the default class, too. 13 OPTCRE = re.compile( r'"?(?P<option>[^:=\s][^="]*)"?\s*(?P<vi>[:=])\s*"?(?P<value>.*)"?$' ) 14 15 # Disable case-insensitivty 16 def optionxform(self, optionstr): 17 return optionstr 18 19 def type( self, val ): 20 if val.startswith( "str(" ): 21 return REG_STR 22 elif val.startswith( "hex:" ): 23 return REG_HEX 24 elif val.startswith( "word:" ): 25 return REG_WORD 26 elif val.startswith( "dword:" ): 27 return REG_DWORD 28 else: 29 return REG_PLAIN 8 30 9 31 def _read(self, fp, fpname): 10 32 """Parse a sectioned setup file. 11 12 33 The sections in setup file contains a title line at the top, 13 34 indicated by a name in square brackets (`[]'), plus key/value … … 17 38 and just about everything else are ignored. 18 39 """ 19 cursect = None # None, or a dictionary40 cursect = None # None, or a dictionary 20 41 optname = None 21 42 lineno = 0 22 e = None # None, or an exception43 e = None # None, or an exception 23 44 while True: 24 45 line = fp.readline() … … 27 48 lineno = lineno + 1 28 49 # skip first line (contains file header like: REGEDIT 4, or WINE REGISTRY 2) 29 if lineno == 1: 50 if lineno == 1 and not line.startswith(";") and not line.startswith("["): 51 self.header = line 30 52 continue 31 53 # comment or blank line? … … 57 79 # no section header in the file? 58 80 elif cursect is None: 59 raise MissingSectionHeaderError(fpname, lineno, line)81 continue 60 82 # an option line? 61 83 else: … … 63 85 if mo: 64 86 optname, vi, optval = mo.group('option', 'vi', 'value') 65 # Strip the quotes off the name & val66 optname = optname[1:-1]67 optval = optval[1:-1]68 87 if vi in ('=', ':') and ';' in optval: 69 88 # ';' is a comment delimiter only if it follows … … 73 92 optval = optval[:pos] 74 93 optval = optval.strip() 75 # allow empty values 76 if optval == '""': 77 optval = '' 78 optname = self.optionxform(optname.rstrip()) 94 # Strip the quotes off the name & val, fix @ 95 #if optname == "@": 96 # optname = "" 97 #elif optname.startswith("\"") and optname.endswith("\""): 98 # optname = optname[1:-1] 99 #if optval.startswith("\"") and optval.endswith("\""): 100 # optval = optval[1:-1] 79 101 cursect[optname] = optval 80 102 else: … … 90 112 raise e 91 113 114 def write(self, fp): 115 """Write an .reg-format representation of the configuration state.""" 116 # Put back header 117 fp.write( self.header + "\n" ) 118 if self._defaults: 119 fp.write("[%s]\n" % DEFAULTSECT) 120 for (key, value) in self._defaults.items(): 121 if key == "": 122 key = "@" 123 else: 124 key = "\"%s\"" % key 125 if self.type( value ) == REG_PLAIN: 126 value = "\"%s\"" % value 127 fp.write("%s=%s\n" % (key, str(value).replace('\n', '\n\t'))) 128 fp.write("\n") 129 for section in self._sections: 130 fp.write("[%s]\n" % section) 131 for (key, value) in self._sections[section].items(): 132 if key != "__name__": 133 if key == "": 134 key = "@" 135 else: 136 key = "\"%s\"" % key 137 if self.type( value ) == REG_PLAIN: 138 value = "\"%s\"" % value 139 fp.write("%s=%s\n" % (key, str(value).replace('\n', '\n\t'))) 140 fp.write("\n")
