Browse Source

First import

master
Stephen Weinberg 12 years ago
commit
c49f1b4ed9
  1. 1
      AUTHORS
  2. 10
      COPYRIGHT
  3. 240
      conf.go
  4. 176
      get.go
  5. 101
      read.go
  6. 62
      write.go

1
AUTHORS

@ -0,0 +1 @@
Stephen Weinberg <stephen@q5comm.com>

10
COPYRIGHT

@ -0,0 +1,10 @@
Copyright (c) 2010, Stephen Weinberg
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of goconf nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

240
conf.go

@ -0,0 +1,240 @@
// This package implements a parser for configuration files.
// This allows easy reading and writing of structured configuration files.
//
// Given a sample configuration file:
//
// [default]
// host=www.example.com
// protocol=http://
// base-url=%(protocol)s%(host)s
//
// [service-1]
// url=%(base-url)s/some/path
// delegation : on
// maxclients=200 # do not set this higher
// comments=This is a multi-line
// entry ; And this is a comment
//
// To read this configuration file, do:
//
// c, err := configfile.ReadConfigFile("config.cfg");
// c.GetString("service-1", "url"); // result is string :http://www.example.com/some/path"
// c.GetInt("service-1", "maxclients"); // result is int 200
// c.GetBool("service-1", "delegation"); // result is bool true
// c.GetString("service-1", "comments"); // result is string "This is a multi-line\nentry"
//
// Note the support for unfolding variables (such as %(base-url)s), which are read from the special
// (reserved) section name [default].
//
// A new configuration file can also be created with:
//
// c := configfile.NewConfigFile();
// c.AddSection("section");
// c.AddOption("section", "option", "value");
// c.WriteConfigFile("config.cfg", 0644, "A header for this file"); // use 0644 as file permission
//
// This results in the file:
//
// # A header for this file
// [section]
// option=value
//
// Note that sections and options are case-insensitive (values are case-sensitive)
// and are converted to lowercase when saved to a file.
//
// The functionality and workflow is loosely based on the configparser.py package
// of the Python Standard Library.
package conf
import (
"regexp"
"strings"
"fmt"
)
// ConfigFile is the representation of configuration settings.
// The public interface is entirely through methods.
type ConfigFile struct {
data map[string]map[string]string; // Maps sections to options to values.
}
const (
// Get Errors
SectionNotFound = iota
OptionNotFound
MaxDepthReached
// Read Errors
BlankSection
// Get and Read Errors
CouldNotParse
)
var (
DefaultSection = "default"; // Default section name (must be lower-case).
DepthValues = 200; // Maximum allowed depth when recursively substituing variable names.
// Strings accepted as bool.
BoolStrings = map[string]bool{
"t": true,
"true": true,
"y": true,
"yes": true,
"on": true,
"1": true,
"f": false,
"false": false,
"n": false,
"no": false,
"off": false,
"0": false,
};
varRegExp = regexp.MustCompile(`%\(([a-zA-Z0-9_.\-]+)\)s`);
)
// AddSection adds a new section to the configuration.
// It returns true if the new section was inserted, and false if the section already existed.
func (c *ConfigFile) AddSection(section string) bool {
section = strings.ToLower(section);
if _, ok := c.data[section]; ok {
return false
}
c.data[section] = make(map[string]string);
return true;
}
// RemoveSection removes a section from the configuration.
// It returns true if the section was removed, and false if section did not exist.
func (c *ConfigFile) RemoveSection(section string) bool {
section = strings.ToLower(section);
switch _, ok := c.data[section]; {
case !ok:
return false
case section == DefaultSection:
return false // default section cannot be removed
default:
for o, _ := range c.data[section] {
c.data[section][o] = "", false
}
c.data[section] = nil, false;
}
return true;
}
// AddOption adds a new option and value to the configuration.
// It returns true if the option and value were inserted, and false if the value was overwritten.
// If the section does not exist in advance, it is created.
func (c *ConfigFile) AddOption(section string, option string, value string) bool {
c.AddSection(section); // make sure section exists
section = strings.ToLower(section);
option = strings.ToLower(option);
_, ok := c.data[section][option];
c.data[section][option] = value;
return !ok;
}
// RemoveOption removes a option and value from the configuration.
// It returns true if the option and value were removed, and false otherwise,
// including if the section did not exist.
func (c *ConfigFile) RemoveOption(section string, option string) bool {
section = strings.ToLower(section);
option = strings.ToLower(option);
if _, ok := c.data[section]; !ok {
return false
}
_, ok := c.data[section][option];
c.data[section][option] = "", false;
return ok;
}
// NewConfigFile creates an empty configuration representation.
// This representation can be filled with AddSection and AddOption and then
// saved to a file using WriteConfigFile.
func NewConfigFile() *ConfigFile {
c := new(ConfigFile);
c.data = make(map[string]map[string]string);
c.AddSection(DefaultSection); // default section always exists
return c;
}
func stripComments(l string) string {
// comments are preceded by space or TAB
for _, c := range []string{" ;", "\t;", " #", "\t#"} {
if i := strings.Index(l, c); i != -1 {
l = l[0:i]
}
}
return l;
}
func firstIndex(s string, delim []byte) int {
for i := 0; i < len(s); i++ {
for j := 0; j < len(delim); j++ {
if s[i] == delim[j] {
return i
}
}
}
return -1;
}
type GetError struct {
Reason int
ValueType string
Value string
Section string
Option string
}
func (err GetError) String() string {
switch err.Reason {
case SectionNotFound:
return fmt.Sprintf("section '%s' not found", err.Section)
case OptionNotFound:
return fmt.Sprintf("option '%s' not found in section '%s'", err.Option, err.Section)
case CouldNotParse:
return fmt.Sprintf("could not parse %s value '%s'", err.ValueType, err.Value)
case MaxDepthReached:
return fmt.Sprintf("possible cycle while unfolding variables: max depth of %d reached", DepthValues)
}
return "invalid get error"
}
type ReadError struct {
Reason int
Line string
}
func (err ReadError) String() string {
switch err.Reason {
case BlankSection:
return "empty section name not allowed"
case CouldNotParse:
return fmt.Sprintf("could not parse line: %s", err.Line)
}
return "invalid read error"
}

176
get.go

@ -0,0 +1,176 @@
package conf
import (
"os"
"strings"
"strconv"
)
// GetSections returns the list of sections in the configuration.
// (The default section always exists.)
func (c *ConfigFile) GetSections() (sections []string) {
sections = make([]string, len(c.data));
i := 0;
for s, _ := range c.data {
sections[i] = s;
i++;
}
return sections;
}
// HasSection checks if the configuration has the given section.
// (The default section always exists.)
func (c *ConfigFile) HasSection(section string) bool {
_, ok := c.data[strings.ToLower(section)];
return ok;
}
// GetOptions returns the list of options available in the given section.
// It returns an error if the section does not exist and an empty list if the section is empty.
// Options within the default section are also included.
func (c *ConfigFile) GetOptions(section string) (options []string, err os.Error) {
section = strings.ToLower(section);
if _, ok := c.data[section]; !ok {
return nil, GetError{SectionNotFound, "", "", section, ""}
}
options = make([]string, len(c.data[DefaultSection])+len(c.data[section]));
i := 0;
for s, _ := range c.data[DefaultSection] {
options[i] = s;
i++;
}
for s, _ := range c.data[section] {
options[i] = s;
i++;
}
return options, nil;
}
// HasOption checks if the configuration has the given option in the section.
// It returns false if either the option or section do not exist.
func (c *ConfigFile) HasOption(section string, option string) bool {
section = strings.ToLower(section);
option = strings.ToLower(option);
if _, ok := c.data[section]; !ok {
return false
}
_, okd := c.data[DefaultSection][option];
_, oknd := c.data[section][option];
return okd || oknd;
}
// GetRawString gets the (raw) string value for the given option in the section.
// The raw string value is not subjected to unfolding, which was illustrated in the beginning of this documentation.
// It returns an error if either the section or the option do not exist.
func (c *ConfigFile) GetRawString(section string, option string) (value string, err os.Error) {
section = strings.ToLower(section);
option = strings.ToLower(option);
if _, ok := c.data[section]; ok {
if value, ok = c.data[section][option]; ok {
return value, nil
}
return "", GetError{OptionNotFound, "", "", section, option};
}
return "", GetError{SectionNotFound, "", "", section, option};
}
// GetString gets the string value for the given option in the section.
// If the value needs to be unfolded (see e.g. %(host)s example in the beginning of this documentation),
// then GetString does this unfolding automatically, up to DepthValues number of iterations.
// It returns an error if either the section or the option do not exist, or the unfolding cycled.
func (c *ConfigFile) GetString(section string, option string) (value string, err os.Error) {
value, err = c.GetRawString(section, option);
if err != nil {
return "", err
}
section = strings.ToLower(section);
var i int;
for i = 0; i < DepthValues; i++ { // keep a sane depth
vr := varRegExp.ExecuteString(value);
if len(vr) == 0 {
break
}
noption := value[vr[2]:vr[3]];
noption = strings.ToLower(noption);
nvalue, _ := c.data[DefaultSection][noption]; // search variable in default section
if _, ok := c.data[section][noption]; ok {
nvalue = c.data[section][noption]
}
if nvalue == "" {
return "", GetError{OptionNotFound, "", "", section, option}
}
// substitute by new value and take off leading '%(' and trailing ')s'
value = value[0:vr[2]-2] + nvalue + value[vr[3]+2:];
}
if i == DepthValues {
return "", GetError{MaxDepthReached, "", "", section, option}
}
return value, nil;
}
// GetInt has the same behaviour as GetString but converts the response to int.
func (c *ConfigFile) GetInt(section string, option string) (value int, err os.Error) {
sv, err := c.GetString(section, option);
if err == nil {
value, err = strconv.Atoi(sv)
if err != nil {
err = GetError{CouldNotParse, "int", sv, section, option}
}
}
return value, err;
}
// GetFloat has the same behaviour as GetString but converts the response to float.
func (c *ConfigFile) GetFloat(section string, option string) (value float, err os.Error) {
sv, err := c.GetString(section, option);
if err == nil {
value, err = strconv.Atof(sv)
if err != nil {
err = GetError{CouldNotParse, "float", sv, section, option}
}
}
return value, err;
}
// GetBool has the same behaviour as GetString but converts the response to bool.
// See constant BoolStrings for string values converted to bool.
func (c *ConfigFile) GetBool(section string, option string) (value bool, err os.Error) {
sv, err := c.GetString(section, option);
if err != nil {
return false, err
}
value, ok := BoolStrings[strings.ToLower(sv)];
if !ok {
return false, GetError{CouldNotParse, "bool", sv, section, option}
}
return value, nil;
}

101
read.go

@ -0,0 +1,101 @@
package conf
import (
"io"
"os"
"bytes"
"bufio"
"strings"
)
// ReadConfigFile reads a file and returns a new configuration representation.
// This representation can be queried with GetString, etc.
func ReadConfigFile(fname string) (c *ConfigFile, err os.Error) {
var file *os.File;
if file, err = os.Open(fname, os.O_RDONLY, 0); err != nil {
return nil, err
}
c = NewConfigFile();
if err = c.Read(file); err != nil {
return nil, err
}
if err = file.Close(); err != nil {
return nil, err
}
return c, nil
}
func ReadConfigBytes(conf []byte) (c *ConfigFile, err os.Error) {
buf := bytes.NewBuffer(conf)
c = NewConfigFile();
if err = c.Read(buf); err != nil {
return nil, err
}
return c, err
}
// Read reads an io.Reader and returns a configuration representation. This
// representation can be queried with GetString, etc.
func (c *ConfigFile) Read(reader io.Reader) (err os.Error) {
buf := bufio.NewReader(reader)
var section, option string;
section = "default"
for {
l, err := buf.ReadString('\n'); // parse line-by-line
if err == os.EOF {
break
} else if err != nil {
return err
}
l = strings.TrimSpace(l);
// switch written for readability (not performance)
switch {
case len(l) == 0: // empty line
continue
case l[0] == '#': // comment
continue
case l[0] == ';': // comment
continue
case len(l) >= 3 && strings.ToLower(l[0:3]) == "rem": // comment (for windows users)
continue
case l[0] == '[' && l[len(l)-1] == ']': // new section
option = ""; // reset multi-line value
section = strings.TrimSpace(l[1 : len(l)-1]);
c.AddSection(section);
case section == "": // not new section and no section defined so far
return ReadError{BlankSection, l}
default: // other alternatives
i := firstIndex(l, []byte{'=', ':'});
switch {
case i > 0: // option and value
i := firstIndex(l, []byte{'=', ':'});
option = strings.TrimSpace(l[0:i]);
value := strings.TrimSpace(stripComments(l[i+1:]));
c.AddOption(section, option, value);
case section != "" && option != "": // continuation of multi-line value
prev, _ := c.GetRawString(section, option);
value := strings.TrimSpace(stripComments(l));
c.AddOption(section, option, prev+"\n"+value);
default:
return ReadError{CouldNotParse, l}
}
}
}
return nil;
}

62
write.go

@ -0,0 +1,62 @@
package conf
import (
"os"
"io"
"bytes"
)
// WriteConfigFile saves the configuration representation to a file.
// The desired file permissions must be passed as in os.Open.
// The header is a string that is saved as a comment in the first line of the file.
func (c *ConfigFile) WriteConfigFile(fname string, perm int, header string) (err os.Error) {
var file *os.File;
if file, err = os.Open(fname, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, perm); err != nil {
return err
}
if err = c.Write(file, header); err != nil {
return err
}
return file.Close();
}
func (c *ConfigFile) WriteConfigBytes(header string) (config []byte) {
buf := bytes.NewBuffer(nil)
c.Write(buf, header)
return buf.Bytes()
}
func (c *ConfigFile) Write(writer io.Writer, header string) (err os.Error) {
buf := bytes.NewBuffer(nil)
if header != "" {
if _, err = buf.WriteString("# " + header + "\n"); err != nil {
return err
}
}
for section, sectionmap := range c.data {
if section == DefaultSection && len(sectionmap) == 0 {
continue // skip default section if empty
}
if _, err = buf.WriteString("[" + section + "]\n"); err != nil {
return err
}
for option, value := range sectionmap {
if _, err = buf.WriteString(option + "=" + value + "\n"); err != nil {
return err
}
}
if _, err = buf.WriteString("\n"); err != nil {
return err
}
}
buf.WriteTo(writer)
return nil;
}
Loading…
Cancel
Save