ConfigParser

This library implements a parser for config files in the style of Windows INI, as parsed by the Python configparser library.

The ConfigParser module includes routines that can parse a file, the contents of a string, or from a stream of lines.

To parse the content of a config file call the parse_file function and pass the file’s path:

{:ok, parse_result} = ConfigParser.parse_file("/path/to/file")

To parse config information out of a string, call the parse_string method:

{:ok, parse_result} = ConfigParser.parse_string("""
  [interesting_config]
  config_key = some interesting value
  """)

Given a stream whose elements represent the successive lines of a config file, the library can parse the content of the stream:

fake_stream = ["[section]", "key1 = value2", "key2:value2"] 
  |> Stream.map(&(&1))

{:ok, parse_result} = ConfigParser.parse_stream(fake_stream)

As mentioned previously the result of doing the parsing is a tuple. If successful, the first element of the tupe is :ok and the second element is the parsed result.

If the parser encounters an error, then the first part of the tuple will be the atom :error and the second element will be a string describing the error that was encountered:

{:error, "Syntax Error on line 3"}
Source

Summary

get(parser_results, section, key, options \\ %{})

Return the value for the option with the given key

getboolean(parser_results, section, key, options \\ %{})

This is a convenience routine which calls ConfigParser.get then tries to construct a boolean value from the result

getfloat(parser_results, section, key, options \\ %{})

This is a convenience routine which calls ConfigParser.get then tries to construct a float value from the result

getint(parser_results, section, key, options \\ %{})

This is a convenience routine which calls ConfigParser.get then tries to construct a integer value from the result

has_option?(parser_results, section, option)

returns true if the parse results define the given option in the section provided

has_section?(parser_results, which_section)

true if the named section is found in the config parser results

options(parser_results, in_section)

return a map with the options in the given section. If the section is not found, returns an empty map

parse_file(config_file_path)

Accepts config_file_path, a file system path to a config file. Attempts to opens and parses the contents of that file

parse_stream(line_stream)

Parses a stream whose elements should be strings representing the individual lines of a config file

parse_string(config_string)

Parse a string as if it was the content of a config file

sections(parser_results)

Return a list of sections in the given config parser state

Functions

get(parser_results, section, key, options \\ %{})

Return the value for the option with the given key.

You can change the way values are looked up using the options map. The following keys are recognized:

  • :raw - reserved for future enhancements
  • :vars - a map of keys and values.
  • :fallback - a value to return if the option given by option_key is not found

    The routine searches for a value with the given key in the :vars map if provided, then in the given section from the parse result.

    If no value is found, and the options map has a :fallback key, the value associated with that key will be returned.

    If all else fails, the routine returns nil

Source
getboolean(parser_results, section, key, options \\ %{})

This is a convenience routine which calls ConfigParser.get then tries to construct a boolean value from the result.

An option value of “true”, “1”, “yes”, or “on” evaluates to true An options value of “false”, “0”, “no”, or “off” evaluates to false

See ConfigParser.get for explainations of the options.

Source
getfloat(parser_results, section, key, options \\ %{})

This is a convenience routine which calls ConfigParser.get then tries to construct a float value from the result.

See ConfigParser.get for explainations of the options.

Source
getint(parser_results, section, key, options \\ %{})

This is a convenience routine which calls ConfigParser.get then tries to construct a integer value from the result.

See ConfigParser.get for explainations of the options.

Source
has_option?(parser_results, section, option)

returns true if the parse results define the given option in the section provided

Source
has_section?(parser_results, which_section)

true if the named section is found in the config parser results

Source
options(parser_results, in_section)

return a map with the options in the given section. If the section is not found, returns an empty map

Source
parse_file(config_file_path)

Accepts config_file_path, a file system path to a config file. Attempts to opens and parses the contents of that file.

Source
parse_stream(line_stream)

Parses a stream whose elements should be strings representing the individual lines of a config file.

Source
parse_string(config_string)

Parse a string as if it was the content of a config file.

Source
sections(parser_results)

Return a list of sections in the given config parser state

Source