configural 0.0.1

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.
@@ -0,0 +1,77 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Configural - Easy configuration file management
6
+ #
7
+ # Copyright (c) 2011 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #++
29
+
30
+
31
+ module Configural
32
+
33
+ # Implementation of FileBase using JSON for serialization.
34
+ #
35
+ # You must have the 'json' library installed to use this format.
36
+ # This library comes standard with some versions of Ruby.
37
+ # Otherwise, install the 'json' gem.
38
+ #
39
+ class Config::JSONFile < Config::FileBase
40
+ def self.format
41
+ 'json'
42
+ end
43
+
44
+ def self.extnames
45
+ ['.json']
46
+ end
47
+
48
+
49
+ def initialize(*args)
50
+ require 'json'
51
+ super
52
+ end
53
+
54
+ def _load
55
+ @data = File.open(path, 'r'){|f| JSON.load(f) }
56
+ @data ||= {}
57
+ rescue Errno::ENOENT
58
+ @data = {}
59
+ rescue JSON::ParserError, Errno::EACCESS => e
60
+ warn( "WARNING: Could not load config file #{path.inspect}:\n" +
61
+ e.inspect + "\nUsing empty dataset instead." )
62
+ @data = {}
63
+ end
64
+
65
+ def _save
66
+ require 'fileutils'
67
+ FileUtils.mkdir_p( File.dirname(path) )
68
+ File.open(path,'w'){ |f|
69
+ f.write( JSON.pretty_generate(@data) )
70
+ }
71
+ rescue JSON::GeneratorError, Errno::EACCESS => e
72
+ warn( "WARNING: Could not save config file #{path.inspect}:\n" +
73
+ e.inspect )
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,74 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Configural - Easy configuration file management
6
+ #
7
+ # Copyright (c) 2011 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #++
29
+
30
+
31
+ module Configural
32
+
33
+ # Implementation of FileBase using XML PList (Property List) for
34
+ # serialization. This format is most commonly used on Mac OS.
35
+ #
36
+ # You must have the 'plist' library installed to use this format.
37
+ # See: <http://plist.rubyforge.org/>
38
+ #
39
+ class Config::PlistFile < Config::FileBase
40
+ def self.format
41
+ 'plist'
42
+ end
43
+
44
+ def self.extnames
45
+ ['.plist']
46
+ end
47
+
48
+
49
+ def initialize(*args)
50
+ require 'plist'
51
+ super
52
+ end
53
+
54
+ def _load
55
+ @data = Plist.parse_xml(path) || {}
56
+ rescue Errno::ENOENT
57
+ @data = {}
58
+ rescue Errno::EACCESS => e
59
+ warn( "WARNING: Could not load config file #{path.inspect}:\n" +
60
+ e.inspect + "\nUsing empty dataset instead." )
61
+ @data = {}
62
+ end
63
+
64
+ def _save
65
+ require 'fileutils'
66
+ FileUtils.mkdir_p( File.dirname(path) )
67
+ File.open(path, 'w'){ |f| f.write( Plist::Emit.dump(@data) ) }
68
+ rescue Errno::EACCESS => e
69
+ warn( "WARNING: Could not save config file #{path.inspect}:\n" +
70
+ e.inspect )
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,145 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Configural - Easy configuration file management
6
+ #
7
+ # Copyright (c) 2011 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #++
29
+
30
+
31
+ module Configural
32
+
33
+ # Implementation of FileBase using SDL (Simple Declarative Language)
34
+ # for serialization.
35
+ #
36
+ # You must have the 'sdl4r' library installed to use this format.
37
+ # See: <http://sdl4r.rubyforge.org/>
38
+ #
39
+ class Config::SDLFile < Config::FileBase
40
+ def self.format
41
+ 'sdl'
42
+ end
43
+
44
+ def self.extnames
45
+ ['.sdl']
46
+ end
47
+
48
+
49
+ def initialize(*args)
50
+ require 'sdl4r'
51
+ super
52
+ end
53
+
54
+
55
+ def _load
56
+ root = File.open(path, 'r'){ |f| SDL4R.read(f) }
57
+ @data = tag_to_hash( root )
58
+ rescue Errno::ENOENT
59
+ @data = {}
60
+ rescue SDL4R::SdlParseError, Errno::EACCESS => e
61
+ warn( "WARNING: Could not load config file #{path.inspect}:\n" +
62
+ e.inspect + "\nUsing empty dataset instead." )
63
+ @data = {}
64
+ end
65
+
66
+
67
+ def _save
68
+ require 'fileutils'
69
+ FileUtils.mkdir_p( File.dirname(path) )
70
+ File.open(path, 'w') { |f|
71
+ hash_to_tag(@data).write(f)
72
+ }
73
+ rescue ArgumentError, Errno::EACCES => e
74
+ warn( "WARNING: Could not save config file #{path.inspect}:\n" +
75
+ e.inspect )
76
+ end
77
+
78
+
79
+ private
80
+
81
+
82
+ # Recursively convert a SDL4R::Tag and its children into a Hash.
83
+ def tag_to_hash( tag )
84
+ hash = {}
85
+
86
+ if tag.namespace.empty?
87
+ hash['@name'] = tag.name
88
+ else
89
+ hash['@name'] = tag.namespace + ':' + tag.name
90
+ end
91
+
92
+ unless tag.attributes.empty?
93
+ hash['@attributes'] = tag.attributes
94
+ end
95
+
96
+ unless tag.values.empty?
97
+ hash['@values'] = tag.values
98
+ end
99
+
100
+ unless tag.child_count < 1
101
+ tag.children do |child|
102
+ child_name =
103
+ if child.namespace.empty?
104
+ child.name
105
+ else
106
+ child.namespace + ':' + child.name
107
+ end
108
+ hash[child_name] ||= []
109
+ hash[child_name] << tag_to_hash(child)
110
+ end
111
+ end
112
+
113
+ hash
114
+ end
115
+
116
+
117
+ # Recursively convert a Hash into a SDL4R::Tag with children.
118
+ def hash_to_tag( hash )
119
+ tag = SDL4R::Tag.new( *(hash['@name'].to_s.split(':')[0,2]) )
120
+
121
+ if hash['@attributes']
122
+ hash['@attributes'].each do |key, value|
123
+ if key.index(':')
124
+ namespace, name = key.split(':')[0,2]
125
+ tag.set_attribute(namespace, name, value)
126
+ else
127
+ tag.set_attribute('', key, value)
128
+ end
129
+ end
130
+ end
131
+
132
+ tag.values = hash['@values'] if hash['@values']
133
+
134
+ child_names = hash.keys.reject{ |key| key.index('@') }
135
+ child_names.each do |name|
136
+ hash[name].each{ |child|
137
+ tag.add_child( hash_to_tag(child) )
138
+ }
139
+ end
140
+
141
+ tag
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,72 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Configural - Easy configuration file management
6
+ #
7
+ # Copyright (c) 2011 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #++
29
+
30
+
31
+ module Configural
32
+
33
+ # Implementation of FileBase using YAML for serialization.
34
+ #
35
+ class Config::YAMLFile < Config::FileBase
36
+ def self.format
37
+ 'yaml'
38
+ end
39
+
40
+ def self.extnames
41
+ ['.yml', '.yaml']
42
+ end
43
+
44
+
45
+ def initialize(*args)
46
+ require 'yaml'
47
+ super
48
+ end
49
+
50
+ def _load
51
+ @data = YAML.load_file(path) || {}
52
+ rescue Errno::ENOENT
53
+ @data = {}
54
+ rescue ArgumentError, Errno::EACCESS => e
55
+ warn( "WARNING: Could not load config file #{path.inspect}:\n" +
56
+ e.inspect + "\nUsing empty dataset instead." )
57
+ @data = {}
58
+ end
59
+
60
+ def _save
61
+ require 'fileutils'
62
+ FileUtils.mkdir_p( File.dirname(path) )
63
+ File.open(path, 'w') { |f|
64
+ YAML.dump(@data, f)
65
+ }
66
+ rescue Errno::EACCES => e
67
+ warn( "WARNING: Could not save config file #{path.inspect}:\n" +
68
+ e.inspect )
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,84 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Configural - Easy configuration file management
6
+ #
7
+ # Copyright (c) 2011 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #++
29
+
30
+
31
+ module Configural
32
+
33
+ class Data
34
+ attr_accessor :owner
35
+
36
+ def initialize( owner )
37
+ @owner = owner
38
+ @files = {}
39
+ end
40
+
41
+ def path
42
+ @owner.data_path
43
+ end
44
+
45
+ def [](name)
46
+ @files[name.to_s] ||= Data::DataFile.new(self, name.to_s)
47
+ end
48
+
49
+ def []=(name, value)
50
+ @files[name.to_s] = value
51
+ end
52
+ end
53
+
54
+
55
+ class Data::DataFile
56
+ def initialize( data, name )
57
+ @data = data
58
+ @name = name
59
+ end
60
+
61
+ def open( mode="w+", &block )
62
+ require 'fileutils'
63
+ path = File.join( @data.path, @name )
64
+ FileUtils.mkdir_p( File.dirname(path) )
65
+ File.open(path, mode, &block)
66
+ end
67
+
68
+ def read
69
+ open('r'){ |f| f.read }
70
+ end
71
+
72
+ def write( contents )
73
+ open('w'){ |f| f.write(contents) }
74
+ end
75
+ end
76
+
77
+
78
+ class Cache < Data
79
+ def path
80
+ @owner.cache_path
81
+ end
82
+ end
83
+
84
+ end