#
# Copyright (c) 2004 Specifix, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any waranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

""" 
Contains information gathered during cooking, to be used mostly if the cook 
failed in order to resume using the same destdir
"""

import time

class BuildInfo(dict):

    def __init__(self, builddir):
	self.__builddir = builddir
	self.__infofile = builddir + "/conary-build-info"
    
    def read(self):
	# don't catch this error
	self.__fd = open(self.__infofile, "r")
	lines = self.__fd.readlines()
	self.__fd.close()
	for line in lines:
	    if line == '\n':
		continue
	    (key, value) = line.split(None, 1)
	    #handle macros.foo 
	    keys = key.split('.')
	    if len(keys) > 1:
		subdicts = keys[:-1]
		key = keys[-1]
		curdict = self.__dict__
		for subdict in subdicts:
		    if subdict not in curdict:
			curdict[subdict] = {}
		    curdict = curdict[subdict]
		curdict[key] = value[:-1] 
	    else:
		self.__dict__[key] = value[:-1] 

    def begin(self):
	self.__fd = open(self.__infofile, "w")
	tm = time.time()
	tmstr = time.asctime()
	self.start = "%s (%s)" % (tm, tmstr)

    def write(self, str):
	self.__fd.write(str)
	self.__fd.flush()

    def stop(self):
	tm = time.time()
	tmstr = time.asctime()
	self.end = "%s (%s)" % (tm, tmstr)
	self.__fd.close()

    def __setattr__(self, name, value):
	if not name.startswith('_BuildInfo_'):
	    self.write('%s %s\n' % (name,value))
	self.__setitem__(name, value)

    def __getattr__(self, name):
	return dict.__getitem__(self, name)

@@ -855,20 +855,16 @@
 d-info%22%0A
-    
 %0A    def
@@ -1216,25 +1216,16 @@
 t = self
-.__dict__
 %0A%09%09for s
@@ -1362,17 +1362,16 @@
 lue%5B:-1%5D
- 
 %0A%09    el
@@ -1384,17 +1384,8 @@
 self
-.__dict__
 %5Bkey
@@ -1398,17 +1398,16 @@
 lue%5B:-1%5D
- 
 %0A%0A    de
@@ -1768,16 +1768,156 @@
 value):%0A
+        # Note that using buildinfo.foo = y%0A        # causes foo to be written to file, while using buildinfo%5B'foo'%5D = y%0A        # does not%0A
 %09if not 
@@ -1999,33 +1999,22 @@
 self
-.__setitem__(
+%5B
 name
-,
+%5D =
  value
-)
 %0A%0A  
@@ -2056,33 +2056,16 @@
 urn 
-dict.__getitem__(
 self
-, 
+%5B
 name
-)
+%5D 
 %0A

Stop using __dict__ in dict subclass