#
# Copyright (c) 2004 Specifix, Inc.
# All rights reserved
#
import os
import versions
import sys

class SrsConfiguration:

    def read(self, file):
	if os.path.exists(file):
	    f = open(file, "r")
	    for line in f:
		self.configLine(line)
	    f.close()

    def configLine(self, line):
	(key, val) = line.split()
	if not self.__dict__.has_key(key):
	    raise KeyError, ("configuration value %s unknown" % key)

	self.__dict__[key] = val

    def display(self):
	keys = self.__dict__.keys()
	keys.sort()
	for item in keys:
	    if type(self.__dict__[item]) is str:
		print "%-20s %s" % (item, self.__dict__[item])
	    elif isinstance(self.__dict__[item], versions.Version):
		print "%-20s %s" % (item, self.__dict__[item].asString())

    def __init__(self):
	self.reppath = "/var/lib/srsrep"
	self.root = "/"
	self.sourcepath = "/usr/src/srs/sources"
	self.buildpath = "/usr/src/srs/builds"
	self.packagenamespace = ":localhost"
	self.defaultbranch = None
	self.lookaside = "/var/cache/srs"
	self.dbpath = "/var/lib/srsdb"

	self.read("/etc/srsrc")
	self.read(os.environ["HOME"] + "/" + ".srsrc")

	if self.defaultbranch:
	    self.defaultbranch = versions.VersionFromString(self.defaultbranch)
	else:
	    self.defaultbranch = versions.VersionFromString("/localhost@local")

	if self.defaultbranch.isVersion():
	    sys.stderr.write("The configured default branch %s specifies " +
		 "version, not a branch.\n" % self.defaultbranch.asString())

@@ -440,16 +440,293 @@
  = val%0A%0A
+%09if key == %22defaultbranch%22:%0A%09    self.defaultbranch = versions.VersionFromString(self.defaultbranch)%0A%0A%09    if self.defaultbranch.isVersion():%0A%09%09sys.stderr.write(%22The configured default branch %25s specifies %22 +%0A%09%09     %22version, not a branch.%5Cn%22 %25 self.defaultbranch.asString())%0A%0A
     def 
@@ -1302,16 +1302,85 @@
 b/srsdb%22
+%0A%09self.defaultbranch = versions.VersionFromString(%22/localhost@local%22)
 %0A%0A%09self.
@@ -1450,352 +1450,4 @@
 c%22)%0A
-%0A%09if self.defaultbranch:%0A%09    self.defaultbranch = versions.VersionFromString(self.defaultbranch)%0A%09else:%0A%09    self.defaultbranch = versions.VersionFromString(%22/localhost@local%22)%0A%0A%09if self.defaultbranch.isVersion():%0A%09    sys.stderr.write(%22The configured default branch %25s specifies %22 +%0A%09%09 %22version, not a branch.%5Cn%22 %25 self.defaultbranch.asString())%0A

allow --branch to work on the command line