knjrbfw 0.0.30 → 0.0.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,21 @@
1
1
  #This module contains various extra errors used by the other Knj-code.
2
2
  module Knj::Errors
3
+ #An error that is used when the error is just a notice.
3
4
  class Notice < StandardError; end
5
+
6
+ #Typically used when an object is not found by the given arguments.
4
7
  class NotFound < StandardError; end
8
+
9
+ #If invalid data was supplied to a list-method or something like it.
5
10
  class InvalidData < StandardError; end
11
+
12
+ #An error that specifies that the caller should retry the operation.
6
13
  class Retry < StandardError; end
14
+
15
+ #The current env does not have access to calling the method.
7
16
  class NoAccess < StandardError; end
17
+
18
+ #The thing you are trying to add already exists or have already been done.
8
19
  class Exists < StandardError; end
9
20
 
10
21
  #Returns a string describing the given error. Possible arguments can be given if you want the returned string formatted as HTML.
@@ -1,6 +1,8 @@
1
+ #Uses Rubinius, Knj::Compiler, RubyVM::InstructionSequence and eval to convert and execute .rhtml-files.
1
2
  class Knj::Eruby
2
3
  attr_reader :connects, :error, :headers, :cookies, :fcgi
3
4
 
5
+ #Sets various arguments and prepares for parsing.
4
6
  def initialize(args = {})
5
7
  @args = args
6
8
 
@@ -33,6 +35,9 @@ class Knj::Eruby
33
35
  self.reset_connects
34
36
  end
35
37
 
38
+ #Imports and evaluates a new .rhtml-file.
39
+ #===Examples
40
+ # erb.import("/path/to/some_file.rhtml")
36
41
  def import(filename)
37
42
  @error = false
38
43
  Dir.mkdir(@tmpdir) if !File.exists?(@tmpdir)
@@ -86,6 +91,7 @@ class Knj::Eruby
86
91
  end
87
92
  end
88
93
 
94
+ #Destroyes this object unsetting all variables and clearing all cache.
89
95
  def destroy
90
96
  @connects.clear if @connects.is_a?(Hash)
91
97
  @headers.clear if @headers.is_a?(Array)
@@ -100,6 +106,7 @@ class Knj::Eruby
100
106
  @cookies = nil
101
107
  end
102
108
 
109
+ #Returns various headers as one complete string ready to be used in a HTTP-request.
103
110
  def print_headers(args = {})
104
111
  header_str = ""
105
112
 
@@ -116,6 +123,7 @@ class Knj::Eruby
116
123
  return header_str
117
124
  end
118
125
 
126
+ #Returns true if containing a status-header.
119
127
  def has_status_header?
120
128
  @headers.each do |header|
121
129
  return true if header[0] == "Status"
@@ -124,23 +132,28 @@ class Knj::Eruby
124
132
  return false
125
133
  end
126
134
 
135
+ #Resets all connections.
127
136
  def reset_connects
128
137
  @connects = {}
129
138
  end
130
139
 
140
+ #Resets all headers.
131
141
  def reset_headers
132
142
  @headers = []
133
143
  @cookies = []
134
144
  end
135
145
 
146
+ #Adds a new header to the list.
136
147
  def header(key, value)
137
148
  @headers << [key, value]
138
149
  end
139
150
 
151
+ #Adds a new cookie to the list.
140
152
  def cookie(cookie_data)
141
153
  @cookies << cookie_data
142
154
  end
143
155
 
156
+ #Connects a block to a certain event.
144
157
  def connect(signal, &block)
145
158
  @connects[signal] = [] if !@connects.key?(signal)
146
159
  @connects[signal] << block
@@ -238,11 +251,14 @@ class Knj::Eruby
238
251
  end
239
252
  end
240
253
 
254
+ #Erubis-handler used to print to $stdout.
241
255
  class Knj::Eruby::Handler < Erubis::Eruby
242
256
  include Erubis::StdoutEnhancer
243
257
  end
244
258
 
259
+ #Default binding-object which makes sure the .rhtml-file is running on an empty object.
245
260
  class Knj::Eruby::Binding
261
+ #Returns the binding to the empty object.
246
262
  def get_binding
247
263
  return binding
248
264
  end
@@ -1,9 +1,22 @@
1
+ #This class is used for event handeling.
2
+ #===Examples
3
+ # events = Knj::Event_handler.new
4
+ # events.add_event(:name => :test_event)
5
+ # events.connect(:test_event) do |*args|
6
+ # print "Test-event called!\n"
7
+ # end
8
+ #
9
+ # events.call(:test_event) #=> prints "Test-event called!\n"
1
10
  class Knj::Event_handler
11
+ #Sets various used variables.
2
12
  def initialize(args = {})
3
13
  @args = args
4
14
  @events = {}
5
15
  end
6
16
 
17
+ #Adds information about a new event.
18
+ #===Examples
19
+ # events.add_event(:name => :test_event)
7
20
  def add_event(event)
8
21
  raise "No name given." if !event[:name]
9
22
 
@@ -15,12 +28,18 @@ class Knj::Event_handler
15
28
  }
16
29
  end
17
30
 
31
+ #Adds multiple events.
32
+ #===Examples
33
+ # events.add_events(:test_event, :another_event, :a_third_event)
18
34
  def add_events(*events)
19
35
  events.each do |event|
20
36
  self.add_event(:name => event)
21
37
  end
22
38
  end
23
39
 
40
+ #Connects the given block to a given event.
41
+ #===Examples
42
+ # events.connect(:test_event){ |*args| print "Test event!\n"}
24
43
  def connect(name, &block)
25
44
  raise "No such event: '#{name}'." if !@events.key?(name)
26
45
 
@@ -40,11 +59,18 @@ class Knj::Event_handler
40
59
  end
41
60
 
42
61
  #Returns true if the given event is connected.
62
+ #===Examples
63
+ # print "Test-event is connected!" if events.connected?(:test_event)
43
64
  def connected?(name)
44
- return true if @events.key?(name)
45
- return false
65
+ raise "No such event." if !@events.key?(name)
66
+ return !@events[name][:callbacks].empty?
46
67
  end
47
68
 
69
+ #Disconnects an event.
70
+ #===Examples
71
+ # connection_id = events.connect(:test_event){print "test event!}
72
+ # events.disconnect(:test_event, connection_id)
73
+ # events.call(:test_event) #=> Doesnt print 'test event!'.
48
74
  def disconnect(name, callback_id)
49
75
  raise "No such event: '#{name}'." if !@events.key?(name)
50
76
  raise "No such connection: '#{name}' --> '#{callback_id}'" if !@events[name].key?(callback_id)
@@ -52,16 +78,17 @@ class Knj::Event_handler
52
78
  @events[name].delete(callback_id)
53
79
  end
54
80
 
81
+ #Returns how many blocks have been connected to an event.
82
+ #===Examples
83
+ # print "More than five connections to test-event!" if events.count_events(:test_event) > 5
55
84
  def count_connects(name)
56
85
  raise "No such event." if !@events.key?(name)
57
86
  return @events[name][:callbacks].length
58
87
  end
59
88
 
60
- def connected?(name)
61
- raise "No such event." if !@events.key?(name)
62
- return !@events[name][:callbacks].empty?
63
- end
64
-
89
+ #Calls an added event.
90
+ #===Examples
91
+ # events.call(:test_event, {:data => 1})
65
92
  def call(name, *args)
66
93
  raise "No such event: '#{name}'." if !@events.key?(name)
67
94
  event = @events[name]
@@ -1,16 +1,21 @@
1
+ #A fallback GetText implementation that basically returns the given strings, but can be useful for using methods that uses GetText without using an actual GetText implementation.
1
2
  module GetText
3
+ #Returns the given string.
2
4
  def self._(string)
3
5
  return string
4
6
  end
5
7
 
8
+ #Returns the given string.
6
9
  def _(string)
7
10
  return string
8
11
  end
9
12
 
13
+ #Returns the given string.
10
14
  def gettext(string)
11
15
  return string
12
16
  end
13
17
 
18
+ #Doesnt do anything.
14
19
  def bindtextdomain(temp1 = nil, temp2 = nil, temp3 = nil)
15
20
  #nothing here.
16
21
  end
@@ -1,6 +1,12 @@
1
+ #This class reads .po-files generated by something like POEdit and can be used to run multi-language applications or websites.
1
2
  class Knj::Gettext_threadded
2
- attr_reader :langs, :args
3
+ #Hash that contains all translations loaded.
4
+ attr_reader :langs
3
5
 
6
+ #Config-hash that contains encoding and more.
7
+ attr_reader :args
8
+
9
+ #Initializes various data.
4
10
  def initialize(args = {})
5
11
  @args = {
6
12
  :encoding => "utf-8"
@@ -10,7 +16,9 @@ class Knj::Gettext_threadded
10
16
  load_dir(@args["dir"]) if @args["dir"]
11
17
  end
12
18
 
13
- #Loads a 'locales'-directory with .mo- and .po-files.
19
+ #Loads a 'locales'-directory with .mo- and .po-files and fills the '@langs'-hash.
20
+ #===Examples
21
+ # gtext.load_dir("#{File.dirname(__FILE__)}/../locales")
14
22
  def load_dir(dir)
15
23
  @dirs << dir
16
24
  check_folders = ["LC_MESSAGES", "LC_ALL"]
@@ -44,6 +52,10 @@ class Knj::Gettext_threadded
44
52
  end
45
53
  end
46
54
 
55
+ #Translates a given string to a given locale from the read .po-files.
56
+ #===Examples
57
+ # str = "Hello" #=> "Hello"
58
+ # gtext.trans("da_DK", str) #=> "Hej"
47
59
  def trans(locale, str)
48
60
  locale = locale.to_s
49
61
  str = str.to_s
@@ -1,3 +1,4 @@
1
+ #Contains various methods for doing stuff quick using the Gtk2-extension.
1
2
  module Knj::Gtk2
2
3
  #Autoloader.
3
4
  def self.const_missing(name)
@@ -5,10 +6,15 @@ module Knj::Gtk2
5
6
  return Knj::Gtk2.const_get(name)
6
7
  end
7
8
 
8
- def msgbox(p1, p2 = "warning", p3 = "Warning")
9
- return Knj::Gtk2.msgbox(p1, p2, p3)
9
+ #Alias for self.msgbox.
10
+ def msgbox(*args, &block)
11
+ return Knj::Gtk2.msgbox(*args, &block)
10
12
  end
11
13
 
14
+ #Shows a dialog on the screen based on various arguments.
15
+ #===Examples
16
+ # Knj::Gtk2.msgbox("Message", "Title", "info")
17
+ # Knj::Gtk2.msgbox("Question", "Title", "yesno") #=> "yes"|"no"|"cancel"|"close"
12
18
  def self.msgbox(paras, type = "warning", title = nil)
13
19
  if paras.is_a?(Array)
14
20
  msg = paras[0]
@@ -151,6 +157,9 @@ module Knj::Gtk2
151
157
  end
152
158
  end
153
159
 
160
+ #Takes a Gtk::Builder-object and runs labels and titles through GetText.gettext in order to translate them.
161
+ #===Examples
162
+ # Knj::Gtk2.translate(builder_obj)
154
163
  def self.translate(builderob)
155
164
  builderob.objects.each do |object|
156
165
  class_str = object.class.to_s
@@ -163,6 +172,9 @@ module Knj::Gtk2
163
172
  end
164
173
  end
165
174
 
175
+ #Makes a Gtk::Table based on the given arguments.
176
+ #===Examples
177
+ # Knj::Gtk2.form([{"type" => "text", "name" => "txtname", "title" => _("Name")}]) #=> {"table" => <Gtk::Table>, "objects" => <Array>}
166
178
  def self.form(paras)
167
179
  table = Gtk::Table.new(paras.length, 2)
168
180
  table.row_spacings = 4
@@ -239,11 +251,15 @@ module Knj::Gtk2
239
251
  }
240
252
  end
241
253
 
254
+ #Takes a given object and sets its value.
255
+ #===Examples
256
+ # Knj::Gtk2.form_setval(text_obj, "Hejsa")
257
+ # Knj::Gtk2.form_setval(checkbox_obj, 1)
242
258
  def self.form_setval(object, val)
243
259
  if object.is_a?(Gtk::Entry)
244
260
  object.text = val.to_s
245
261
  elsif object.is_a?(Gtk::CheckButton)
246
- if (val.to_s == "1")
262
+ if val.to_s == "1"
247
263
  object.active = true
248
264
  else
249
265
  object.active = false
@@ -253,11 +269,15 @@ module Knj::Gtk2
253
269
  end
254
270
  end
255
271
 
272
+ #Returns the value of an object regardless of that type the object is.
273
+ #===Examples
274
+ # Knj::Gtk2.form_getval(text_obj) #=> "Hejsa"
275
+ # Knj::Gtk2.form_getval(checkbox_obj) #=> "1"
256
276
  def self.form_getval(object)
257
277
  if object.is_a?(Gtk::Entry)
258
278
  return object.text
259
279
  elsif object.is_a?(Gtk::CheckButton)
260
- if (object.active?)
280
+ if object.active?
261
281
  return "1"
262
282
  else
263
283
  return "0"
@@ -271,7 +291,9 @@ module Knj::Gtk2
271
291
  end
272
292
  end
273
293
 
294
+ #Defines a shortcut-method on Gtk::Builder
274
295
  class Gtk::Builder
296
+ #Proxies to Knj::Gtk2.translate
275
297
  def translate
276
298
  return Knj::Gtk2.translate(self)
277
299
  end
@@ -1,5 +1,14 @@
1
+ #This class holds methods to manipulate images.
1
2
  class Knj::Image
2
3
  #This function can make rounded transparent corners on an image with a given radius. Further more it can also draw borders around the entire image in a given color and take the border into account.
4
+ #===Examples
5
+ # img = Magick::Image.read(path_str)
6
+ # Knj::Image.rounded_corners(
7
+ # :img => img,
8
+ # :radius => 25,
9
+ # :border => 1,
10
+ # :border_color => "#000000"
11
+ # )
3
12
  def self.rounded_corners(args)
4
13
  raise "No or invalid ':img' given: '#{args}'." if !args[:img]
5
14
  raise "No or invalid ':radius' given: '#{args}'." if !args[:radius].respond_to?("to_i") or args[:radius].to_i <= 0
@@ -154,11 +163,15 @@ class Knj::Image
154
163
  end
155
164
 
156
165
  #Returns the width relative to the height.
166
+ #===Examples
167
+ # Knj::Image.width_for_height(640, 480, 400) #=> 533
157
168
  def self.width_for_height(orig_width, orig_height, new_height)
158
169
  return (orig_width.to_f / (orig_height.to_f / new_height.to_f)).to_i
159
170
  end
160
171
 
161
172
  #Returns the height relative to the width.
173
+ #===Examples
174
+ # Knj::Image.height_for_width(640, 480, 533) #=> 399
162
175
  def self.height_for_width(orig_width, orig_height, new_width)
163
176
  return (orig_height.to_f / (orig_width.to_f / new_width.to_f)).to_i
164
177
  end
@@ -1,6 +1,9 @@
1
+ #This class helps handeling time-columns in databases.
1
2
  class Knj::Db::Dbtime
3
+ #These variables return information about the object.
2
4
  attr_reader :hours, :mins, :secs, :total_secs
3
5
 
6
+ #Initializes the object from arguments useually given by Knj::Datarow.
4
7
  def initialize(args)
5
8
  args = {:time => args} if args.is_a?(String)
6
9
 
@@ -20,10 +23,12 @@ class Knj::Db::Dbtime
20
23
  @total_secs += @secs
21
24
  end
22
25
 
26
+ #Returns the total amount of hours.
23
27
  def hours_total
24
28
  return (@total_secs.to_f / 3600)
25
29
  end
26
30
 
31
+ #Return the total amount of minutes.
27
32
  def mins_total
28
33
  return (@total_secs.to_f / 60)
29
34
  end
@@ -248,7 +248,7 @@ class Knj::Db::Revision
248
248
  table_data["on_create_after"].call("db" => db, "table_name" => table_name, "table_data" => table_data)
249
249
  end
250
250
 
251
- self.rows_init("db" => db, "table" => table_obj, "rows" => table_data["rows"]) if table_data["rows"]
251
+ rows_init("db" => db, "table" => table_obj, "rows" => table_data["rows"]) if table_data["rows"]
252
252
  end
253
253
  rescue Knj::Errors::Retry
254
254
  retry
@@ -1,4 +1,10 @@
1
+ #This class and subclasses holds various functionality to view status for Kvm-instances.
1
2
  class Knj::Kvm
3
+ #Lists all running Kvm-instances on this machine.
4
+ #===Examples
5
+ # Knj::Kvm.list do |kvm|
6
+ # print kvm.pid
7
+ # end
2
8
  def self.list
3
9
  list = []
4
10
 
@@ -35,24 +41,32 @@ class Knj::Kvm
35
41
  end
36
42
  end
37
43
 
44
+ #Describes each Kvm-instance.
38
45
  class Knj::Kvm::Machine
46
+ #Sets the data called from Knj::Kvm.list.
39
47
  def initialize(args)
40
48
  @args = args
41
49
  end
42
50
 
51
+ #Returns the PID of the Kvm-instance.
43
52
  def pid
44
53
  return @args[:pid]
45
54
  end
46
55
 
56
+ #Returns the name from the Kvm-instance.
47
57
  def name
48
58
  return @args[:name]
49
59
  end
50
60
 
61
+ #Returns the MAC from a network interfaces on the Kvm-instance.
51
62
  def mac
52
63
  raise "No MAC-address has been registered for this machine." if !@args.key?(:mac)
53
64
  return @args[:mac]
54
65
  end
55
66
 
67
+ #Returns what virtual interface the Kvm is using.
68
+ #===Examples
69
+ # kvm.iface #=> "vnet12"
56
70
  def iface
57
71
  if !@iface
58
72
  res = Knj::Os.shellcmd("ifconfig | grep \"#{self.mac[3, self.mac.length]}\"")
@@ -67,6 +81,9 @@ class Knj::Kvm::Machine
67
81
  return @iface
68
82
  end
69
83
 
84
+ #Returns various data about the networking (how much have been sent and recieved).
85
+ #===Examples
86
+ # kvm.net_status #=> {:tx => 1024, :rx => 2048}
70
87
  def net_status
71
88
  res = Knj::Os.shellcmd("ifconfig \"#{self.iface}\"")
72
89
 
@@ -83,6 +100,9 @@ class Knj::Kvm::Machine
83
100
  return ret
84
101
  end
85
102
 
103
+ #Returns various data about how much disk IO the Kvm-instance have been using.
104
+ #===Examples
105
+ # kvm.io_status #=> {:read_bytes => 1024, :write_bytes => 2048}
86
106
  def io_status
87
107
  io_status = File.read("/proc/#{self.pid}/io")
88
108