xregistry 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5fb2c7081e707996d70925d8263e6c21931de47b
4
+ data.tar.gz: ae667713a9342fefa9edd27d59c756746c8ad515
5
+ SHA512:
6
+ metadata.gz: 98c75548ee27d5ec9c138226de3317bc5c13138a4ac6900eb7479815143ffeb2b15a6e8b38d53671416499d21039dbc777f53e2be55107bfb3cf7a3c97bbe159
7
+ data.tar.gz: e9752864707a4ee539ba9445cdc58f851dc73ed28a858507a521830283c98286c83c3a893b38596c8080eddc5f22c6230d5930cf4b89ac4fedfbffad3b3fc6c9
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2016 William Parsons
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Rakefile for xregistry gem.
2
+ # Last modified: 28-Apr-2017 Wm. Parsons
3
+
4
+ require 'rake/testtask'
5
+
6
+ name = 'xregistry'
7
+ version = IO.read('VERSION').chomp
8
+ package = "#{name}-#{version}"
9
+
10
+ task :default => [:build]
11
+
12
+ desc "Build #{name} gem"
13
+ task :build do
14
+ system "gem build #{name}.gemspec"
15
+ end
16
+
17
+ desc "Clean up generated files"
18
+ task :clean do
19
+ Dir.glob('*.gem').each { |x| FileUtils.rm_f x }
20
+ end
21
+
22
+ desc "Install #{name} gem"
23
+ task :install do
24
+ system "gem install #{package}.gem --document rdoc,ri"
25
+ end
26
+
27
+ desc "Install #{name} gem as user"
28
+ task :userinstall do
29
+ system "gem install --user-install #{package}.gem --document rdoc,ri"
30
+ end
31
+
32
+ desc "Uninstall #{name} gem"
33
+ task :uninstall do
34
+ system "gem uninstall #{name} -v #{version}"
35
+ end
36
+
37
+ Rake::TestTask.new :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.6.0
@@ -0,0 +1,246 @@
1
+ ################################################################################
2
+ # xregistry.rb
3
+ #
4
+ # This file defines a cross-platform Registry class for storing application
5
+ # configuration parameters.
6
+ #
7
+ # The application programming interface (API) is based on the FOX registry
8
+ # class, which is part of the FOX GUI toolkit (http://www.fox-toolkit.org/).
9
+ # This file defines a class that can be used in the same way as the FOX
10
+ # registry, but without needing the FOX toolkit.
11
+ #
12
+ # The original FOX registry uses the Windows registry if running under Windows,
13
+ # and an .INI type file if running under Unix-type systems. This class also
14
+ # uses the Windows registry if running under Windows, but uses an SQLite3
15
+ # database if running under a Unix-type system.
16
+ #
17
+ # Last revised: 03-Aug-2016 Wm. Parsons
18
+ ################################################################################
19
+
20
+
21
+ class Registry
22
+ # Construct registry object; app_key and vendor_key must be string constants.
23
+ # Regular applications SHOULD set a vendor key!
24
+ def initialize(app_key = '', vendor_key = '')
25
+ @app_key = app_key
26
+ @vendor_key = vendor_key
27
+ @on_windows = RUBY_PLATFORM =~ /w(in)?32/
28
+
29
+ if @on_windows
30
+ require 'win32/registry'
31
+ @root_key = ['SOFTWARE', vendor_key, app_key].join('\\')
32
+ else
33
+ require 'sqlite3'
34
+
35
+ # create registry directory if necessary
36
+ regdir = ENV['HOME'] + '/.registry'
37
+ File.directory?(regdir) || Dir.mkdir(regdir)
38
+ regdir += '/' + vendor_key
39
+ File.directory?(regdir) || Dir.mkdir(regdir)
40
+
41
+ @db = SQLite3::Database.new("#{regdir}/#{app_key}.db3")
42
+ end
43
+ end
44
+
45
+ # Return application key
46
+ def appKey
47
+ @app_key
48
+ end
49
+ alias app_key appKey
50
+
51
+ # Return vendor key
52
+ def vendorKey
53
+ @vendor_key
54
+ end
55
+ alias vendor_key vendorKey
56
+
57
+ # Delete the registry entry for the specified section and key.
58
+ def deleteEntry(section, key)
59
+ if @on_windows
60
+ begin
61
+ pkey = [@root_key, section].join('\\')
62
+ access = Win32::Registry::KEY_ALL_ACCESS
63
+ Win32::Registry::HKEY_CURRENT_USER.open(pkey, access) do |reg|
64
+ reg.delete_value(key)
65
+ end
66
+ rescue Win32::Registry::Error
67
+ end
68
+ else
69
+ begin
70
+ @db.execute %Q[delete from "#{section}" where key=?], key
71
+ rescue SQLite3::SQLException
72
+ end
73
+ end
74
+ end
75
+ alias delete_entry deleteEntry
76
+
77
+ # Delete a specified section from the registry.
78
+ def deleteSection(section)
79
+ if @on_windows
80
+ begin
81
+ access = Win32::Registry::KEY_ALL_ACCESS
82
+ Win32::Registry::HKEY_CURRENT_USER.open(@root_key, access) do |reg|
83
+ reg.delete_key(section, true)
84
+ end
85
+ rescue Win32::Registry::Error
86
+ end
87
+ else
88
+ @db.execute "drop table if exists \"#{section}\""
89
+ end
90
+ end
91
+ alias delete_section deleteSection
92
+
93
+ # Iterate over the section keys of the registry.
94
+ def each_key
95
+ if @on_windows
96
+ Win32::Registry::HKEY_CURRENT_USER.open(@root_key) do |reg|
97
+ reg.each_key { |subkey, wtime| yield subkey }
98
+ end
99
+ else
100
+ @db.execute <<-EOS do |name|
101
+ select name from sqlite_master where type='table' order by name
102
+ EOS
103
+ yield name[0]
104
+ end
105
+ end
106
+ end
107
+
108
+ # Find a section in the registry.
109
+ # The properties of the value returned are undefined except that it must
110
+ # support an "each_key" to iterate thru the keys and a "find" method to
111
+ # locate the value associated with a key.
112
+ def find(section)
113
+ hash = {}
114
+ if @on_windows
115
+ pkey = [@root_key, section].join('\\')
116
+ Win32::Registry::HKEY_CURRENT_USER.open(pkey) do |reg|
117
+ reg.each do |subkey, type, value|
118
+ hash[subkey] = value
119
+ end
120
+ end
121
+ else
122
+ @db.execute %Q[select key, value from "#{section}" order by key] do |row|
123
+ hash[row[0]] = row[1]
124
+ end
125
+ end
126
+ class << hash
127
+ def find(key)
128
+ self[key]
129
+ end
130
+ end
131
+ hash
132
+ end
133
+
134
+ # Read a boolean registry entry from the specified section and key. If no
135
+ # value is found, the default value is returned. A type error is
136
+ # raised if the retrieved value is real or a string.
137
+ def readBoolEntry(section, key, default = false)
138
+ value = readEntry(section, key, default)
139
+ raise TypeError if value.class == Float || value.class == String
140
+ value && value != 0
141
+ end
142
+ alias read_bool_entry readBoolEntry
143
+
144
+ # Read an integer registry entry from the specified section and key. If no
145
+ # value is found, the default value is returned.
146
+ def readIntEntry(section, key, default = 0)
147
+ readEntry(section, key, default).to_i
148
+ end
149
+ alias read_int_entry readIntEntry
150
+
151
+ # Read a double-precision floating point registry entry from the specified
152
+ # section and key. If no value is found, the default value is returned.
153
+ def readRealEntry(section, key, default = 0.0)
154
+ readEntry(section, key, default).to_f
155
+ end
156
+ alias read_real_entry readRealEntry
157
+
158
+ # Read a string registry entry from the specified section and key. If no
159
+ # value is found, the default value is returned.
160
+ def readStringEntry(section, key, default = '')
161
+ readEntry(section, key, default).to_s
162
+ end
163
+ alias read_string_entry readStringEntry
164
+
165
+ # Write a boolean registry value to the specified section and key.
166
+ def writeBoolEntry(section, key, value)
167
+ writeEntry(section, key, value ? 1 : 0)
168
+ end
169
+ alias write_bool_entry writeBoolEntry
170
+
171
+ # Write an integer registry value to the specified section and key.
172
+ def writeIntEntry(section, key, value)
173
+ writeEntry(section, key, value.to_i)
174
+ end
175
+ alias write_int_entry writeIntEntry
176
+
177
+ # Write a double-precision floating point registry value to the specified
178
+ # section and key.
179
+ def writeRealEntry(section, key, value)
180
+ writeEntry(section, key, value.to_f)
181
+ end
182
+ alias write_real_entry writeRealEntry
183
+
184
+ # Write a string registry value to the specified section and key.
185
+ def writeStringEntry(section, key, value)
186
+ writeEntry(section, key, value.to_s)
187
+ end
188
+ alias write_string_entry writeStringEntry
189
+
190
+ #######
191
+ private
192
+ #######
193
+
194
+ # Read a registry entry from the specified section and key. If no
195
+ # value is found, the default value is returned.
196
+ def readEntry(section, key, default)
197
+ if @on_windows
198
+ pkey = [@root_key, section].join('\\')
199
+ reg = Win32::Registry::HKEY_CURRENT_USER.open(pkey)
200
+ reg.read(key)[1]
201
+ else
202
+ @db.get_first_value("select value from \"#{section}\" where key=?",
203
+ key) || default
204
+ end
205
+ rescue
206
+ default
207
+ end
208
+
209
+ # Write a registry value to the specified section and key.
210
+ def writeEntry(section, key, value)
211
+ if @on_windows
212
+ # store real values as strings under Windows
213
+ value = value.to_s if value.class == Float
214
+ begin
215
+ pkey = [@root_key, section].join('\\')
216
+ access = Win32::Registry::KEY_WRITE
217
+ Win32::Registry::HKEY_CURRENT_USER.open(pkey, access) do |reg|
218
+ reg[key] = value
219
+ end
220
+ rescue
221
+ Win32::Registry::HKEY_CURRENT_USER.create(pkey, access) do |reg|
222
+ reg[key] = value
223
+ end
224
+ end
225
+ else
226
+ begin
227
+ @db.execute("update \"#{section}\" set value=? where key=?",
228
+ [value, key])
229
+ if @db.changes == 0
230
+ # no row was updated so insert a new row
231
+ @db.execute("insert into \"#{section}\"(key, value) values(?,?)",
232
+ [key, value])
233
+ end
234
+ rescue SQLite3::Exception => e
235
+ if e.to_s =~ /no such table/
236
+ @db.execute "create table \"#{section}\"(key text unique, value)"
237
+
238
+ @db.execute("insert into \"#{section}\"(key, value) values(?,?)",
239
+ [key, value])
240
+ else
241
+ raise e.to_s
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,216 @@
1
+ # test program for wregistry gem
2
+ # Last modified: 02-Aug-2016
3
+
4
+ require 'xregistry'
5
+ require 'test/unit'
6
+
7
+ class TestXregistry < Test::Unit::TestCase
8
+ @@app_key = 'testxreg'
9
+ @@vendor_key = 'Wikareia'
10
+
11
+ def setup
12
+ @@reg ||= Registry.new(@@app_key, @@vendor_key)
13
+ end
14
+
15
+ def test_new
16
+ assert_equal(Registry, @@reg.class)
17
+ rescue
18
+ flunk "error encountered opening database: #{$!}"
19
+ end
20
+
21
+ # test of each_key
22
+ def test_each_key
23
+ keys = []
24
+ @@reg.each_key { |k| keys << k }
25
+ keys.each { |k| @@reg.deleteSection(k) }
26
+ @@reg.writeIntEntry('Section1', 'key', 1)
27
+ @@reg.writeIntEntry('Section2', 'key', 2)
28
+ @@reg.writeIntEntry('Section3', 'key', 3)
29
+ value = []
30
+ @@reg.each_key { |k| value << k }
31
+ assert_equal(%w[Section1 Section2 Section3], value)
32
+ end
33
+
34
+ # test of find
35
+ def test_find
36
+ @@reg.writeIntEntry('Section', 'key1', 1)
37
+ @@reg.writeIntEntry('Section', 'key2', 2)
38
+ @@reg.writeIntEntry('Section', 'key3', 3)
39
+ value = []
40
+ section = @@reg.find('Section')
41
+ section.each_key { |k| value << [k, section.find(k)] }
42
+ assert_equal([['key1', 1], ['key2', 2], ['key3', 3]], value)
43
+ end
44
+
45
+ # test of readBoolEntry
46
+ def test_read_bool_entry
47
+ value = @@reg.readBoolEntry('Settings', 'bool_key1')
48
+ assert_equal(false, value)
49
+
50
+ value = @@reg.readBoolEntry('Settings', 'bool_key1', true)
51
+ assert_equal(true, value)
52
+
53
+ value = @@reg.readBoolEntry('Settings', 'bool_key1', false)
54
+ assert_equal(false, value)
55
+
56
+ @@reg.writeIntEntry('Settings', 'bool_key1', 2)
57
+ value = @@reg.readBoolEntry('Settings', 'bool_key1')
58
+ assert_equal(true, value)
59
+
60
+ @@reg.writeRealEntry('Settings', 'bool_key1', 2.5)
61
+ assert_raise(TypeError) { @@reg.readBoolEntry('Settings', 'bool_key1') }
62
+
63
+ @@reg.writeStringEntry('Settings', 'bool_key1', '2')
64
+ assert_raise(TypeError) { @@reg.readBoolEntry('Settings', 'bool_key1') }
65
+ end
66
+
67
+ # test of readIntEntry
68
+ def test_read_int_entry
69
+ value = @@reg.readIntEntry('Settings', 'int_key1')
70
+ assert_equal(0, value)
71
+
72
+ value = @@reg.readIntEntry('Settings', 'int_key1', 123)
73
+ assert_equal(123, value)
74
+
75
+ @@reg.writeBoolEntry('Settings', 'int_key1', true)
76
+ value = @@reg.readIntEntry('Settings', 'int_key1')
77
+ assert_equal(1, value)
78
+
79
+ @@reg.writeRealEntry('Settings', 'int_key1', 2.5)
80
+ value = @@reg.readIntEntry('Settings', 'int_key1')
81
+ assert_equal(2, value)
82
+
83
+ @@reg.writeStringEntry('Settings', 'int_key1', '2')
84
+ value = @@reg.readIntEntry('Settings', 'int_key1')
85
+ assert_equal(2, value)
86
+
87
+ @@reg.writeStringEntry('Settings', 'int_key1', 'abc')
88
+ value = @@reg.readIntEntry('Settings', 'int_key1')
89
+ assert_equal(0, value)
90
+ end
91
+
92
+ # test of readRealEntry
93
+ def test_read_real_entry
94
+ value = @@reg.readRealEntry('Settings', 'real_key1')
95
+ assert_equal(0.0, value)
96
+
97
+ value = @@reg.readRealEntry('Settings', 'real_key1', 12.5)
98
+ assert_equal(12.5, value)
99
+
100
+ @@reg.writeBoolEntry('Settings', 'real_key1', true)
101
+ value = @@reg.readRealEntry('Settings', 'real_key1')
102
+ assert_equal(1.0, value)
103
+
104
+ @@reg.writeIntEntry('Settings', 'real_key1', 2)
105
+ value = @@reg.readRealEntry('Settings', 'real_key1')
106
+ assert_equal(2.0, value)
107
+
108
+ @@reg.writeStringEntry('Settings', 'real_key1', '2.5')
109
+ value = @@reg.readRealEntry('Settings', 'real_key1')
110
+ assert_equal(2.5, value)
111
+
112
+ @@reg.writeStringEntry('Settings', 'real_key1', 'abc')
113
+ value = @@reg.readRealEntry('Settings', 'real_key1')
114
+ assert_equal(0, value)
115
+ end
116
+
117
+ # test of readStringEntry
118
+ def test_read_string_entry
119
+ string = @@reg.readStringEntry('Settings', 'string_key1')
120
+ assert_equal('', string)
121
+
122
+ string = @@reg.readStringEntry('Settings', 'string_key1', 'default value')
123
+ assert_match(/ value/, string)
124
+
125
+ @@reg.writeBoolEntry('Settings', 'string_key1', true)
126
+ value = @@reg.readStringEntry('Settings', 'string_key1')
127
+ assert_equal('1', value)
128
+
129
+ @@reg.writeIntEntry('Settings', 'string_key1', 2)
130
+ value = @@reg.readStringEntry('Settings', 'string_key1')
131
+ assert_equal('2', value)
132
+
133
+ @@reg.writeRealEntry('Settings', 'string_key1', 2.5)
134
+ value = @@reg.readStringEntry('Settings', 'string_key1')
135
+ assert_equal('2.5', value)
136
+ end
137
+
138
+ # test of writeBoolEntry
139
+ def test_write_bool_entry
140
+ @@reg.writeBoolEntry('Settings', 'bool_key2', true)
141
+ value = @@reg.readBoolEntry('Settings', 'bool_key2')
142
+ assert_equal(true, value)
143
+
144
+ @@reg.writeBoolEntry('Settings', 'bool_key2', false)
145
+ value = @@reg.readBoolEntry('Settings', 'bool_key2')
146
+ assert_equal(false, value)
147
+ end
148
+
149
+ # test of writeIntEntry
150
+ def test_write_int_entry
151
+ @@reg.writeIntEntry('Settings', 'int_key2', 456)
152
+ value = @@reg.readIntEntry('Settings', 'int_key2')
153
+ assert_equal(456, value)
154
+ end
155
+
156
+ # test of writeRealEntry
157
+ def test_write_real_entry
158
+ @@reg.writeRealEntry('Settings', 'real_key2', 25.5)
159
+ value = @@reg.readRealEntry('Settings', 'real_key2')
160
+ assert_equal(25.5, value)
161
+ end
162
+
163
+ # test of writeStringEntry
164
+ def test_write_string_entry
165
+ @@reg.writeStringEntry('Settings', 'string_key2', 'test value')
166
+ string = @@reg.readStringEntry('Settings', 'string_key2')
167
+ assert_equal('test value', string)
168
+ end
169
+
170
+ # test of deleteEntry
171
+ def test_delete_entry
172
+ @@reg.writeStringEntry('Settings', 'key1', 'dummy value')
173
+ string = @@reg.readStringEntry('Settings', 'key1')
174
+ assert_equal('dummy value', string)
175
+ @@reg.writeStringEntry('Settings', 'key2', 'dummy value')
176
+ string = @@reg.readStringEntry('Settings', 'key2')
177
+ assert_equal('dummy value', string)
178
+ @@reg.deleteEntry('Settings', 'key1')
179
+ string = @@reg.readStringEntry('Settings', 'key1', 'no value')
180
+ assert_equal('no value', string)
181
+
182
+ # test of deleteEntry when key doesn't exist
183
+ @@reg.deleteEntry('Settings', 'key1')
184
+ string = @@reg.readStringEntry('Settings', 'key1', 'no value')
185
+ assert_equal('no value', string)
186
+
187
+ # test of deleteEntry when section doesn't exist
188
+ @@reg.deleteSection('Settings')
189
+ @@reg.deleteEntry('Settings', 'key2')
190
+ string = @@reg.readStringEntry('Settings', 'key2', 'no value')
191
+ assert_equal('no value', string)
192
+ end
193
+
194
+ # test of deleteSection
195
+ def test_delete_section
196
+ @@reg.writeStringEntry('Settings', 'key', 'dummy value')
197
+ @@reg.deleteSection('Settings')
198
+ string = @@reg.readStringEntry('Settings', 'key', 'no value')
199
+ assert_equal('no value', string)
200
+
201
+ # test of deleteSection when section doesn't exist
202
+ @@reg.deleteSection('Settings')
203
+ string = @@reg.readStringEntry('Settings', 'key', 'no value')
204
+ assert_equal('no value', string)
205
+ end
206
+
207
+ # test of appKey
208
+ def test_app_key
209
+ assert_equal(@@app_key, @@reg.appKey)
210
+ end
211
+
212
+ # test of vendorKey
213
+ def test_vendor_key
214
+ assert_equal(@@vendor_key, @@reg.vendorKey)
215
+ end
216
+ end
@@ -0,0 +1,17 @@
1
+ # xregistry.gemspec
2
+ # Last modified: 22-Feb-2016
3
+
4
+ spec = Gem::Specification.new do |s|
5
+ s.name = 'xregistry'
6
+ s.version = IO.read('VERSION')
7
+ s.summary = 'A cross-platform registry class for Ruby.'
8
+ s.description = <<-EOS
9
+ This provides a cross-platform registry class for Ruby.
10
+ EOS
11
+ s.author = 'William Parsons'
12
+ s.email = 'wbparsons@alum.mit.edu'
13
+ s.license = 'MIT'
14
+ s.homepage = 'http://chiselapp.com/user/varro/repository/Xregistry'
15
+ s.files = Dir['{lib,test}/*.rb'] +
16
+ %w[LICENSE Rakefile VERSION xregistry.gemspec]
17
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xregistry
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.0
5
+ platform: ruby
6
+ authors:
7
+ - William Parsons
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'This provides a cross-platform registry class for Ruby.
14
+
15
+ '
16
+ email: wbparsons@alum.mit.edu
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - Rakefile
23
+ - VERSION
24
+ - lib/xregistry.rb
25
+ - test/test_xregistry.rb
26
+ - xregistry.gemspec
27
+ homepage: http://chiselapp.com/user/varro/repository/Xregistry
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.6.12
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: A cross-platform registry class for Ruby.
51
+ test_files: []