GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bigkevmcd/go-configparser: An implementation of the Python ConfigParser in Go

BranchesTags
Open more actions menu

Latest commit

 

History

88 Commits
88 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
chainmap
chainmap
 
 
testdata
testdata
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
TODO.md
TODO.md
 
 
configparser.go
configparser.go
 
 
configparser_multiline_test.go
configparser_multiline_test.go
 
 
configparser_test.go
configparser_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
interpolation.go
interpolation.go
 
 
interpolation_test.go
interpolation_test.go
 
 
methods.go
methods.go
 
 
methods_test.go
methods_test.go
 
 
options.go
options.go
 
 
options_test.go
options_test.go
 
 
section.go
section.go
 
 

Repository files navigation

go-configparser Go

Go implementation of the Python ConfigParser class.

This can parse Python-compatible ConfigParser config files, including support for option interpolation.

Setup

  import (
    "github.com/bigkevmcd/go-configparser"
  )

Parsing configuration files

It's easy to parse a configuration file.

  p, err := configparser.NewConfigParserFromFile("example.cfg")
  if err != nil {
    ...
  }

Methods

The ConfigParser implements most of the Python ConfigParser API

  v, err := p.Get("section", "option")
  err = p.Set("section", "newoption", "value")

  s := p.Sections()

Interpolation

The ConfigParser implements interpolation in the same format as the Python implementation.

Given the configuration

  [DEFAULTS]
  dir: testing

  [testing]
  something: %(dir)s/whatever
  v, err := p.GetInterpolated("testing, something")

It's also possible to override the values to use when interpolating values by providing a Dict to lookup values in.

  d := make(configparser.Dict)
  d["dir"] = "/a/non/existent/path"
  result, err := p.GetInterpolatedWithVars("testing", "something", d)

Will get testing/whatever as the value

Options

The ConfigParser supports almost all custom options available in the Python version.

  • Delimiters - allows to set custom key-value pair delimiters.
  • CommentPrefixes - allows to set custom comment line prefix. If line starts with one of the given Prefixes it will be passed during parsing.
  • InlineCommentPrefixes - allows to set custom inline comment delimiter. This option checks if the line contains any of the given Prefixes and if so, splits the string by the prefix and returns the 0 index of the slice.
  • MultilinePrefixes - allows to set custom multiline values prefixes. This option checks if the line starts with one of the given Prefixes and if so, counts it as a part of the current value.
  • Strict - if set to true, parser will return new wrapped ErrAlreadyExist for duplicates of sections or options in one source.
  • AllowEmptyLines - if set to true allows multiline values to include empty lines as their part. Otherwise the value will be parsed until an empty line or the line which does not start with one of the allowed multiline prefixes.
  • Interpolation - allows to set custom behaviour for values interpolation. Interface was added, which defaults to chainmap.ChainMap instance.
type Interpolator interface {
	Add(...chainmap.Dict)
	Len() int
	Get(string) string
}
  • Converters - allows to set custom values parsers.
type ConvertFunc func(string) (any, error)

ConvertFunc can modify requested value if needed e.g.,

package main

import (
	"fmt"
	"strings"

	"github.com/bigkevmcd/go-configparser"
)

func main() {
	stringConv := func(s string) (any, error) {
		return s + "_updated", nil
	}

	conv := configparser.Converter{
		configparser.String: stringConv,
	}

	p, err := configparser.ParseReaderWithOptions(
		strings.NewReader("[section]\noption=value\n\n"),
		configparser.Converters(conv),
	)
	// handle err

	v, err := p.Get("section", "option")
	// handle err

	fmt.Println(v == "value_updated") // true
}

Those functions triggered inside ConfigParser.Get* methods if presented and wraps the return value.

type Converter map[string]ConvertFunc

Converter is a map type, which supports int (for int64), string, bool, float (for float64) keys.


Default options, which are always preset:

func defaultOptions() *options {
	return &options{
		interpolation:     chainmap.New(),
		defaultSection:    defaultSectionName,
		delimiters:        ":=",
		commentPrefixes:   Prefixes{"#", ";"},
		multilinePrefixes: Prefixes{"\t", " "},
		converters: Converter{
			StringConv: defaultGet,
			IntConv:    defaultGetInt64,
			FloatConv:  defaultGetFloat64,
			BoolConv:   defaultGetBool,
		},
	}
}

About

An implementation of the Python ConfigParser in Go

Resources

Readme
MIT license
Activity

Stars

30 stars

Watchers

1 watching

Forks

19 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.