lib/spiderfw.rb in spiderfw-0.5.13 vs lib/spiderfw.rb in spiderfw-0.5.14
- old
+ new
@@ -36,10 +36,14 @@
# ::tmp:: Temp folder. Must be writable.
# ::log:: Log location.
attr_reader :paths
# Current Home
attr_reader :home
+ # Registered resource types
+ attr_reader :resource_types
+ # Main site
+ attr_accessor :site
# Initializes the runtime environment. This method is called when spider is required. Apps may implement
# an app_init method, that will be called after Spider::init is done.
def init(force=false)
return if @init_done && !force
@@ -73,10 +77,11 @@
if File.exist?($SPIDER_RUN_PATH+'/init.rb')
@home.instance_eval(File.read($SPIDER_RUN_PATH+'/init.rb'), $SPIDER_RUN_PATH+'/init.rb')
end
@logger.close(STDERR)
@logger.open(STDERR, Spider.conf.get('debug.console.level')) if Spider.conf.get('debug.console.level')
+ GetText::LocalePath.memoize_clear # since new paths have been added to GetText
@apps.each do |name, mod|
GetText.bindtextdomain(mod.short_name) if File.directory?(mod.path+'/po')
mod.app_init if mod.respond_to?(:app_init)
end
@init_done=true
@@ -105,10 +110,16 @@
raise "The server must be started from the root directory"
end
if (Spider.conf.get('template.cache.reload_on_restart'))
FileUtils.touch("#{Spider.paths[:tmp]}/templates_reload.txt")
end
+ if domain = Spider.conf.get('site.domain')
+ ssl_port = Spider.conf.get('site.ssl') ? Spider.conf.get('site.ssl_port') : nil
+ Spider.site = Site.new(domain, Spider.conf.get('site.port'), ssl_port)
+ elsif File.exists?(Site.cache_file)
+ Spider.site = Site.load_cache
+ end
if (Spider.conf.get('request.mutex'))
mutex_requests!
end
@apps.each do |name, mod|
mod.app_startup if mod.respond_to?(:app_startup)
@@ -121,30 +132,30 @@
end
# Invoked when a server is shutdown. Apps may implement the app_shutdown method, that will be called.
def shutdown
return unless Thread.current == Thread.main
- Debugger.post_mortem = false if Debugger && Debugger.post_mortem?
+ Debugger.post_mortem = false if Object.const_defined?(:Debugger) && Debugger.post_mortem?
@apps.each do |name, mod|
mod.app_shutdown if mod.respond_to?(:app_shutdown)
end
end
- def thread_current
- Thread.current[:spider] ||= {}
+ def current
+ Spider::Request.current
end
- def reset_thread_current
- Thread.current[:spider] = {}
- end
-
def request_started
@request_mutex.lock if (@request_mutex)
+ Spider::Request.current = {
+ :_start => Time.now
+ }
end
def request_finished
- reset_thread_current
+ # Spider.logger.info("Done in #{(Time.now - Spider::Request.current[:_start])*1000}ms")
+ Spider::Request.reset_current
@request_mutex.unlock if (@request_mutex)
end
def mutex_requests!
@request_mutex = Mutex.new
@@ -223,12 +234,18 @@
def load_app_at_path(path)
return if @loaded_apps[path]
@loaded_apps[path] = true
last_name = path.split('/')[-1]
- app_files = ['_init.rb', last_name+'.rb', 'config/options.rb', 'cmd.rb']
+ app_files = ['_init.rb', last_name+'.rb', 'cmd.rb']
app_files.each{ |f| require path+'/'+f if File.exist?(path+'/'+f)}
+ # if File.exist?("#{path}/data/locale")
+ # ENV['GETTEXT_PATH'] += ',' if ENV['GETTEXT_PATH']
+ # ENV['GETTEXT_PATH'] += "#{path}/data/locale"
+ # end
+ # GETTEXT_PATH is broken at the moment in gettext 2.1.0
+ GetText::LocalePath.add_default_rule("#{path}/data/locale/%{lang}/LC_MESSAGES/%{name}.mo")
end
def load_apps(*l)
l.each do |app|
load_app(app)
@@ -275,12 +292,14 @@
@apps[mod.name] = mod
@apps_by_path[mod.relative_path] = mod
@apps_by_short_name[mod.short_name] = mod
end
- def app?(path)
- @apps_by_path[path] ? true : false
+ def app?(path_or_name)
+ return true if @apps_by_path[path_or_name]
+ return true if @apps_by_short_name[path_or_name]
+ return false
end
def load_configuration(path)
return unless File.directory?(path)
path += '/' unless path[-1] == ?o
@@ -311,14 +330,21 @@
SpiderController
end
# Sets routes on the #controller for the given apps.
def route_apps(*apps)
+ options = {}
+ if apps[-1].is_a?(Hash)
+ options = apps.pop
+ end
@route_apps = apps.empty? ? true : apps
if (@route_apps)
apps_to_route = @route_apps == true ? self.apps.values : @route_apps.map{ |name| self.apps[name] }
end
+ if options[:except]
+ apps_to_route.reject{ |app| options[:except].include?(app) }
+ end
if (apps_to_route)
apps_to_route.each{ |app| @home.controller.route_app(app) }
end
end
@@ -334,10 +360,18 @@
:extensions => options[:extensions],
:path => options[:path] || name.to_s
}
end
+ def path
+ $SPIDER_PATH
+ end
+
+ def relative_path
+ '/spider'
+ end
+
# Returns the full path of a resource.
# resource_type may be :views, or any other type registered with #register_resource_type
# path is the path of the resource, relative to the resource folder
# cur_path, if provided, is the current working path
# owner_class, if provided, must respond to *app*
@@ -354,11 +388,14 @@
return full if (File.exist?(full))
end
return nil
end
+ search_paths ||= []
owner_classes.each do |owner_class| # FIXME: refactor
+ next if owner_class.is_a?(Spider::Home) # home is already checked for other owner_classes
+ # FIXME: maybe it shouldn't get here?
owner_class = nil if owner_class == NilClass
resource_config = @resource_types[resource_type]
raise "Unknown resource type #{resource_type}" unless resource_config
resource_rel_path = resource_config[:path]
extensions = [nil] + resource_config[:extensions]
@@ -375,46 +412,93 @@
elsif (path[0..1] == '../')
return Resource.new(first_found(extensions, File.dirname(cur_path)+path[2..-1]), owner_class)
end
end
app = nil
+ path_app = nil
if (path[0].chr == '/')
first_part = path[1..-1].split('/')[0]
Spider.apps_by_path.each do |p, a|
- if path.index(p) == 1 # FIXME: breaks something
+ if path.index(p+'/') == 1 # FIXME: might not be correct
#if first_part == p
- app = a
+ path_app = a
path = path[p.length+2..-1]
break
end
end
- elsif (owner_class.is_a?(Spider::App))
+ app = path_app
+ elsif owner_class <= Spider::App
app = owner_class
else
app = owner_class.app if (owner_class && owner_class.app)
end
return Resource.new(cur_path+'/'+path, owner_class) if cur_path && File.exist?(cur_path+'/'+path) # !app
raise "Can't find owner app for resource #{path}" unless app
- search_locations = [["#{Spider.paths[:root]}/#{resource_rel_path}/#{app.relative_path}", @home]]
- if app.respond_to?("#{resource_type}_path")
- search_locations << [app.send("#{resource_type}_path"), app]
- else
- search_locations << [app.path+'/'+resource_rel_path, app]
- end
- search_locations << [$SPIDER_PATH+'/'+resource_rel_path, self]
+ search_locations = resource_search_locations(resource_type, app)
search_paths.each do |p|
p = [p, owner_class] unless p.is_a?(Array)
search_locations << p
end
search_locations.each do |p|
found = first_found(extensions, p[0]+'/'+path)
- return Resource.new(found, p[1]) if found
+ definer = path_app || p[1]
+ return Resource.new(found, definer) if found
end
end
return Resource.new(path)
end
+ def resource_search_locations(resource_type, app=nil)
+ resource_config = @resource_types[resource_type]
+ resource_rel_path = resource_config[:path]
+ app_rel_path = app && app.respond_to?(:relative_path) ? app.relative_path : nil
+ root_search = File.join(Spider.paths[:root], resource_rel_path)
+ root_search = File.join(root_search, app_rel_path) if app_rel_path
+ # unless cur_path && cur_path == File.join(root_search, path)
+ search_locations = [[root_search, @home]]
+ # end
+ if app
+ if app.respond_to?("#{resource_type}_path")
+ search_locations << [app.send("#{resource_type}_path"), app]
+ else
+ search_locations << [File.join(app.path, resource_rel_path), app]
+ end
+ end
+ spider_path = File.join($SPIDER_PATH, resource_rel_path)
+ search_locations << [spider_path, self]
+ search_locations
+ end
+
+ def list_resources(resource_type, owner_class=nil, start=nil, search_paths = [])
+ app = nil
+ if owner_class <= Spider::App
+ app = owner_class
+ else
+ app = owner_class.app if (owner_class && owner_class.app)
+ end
+ search_locations = resource_search_locations(resource_type, app)
+ resource_config = @resource_types[resource_type]
+ extensions = resource_config[:extensions]
+ search_paths.each do |p|
+ p = [p, owner_class] unless p.is_a?(Array)
+ search_locations << p
+ end
+ res = []
+ search_locations.reverse.each do |p|
+ pname = Pathname.new(p[0])
+ base = p[0]
+ base = File.join(base, start) if start
+ extensions.each do |ext|
+ Dir.glob("#{base}/*.#{ext}").each do |f|
+ res << (Pathname.new(f).relative_path_from(pname)).to_s
+ end
+ end
+ end
+ res.uniq
+
+ end
+
def find_resource_path(resource_type, path, cur_path=nil, owner_classes=nil, search_paths=[])
res = find_resource(resource_type, path, cur_path, owner_classes, search_paths)
return res ? res.path : nil
end
@@ -463,30 +547,28 @@
def runmode=(mode)
raise "Can't change runmode" if @runmode
@runmode = mode
@configuration.include_set(mode)
- case mode
- when 'devel'
+ if mode == 'devel' || File.exists?(File.join($SPIDER_RUN_PATH,'tmp', 'debug.txt'))
init_debug
end
if (mode != 'production')
Spider.paths[:var] += "/#{mode}"
end
end
def init_debug
- if (RUBY_VERSION_PARTS[1] == '8')
- begin
- require 'ruby-debug'
- if (Spider.conf.get('devel.trace.extended'))
- require 'spiderfw/utils/monkey/debugger'
- Debugger.start
- Debugger.post_mortem
- end
- require 'ruby-prof'
- rescue LoadError, RuntimeError; end
- end
+ begin
+ require 'ruby-debug'
+ if File.exists?(File.join($SPIDER_RUN_PATH,'tmp', 'debug.txt'))
+ Debugger.wait_connection = true
+ Debugger.start_remote
+ File.delete(File.join($SPIDER_RUN_PATH,'tmp', 'debug.txt'))
+ else
+ Debugger.start
+ end
+ rescue LoadError, RuntimeError; end
end
def locale
Locale.current[0]
end