Class: Aio::Module::Loader
- Inherits:
-
Object
- Object
- Aio::Module::Loader
- Defined in:
- lib/aio/core/module_loader.rb
Constant Summary collapse
- Reg_klass_filter =
/class (.*)[\s]+<[\s]+Aio::Module/
- Reg_ruby_file =
/.rb$/
Instance Attribute Summary collapse
-
#module_manager ⇒ Object
Returns the value of attribute module_manager.
Class Method Summary collapse
-
.modules_path ⇒ Object
获取modules的路径.
Instance Method Summary collapse
-
#each_module_reference_name(path, opts = {}) ⇒ Object
output 放置各种输出方式的模块.
-
#get_module_klass_name(full_path) ⇒ Object
取模块的类名,以便以后调用.
-
#initialize(module_manager) ⇒ Loader
constructor
A new instance of Loader.
-
#load_module(full_module_path, module_type, module_layer_2, module_reference_name, module_klass_name, opts = {}) ⇒ Object
真正加载处 并将模块全部实例化.
-
#load_modules(path, opts = {}) ⇒ Object
全局加载模块.
- #module_reference_name_from_path(path) ⇒ Object
-
#modules_path ⇒ Object
获取modules的路径.
-
#vaild_ruby_file?(path) ⇒ Boolean
是否是有效的Ruby文件.
Constructor Details
#initialize(module_manager) ⇒ Loader
Returns a new instance of Loader.
14 15 16 |
# File 'lib/aio/core/module_loader.rb', line 14 def initialize(module_manager) @module_manager = module_manager end |
Instance Attribute Details
#module_manager ⇒ Object
Returns the value of attribute module_manager.
12 13 14 |
# File 'lib/aio/core/module_loader.rb', line 12 def module_manager @module_manager end |
Class Method Details
.modules_path ⇒ Object
获取modules的路径
160 161 162 163 |
# File 'lib/aio/core/module_loader.rb', line 160 def self.modules_path local_path = Pathname.new(File.dirname(__FILE__)).realpath return File.join(local_path, "..", "..", "modules") end |
Instance Method Details
#each_module_reference_name(path, opts = {}) ⇒ Object
output 放置各种输出方式的模块
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aio/core/module_loader.rb', line 25 def each_module_reference_name(path, opts={}) ::Dir.foreach(path) do |entry| if entry.downcase == "." or entry.downcase == ".." next end full_entry_path = ::File.join(path, entry) module_type = entry # 第一层目录结构,判断是否为cmd, input, output 的模块类型 unless ::File.directory?(full_entry_path) and module_manager.module_type_enable?(module_type) next end # module_type = "cmd" # full_entry_path = "~/aio/lib/modules/cmd" full_entry_pathname = Pathname.new(full_entry_path) Find.find(full_entry_path) do |entry_descendant_path| if File.directory?(entry_descendant_path) next end # 判断是不是.rb 结尾,而不是 .rb.swp 结尾 unless vaild_ruby_file?(entry_descendant_path) next end # entry_descendant_path 为ruby文件的完整绝对路径 entry_descendant_pathname = Pathname.new(entry_descendant_path) # 获得 modules 的二级目录名称, cmd/cisco 中的cisco # 或者 input/style 中的style base, _i = entry_descendant_pathname.split _i, module_layer_2 = base.split module_layer_2 = module_layer_2.to_s # 查询是否有效并且取得模块的类名 module_klass_name = get_module_klass_name(entry_descendant_pathname) if module_klass_name.empty? next end # 获得参考名 relative_entry_descendant_pathname = entry_descendant_pathname.relative_path_from(full_entry_pathname) _i, relative_entry_descendant_pathname = relative_entry_descendant_pathname.split relative_entry_descendant_path = relative_entry_descendant_pathname.to_s module_reference_name = module_reference_name_from_path(relative_entry_descendant_path) module_reference_name = [module_type, module_layer_2, module_reference_name].join("/") yield entry_descendant_path, module_type, module_layer_2, module_reference_name, module_klass_name end end end |
#get_module_klass_name(full_path) ⇒ Object
取模块的类名,以便以后调用
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/aio/core/module_loader.rb', line 84 def get_module_klass_name(full_path) fo = File.open(full_path) fo.each_line do |l| l = Aio::Base::Toolkit::String.safe(l) klass_name = Reg_klass_filter.match(l) if klass_name == nil next end klass_name = klass_name[1].strip return klass_name end return "" end |
#load_module(full_module_path, module_type, module_layer_2, module_reference_name, module_klass_name, opts = {}) ⇒ Object
真正加载处并将模块全部实例化
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/aio/core/module_loader.rb', line 134 def load_module(full_module_path, module_type,module_layer_2, module_reference_name, module_klass_name, opts={}) begin require "#{full_module_path}" module_klass = ::Object::const_get(module_klass_name).new rescue Exception puts "[-] Can not load module: #{full_module_path}" #puts caller return end @module_manager.add_module(full_module_path, module_type, module_layer_2, module_reference_name, module_klass) # 计数 # count_by_module_type = {"cmd" => { "cisco" => 2 }, "input" => { "style" => 1 } } count_by_module_type = opts[:count_by_module_type] if count_by_module_type if !count_by_module_type[module_type] count_by_module_type[module_type] = {} end count_by_module_type[module_type][module_layer_2] ||= 0 count_by_module_type[module_type][module_layer_2] += 1 end end |
#load_modules(path, opts = {}) ⇒ Object
全局加载模块
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/aio/core/module_loader.rb', line 111 def load_modules(path, opts={}) count_by_module_type = {} each_module_reference_name(path, opts={}) do |full_module_path, module_type, module_layer_2, module_reference_name, module_klass_name| load_module(full_module_path, module_type, module_layer_2, module_reference_name, module_klass_name, { # :recalcuate_by_device_type => recalculate_by_device_type, :count_by_module_type => count_by_module_type, } ) end module_manager.notify({ :count_by_module_type => count_by_module_type, }) end |
#module_reference_name_from_path(path) ⇒ Object
98 99 100 |
# File 'lib/aio/core/module_loader.rb', line 98 def module_reference_name_from_path(path) path.gsub(Reg_ruby_file, '') end |
#modules_path ⇒ Object
获取modules的路径
166 167 168 |
# File 'lib/aio/core/module_loader.rb', line 166 def modules_path self.class.modules_path end |
#vaild_ruby_file?(path) ⇒ Boolean
是否是有效的Ruby文件
103 104 105 106 107 108 |
# File 'lib/aio/core/module_loader.rb', line 103 def vaild_ruby_file?(path) if Reg_ruby_file.match(path) return true end return false end |