pycf 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +6 -0
- data/lib/pycf.rb +4 -0
- data/lib/pycf/dump.rb +24 -0
- data/lib/pycf/error.rb +22 -0
- data/lib/pycf/load.rb +70 -0
- data/lib/pycf/version.rb +3 -0
- data/pycf.gemspec +26 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08c37334c89642984bb15ba5505ac99d022c6659
|
4
|
+
data.tar.gz: f2d4f470e88cb78a17a3595301609d0308ffd129
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5046dae9c6e705bcb6e3ce66cd66be08f7851a20404d38323a0b6a29f9a2acb977af7fd64526a4793b75011fc57f83a850015f3e55ba56b448857e2feb098f8d
|
7
|
+
data.tar.gz: 953cf4888e1f9ed2e22b641a3055d0582f20ca253ad610ea058c190d25f2ea8e16ccb548d1717a0040b7d00a2b3eacacca43e14991a689e0bf1586b06ddc4021
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Genki Sugawara
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Pycf
|
2
|
+
|
3
|
+
Configuration file parser for [Python 2.7 basic configuration file](https://docs.python.org/2.7/library/configparser.html).
|
4
|
+
|
5
|
+
see [ConfigParser.py](https://github.com/python/cpython/blob/2.7/Lib/ConfigParser.py).
|
6
|
+
|
7
|
+
[](http://badge.fury.io/rb/pycf)
|
8
|
+
[](https://travis-ci.org/winebarrel/pycf)
|
9
|
+
[](https://coveralls.io/r/winebarrel/pycf?branch=master)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'pycf'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install pycf
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### load
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'pycf'
|
33
|
+
|
34
|
+
python_config = <<EOS
|
35
|
+
[DEFAULT]
|
36
|
+
ServerAliveInterval = 45
|
37
|
+
Compression = yes
|
38
|
+
CompressionLevel = 9
|
39
|
+
ForwardX11 = yes
|
40
|
+
|
41
|
+
[bitbucket.org]
|
42
|
+
User = hg
|
43
|
+
|
44
|
+
[topsecret.server.com]
|
45
|
+
Port = 50022
|
46
|
+
ForwardX11 = no
|
47
|
+
EOS
|
48
|
+
|
49
|
+
p Pycf.load(python_config)
|
50
|
+
# => {"DEFAULT"=>
|
51
|
+
# {"serveraliveinterval"=>"45",
|
52
|
+
# "compression"=>"yes",
|
53
|
+
# "compressionlevel"=>"9",
|
54
|
+
# "forwardx11"=>"yes"},
|
55
|
+
# "bitbucket.org"=>{"user"=>"hg"},
|
56
|
+
# "topsecret.server.com"=>{"port"=>"50022", "forwardx11"=>"no"}}
|
57
|
+
```
|
58
|
+
|
59
|
+
### dump
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require 'pycf'
|
63
|
+
require 'pp'
|
64
|
+
|
65
|
+
hash = {"DEFAULT"=>
|
66
|
+
{"serveraliveinterval"=>"45",
|
67
|
+
"compression"=>"yes",
|
68
|
+
"compressionlevel"=>"9",
|
69
|
+
"forwardx11"=>"yes"},
|
70
|
+
"bitbucket.org"=>{"user"=>"hg"},
|
71
|
+
"topsecret.server.com"=>{"port"=>"50022", "forwardx11"=>"no"}}
|
72
|
+
|
73
|
+
puts Pycf.dump(hash)
|
74
|
+
# => [DEFAULT]
|
75
|
+
# serveraliveinterval = 45
|
76
|
+
# compression = yes
|
77
|
+
# compressionlevel = 9
|
78
|
+
# forwardx11 = yes
|
79
|
+
# [bitbucket.org]
|
80
|
+
# user = hg
|
81
|
+
# [topsecret.server.com]
|
82
|
+
# port = 50022
|
83
|
+
# forwardx11 = no
|
84
|
+
```
|
data/Rakefile
ADDED
data/lib/pycf.rb
ADDED
data/lib/pycf/dump.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# https://github.com/python/cpython/blob/2.7/Lib/ConfigParser.py
|
2
|
+
# (License: https://github.com/python/cpython/blob/2.7/LICENSE)
|
3
|
+
module Pycf
|
4
|
+
def dump(hash)
|
5
|
+
unless hash.is_a?(Hash)
|
6
|
+
raise TypeError, "wrong argument type #{hash.class} (expected Hash)"
|
7
|
+
end
|
8
|
+
|
9
|
+
python_config = []
|
10
|
+
|
11
|
+
hash.each do |section, key_values|
|
12
|
+
python_config << "[#{section}]"
|
13
|
+
|
14
|
+
key_values.each do |key, value|
|
15
|
+
value = value.to_s.gsub("\n", "\n\t")
|
16
|
+
value = '""' if value.empty?
|
17
|
+
python_config << "#{key} = #{value}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
python_config.join("\n")
|
22
|
+
end
|
23
|
+
module_function :dump
|
24
|
+
end
|
data/lib/pycf/error.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# https://github.com/python/cpython/blob/2.7/Lib/ConfigParser.py
|
2
|
+
# (License: https://github.com/python/cpython/blob/2.7/LICENSE)
|
3
|
+
module Pycf
|
4
|
+
class ParsingError < StandardError
|
5
|
+
def initialize(lineno, line)
|
6
|
+
super(
|
7
|
+
error_message +
|
8
|
+
"\n\t[line %2d]: %s" % [lineno, line]
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
def error_message
|
13
|
+
'parsing errors.'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class MissingSectionHeaderError < ParsingError
|
18
|
+
def error_message
|
19
|
+
'no section headers.'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/pycf/load.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# https://github.com/python/cpython/blob/2.7/Lib/ConfigParser.py
|
2
|
+
# (License: https://github.com/python/cpython/blob/2.7/LICENSE)
|
3
|
+
module Pycf
|
4
|
+
SECTCRE = /\A\[(?<header>[^\]]+)\]/
|
5
|
+
OPTCRE_NV = /\A(?<option>[^:=\s][^:=]*)\s*(?:(?<vi>[:=])\s*(?<value>.*))?\z/ # everything up to eol
|
6
|
+
|
7
|
+
def load(python_config, option = {})
|
8
|
+
hash = {}
|
9
|
+
|
10
|
+
cursect = nil
|
11
|
+
optname = nil
|
12
|
+
|
13
|
+
python_config.split("\n").each_with_index do |line, lineno_minus_1|
|
14
|
+
lineno = lineno_minus_1 + 1
|
15
|
+
|
16
|
+
if line.split.empty? or line =~ /\A[#;]/
|
17
|
+
next
|
18
|
+
end
|
19
|
+
|
20
|
+
if line =~ /\Arem\s+/i
|
21
|
+
next
|
22
|
+
end
|
23
|
+
|
24
|
+
if line =~ /\A\s/ and cursect and optname
|
25
|
+
line.strip!
|
26
|
+
cursect[optname] << "\n#{line}" unless line.empty?
|
27
|
+
else
|
28
|
+
if line =~ SECTCRE
|
29
|
+
sectname = $~[:header]
|
30
|
+
|
31
|
+
unless hash.has_key?(sectname)
|
32
|
+
hash[sectname] = {}
|
33
|
+
end
|
34
|
+
|
35
|
+
cursect = hash[sectname]
|
36
|
+
optname = nil
|
37
|
+
elsif not cursect
|
38
|
+
raise MissingSectionHeaderError.new(lineno, line)
|
39
|
+
else
|
40
|
+
if line =~ OPTCRE_NV
|
41
|
+
optname = $~[:option].downcase.rstrip
|
42
|
+
vi = $~[:vi]
|
43
|
+
optval = $~[:value]
|
44
|
+
|
45
|
+
if optval
|
46
|
+
if %w(= :).include?(vi)
|
47
|
+
optval.sub!(/\s;.*\z/, '')
|
48
|
+
end
|
49
|
+
|
50
|
+
optval.strip!
|
51
|
+
|
52
|
+
if optval == '""'
|
53
|
+
optval = ''
|
54
|
+
end
|
55
|
+
elsif not option[:allow_no_value]
|
56
|
+
raise ParsingError.new(lineno, line)
|
57
|
+
end
|
58
|
+
|
59
|
+
cursect[optname] = optval
|
60
|
+
else
|
61
|
+
raise ParsingError.new(lineno, line)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
hash
|
68
|
+
end
|
69
|
+
module_function :load
|
70
|
+
end
|
data/lib/pycf/version.rb
ADDED
data/pycf.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pycf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'pycf'
|
8
|
+
spec.version = Pycf::VERSION
|
9
|
+
spec.authors = ['Genki Sugawara']
|
10
|
+
spec.email = ['sugawara@cookpad.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Configuration file parser for Python 2.7 basic configuration file.}
|
13
|
+
spec.description = %q{Configuration file parser for Python 2.7 basic configuration file.}
|
14
|
+
spec.homepage = 'https://github.com/winebarrel/pycf'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec', '>= 3.0.0'
|
25
|
+
spec.add_development_dependency 'coveralls'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pycf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Configuration file parser for Python 2.7 basic configuration file.
|
70
|
+
email:
|
71
|
+
- sugawara@cookpad.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/pycf.rb
|
84
|
+
- lib/pycf/dump.rb
|
85
|
+
- lib/pycf/error.rb
|
86
|
+
- lib/pycf/load.rb
|
87
|
+
- lib/pycf/version.rb
|
88
|
+
- pycf.gemspec
|
89
|
+
homepage: https://github.com/winebarrel/pycf
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.0.14
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Configuration file parser for Python 2.7 basic configuration file.
|
113
|
+
test_files: []
|