tkxxs 0.1.1 → 0.1.2

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.
@@ -1,259 +1,259 @@
1
- # encoding: windows-1252
2
- # Copyright (c) 2010-2014 Axel Friedrich
3
- $stdout.sync = true
4
- $stderr.sync = true
5
- $FILE_SEP = File::ALT_SEPARATOR || File::SEPARATOR
6
- $__DIR__ = File.dirname( File.expand_path( __FILE__ ) ).gsub('\\', '/')
7
- $__DIR0__ = File.dirname( File.expand_path( $0 ) ).gsub('\\', '/')
8
- $: << File.dirname( File.expand_path( __FILE__ ) )
9
- $:.uniq!
10
-
11
- require 'fileutils'
12
- require 'yaml'
13
- require 'Platform'
14
-
15
- class Object
16
- ##################################################################
17
- # ,dup,nil,Fixnum,can't dup Fixnum
18
- #
19
- # Potential solution for x = x.dup if x; x = x.is_a?(Fixnum) ? x : x.dup
20
- #
21
- # From: comp.lang.ruby, Feb 17 2007, "Oppinions on RCR for dup on immutable classes"
22
- def dup?
23
- dup # Better?: true
24
- rescue TypeError
25
- false
26
- end
27
- end # class Object
28
-
29
- ##########################################################################
30
- ##########################################################################
31
- # Keywords: ,configuration ,ini ,preferences ,setup
32
- # Helps storing and managing configuration data. At present, uses YAML-files to
33
- # store the data.
34
- #
35
- # Important methods:
36
- #
37
- # Conf.new
38
- # Conf#filename Returns the full path of file, where Conf is saved
39
- # Conf#section= section section must be String, Integer, Float or nil, _not_ a Symbol
40
- # Conf#or_default(key, defaultValue)
41
- # Conf#[key]= value key must be a Symbol or String
42
- # Conf#[key] Returns the stored value
43
- # Conf#save Writes Conf to file
44
- # Conf#instant_save = true/false: switches on save to file for every change of Conf
45
- #
46
- # Having keys and section of the same type and value is no problem.
47
- #
48
- # Example usage:
49
- #
50
- # require 'axel/conf'
51
- # CONF = Conf.new # Filename for storing CONF can be given as attribute;
52
- #
53
- # class MyClass
54
- #
55
- # def initialize( )
56
- # CONF.section = "my_section_1" # Optional, default is provided
57
- # # Defines a section (hash key) where to read and store data
58
- # p x = CONF.or_default( :x, 123 )
59
- # # If run the first time, usually Conf["my_section"][:x] is not set to any value.
60
- # # If Conf["my_section"][:x] is not set to any value, sets x to 123,
61
- # # stores 123 in Conf["my_section"][:x] and saves it to the conf-File.
62
- # # If run the next time, Conf["my_section"][:x] is already set to a value
63
- # # and x is set to the value of Conf["my_section"][:x]
64
- #
65
- # p @y = CONF.or_default( :@y, "abc" )
66
- #
67
- # puts "Config-File is: " + CONF.filename
68
- # myMethod(x)
69
- # end # initialize
70
- #
71
- # def myMethod( x )
72
- # x = x*x
73
- # @y = "new_y"
74
- #
75
- # CONF[:x] = x # sets Conf["my_section"][:x] to the new value of x
76
- # CONF[:@y] = @y
77
- # puts "CONF[:x] is " + CONF[:x].inspect
78
- # puts "CONF[:@y] is " + CONF[:@y].inspect
79
- #
80
- # CONF.save # saves the content of Conf.
81
- # end # myMethod
82
- #
83
- # end # MyClass
84
- #
85
- # klass = MyClass.new
86
- #
87
- class Conf < Hash
88
-
89
- alias of []
90
- alias orig_to_hash to_hash
91
-
92
- attr_accessor :instant_save
93
- attr_reader :section
94
- ##########################################################################
95
- # Attributes: configFileDNE: path including filename and fileextension
96
- def initialize( configFileDNE=Conf.filename_proposal )
97
- #p :xxx
98
- raise unless configFileDNE
99
- @configFileDNE = configFileDNE.dup.strip.gsub('\\', '/')
100
- @instant_save = false
101
- @section = nil
102
-
103
- if File.file?( @configFileDNE )
104
- dataFromFile = read
105
- self.replace( dataFromFile ) if dataFromFile
106
- end
107
- #exit
108
- self.section = nil unless self.has_key?(nil) # section "nil" always needed
109
-
110
- super()
111
- save() # writeable?
112
- end # initialize
113
-
114
- def delete_conf_file( )
115
- File.delete( @configFileDNE )
116
- end # delete_conf_file
117
-
118
- def to_hash( )
119
- h = Hash.new
120
- self.each {|key, val|
121
- h[key] = val.dup ? val.dup : val
122
- }
123
- h
124
- end # to_hash
125
-
126
- ##################################################################
127
- # Returns the filename of the config file
128
- def filename( )
129
- @configFileDNE
130
- end # filename
131
-
132
- ##################################################################
133
- # Returns the value corresponding to key, while Config is pointing to the
134
- # section set by Conf::section= . Attributes: key: Symbol or String
135
- def []( key )
136
- raise "Key must be a Symbol or String" unless key.is_a?( Symbol ) || key.is_a?( String )
137
-
138
- res = self.of(@section)[key]
139
- res = res.dup? ? res.dup : res
140
- res
141
- end # []
142
-
143
- ##################################################################
144
- # Sets the value for a corresponding key. If key does not exist, it will be created.
145
- # Attributes: key: Symbol or String; value
146
- def []=( key, value )
147
- raise "key must be a Symbol or String" unless key.is_a?( Symbol ) || key.is_a?( String )
148
-
149
- self.of(@section).store( key, value)
150
- self.save if @instant_save
151
- value
152
- end # []=
153
-
154
- ##################################################################
155
- # If key is not an existing key, key will be created with value =
156
- # defaultValue and saves it to the config-File. Returns defaultValue.
157
- #
158
- # If key is an exsiting key, the correspondig value will be returned.
159
- def or_default( key, defaultValue )
160
- raise "key must be a Symbol or String" unless key.is_a?( Symbol ) || key.is_a?( String )
161
-
162
- if self.of(@section).has_key?( key ) # ToDo: Nicer: create Conf::has_key?
163
- res = self[key]
164
- else
165
- self[key]= defaultValue
166
- res = defaultValue
167
- self.save ## if @instant_save # 2009-06-11
168
- end
169
-
170
- res
171
- end # default[]=
172
-
173
- ##################################################################
174
- # Sets Config to a section. Different sections may have the same keys.
175
- # section=nil is a valid section (it's somehow the 'root'-section)
176
- # Attributes: section = any String, Integer or Float or nil; default is nil
177
- def section=( section=nil )
178
- unless ( section.is_a?(String) || section.is_a?(Integer) || section.is_a?(Float) || !section )
179
- raise "Section must be String, Integer, Float or nil but #{ section.inspect } is a #{ section.class }!"
180
- end
181
- unless self.has_key?(section)
182
- self.store(section, {})
183
- self.save if @instant_save
184
- end
185
-
186
- @section = section.dup? ? section.dup : nil
187
- end # section=
188
-
189
- def read()
190
- FileUtils.mkpath( File.dirname( @configFileDNE ) )
191
- # Ruby 1.8.7 cannot read psych-formated YAML-Files. As of 2013-03-15, Ruby
192
- # 1.8.7 and 1.9 can read syck-formated YAML Files. For compatibility, I
193
- # choose syck-format.
194
- YAML::ENGINE.yamler = "syck" if YAML.const_defined?( :ENGINE )
195
- File.open( @configFileDNE ) { |f| YAML.load(f) }
196
- end
197
-
198
- ## def save()
199
- ## FileUtils.mkpath( File.dirname( @configFileDNE ) )
200
- ## File.open( @configFileDNE, "w" ) { |f| YAML.dump(self, f) }
201
- ## end
202
-
203
- def save()
204
- FileUtils.mkpath( File.dirname( @configFileDNE ) )
205
- YAML::ENGINE.yamler = "syck" if YAML.const_defined?( :ENGINE )
206
- File.open( @configFileDNE, "w" ) { |f| YAML.dump(self, f) }
207
- # I write a 2nd File in psych-format in case syck is not longer available
208
- # sometimes.
209
- if YAML.const_defined?( :ENGINE )
210
- YAML::ENGINE.yamler = "psych"
211
- File.open( @configFileDNE + '_psy', "w" ) { |f| YAML.dump(self, f) }
212
- end
213
- end
214
-
215
-
216
- ##################################################################
217
- # Deletes the config-File and clears all data of Conf.
218
- def delete( )
219
- File.delete(@configFileDNE)
220
- self.clear
221
- self
222
- end # delete
223
-
224
- ##################################################################
225
- # From: Joel VanderWerf: "preferences-0.3.0".
226
- # Utility method for guessing a suitable directory to store preferences.
227
- # On win32, tries +APPDATA+, +USERPROFILE+, and +HOME+. On other platforms,
228
- # tries +HOME+ and ~. Raises EnvError if preferences dir cannot be chosen.
229
- # Not called by any of the Preferences library code, but available to the
230
- # client code for constructing an argument to Preferences.new.
231
- #
232
- # Some modifications by Axel
233
- def Conf.dir
234
- case Platform::OS.to_s.downcase
235
- when 'win32'
236
- dir =
237
- ENV['APPDATA'] || # C:\Documents and Settings\name\Application Data
238
- ENV['USERPROFILE'] || # C:\Documents and Settings\name
239
- ENV['HOME']
240
- else
241
- dir =
242
- ENV['HOME'] ||
243
- File.expand_path('~')
244
- end
245
-
246
- unless dir
247
- raise EnvError, "Can't determine a configuration directory."
248
- end
249
- dir = dir.gsub('\\', '/')
250
- dir
251
- end # Conf.dir
252
-
253
- def Conf.filename_proposal( extension='.cfg' )
254
- fileN = File.basename( $0, File.extname($0) )
255
- configFileDNE = File.join( Conf.dir, fileN, fileN + extension.to_s)
256
- configFileDNE
257
- end # Conf.filename_proposal
258
-
259
- end # class Conf
1
+ # encoding: windows-1252
2
+ # Copyright (c) 2010-2014 Axel Friedrich
3
+ $stdout.sync = true
4
+ $stderr.sync = true
5
+ $FILE_SEP = File::ALT_SEPARATOR || File::SEPARATOR
6
+ $__DIR__ = File.dirname( File.expand_path( __FILE__ ) ).gsub('\\', '/')
7
+ $__DIR0__ = File.dirname( File.expand_path( $0 ) ).gsub('\\', '/')
8
+ $: << File.dirname( File.expand_path( __FILE__ ) )
9
+ $:.uniq!
10
+
11
+ require 'fileutils'
12
+ require 'yaml'
13
+ require 'platform'
14
+
15
+ class Object
16
+ ##################################################################
17
+ # ,dup,nil,Fixnum,can't dup Fixnum
18
+ #
19
+ # Potential solution for x = x.dup if x; x = x.is_a?(Fixnum) ? x : x.dup
20
+ #
21
+ # From: comp.lang.ruby, Feb 17 2007, "Oppinions on RCR for dup on immutable classes"
22
+ def dup?
23
+ dup # Better?: true
24
+ rescue TypeError
25
+ false
26
+ end
27
+ end # class Object
28
+
29
+ ##########################################################################
30
+ ##########################################################################
31
+ # Keywords: ,configuration ,ini ,preferences ,setup
32
+ # Helps storing and managing configuration data. At present, uses YAML-files to
33
+ # store the data.
34
+ #
35
+ # Important methods:
36
+ #
37
+ # Conf.new
38
+ # Conf#filename Returns the full path of file, where Conf is saved
39
+ # Conf#section= section section must be String, Integer, Float or nil, _not_ a Symbol
40
+ # Conf#or_default(key, defaultValue)
41
+ # Conf#[key]= value key must be a Symbol or String
42
+ # Conf#[key] Returns the stored value
43
+ # Conf#save Writes Conf to file
44
+ # Conf#instant_save = true/false: switches on save to file for every change of Conf
45
+ #
46
+ # Having keys and section of the same type and value is no problem.
47
+ #
48
+ # Example usage:
49
+ #
50
+ # require 'axel/conf'
51
+ # CONF = Conf.new # Filename for storing CONF can be given as attribute;
52
+ #
53
+ # class MyClass
54
+ #
55
+ # def initialize( )
56
+ # CONF.section = "my_section_1" # Optional, default is provided
57
+ # # Defines a section (hash key) where to read and store data
58
+ # p x = CONF.or_default( :x, 123 )
59
+ # # If run the first time, usually Conf["my_section"][:x] is not set to any value.
60
+ # # If Conf["my_section"][:x] is not set to any value, sets x to 123,
61
+ # # stores 123 in Conf["my_section"][:x] and saves it to the conf-File.
62
+ # # If run the next time, Conf["my_section"][:x] is already set to a value
63
+ # # and x is set to the value of Conf["my_section"][:x]
64
+ #
65
+ # p @y = CONF.or_default( :@y, "abc" )
66
+ #
67
+ # puts "Config-File is: " + CONF.filename
68
+ # myMethod(x)
69
+ # end # initialize
70
+ #
71
+ # def myMethod( x )
72
+ # x = x*x
73
+ # @y = "new_y"
74
+ #
75
+ # CONF[:x] = x # sets Conf["my_section"][:x] to the new value of x
76
+ # CONF[:@y] = @y
77
+ # puts "CONF[:x] is " + CONF[:x].inspect
78
+ # puts "CONF[:@y] is " + CONF[:@y].inspect
79
+ #
80
+ # CONF.save # saves the content of Conf.
81
+ # end # myMethod
82
+ #
83
+ # end # MyClass
84
+ #
85
+ # klass = MyClass.new
86
+ #
87
+ class Conf < Hash
88
+
89
+ alias of []
90
+ alias orig_to_hash to_hash
91
+
92
+ attr_accessor :instant_save
93
+ attr_reader :section
94
+ ##########################################################################
95
+ # Attributes: configFileDNE: path including filename and fileextension
96
+ def initialize( configFileDNE=Conf.filename_proposal )
97
+ #p :xxx
98
+ raise unless configFileDNE
99
+ @configFileDNE = configFileDNE.dup.strip.gsub('\\', '/')
100
+ @instant_save = false
101
+ @section = nil
102
+
103
+ if File.file?( @configFileDNE )
104
+ dataFromFile = read
105
+ self.replace( dataFromFile ) if dataFromFile
106
+ end
107
+ #exit
108
+ self.section = nil unless self.has_key?(nil) # section "nil" always needed
109
+
110
+ super()
111
+ save() # writeable?
112
+ end # initialize
113
+
114
+ def delete_conf_file( )
115
+ File.delete( @configFileDNE )
116
+ end # delete_conf_file
117
+
118
+ def to_hash( )
119
+ h = Hash.new
120
+ self.each {|key, val|
121
+ h[key] = val.dup ? val.dup : val
122
+ }
123
+ h
124
+ end # to_hash
125
+
126
+ ##################################################################
127
+ # Returns the filename of the config file
128
+ def filename( )
129
+ @configFileDNE
130
+ end # filename
131
+
132
+ ##################################################################
133
+ # Returns the value corresponding to key, while Config is pointing to the
134
+ # section set by Conf::section= . Attributes: key: Symbol or String
135
+ def []( key )
136
+ raise "Key must be a Symbol or String" unless key.is_a?( Symbol ) || key.is_a?( String )
137
+
138
+ res = self.of(@section)[key]
139
+ res = res.dup? ? res.dup : res
140
+ res
141
+ end # []
142
+
143
+ ##################################################################
144
+ # Sets the value for a corresponding key. If key does not exist, it will be created.
145
+ # Attributes: key: Symbol or String; value
146
+ def []=( key, value )
147
+ raise "key must be a Symbol or String" unless key.is_a?( Symbol ) || key.is_a?( String )
148
+
149
+ self.of(@section).store( key, value)
150
+ self.save if @instant_save
151
+ value
152
+ end # []=
153
+
154
+ ##################################################################
155
+ # If key is not an existing key, key will be created with value =
156
+ # defaultValue and saves it to the config-File. Returns defaultValue.
157
+ #
158
+ # If key is an exsiting key, the correspondig value will be returned.
159
+ def or_default( key, defaultValue )
160
+ raise "key must be a Symbol or String" unless key.is_a?( Symbol ) || key.is_a?( String )
161
+
162
+ if self.of(@section).has_key?( key ) # ToDo: Nicer: create Conf::has_key?
163
+ res = self[key]
164
+ else
165
+ self[key]= defaultValue
166
+ res = defaultValue
167
+ self.save ## if @instant_save # 2009-06-11
168
+ end
169
+
170
+ res
171
+ end # default[]=
172
+
173
+ ##################################################################
174
+ # Sets Config to a section. Different sections may have the same keys.
175
+ # section=nil is a valid section (it's somehow the 'root'-section)
176
+ # Attributes: section = any String, Integer or Float or nil; default is nil
177
+ def section=( section=nil )
178
+ unless ( section.is_a?(String) || section.is_a?(Integer) || section.is_a?(Float) || !section )
179
+ raise "Section must be String, Integer, Float or nil but #{ section.inspect } is a #{ section.class }!"
180
+ end
181
+ unless self.has_key?(section)
182
+ self.store(section, {})
183
+ self.save if @instant_save
184
+ end
185
+
186
+ @section = section.dup? ? section.dup : nil
187
+ end # section=
188
+
189
+ def read()
190
+ FileUtils.mkpath( File.dirname( @configFileDNE ) )
191
+ # Ruby 1.8.7 cannot read psych-formated YAML-Files. As of 2013-03-15, Ruby
192
+ # 1.8.7 and 1.9 can read syck-formated YAML Files. For compatibility, I
193
+ # choose syck-format.
194
+ YAML::ENGINE.yamler = "syck" if YAML.const_defined?( :ENGINE )
195
+ File.open( @configFileDNE ) { |f| YAML.load(f) }
196
+ end
197
+
198
+ ## def save()
199
+ ## FileUtils.mkpath( File.dirname( @configFileDNE ) )
200
+ ## File.open( @configFileDNE, "w" ) { |f| YAML.dump(self, f) }
201
+ ## end
202
+
203
+ def save()
204
+ FileUtils.mkpath( File.dirname( @configFileDNE ) )
205
+ YAML::ENGINE.yamler = "syck" if YAML.const_defined?( :ENGINE )
206
+ File.open( @configFileDNE, "w" ) { |f| YAML.dump(self, f) }
207
+ # I write a 2nd File in psych-format in case syck is not longer available
208
+ # sometimes.
209
+ if YAML.const_defined?( :ENGINE )
210
+ YAML::ENGINE.yamler = "psych"
211
+ File.open( @configFileDNE + '_psy', "w" ) { |f| YAML.dump(self, f) }
212
+ end
213
+ end
214
+
215
+
216
+ ##################################################################
217
+ # Deletes the config-File and clears all data of Conf.
218
+ def delete( )
219
+ File.delete(@configFileDNE)
220
+ self.clear
221
+ self
222
+ end # delete
223
+
224
+ ##################################################################
225
+ # From: Joel VanderWerf: "preferences-0.3.0".
226
+ # Utility method for guessing a suitable directory to store preferences.
227
+ # On win32, tries +APPDATA+, +USERPROFILE+, and +HOME+. On other platforms,
228
+ # tries +HOME+ and ~. Raises EnvError if preferences dir cannot be chosen.
229
+ # Not called by any of the Preferences library code, but available to the
230
+ # client code for constructing an argument to Preferences.new.
231
+ #
232
+ # Some modifications by Axel
233
+ def Conf.dir
234
+ case Platform::OS.to_s.downcase
235
+ when 'win32'
236
+ dir =
237
+ ENV['APPDATA'] || # C:\Documents and Settings\name\Application Data
238
+ ENV['USERPROFILE'] || # C:\Documents and Settings\name
239
+ ENV['HOME']
240
+ else
241
+ dir =
242
+ ENV['HOME'] ||
243
+ File.expand_path('~')
244
+ end
245
+
246
+ unless dir
247
+ raise EnvError, "Can't determine a configuration directory."
248
+ end
249
+ dir = dir.gsub('\\', '/')
250
+ dir
251
+ end # Conf.dir
252
+
253
+ def Conf.filename_proposal( extension='.cfg' )
254
+ fileN = File.basename( $0, File.extname($0) )
255
+ configFileDNE = File.join( Conf.dir, fileN, fileN + extension.to_s)
256
+ configFileDNE
257
+ end # Conf.filename_proposal
258
+
259
+ end # class Conf