From 4f5a70e700623da0b799d6f861bd7fb1a6dbbc85 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Mon, 24 Apr 2017 01:13:57 +0200 Subject: Extend the readme --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index f74be3e..5297220 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,63 @@ # Config A perl 6 library for reading and writing configuration files. +## Installation +This module can be installed using `zef`: + +``` +zef install Config +``` + +Depending on the type of configuration file you want to work on, you will need a +`Config::Parser::` module as well. If you just want an easy-to-use configuration +object without reading/writing a file, no parser is needed. + +## Usage +Include the `Config` module in your script, instantiate it and use it as you +please. + +```perl6 +use Config; + +my $config = Config.new(); + +# loading a simple configuration hash +$config.read({ + keyOne => "value", + keyTwo => { + NestedKey => "other value" + } +}); + +# loading a configuration files +$config.read("/etc/config.yaml"); + +# retrieving a simple key +$config.get("keyOne"); + +# retrieving a nested key +$config.get("keyTwo.NestedKey"); +``` + +### Available parsers +Because there's so many ways to structure your configuration files, the parsers +for these are their own modules. This allows for easy implementing new parsers, +or providing a custom parser for your project's configuration file. + +The parser will be loaded during runtime, but you have to make sure it is +installed yourself. + +The following parsers are available: + +- [`Config::Parser::yaml`](https://github.com/scriptkitties/p6-Config-Parser-yaml) + +### Writing your own parser +If you want to make your own parser, simply make a new class in the +`Config::Parser` namespace. This class should extend the `Config::Parser` class, +and implement the `read` and `write` methods. The `read` method *must* return a +`Hash`. The `write` method *must* return a `Bool`, `True` when writing was +successful, `False` if not. + ## License This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software -- cgit v1.1 From 940c920c6ca61e0690ad5a8c8e1aaa65e06bc46f Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Mon, 24 Apr 2017 01:22:57 +0200 Subject: Add build status icon --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5297220..6b9c52b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Config A perl 6 library for reading and writing configuration files. +[![Build Status](https://travis-ci.org/scriptkitties/p6-Config.svg?branch=master)](https://travis-ci.org/scriptkitties/p6-Config) + ## Installation This module can be installed using `zef`: -- cgit v1.1 From 10c2bd55f7fcdae2235a2df1cd9373c23477f93c Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Mon, 24 Apr 2017 08:45:45 +0200 Subject: Sort Config methods alphabetically --- lib/Config.pm6 | 114 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 16aa7f7..4078d73 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -14,36 +14,6 @@ class Config is export has $!path; has $!parser; - multi method read() - { - return self.load($!path); - } - - multi method read(Str $path, Str $parser = "") - { - Config::Exception::FileNotFoundException.new.throw() unless $path.IO.f; - - $!parser = self.get-parser($path, $parser); - - require ::($!parser); - $!content = ::($!parser).read($path); - - return True; - } - - multi method read(Hash $hash) - { - $!content = $hash; - } - - method write(Str $path, Str $parser = "") - { - $parser = self.get-parser($path, $parser); - - require ::($parser); - return ::($parser).write($path, $!content); - } - multi method get(Str $key, Any $default = Nil) { my $index = $!content; @@ -70,33 +40,6 @@ class Config is export $index; } - method has(Str $key) { - my $index = $!content; - - for $key.split(".") -> $part { - return False unless defined($index{$part}); - - $index = $index{$part}; - } - - True; - } - - method set(Str $key, Any $value) - { - my $index := $!content; - - for $key.split(".") -> $part { - $index{$part} = {} unless defined($index{$part}); - - $index := $index{$part}; - } - - $index = $value; - - self; - } - method get-parser(Str $path, Str $parser = "") { if ($parser ne "") { @@ -128,4 +71,61 @@ class Config is export return Config::Type::unknown; } + + method has(Str $key) { + my $index = $!content; + + for $key.split(".") -> $part { + return False unless defined($index{$part}); + + $index = $index{$part}; + } + + True; + } + + multi method read() + { + return self.load($!path); + } + + multi method read(Str $path, Str $parser = "") + { + Config::Exception::FileNotFoundException.new.throw() unless $path.IO.f; + + $!parser = self.get-parser($path, $parser); + + require ::($!parser); + $!content = ::($!parser).read($path); + + return True; + } + + multi method read(Hash $hash) + { + $!content = $hash; + } + + method set(Str $key, Any $value) + { + my $index := $!content; + + for $key.split(".") -> $part { + $index{$part} = {} unless defined($index{$part}); + + $index := $index{$part}; + } + + $index = $value; + + self; + } + + method write(Str $path, Str $parser = "") + { + $parser = self.get-parser($path, $parser); + + require ::($parser); + return ::($parser).write($path, $!content); + } } -- cgit v1.1 From ede6b8c4cfac8c5eed668ba8357f9e8b11ccbc43 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Mon, 24 Apr 2017 08:50:55 +0200 Subject: Extend the examples --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6b9c52b..94daa4c 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ use Config; my $config = Config.new(); -# loading a simple configuration hash +# load a simple configuration hash $config.read({ keyOne => "value", keyTwo => { @@ -31,14 +31,23 @@ $config.read({ } }); -# loading a configuration files +# load a configuration files $config.read("/etc/config.yaml"); -# retrieving a simple key +# load a configuration file with a specific parser +$config.read("/etc/config", "Config::Parser::ini"); + +# retrieve a simple key $config.get("keyOne"); -# retrieving a nested key +# retrieve a nested key $config.get("keyTwo.NestedKey"); + +# write out the configuration file +$config.write("/etc/config.yaml"); + +# write out the configuration in another format +$config.write("/etc/config.json", "Config::Parser::json"); ``` ### Available parsers -- cgit v1.1 From b01a053e3d4012064a667745634010dd654b4777 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Mon, 24 Apr 2017 11:12:08 +0200 Subject: Update exceptions with more meaningful messages --- META6.json | 2 +- lib/Config.pm6 | 24 +++++++++++++++++----- lib/Config/Exception/FileNotFoundException.pm6 | 4 +++- lib/Config/Exception/MissingParserException.pm6 | 13 ++++++++++++ .../Exception/UnimplementedMethodException.pm6 | 4 +++- lib/Config/Exception/UnknownTypeException.pm6 | 4 +++- lib/Config/Exception/UnsupportedTypeException.pm6 | 11 ---------- lib/Config/Parser.pm6 | 8 ++++++-- t/reading.t | 15 ++++++++++++++ t/test-stub | 0 10 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 lib/Config/Exception/MissingParserException.pm6 delete mode 100644 lib/Config/Exception/UnsupportedTypeException.pm6 create mode 100644 t/reading.t create mode 100644 t/test-stub diff --git a/META6.json b/META6.json index e472a2d..a3614c3 100644 --- a/META6.json +++ b/META6.json @@ -15,7 +15,7 @@ "Config::Exception::FileNotFoundException": "lib/Config/Exception/FileNotFoundException.pm6", "Config::Exception::UnimplementedMethodException": "lib/Config/Exception/UnimplementedMethodException.pm6", "Config::Exception::UnknownTypeException": "lib/Config/Exception/UnknownTypeException.pm6", - "Config::Exception::UnsupportedTypeException": "lib/Config/Exception/UnsupportedTypeException.pm6" + "Config::Exception::MissingParserException": "lib/Config/Exception/MissingParserException.pm6" }, "authors": [ "Patrick Spek " diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 4078d73..2fa01bf 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -2,7 +2,7 @@ use v6.c; -use Config::Exception::UnsupportedTypeException; +use Config::Exception::MissingParserException; use Config::Exception::UnknownTypeException; use Config::Exception::FileNotFoundException; use Config::Type; @@ -48,7 +48,9 @@ class Config is export my $type = self.get-parser-type($path); - Config::Exception::UnknownTypeException.new.throw() if $type eq Config::Type::unknown; + Config::Exception::UnknownTypeException.new( + type => $type + ).throw() if $type eq Config::Type::unknown; "Config::Parser::" ~ $type; } @@ -91,12 +93,24 @@ class Config is export multi method read(Str $path, Str $parser = "") { - Config::Exception::FileNotFoundException.new.throw() unless $path.IO.f; + Config::Exception::FileNotFoundException.new( + path => $path + ).throw() unless $path.IO.f; $!parser = self.get-parser($path, $parser); - require ::($!parser); - $!content = ::($!parser).read($path); + try { + CATCH { + when X::CompUnit::UnsatisfiedDependency { + Config::Exception::MissingParserException.new( + parser => $parser + ).throw(); + } + } + + require ::($!parser); + $!content = ::($!parser).read($path); + } return True; } diff --git a/lib/Config/Exception/FileNotFoundException.pm6 b/lib/Config/Exception/FileNotFoundException.pm6 index 74d7bc1..b00f956 100644 --- a/lib/Config/Exception/FileNotFoundException.pm6 +++ b/lib/Config/Exception/FileNotFoundException.pm6 @@ -4,8 +4,10 @@ use v6.c; class Config::Exception::FileNotFoundException is Exception { + has Str $.path; + method message() { - "Could not find file" + "Could not find file at $!path" } } diff --git a/lib/Config/Exception/MissingParserException.pm6 b/lib/Config/Exception/MissingParserException.pm6 new file mode 100644 index 0000000..484026a --- /dev/null +++ b/lib/Config/Exception/MissingParserException.pm6 @@ -0,0 +1,13 @@ +#! /usr/bin/env false + +use v6.c; + +class Config::Exception::MissingParserException is Exception +{ + has Str $.parser; + + method message() + { + "$!parser is not a valid parser. Are you sure its installed?" + } +} diff --git a/lib/Config/Exception/UnimplementedMethodException.pm6 b/lib/Config/Exception/UnimplementedMethodException.pm6 index ae87db9..10af289 100644 --- a/lib/Config/Exception/UnimplementedMethodException.pm6 +++ b/lib/Config/Exception/UnimplementedMethodException.pm6 @@ -4,8 +4,10 @@ use v6.c; class Config::Exception::UnimplementedMethodException is Exception { + has Str $.method; + method message() { - "This method is not implemented" + "The $!method method is not implemented" } } diff --git a/lib/Config/Exception/UnknownTypeException.pm6 b/lib/Config/Exception/UnknownTypeException.pm6 index de58755..095572b 100644 --- a/lib/Config/Exception/UnknownTypeException.pm6 +++ b/lib/Config/Exception/UnknownTypeException.pm6 @@ -4,8 +4,10 @@ use v6.c; class Config::Exception::UnknownTypeException is Exception { + has Str $.file; + method message() { - "Could not deduce loader type." + "Could not deduce loader type for $!file." } } diff --git a/lib/Config/Exception/UnsupportedTypeException.pm6 b/lib/Config/Exception/UnsupportedTypeException.pm6 deleted file mode 100644 index c2f6f10..0000000 --- a/lib/Config/Exception/UnsupportedTypeException.pm6 +++ /dev/null @@ -1,11 +0,0 @@ -#! /usr/bin/env false - -use v6.c; - -class Config::Exception::UnsupportedTypeException is Exception -{ - method message() - { - "No parser support for the given file. Have you imported a correct parser?" - } -} diff --git a/lib/Config/Parser.pm6 b/lib/Config/Parser.pm6 index 23effc0..1a99ad6 100644 --- a/lib/Config/Parser.pm6 +++ b/lib/Config/Parser.pm6 @@ -8,11 +8,15 @@ class Config::Parser { method read(Str $path --> Hash) { - Config::Exception::UnimplementedMethodException.new.throw(); + Config::Exception::UnimplementedMethodException.new( + method => "read" + ).throw(); } method write(Str $path, Hash $config --> Hash) { - Config::Exception::UnimplementedMethodException.new.throw(); + Config::Exception::UnimplementedMethodException.new( + method => "write" + ).throw(); } } diff --git a/t/reading.t b/t/reading.t new file mode 100644 index 0000000..52859af --- /dev/null +++ b/t/reading.t @@ -0,0 +1,15 @@ +#! /usr/bin/env perl6 + +use v6.c; +use Test; +use lib "lib"; + +plan 3; + +use Config; + +my $config = Config.new(); + +throws-like { $config.read("nonexistant-config") }, Config::Exception::FileNotFoundException, "Reading nonexisting file"; +throws-like { $config.read("t/test-stub") }, Config::Exception::UnknownTypeException, "Reading file of unknown type"; +throws-like { $config.read("t/test-stub", "Config::Parser:NoSuchParserForTest") }, Config::Exception::MissingParserException, "Using non-existing parser"; diff --git a/t/test-stub b/t/test-stub new file mode 100644 index 0000000..e69de29 -- cgit v1.1