rUtilAnts 0.3.0.20110825 → 1.0.0.20120223

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ #--
2
+ # Copyright (c) 2012 Muriel Salvan (muriel@x-aeon.com)
3
+ # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
+ #++
5
+
6
+ module RUtilAnts
7
+
8
+ # Make public methods from a module accessible in another module, using a singleton as proxy
9
+ #
10
+ # Parameters::
11
+ # * *iSrcModule* (_Module_): Module to get public methods from
12
+ # * *oDstModule* (_Module_): Module that will encapsulate the singleton and route all public methods through that singleton
13
+ def self.make_singleton_proxy(iSrcModule, oDstModule)
14
+ lSrcModuleConstName = iSrcModule.name.gsub(/\W/,'_')
15
+
16
+ # Create the singleton class
17
+ lSingletonClass = oDstModule.const_set("SingletonClassForModule__#{lSrcModuleConstName}", Class.new)
18
+ lSingletonClass.class_eval("include #{iSrcModule}")
19
+
20
+ # Instantiate it in the module
21
+ lSymSingletonVarName = "@@__SingletonForModule__#{lSrcModuleConstName}".to_sym
22
+ oDstModule.send(:class_variable_set, lSymSingletonVarName, lSingletonClass.new)
23
+
24
+ # Create public methods from iSrcModule to oDstModule, using the singleton as a proxy
25
+ iSrcModule.instance_methods.map { |iMethodName| iMethodName.to_sym }.each do |iSymMethodName|
26
+ oDstModule.send(:define_method, iSymMethodName) do |*iArgs, &iBlock|
27
+ oDstModule.send(:class_variable_get, lSymSingletonVarName).send(iSymMethodName, *iArgs, &iBlock)
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2009 - 2011 Muriel Salvan (murielsalvan@users.sourceforge.net)
2
+ # Copyright (c) 2009 - 2012 Muriel Salvan (muriel@x-aeon.com)
3
3
  # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
4
  #++
5
5
 
@@ -18,11 +18,10 @@ module RUtilAnts
18
18
  class RedirectionError < RuntimeError
19
19
  end
20
20
 
21
- # Class
22
- class Manager
21
+ module URLAccessInterface
23
22
 
24
23
  # Constructor
25
- def initialize
24
+ def init_url_access
26
25
  # Get the map of plugins to read URLs
27
26
  # map< String, [ list<Regexp>, String ] >
28
27
  # map< PluginName, [ List of matching regexps, Plugin class name ] >
@@ -32,11 +31,11 @@ module RUtilAnts
32
31
  lPluginName = File.basename(iFileName)[0..-4]
33
32
  require "rUtilAnts/URLHandlers/#{lPluginName}"
34
33
  @Plugins[lPluginName] = [
35
- eval("RUtilAnts::URLCache::URLHandlers::#{lPluginName}::getMatchingRegexps"),
36
- "RUtilAnts::URLCache::URLHandlers::#{lPluginName}"
34
+ eval("RUtilAnts::URLAccess::URLHandlers::#{lPluginName}::get_matching_regexps"),
35
+ "RUtilAnts::URLAccess::URLHandlers::#{lPluginName}"
37
36
  ]
38
37
  rescue Exception
39
- logExc$!, "Error while requiring URLHandler plugin #{iFileName}"
38
+ log_exc$!, "Error while requiring URLHandler plugin #{iFileName}"
40
39
  end
41
40
  end
42
41
  end
@@ -45,27 +44,27 @@ module RUtilAnts
45
44
  # No cache.
46
45
  # It calls a code block with the binary content of the URL (or a local file name if required).
47
46
  #
48
- # Parameters:
47
+ # Parameters::
49
48
  # * *iURL* (_String_): The URL (used to detect cyclic redirections)
50
49
  # * *iParameters* (<em>map<Symbol,Object></em>): Additional parameters:
51
- # ** *:FollowRedirections* (_Boolean_): Do we follow redirections ? [optional = true]
52
- # ** *:NbrRedirectionsAllowed* (_Integer_): Number of redirections allowed [optional = 10]
53
- # ** *:LocalFileAccess* (_Boolean_): Do we need a local file to read the content from ? If not, the content itslef will be given the code block. [optional = false]
54
- # ** *:URLHandler* (_Object_): The URL handler, if it has already been instantiated, or nil otherwise [optional = nil]
50
+ # * *:follow_redirections* (_Boolean_): Do we follow redirections ? [optional = true]
51
+ # * *:nbr_redirections_allowed* (_Integer_): Number of redirections allowed [optional = 10]
52
+ # * *:local_file_access* (_Boolean_): Do we need a local file to read the content from ? If not, the content itslef will be given the code block. [optional = false]
53
+ # * *:url_handler* (_Object_): The URL handler, if it has already been instantiated, or nil otherwise [optional = nil]
55
54
  # * _CodeBlock_: The code returning the object corresponding to the content:
56
- # ** *iContent* (_String_): File content, or file name if :LocalFileAccess was true
57
- # ** *iFileBaseName* (_String_): The base name the file could have. Useful to get file name extensions.
58
- # ** Return:
59
- # ** _Exception_: The error encountered, or nil in case of success
60
- # Return:
55
+ # * *iContent* (_String_): File content, or file name if :local_file_access was true
56
+ # * *iFileBaseName* (_String_): The base name the file could have. Useful to get file name extensions.
57
+ # * Return::
58
+ # * _Exception_: The error encountered, or nil in case of success
59
+ # Return::
61
60
  # * _Exception_: The error encountered, or nil in case of success
62
- def accessFile(iURL, iParameters = {})
61
+ def access_file(iURL, iParameters = {})
63
62
  rError = nil
64
63
 
65
64
  lFollowRedirections = iParameters[:lFollowRedirections]
66
- lNbrRedirectionsAllowed = iParameters[:NbrRedirectionsAllowed]
67
- lLocalFileAccess = iParameters[:LocalFileAccess]
68
- lURLHandler = iParameters[:URLHandler]
65
+ lNbrRedirectionsAllowed = iParameters[:nbr_redirections_allowed]
66
+ lLocalFileAccess = iParameters[:local_file_access]
67
+ lURLHandler = iParameters[:url_handler]
69
68
  if (lFollowRedirections == nil)
70
69
  lFollowRedirections = true
71
70
  end
@@ -76,10 +75,10 @@ module RUtilAnts
76
75
  lLocalFileAccess = false
77
76
  end
78
77
  if (lURLHandler == nil)
79
- lURLHandler = getURLHandler(iURL)
78
+ lURLHandler = get_url_handler(iURL)
80
79
  end
81
80
  # Get the content from the handler
82
- lContentFormat, lContent = lURLHandler.getContent(lFollowRedirections)
81
+ lContentFormat, lContent = lURLHandler.get_content(lFollowRedirections)
83
82
  case (lContentFormat)
84
83
  when CONTENT_ERROR
85
84
  rError = lContent
@@ -92,10 +91,10 @@ module RUtilAnts
92
91
  elsif (lFollowRedirections)
93
92
  # Follow the redirection if we want it
94
93
  lNewParameters = iParameters.clone
95
- lNewParameters[:NbrRedirectionsAllowed] = lNbrRedirectionsAllowed - 1
94
+ lNewParameters[:nbr_redirections_allowed] = lNbrRedirectionsAllowed - 1
96
95
  # Reset the URL handler for the new parameters.
97
- lNewParameters[:URLHandler] = nil
98
- rError = accessFile(lContent, lNewParameters) do |iContent, iBaseName|
96
+ lNewParameters[:url_handler] = nil
97
+ rError = access_file(lContent, lNewParameters) do |iContent, iBaseName|
99
98
  yield(iContent, iBaseName)
100
99
  end
101
100
  else
@@ -106,7 +105,7 @@ module RUtilAnts
106
105
  if (lLocalFileAccess)
107
106
  # Write the content in a local temporary file
108
107
  require 'tmpdir'
109
- lBaseName = lURLHandler.getCorrespondingFileBaseName
108
+ lBaseName = lURLHandler.get_corresponding_file_base_name
110
109
  lLocalFileName = "#{Dir.tmpdir}/URLCache/#{lBaseName}"
111
110
  begin
112
111
  require 'fileutils'
@@ -125,7 +124,7 @@ module RUtilAnts
125
124
  end
126
125
  else
127
126
  # Give it to the code block directly
128
- yield(lContent, lURLHandler.getCorrespondingFileBaseName)
127
+ yield(lContent, lURLHandler.get_corresponding_file_base_name)
129
128
  end
130
129
  when CONTENT_LOCALFILENAME, CONTENT_LOCALFILENAME_TEMPORARY
131
130
  lLocalFileName = lContent
@@ -142,7 +141,7 @@ module RUtilAnts
142
141
  end
143
142
  end
144
143
  if (rError == nil)
145
- yield(lContent, lURLHandler.getCorrespondingFileBaseName)
144
+ yield(lContent, lURLHandler.get_corresponding_file_base_name)
146
145
  end
147
146
  # If the file was temporary, delete it
148
147
  if (lContentFormat == CONTENT_LOCALFILENAME_TEMPORARY)
@@ -155,11 +154,11 @@ module RUtilAnts
155
154
 
156
155
  # Get the URL handler corresponding to this URL
157
156
  #
158
- # Parameters:
157
+ # Parameters::
159
158
  # * *iURL* (_String_): The URL
160
- # Return:
159
+ # Return::
161
160
  # * _Object_: The URL handler
162
- def getURLHandler(iURL)
161
+ def get_url_handler(iURL)
163
162
  rURLHandler = nil
164
163
 
165
164
  # Try out every regexp unless it matches.
@@ -187,44 +186,23 @@ module RUtilAnts
187
186
 
188
187
  end
189
188
 
190
- # Initialize a global plugins cache
191
- def self.initializeURLAccess
192
- $rUtilAnts_URLAccess_Manager = Manager.new
193
- Object.module_eval('include RUtilAnts::URLAccess')
194
- end
189
+ # A class giving access to the URL access functionnality
190
+ class Manager
195
191
 
196
- # Access the content of a URL.
197
- # No cache.
198
- # It calls a code block with the binary content of the URL (or a local file name if required).
199
- #
200
- # Parameters:
201
- # * *iURL* (_String_): The URL (used to detect cyclic redirections)
202
- # * *iParameters* (<em>map<Symbol,Object></em>): Additional parameters:
203
- # ** *:FollowRedirections* (_Boolean_): Do we follow redirections ? [optional = true]
204
- # ** *:NbrRedirectionsAllowed* (_Integer_): Number of redirections allowed [optional = 10]
205
- # ** *:LocalFileAccess* (_Boolean_): Do we need a local file to read the content from ? If not, the content itself will be given the code block. [optional = false]
206
- # ** *:URLHandler* (_Object_): The URL handler, if it has already been instantiated, or nil otherwise [optional = nil]
207
- # * _CodeBlock_: The code returning the object corresponding to the content:
208
- # ** *iContent* (_String_): File content, or file name if :LocalFileAccess was true
209
- # ** *iFileBaseName* (_String_): The base name the file could have. Useful to get file name extensions.
210
- # ** Return:
211
- # ** _Exception_: The error encountered, or nil in case of success
212
- # Return:
213
- # * _Exception_: The error encountered, or nil in case of success
214
- def accessFile(iURL, iParameters = {})
215
- return $rUtilAnts_URLAccess_Manager.accessFile(iURL, iParameters) do |iContent, iBaseName|
216
- yield(iContent, iBaseName)
192
+ include URLAccessInterface
193
+
194
+ # Constructor
195
+ def initialize
196
+ init_url_access
217
197
  end
198
+
218
199
  end
219
200
 
220
- # Get the URL handler corresponding to this URL
221
- #
222
- # Parameters:
223
- # * *iURL* (_String_): The URL
224
- # Return:
225
- # * _Object_: The URL handler
226
- def getURLHandler(iURL)
227
- return $rUtilAnts_URLAccess_Manager.getURLHandler(iURL)
201
+ # Initialize a global plugins cache
202
+ def self.install_url_access_on_object
203
+ require 'rUtilAnts/SingletonProxy'
204
+ RUtilAnts::make_singleton_proxy(RUtilAnts::URLAccess::URLAccessInterface, Object)
205
+ init_url_access
228
206
  end
229
207
 
230
208
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2009 - 2011 Muriel Salvan (murielsalvan@users.sourceforge.net)
2
+ # Copyright (c) 2009 - 2012 Muriel Salvan (muriel@x-aeon.com)
3
3
  # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
4
  #++
5
5
 
@@ -7,17 +7,14 @@ module RUtilAnts
7
7
 
8
8
  module URLCache
9
9
 
10
- # Class that caches every access to a URI (local file name, http, data...).
11
- # This ensures just that several files are instantiated just once.
12
- # For local files, it takes into account the file modification date/time to know if the Wx::Bitmap file has to be refreshed.
13
- class URLCache
10
+ # Exception for reporting server down errors.
11
+ class ServerDownError < RuntimeError
12
+ end
14
13
 
15
- # Exception for reporting server down errors.
16
- class ServerDownError < RuntimeError
17
- end
14
+ module URLCacheInterface
18
15
 
19
16
  # Constructor
20
- def initialize
17
+ def init_url_cache
21
18
  # Map of known contents, interpreted in many flavors
22
19
  # map< Integer, [ Integer, Object ] >
23
20
  # map< URL's hash, [ CRC, Content ] >
@@ -35,39 +32,39 @@ module RUtilAnts
35
32
  # * file:// protocol
36
33
  # It also handles redirections or zipped files
37
34
  #
38
- # Parameters:
35
+ # Parameters::
39
36
  # * *iURL* (_String_): The URL
40
37
  # * *iParameters* (<em>map<Symbol,Object></em>): Additional parameters:
41
- # ** *:ForceLoad* (_Boolean_): Do we force to refresh the cache ? [optional = false]
42
- # ** *:FollowRedirections* (_Boolean_): Do we follow redirections ? [optional = true]
43
- # ** *:NbrRedirectionsAllowed* (_Integer_): Number of redirections allowed [optional = 10]
44
- # ** *:LocalFileAccess* (_Boolean_): Do we need a local file to read the content from ? If not, the content itslef will be given the code block. [optional = false]
38
+ # * *:force_load* (_Boolean_): Do we force to refresh the cache ? [optional = false]
39
+ # * *:follow_redirections* (_Boolean_): Do we follow redirections ? [optional = true]
40
+ # * *:nbr_redirections_allowed* (_Integer_): Number of redirections allowed [optional = 10]
41
+ # * *:local_file_access* (_Boolean_): Do we need a local file to read the content from ? If not, the content itslef will be given the code block. [optional = false]
45
42
  # * _CodeBlock_: The code returning the object corresponding to the content:
46
- # ** *iContent* (_String_): File content, or file name if :LocalFileAccess was true
47
- # ** Returns:
48
- # ** _Object_: Object read from the content, or nil in case of error
49
- # ** _Exception_: The error encountered, or nil in case of success
50
- # Return:
43
+ # * *iContent* (_String_): File content, or file name if :local_file_access was true
44
+ # * Return::
45
+ # * _Object_: Object read from the content, or nil in case of error
46
+ # * _Exception_: The error encountered, or nil in case of success
47
+ # Return::
51
48
  # * <em>Object</em>: The corresponding URL content, or nil in case of failure
52
49
  # * _Exception_: The error, or nil in case of success
53
- def getURLContent(iURL, iParameters = {})
50
+ def get_url_content(iURL, iParameters = {})
54
51
  rObject = nil
55
52
  rError = nil
56
53
 
57
54
  # Parse parameters
58
- lForceLoad = iParameters[:ForceLoad]
55
+ lForceLoad = iParameters[:force_load]
59
56
  if (lForceLoad == nil)
60
57
  lForceLoad = false
61
58
  end
62
59
  # Get the URL handler corresponding to this URL
63
- lURLHandler = getURLHandler(iURL)
64
- lServerID = lURLHandler.getServerID
60
+ lURLHandler = get_url_handler(iURL)
61
+ lServerID = lURLHandler.get_server_id
65
62
  if (@HostsDown.has_key?(lServerID))
66
63
  rError = ServerDownError.new("Server #{iURL} is currently down.")
67
64
  else
68
65
  lURLHash = iURL.hash
69
66
  # Check if it is in the cache, or if we force refresh, or if the URL was invalidated
70
- lCurrentCRC = lURLHandler.getCRC
67
+ lCurrentCRC = lURLHandler.get_crc
71
68
  if ((@URLs[lURLHash] == nil) or
72
69
  (lForceLoad) or
73
70
  (@URLs[lURLHash][0] != lCurrentCRC))
@@ -76,7 +73,7 @@ module RUtilAnts
76
73
  @URLs[lURLHash] = nil
77
74
  # Get the object
78
75
  lObject = nil
79
- lAccessError = accessFile(iURL, iParameters.merge(:URLHandler => lURLHandler)) do |iContent, iBaseName|
76
+ lAccessError = access_file(iURL, iParameters.merge(:url_handler => lURLHandler)) do |iContent, iBaseName|
80
77
  lObject, rError = yield(iContent)
81
78
  end
82
79
  if (lAccessError != nil)
@@ -105,39 +102,25 @@ module RUtilAnts
105
102
 
106
103
  end
107
104
 
108
- # Initialize a global cache
109
- def self.initializeURLCache
110
- $rUtilAnts_URLCache = URLCache.new
111
- Object.module_eval('include RUtilAnts::URLCache')
112
- end
105
+ # Class that caches every access to a URI (local file name, http, data...).
106
+ # This ensures just that several files are instantiated just once.
107
+ # For local files, it takes into account the file modification date/time to know if the Wx::Bitmap file has to be refreshed.
108
+ class URLCache
113
109
 
114
- # Get a content from a URL.
115
- # Here are the different formats the URL can have:
116
- # * Local file name
117
- # * http/https/ftp/ftps:// protocols
118
- # * data:image URI
119
- # * file:// protocol
120
- # It also handles redirections or zipped files
121
- #
122
- # Parameters:
123
- # * *iURL* (_String_): The URL
124
- # * *iParameters* (<em>map<Symbol,Object></em>): Additional parameters:
125
- # ** *:ForceLoad* (_Boolean_): Do we force to refresh the cache ? [optional = false]
126
- # ** *:FollowRedirections* (_Boolean_): Do we follow redirections ? [optional = true]
127
- # ** *:NbrRedirectionsAllowed* (_Integer_): Number of redirections allowed [optional = 10]
128
- # ** *:LocalFileAccess* (_Boolean_): Do we need a local file to read the content from ? If not, the content itself will be given the code block. [optional = false]
129
- # * _CodeBlock_: The code returning the object corresponding to the content:
130
- # ** *iContent* (_String_): File content, or file name if :LocalFileAccess was true
131
- # ** Returns:
132
- # ** _Object_: Object read from the content, or nil in case of error
133
- # ** _Exception_: The error encountered, or nil in case of success
134
- # Return:
135
- # * <em>Object</em>: The corresponding URL content, or nil in case of failure
136
- # * _Exception_: The error, or nil in case of success
137
- def getURLContent(iURL, iParameters = {})
138
- return $rUtilAnts_URLCache.getURLContent(iURL, iParameters) do |iContent|
139
- next yield(iContent)
110
+ include URLCacheInterface
111
+
112
+ # Constructor
113
+ def initialize
114
+ init_url_cache
140
115
  end
116
+
117
+ end
118
+
119
+ # Initialize a global cache
120
+ def self.install_url_cache_on_object
121
+ require 'rUtilAnts/SingletonProxy'
122
+ RUtilAnts::make_singleton_proxy(RUtilAnts::URLCache::URLCacheInterface, Object)
123
+ init_url_cache
141
124
  end
142
125
 
143
126
  end
@@ -1,11 +1,11 @@
1
1
  #--
2
- # Copyright (c) 2009 - 2011 Muriel Salvan (murielsalvan@users.sourceforge.net)
2
+ # Copyright (c) 2009 - 2012 Muriel Salvan (muriel@x-aeon.com)
3
3
  # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
4
  #++
5
5
 
6
6
  module RUtilAnts
7
7
 
8
- module URLCache
8
+ module URLAccess
9
9
 
10
10
  module URLHandlers
11
11
 
@@ -14,9 +14,9 @@ module RUtilAnts
14
14
 
15
15
  # Get a list of regexps matching the URL to get to this handler
16
16
  #
17
- # Return:
17
+ # Return::
18
18
  # * <em>list<Regexp></em>: The list of regexps matching URLs from this handler
19
- def self.getMatchingRegexps
19
+ def self.get_matching_regexps
20
20
  return [
21
21
  /^data:image.*$/
22
22
  ]
@@ -24,13 +24,13 @@ module RUtilAnts
24
24
 
25
25
  # Constructor
26
26
  #
27
- # Parameters:
27
+ # Parameters::
28
28
  # * *iURL* (_String_): The URL that this handler will manage
29
29
  def initialize(iURL)
30
30
  @URL = iURL
31
31
  lMatchData = @URL.match(/data:image\/(.*);base64,(.*)/)
32
32
  if (lMatchData == nil)
33
- logBug "URL #{iURL[0..23]}... was identified as a data:image like, but it appears to be false."
33
+ log_bug "URL #{iURL[0..23]}... was identified as a data:image like, but it appears to be false."
34
34
  else
35
35
  @Ext = lMatchData[1]
36
36
  if (@Ext == 'x-icon')
@@ -42,17 +42,17 @@ module RUtilAnts
42
42
 
43
43
  # Get the server ID
44
44
  #
45
- # Return:
45
+ # Return::
46
46
  # * _String_: The server ID
47
- def getServerID
47
+ def get_server_id
48
48
  return nil
49
49
  end
50
50
 
51
51
  # Get the current CRC of the URL
52
52
  #
53
- # Return:
53
+ # Return::
54
54
  # * _Integer_: The CRC
55
- def getCRC
55
+ def get_crc
56
56
  # As the content is in the URL, it will be natural to not find it anymore in the cache when it is changed.
57
57
  # Therefore there is no need to return a CRC.
58
58
  return 0
@@ -61,25 +61,25 @@ module RUtilAnts
61
61
  # Get a corresponding file base name.
62
62
  # This method has to make sure file extensions are respected, as it can be used for further processing.
63
63
  #
64
- # Return:
64
+ # Return::
65
65
  # * _String_: The file name
66
- def getCorrespondingFileBaseName
66
+ def get_corresponding_file_base_name
67
67
  return "DataImage.#{@Ext}"
68
68
  end
69
69
 
70
70
  # Get the content of the URL
71
71
  #
72
- # Parameters:
72
+ # Parameters::
73
73
  # * *iFollowRedirections* (_Boolean_): Do we follow redirections while accessing the content ?
74
- # Return:
74
+ # Return::
75
75
  # * _Integer_: Type of content returned
76
76
  # * _Object_: The content, depending on the type previously returned:
77
- # ** _Exception_ if CONTENT_ERROR: The corresponding error
78
- # ** _String_ if CONTENT_REDIRECT: The new URL
79
- # ** _String_ if CONTENT_STRING: The real content
80
- # ** _String_ if CONTENT_LOCALFILENAME: The name of the local file name storing the content
81
- # ** _String_ if CONTENT_LOCALFILENAME_TEMPORARY: The name of the temporary local file name storing the content
82
- def getContent(iFollowRedirections)
77
+ # * _Exception_ if CONTENT_ERROR: The corresponding error
78
+ # * _String_ if CONTENT_REDIRECT: The new URL
79
+ # * _String_ if CONTENT_STRING: The real content
80
+ # * _String_ if CONTENT_LOCALFILENAME: The name of the local file name storing the content
81
+ # * _String_ if CONTENT_LOCALFILENAME_TEMPORARY: The name of the temporary local file name storing the content
82
+ def get_content(iFollowRedirections)
83
83
  rContentFormat = nil
84
84
  rContent = nil
85
85