User:Tardis/regdiff.py: Difference between revisions

Content deleted Content added
version 0.3: check on and report progress
m Replaced deprecated <source> tags with <syntaxhighlight> (via WP:JWB)
 
(One intermediate revision by one other user not shown)
Line 1:
<sourcesyntaxhighlight lang="python">
#!/usr/bin/env python
#regdiff.py
#Created November 9 2008
#Updated November 910 2008
#Version 0.3.1
 
#This program is free software; you can redistribute it and/or modify it under
Line 25:
#The output deletes all keys and values present in the first but not in the
#second! Therefore, this should be run only on exports of complete subtrees.
 
#The input must be sorted (values sorted within each key); I believe that
#Regedit's export does this, and will also guarantee complete subtrees.
 
#It's probably wise to double check any key removals in the output: look for
Line 48 ⟶ 51:
# - verify continuous progress
# - support displaying progress
#Version 0.3.1, November 10 2008:
# - use case-insensitive and subkey-aware comparisons for ordering
 
#Bugs:
Line 54 ⟶ 59:
 
#I don't know whether .REG files are really UTF-16 or UCS-2.
 
#I'm not sure that the last blank line is really necessary; a trailing CRLF
#may be sufficient.
 
import sys,codecs
 
def keycompare(a,b):
"""Return an integer indicating the ordering of keys a and b."""
return cmp(a.lower().split('\\'),b.lower().split('\\'))
 
class line(object):
Line 75 ⟶ 87:
self.delete=s[1]=='-'
self.lastkey=self.name=s[1+self.delete:-3] # ends in "]\r\n"
if k is not None and self.lastkey<k.lastkey: is not None and\
keycompare(self.lastkey,k.lastkey)<0:
raise ValueError,"key %r precedes %r in input"%\
(k.lastkey,self.lastkey)
Line 87 ⟶ 100:
elif c=='"': break
else: raise IOError,"unterminated name in "+repr(s)
elif s[0]=!='@': passraise #IOError,"unrecognized key'format: "+repr(s default value)
else: raise IOError,"unrecognized format: "+repr(s)
# The name for @ is "", which properly sorts before everything.
self.name=s[1:index-1]
Line 99 ⟶ 111:
else:
self.lastkey=k.lastkey
if not k.iskey and self.name.lower()<k.name.lower():
raise ValueError,"value %r precedes %r in input"%\
(k.name,self.name)
Line 192 ⟶ 204:
# values (since the values go with a previous key), and EOF comes after
# everything. Positive values mean that n comes first.
c=o.eof-n.eof or cmpkeycompare(o.lastkey,n.lastkey) or\
o.iskey-n.iskey or cmp(o.name.lower(),n.name.lower())
o.old=c<=0
n.old=c>=0
Line 217 ⟶ 229:
out.write("\r\n")
if displayProgress: sys.stderr.write('\n')
</syntaxhighlight>
</source>