-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_config.rb
82 lines (78 loc) · 2.72 KB
/
project_config.rb
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
73
74
75
76
77
78
79
80
81
82
# @BEGIN_LICENSE
#
# Halyard - Multimedia authoring and playback system
# Copyright 1993-2009 Trustees of Dartmouth College
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# @END_LICENSE
require 'pathname'
# This reads a projcet configuration file from config/project.conf.
# The format is a subset of the semi-standard INI file format (of which
# variations are supported by various Windows utilities, Samba, wxWidgets,
# and Git). It supports sections delimited by [ and ], key value pairs
# separated with an =, and blank lines. It explicitly errors on common
# INI syntax that it does not understand, such as "quoted strings", \
# escapes, and comments denoted by ; and #.
#
# After each section heading, every key is conisdered to be in that
# section untile the next section heading. This is represented by the
# key being stored as "section.key". Thus,
# [section]
# key = val
# is equivalent to
# section.key = val
#
# When you create a ProjectConfig object, it looks for a config file at
# config/project.conf. It would perhaps be nice in the future to search
# up the directory hierarchy to find the top-level project dir, but it
# does not do that at this time; it simply looks for the file relative to
# the current working directory.
class ProjectConfig
KeyRE = /[a-zA-Z0-9._-]+/
ValRE = /[^";\\#]*[^";\\# \t]/
def initialize
@file = Pathname.new 'config/project.conf'
unless @file.exist?
raise "Could not find config/project.conf"
end
@items = { }
section = nil
@file.read.each_line do |line|
l = line.chomp
case l
when /^[ \t]*$/
# skip this line, do nothing
when /^[ \t]*\[(#{KeyRE})\][ \t]*$/
section = $1
when /^[ \t]*(#{KeyRE})[ \t]*=[ \t]*(#{ValRE})[ \t]*$/
key = if section then section + "." + $1 else $1 end
@items[key] = $2
else
raise <<EOF
config/project.conf: unrecognized format in line:
#{line}
EOF
end
end
end
def [] key
@items[key]
end
def print
@items.each { |key,val| puts "#{key}=#{val}"}
end
end