aboutsummaryrefslogtreecommitdiff
blob: 0ebac77e024cb967f5d468503b5c09c6a3f267c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: UTF-8 -*-

# Copyright 2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# Author: Saleem Abdulrasool <compnerd@gentoo.org>
# Maintainer: Gentoo Java Herd <java@gentoo.org>
# Java Subsystem Configuration Utility for Gentoo Linux

# ChangeLog
# Saleem A. <compnerd@gentoo.org>
#     December 30, 2004 - Initial Rewrite
#                       - Based on the collective works of the following:
#                         {karltk,axxo,aether}@gentoo.org

from Errors import *
import os

class EnvFileParser:
   config = {}
   
   def __init__(self, file):
      self.config.clear()

      # Create the config from the file
      if not os.path.isfile(file):
         raise InvalidConfigError(file)
      if not os.access(file, os.R_OK):
         raise PermissionError

      stream = open(file, 'r')
      read = stream.readline()
      while read:
         if read.isspace() or read == '' or read.startswith('#'):
            read = stream.readline()
         else:
            read = read.split('\n')[0]
            name, value = read.split('=')

            if value == '':
               raise InvalidConfigError(file)

            value = value.strip('\\').strip('\'\"')

            values  = value.split(':')
            for item in values:
               if item.find('${') >= 0:
                  item = item[item.find('${')+2:item.find('}')]
                  
                  if self.config.has_key(item):
                     val = self.config[item]
                  else:
                     val = ''
                  
                  value = value.replace('${%s}' % item, val)
               else:
                  if self.config.has_key(item):
                     val = self.config[item]
                  else:
                     val = ''

                  value = value.replace('$%s' % item, val)

            self.config[name] = value

            read = stream.readline()
      stream.close()

   def get_config(self):
      return self.config.copy()
# vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3: