libisi 0.3.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.
- data/LICENSE +677 -0
- data/Manifest +89 -0
- data/Rakefile +34 -0
- data/lib/inifile.rb +119 -0
- data/lib/libisi.rb +948 -0
- data/lib/libisi/attribute.rb +32 -0
- data/lib/libisi/attribute/activerecord.rb +34 -0
- data/lib/libisi/attribute/base.rb +33 -0
- data/lib/libisi/base.rb +109 -0
- data/lib/libisi/bridge.rb +21 -0
- data/lib/libisi/bridge/base.rb +23 -0
- data/lib/libisi/bridge/java.rb +71 -0
- data/lib/libisi/bridge/python.rb +37 -0
- data/lib/libisi/cache.rb +21 -0
- data/lib/libisi/cache/base.rb +67 -0
- data/lib/libisi/cache/file_cache.rb +24 -0
- data/lib/libisi/chart.rb +21 -0
- data/lib/libisi/chart/base.rb +320 -0
- data/lib/libisi/chart/jfreechart.rb +682 -0
- data/lib/libisi/chart/jfreechart_generator.rb +206 -0
- data/lib/libisi/color.rb +21 -0
- data/lib/libisi/color/base.rb +66 -0
- data/lib/libisi/color/colortools.rb +92 -0
- data/lib/libisi/color/java.rb +44 -0
- data/lib/libisi/concept.rb +33 -0
- data/lib/libisi/concept/activerecord.rb +39 -0
- data/lib/libisi/concept/base.rb +58 -0
- data/lib/libisi/doc.rb +35 -0
- data/lib/libisi/doc/base.rb +414 -0
- data/lib/libisi/doc/html.rb +85 -0
- data/lib/libisi/doc/text.rb +98 -0
- data/lib/libisi/doc/wiki.rb +55 -0
- data/lib/libisi/environment.rb +21 -0
- data/lib/libisi/environment/base.rb +36 -0
- data/lib/libisi/environment/http.rb +105 -0
- data/lib/libisi/environment/rails.rb +27 -0
- data/lib/libisi/environment/root.rb +23 -0
- data/lib/libisi/fake_logger/logger.rb +61 -0
- data/lib/libisi/function/base.rb +30 -0
- data/lib/libisi/hal.rb +558 -0
- data/lib/libisi/instance.rb +27 -0
- data/lib/libisi/instance/activerecord.rb +21 -0
- data/lib/libisi/instance/base.rb +42 -0
- data/lib/libisi/log.rb +237 -0
- data/lib/libisi/mail/base.rb +32 -0
- data/lib/libisi/mail/tmail.rb +120 -0
- data/lib/libisi/parameter/base.rb +41 -0
- data/lib/libisi/property.rb +27 -0
- data/lib/libisi/property/base.rb +28 -0
- data/lib/libisi/reciever/base.rb +31 -0
- data/lib/libisi/reciever/socket.rb +31 -0
- data/lib/libisi/relation.rb +23 -0
- data/lib/libisi/request.rb +22 -0
- data/lib/libisi/request/base.rb +29 -0
- data/lib/libisi/request/http.rb +129 -0
- data/lib/libisi/response/base.rb +27 -0
- data/lib/libisi/task/base.rb +27 -0
- data/lib/libisi/task/http.rb +90 -0
- data/lib/libisi/tee.rb +296 -0
- data/lib/libisi/ui/base.rb +116 -0
- data/lib/libisi/ui/console.rb +238 -0
- data/lib/libisi/ui/kde.rb +94 -0
- data/lib/libisi/ui/nobody.rb +29 -0
- data/lib/libisi/ui/rails.rb +150 -0
- data/lib/libisi/ui/x11.rb +55 -0
- data/lib/libisi/uri.rb +42 -0
- data/lib/libisi/uri/activerecord.rb +152 -0
- data/lib/libisi/uri/base.rb +115 -0
- data/lib/libisi/uri/file.rb +43 -0
- data/lib/libisi/uri/ldap.rb +72 -0
- data/lib/libisi/uri/mysql.rb +98 -0
- data/lib/libisi/value.rb +31 -0
- data/lib/libisi/value/attribute_value.rb +19 -0
- data/lib/libisi/value/base.rb +55 -0
- data/lib/libisi/value/property_value.rb +19 -0
- data/lib/libisi/value/relation_value.rb +19 -0
- data/lib/ordered_hash.rb +228 -0
- data/libisi.gemspec +31 -0
- data/test/bridge_test.rb +77 -0
- data/test/cache_test.rb +65 -0
- data/test/chart_test.rb +179 -0
- data/test/color_test.rb +64 -0
- data/test/concept_test.rb +56 -0
- data/test/doc_test.rb +172 -0
- data/test/fixtures/test.db +0 -0
- data/test/ordered_hash_test.rb +39 -0
- data/test/profile_test.rb +36 -0
- data/test/request_test.rb +121 -0
- data/test/test +0 -0
- data/test/ui_test.rb +62 -0
- metadata +244 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Libisi.
|
|
4
|
+
#
|
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "libisi/ui/base.rb"
|
|
19
|
+
class KdeUI < BaseUI
|
|
20
|
+
def kdialog(option, text)
|
|
21
|
+
$log.debug("Kdialog: #{option.inspect} #{text.inspect}")
|
|
22
|
+
ret = system("kdialog",option, text)
|
|
23
|
+
$log.debug("Kdialog ret: #{ret.inspect}")
|
|
24
|
+
ret
|
|
25
|
+
end
|
|
26
|
+
def question(text, options = {})
|
|
27
|
+
kdialog("--yesno", text)
|
|
28
|
+
end
|
|
29
|
+
def warn(text, options = {})
|
|
30
|
+
kdialog("--sorry", text)
|
|
31
|
+
end
|
|
32
|
+
def info(text, options = {})
|
|
33
|
+
kdialog("--msgbox", text)
|
|
34
|
+
end
|
|
35
|
+
def info_non_blocking(text, options = {})
|
|
36
|
+
system("xmessage", "-timeout", "5", "-center",text)
|
|
37
|
+
end
|
|
38
|
+
def error(text)
|
|
39
|
+
kdialog("--error", text)
|
|
40
|
+
end
|
|
41
|
+
def password(text)
|
|
42
|
+
$log.debug("Kdialog passwod: #{text.inspect}")
|
|
43
|
+
pw = open("|kdialog --password \"#{text}\"") {|f|
|
|
44
|
+
f.readlines.join.gsub(/\n$/,"")
|
|
45
|
+
}
|
|
46
|
+
if $?.exitstatus == 0
|
|
47
|
+
$log.debug("Pwlength: #{pw.length}")
|
|
48
|
+
pw
|
|
49
|
+
else
|
|
50
|
+
$log.debug("Pw: nil")
|
|
51
|
+
nil
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def progress_bar_implementation(text,total)
|
|
56
|
+
ret = nil
|
|
57
|
+
begin
|
|
58
|
+
@pbar_progress = 0
|
|
59
|
+
@pbar = open("|kdialog --progressbar '#{text.gsub("'","\\'")}' #{total}") {|f| f.readlines.join.strip}
|
|
60
|
+
ret = yield
|
|
61
|
+
pmsg
|
|
62
|
+
system("dcop",@pbar,"close")
|
|
63
|
+
@pbar = nil
|
|
64
|
+
ensure
|
|
65
|
+
if @pbar
|
|
66
|
+
system("dcop",@pbar,"close")
|
|
67
|
+
@pbar = nil
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
ret
|
|
71
|
+
end
|
|
72
|
+
def progress(count)
|
|
73
|
+
return unless @pbar
|
|
74
|
+
@pbar_progress = count
|
|
75
|
+
system("dcop",@pbar,"setProgress",count.to_s)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def progress_message(message)
|
|
79
|
+
return unless @pbar
|
|
80
|
+
system("dcop",@pbar,"setLabel",message.to_s)
|
|
81
|
+
end
|
|
82
|
+
def progress_inc
|
|
83
|
+
progress(@pbar_progress + 1)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def execute_in_console(command, options = {})
|
|
87
|
+
command = command_line_parse(command)
|
|
88
|
+
konsole = ["konsole", "-T", command.join(" "), "--nomenubar","--noframe","-e"]
|
|
89
|
+
new_command = konsole + command
|
|
90
|
+
$log.debug("Executing konsole command #{new_command.inspect}")
|
|
91
|
+
system(*new_command)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Libisi.
|
|
4
|
+
#
|
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "libisi/ui/base.rb"
|
|
19
|
+
class NobodyUI < BaseUI
|
|
20
|
+
def info(text, options = {})
|
|
21
|
+
system("logger -t '#{ENV["PROGRAM_NAME"]}' '#{text}'")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def question(text, options = {})
|
|
25
|
+
raise "Cannot ask question. Nobody is here." if options[:default].nil?
|
|
26
|
+
info(text + ": #{options[:default]}")
|
|
27
|
+
return options[:default]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Libisi.
|
|
4
|
+
#
|
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "libisi/ui/base.rb"
|
|
19
|
+
class RailsUI < BaseUI
|
|
20
|
+
$progress_names = {}
|
|
21
|
+
def info(text, options = {})
|
|
22
|
+
print "#{text}\n"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# PROGRESS
|
|
26
|
+
def enable_progress_bar(val = true, &block)
|
|
27
|
+
raise "Need block around progress for rails ui." unless block_given?
|
|
28
|
+
params = eval("params",block.binding)
|
|
29
|
+
if val and not params[:no_progress]
|
|
30
|
+
@progress = true
|
|
31
|
+
|
|
32
|
+
progress_name = params[:progress_name]
|
|
33
|
+
|
|
34
|
+
if progress_name
|
|
35
|
+
eval("@progress_name = #{progress_name.inspect}",block.binding)
|
|
36
|
+
if params[:real_task]
|
|
37
|
+
# now this processid will stay until
|
|
38
|
+
# the task is finished, takover the file
|
|
39
|
+
orig_file = progress_file(progress_name)
|
|
40
|
+
new_file = progress_file(current_progress_name)
|
|
41
|
+
begin
|
|
42
|
+
FileUtils.mv(orig_file,new_file)
|
|
43
|
+
orig_file.make_symlink(new_file)
|
|
44
|
+
|
|
45
|
+
$progress_cache[Process.pid] = load_progress
|
|
46
|
+
|
|
47
|
+
# this is the real task, go for it
|
|
48
|
+
yield
|
|
49
|
+
eval('render :layout => false', block.binding)
|
|
50
|
+
ensure
|
|
51
|
+
orig_file.delete if orig_file.exist?
|
|
52
|
+
new_file.delete if new_file.exist?
|
|
53
|
+
$progress_cache[Process.pid] = nil
|
|
54
|
+
end
|
|
55
|
+
else
|
|
56
|
+
# this is the progressbar call, render it
|
|
57
|
+
eval('render :layout => false, :action => "../application/progress_bar"', block.binding)
|
|
58
|
+
end
|
|
59
|
+
else
|
|
60
|
+
# this is the initialization step
|
|
61
|
+
# create a new id for the progressbar
|
|
62
|
+
# this name must be able to be an id of a html tag
|
|
63
|
+
progress_name = current_progress_name
|
|
64
|
+
# make it unique
|
|
65
|
+
progress_name += "#{Time.now.to_i}"
|
|
66
|
+
eval("@progress_name = #{progress_name.inspect}",block.binding)
|
|
67
|
+
|
|
68
|
+
$progress_cache[Process.pid] = {}
|
|
69
|
+
save_progress(progress_name)
|
|
70
|
+
$progress_cache[Process.pid] = nil
|
|
71
|
+
|
|
72
|
+
# render a page with progressbar
|
|
73
|
+
# and a content for the main page
|
|
74
|
+
eval('render :action => "../application/progress_page"', block.binding)
|
|
75
|
+
end
|
|
76
|
+
else
|
|
77
|
+
@progress = false
|
|
78
|
+
yield
|
|
79
|
+
end
|
|
80
|
+
@progress = false
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def progress_bar_implementation(text, total, &block)
|
|
84
|
+
if progress_bar_enabled?
|
|
85
|
+
$progress_cache[Process.pid] = {:count => total.to_i, :current => 0, :text => text}
|
|
86
|
+
save_progress
|
|
87
|
+
yield
|
|
88
|
+
$progress_cache[Process.pid] = nil
|
|
89
|
+
save_progress
|
|
90
|
+
else
|
|
91
|
+
yield
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def progress(count)
|
|
96
|
+
$progress_cache[Process.pid][:current] = count
|
|
97
|
+
save_progress
|
|
98
|
+
end
|
|
99
|
+
def progress_inc
|
|
100
|
+
$progress_cache[Process.pid][:current] += 1
|
|
101
|
+
save_progress
|
|
102
|
+
end
|
|
103
|
+
def progress_message(text)
|
|
104
|
+
$progress_cache[Process.pid][:text] = text
|
|
105
|
+
save_progress
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def progress_values(name)
|
|
109
|
+
load_progress(name)
|
|
110
|
+
end
|
|
111
|
+
private
|
|
112
|
+
$progress_cache = {}
|
|
113
|
+
$progress_mutex ||= Mutex.new
|
|
114
|
+
def current_progress_name
|
|
115
|
+
"P#{Process.pid}"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def progress_file(name = nil)
|
|
119
|
+
name = current_progress_name if name.nil?
|
|
120
|
+
Pathname.new("/tmp/progress_#{name}")
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def save_progress(name = nil)
|
|
124
|
+
name = current_progress_name if name.nil?
|
|
125
|
+
$progress_mutex.synchronize {
|
|
126
|
+
f = progress_file(name)
|
|
127
|
+
# save if more than 2 seconds
|
|
128
|
+
# have been passed till last
|
|
129
|
+
# save
|
|
130
|
+
if !f.exist? or ((Time.new - f.mtime) > 2)
|
|
131
|
+
yaml = $progress_cache[Process.pid].to_yaml
|
|
132
|
+
f.open("w") {|f| f.write(yaml) }
|
|
133
|
+
end
|
|
134
|
+
}
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def load_progress(name = nil)
|
|
138
|
+
name = current_progress_name if name.nil?
|
|
139
|
+
$progress_mutex.synchronize {
|
|
140
|
+
f = progress_file(name)
|
|
141
|
+
if f.exist?
|
|
142
|
+
YAML::load(f.readlines.join)
|
|
143
|
+
else
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
}
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
end
|
|
150
|
+
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Libisi.
|
|
4
|
+
#
|
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "libisi/ui/base.rb"
|
|
19
|
+
class X11UI < BaseUI
|
|
20
|
+
def info(text, options = {})
|
|
21
|
+
system("xmessage", "-center",text)
|
|
22
|
+
end
|
|
23
|
+
def info_non_blocking(text, options = {})
|
|
24
|
+
system("xmessage", "-timeout", "5", "-center",text)
|
|
25
|
+
end
|
|
26
|
+
def question(text, options = {})
|
|
27
|
+
system("xmessage", "-buttons","yes,no","-center",text)
|
|
28
|
+
$?.exitstatus == 101
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def password(text)
|
|
32
|
+
Open3.popen3("pinentry") { |stdin, stdout, stderr|
|
|
33
|
+
stdin.write("SETPROMPT Password\n")
|
|
34
|
+
$log.debug(stdout.readline)
|
|
35
|
+
stdin.write("SETDESC " + text.gsub("\n","\\\n") + "\n")
|
|
36
|
+
$log.debug(stdout.readline)
|
|
37
|
+
stdin.write("GETPIN\n")
|
|
38
|
+
$log.debug(stdout.readline)
|
|
39
|
+
ans = stdout.readline
|
|
40
|
+
return nil if ans == "ERR 111 canceled\n"
|
|
41
|
+
unless ans =~ /^D (.*)\n$/
|
|
42
|
+
raise "Unexpected answer #{ans} from pinentry."
|
|
43
|
+
end
|
|
44
|
+
$1.gsub("%25","%")
|
|
45
|
+
}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def execute_in_console(command, options = {})
|
|
49
|
+
command = command_line_parse(command)
|
|
50
|
+
konsole = ["xterm", "-T", command.join(" "),"-e"]
|
|
51
|
+
new_command = konsole + command
|
|
52
|
+
$log.debug("Executing konsole command #{new_command.inspect}")
|
|
53
|
+
system(*new_command)
|
|
54
|
+
end
|
|
55
|
+
end
|
data/lib/libisi/uri.rb
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Libisi.
|
|
4
|
+
#
|
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "libisi/base.rb"
|
|
19
|
+
require "libisi/uri/activerecord.rb"
|
|
20
|
+
require "uri"
|
|
21
|
+
class Uri < Base
|
|
22
|
+
|
|
23
|
+
def self.parse(string)
|
|
24
|
+
URI.parse(string)
|
|
25
|
+
rescue URI::InvalidURIError
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.create(uri, options = {})
|
|
30
|
+
uri = "file://localhost/#{Pathname.pwd + uri}" unless URI::parse(uri.to_s).scheme
|
|
31
|
+
uri = URI::parse(uri.to_s)
|
|
32
|
+
|
|
33
|
+
if ActiveRecordData.supports?(uri)
|
|
34
|
+
ActiveRecordData.new(uri,options)
|
|
35
|
+
else
|
|
36
|
+
require "libisi/uri/#{uri.scheme.to_s}.rb"
|
|
37
|
+
klass = eval("#{uri.scheme.capitalize}Data")
|
|
38
|
+
klass.new(uri, options)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Libisi.
|
|
4
|
+
#
|
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU General Public License
|
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "libisi/uri/base"
|
|
19
|
+
class ActiveRecordData < BaseUri
|
|
20
|
+
@@num = 0
|
|
21
|
+
|
|
22
|
+
ADAPTERS = ["mysql","postgresql","sqlite3","sqlite"]
|
|
23
|
+
|
|
24
|
+
def ActiveRecordData.split_path(uri)
|
|
25
|
+
return {} unless ADAPTERS.include?(uri.scheme)
|
|
26
|
+
|
|
27
|
+
return {} if uri.path.length <= 1
|
|
28
|
+
splited = uri.path[1..-1].split("/")
|
|
29
|
+
case uri.scheme
|
|
30
|
+
when "mysql","postgresql"
|
|
31
|
+
ret = nil
|
|
32
|
+
case splited.length
|
|
33
|
+
when 1
|
|
34
|
+
ret = {:database => splited[0]}
|
|
35
|
+
when 2
|
|
36
|
+
ret = {:database => splited[0], :table => splited[1]}
|
|
37
|
+
else
|
|
38
|
+
ret = {}
|
|
39
|
+
end
|
|
40
|
+
when "sqlite","sqlite3"
|
|
41
|
+
while splited.length > 1 and (exist = Pathname.new(splited[0]).exist?) and Pathname.new(splited[0]).directory?
|
|
42
|
+
$log.debug("#{splited[0]} exist and is a directory")
|
|
43
|
+
splited = [splited[0..1].join("/")] + splited[2..-1]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
ret = {:database => splited[0], :table => splited[1]}
|
|
47
|
+
end
|
|
48
|
+
$log.debug("ActiveRecordData split_path: #{uri.inspect} => #{ret.inspect}")
|
|
49
|
+
ret
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def ActiveRecordData.supports?(uri)
|
|
53
|
+
!split_path(uri)[:table].nil?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
attr_reader :adapter, :database, :table
|
|
57
|
+
def initialize(uri, options)
|
|
58
|
+
require "rubygems"
|
|
59
|
+
require "active_record"
|
|
60
|
+
ActiveRecord::Base.logger = $log
|
|
61
|
+
|
|
62
|
+
super
|
|
63
|
+
|
|
64
|
+
@adapter = uri.scheme
|
|
65
|
+
|
|
66
|
+
vals = ActiveRecordData.split_path(uri)
|
|
67
|
+
@database = vals[:database]
|
|
68
|
+
@table = vals[:table]
|
|
69
|
+
raise "No database given" unless @database
|
|
70
|
+
raise "No table given" unless @table
|
|
71
|
+
|
|
72
|
+
if options[:model]
|
|
73
|
+
require "active_support/core_ext/string/inflections.rb"
|
|
74
|
+
|
|
75
|
+
$log.info("Loading classes from #{options[:model]}")
|
|
76
|
+
@model_path = Pathname.new(options[:model])
|
|
77
|
+
@class_names = []
|
|
78
|
+
|
|
79
|
+
raise "Model path #{@model_path} not found" unless @model_path.exist?
|
|
80
|
+
|
|
81
|
+
use_activesupport = true
|
|
82
|
+
if use_activesupport
|
|
83
|
+
ActiveSupport::Dependencies.load_paths << @model_path.to_s
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
@model_path.find {|p|
|
|
87
|
+
next unless p.to_s =~ /\/([^\/]+)\.rb$/
|
|
88
|
+
|
|
89
|
+
class_name = $1.camelize
|
|
90
|
+
@class_names << class_name
|
|
91
|
+
|
|
92
|
+
unless use_activesupport
|
|
93
|
+
$log.debug("Define autoload: #{class_name.to_sym} => #{p}")
|
|
94
|
+
autoload(class_name.to_sym, p.to_s)
|
|
95
|
+
end
|
|
96
|
+
}
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def primary_key; active_record_class.primary_key.to_s ;end
|
|
101
|
+
|
|
102
|
+
def column_names; active_record_class.column_names; end
|
|
103
|
+
|
|
104
|
+
def items; active_record_class.find(:all).to_a; end
|
|
105
|
+
def entry_not_found_exception; ActiveRecord::RecordNotFound; end
|
|
106
|
+
def create(attributes,&block); active_record.create(attributes,&block) ;end
|
|
107
|
+
|
|
108
|
+
private
|
|
109
|
+
def active_record_class
|
|
110
|
+
return @myclass if @myclass
|
|
111
|
+
|
|
112
|
+
if @model_path
|
|
113
|
+
$log.debug("Trying to access class: #{@table.inspect}")
|
|
114
|
+
|
|
115
|
+
raise "Model path does not define #{@table.inspect}" unless
|
|
116
|
+
@class_names.include?(@table)
|
|
117
|
+
|
|
118
|
+
@myclass = eval(@table)
|
|
119
|
+
else
|
|
120
|
+
ActiveRecordData.module_eval("class ActiveRecordClass#{@@num} < ActiveRecord::Base; end")
|
|
121
|
+
@myclass = eval("ActiveRecordClass#{@@num}")
|
|
122
|
+
@@num += 1
|
|
123
|
+
|
|
124
|
+
@myclass.set_table_name(table)
|
|
125
|
+
@class_names = [@myclass.name]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
@class_names.each {|klass_name|
|
|
129
|
+
# klass.connection = @myclass.connection
|
|
130
|
+
begin
|
|
131
|
+
klass = eval(klass_name)
|
|
132
|
+
connection_options = {
|
|
133
|
+
:adapter => adapter,
|
|
134
|
+
:host => uri.host,
|
|
135
|
+
:port => uri.port,
|
|
136
|
+
:username => user,
|
|
137
|
+
:password => password,
|
|
138
|
+
:database => database
|
|
139
|
+
}
|
|
140
|
+
connection_options[:encoding] = options[:encoding] if options[:encoding]
|
|
141
|
+
|
|
142
|
+
klass.establish_connection(connection_options) if klass.respond_to?(:establish_connection)
|
|
143
|
+
rescue LoadError, NameError
|
|
144
|
+
raise $! if klass_name == @table
|
|
145
|
+
$log.warn("Error connecting class #{klass_name}, maybe this is not used, continue")
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
}
|
|
149
|
+
@myclass
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
end
|