On Thu, 2004-01-22 at 14:01, Rainer Dorsch wrote:
> Wow incredibly quick answer...right now I probably don't find the time to
> implement and donate that feature....I would be happy to donate 30$ if someone
> implements it until 06/30/04 (then I either start myself or merge with emacs
> ;-). I know, the bounty is not very high, but many small pieces...
I don't have time to implement and donate the feature, but I can give
you the python script that I used for the purpose.
One caveat, it's not very smart - the accounts that it processes have to
have unique names. It doesn't walk the tree. (I have experimented with
that in SQL space, but don't have anything at the moment - gave up on
SQL for now since it seems to have a longer startup time than the XML.)
Usage:
./process oldbook.xac newbook.xac AccountName
It will read oldbook.xac and newbook.xac and build a translation table
for account guids. It will then take the transactions from AccountName
and update the guids in the splits. Finally it writes the new
transactions to stdout. (You get to insert them in newbook.xac
yourself.) It does not modify any files.
The transaction count after the insert will be wrong, but gnucash fixes
this on the next save. Also, it will print a bunch of warnings if it
couldn't match up some accounts - change account names to match up (or
create accounts) and retry. It only cares about accounts that are used
in the copied transactions.
This was just a quick and dirty script I used to transfer some credit
card and bank transactions from one gnucash, but it did serve the
purpose well.
Steve
[EMAIL PROTECTED]
#!/usr/bin/python
from xml.dom import minidom
import sys
def children(node,name):
return [ e for e in node.childNodes if isinstance(e,minidom.Element) and e.tagName
== name ]
def textdata(el):
return "".join([e.data for e in el.childNodes if isinstance(e,minidom.Text)])
def getField(node,name):
return textdata(children(node,name)[0])
def acctmap(accts):
rval1 = dict()
rval2 = dict()
for acct in accts:
name = getField(acct, 'act:name')
id = getField(acct, 'act:id')
rval1[name]=id
rval2[id]=name
return (rval1,rval2)
old = minidom.parse(sys.argv[1])
oldaccts = old.getElementsByTagName('gnc:account')
(oldid,oldname) = acctmap(oldaccts)
xacts = old.getElementsByTagName('gnc:transaction')
uid = oldid[sys.argv[3]]
def accounts(xact):
u = {}
for x in map(textdata,xact.getElementsByTagName('split:account')):
u[x] = 1
return u.keys()
xacts2 = [ xact for xact in xacts if uid in accounts(xact) ]
new = minidom.parse(sys.argv[2])
newaccts = new.getElementsByTagName('gnc:account')
(newid,newname) = acctmap(newaccts)
idmap = {}
for key in oldid.keys():
if newid.has_key(key):
idmap[oldid[key]] = newid[key]
else:
pass
#print key, "is missing"
for xact in xacts2:
for el in xact.getElementsByTagName('split:account'):
if len(el.childNodes) != 1:
raise Error("data format error")
if not idmap.has_key(el.firstChild.data):
print "Missing key",oldname[el.firstChild.data],"["+el.firstChild.data+"]"
else:
el.firstChild.data = idmap[el.firstChild.data]
print "\n".join([ x.toxml() for x in xacts2 ])
#print idmap
_______________________________________________
gnucash-devel mailing list
[EMAIL PROTECTED]
http://www.gnucash.org/cgi-bin/mailman/listinfo/gnucash-devel