plugin-loader 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plugin-loader
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Green
@@ -70,14 +70,8 @@ extensions: []
70
70
 
71
71
  extra_rdoc_files: []
72
72
 
73
- files:
74
- - lib/plugin/format/dbus.rb
75
- - lib/plugin/format/script.rb
76
- - lib/plugin/format/ruby.rb
77
- - lib/plugin/format/data.rb
78
- - lib/plugin/format/command.rb
79
- - lib/plugin/format.rb
80
- - lib/plugin/loader.rb
73
+ files: []
74
+
81
75
  has_rdoc: true
82
76
  homepage: https://launchpad.net/libplugin-loader-ruby
83
77
  licenses: []
data/lib/plugin/format.rb DELETED
@@ -1,68 +0,0 @@
1
- # format.rb
2
- #
3
- # Defines a class allowing new plugin formats to be added.
4
- #
5
- # Copyright 2010 David Green <david4dev@gmail.com>
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
- # MA 02110-1301, USA.
21
-
22
-
23
- module Plugin
24
-
25
- #An instance of this class represents a plugin format.
26
- class Format
27
- @@formats = {}
28
- #To create a new annonymous plugin format:
29
- # Plugin::Format.new do |exec, args, opts|
30
- # ...
31
- # return return_data
32
- # end
33
- #Argument 'exec' provides the data in the '$' key of the plugin metadata file.
34
- #Argument 'args' is an array of arguments passed to this format by a Plugin::Loader instance when its run method is called.
35
- #Argument 'opts' is a hash of options from Plugin::Loader that may be required by a plugin format. At present opts conatins the following keys:
36
- # :plugin_dir
37
- # :data_dir
38
- # :script_dir
39
- #These are the same as the options given when the Plugin::Loader was initialized.
40
- #
41
- #Plugin::Format.add should be used to create a new plugin format because the format is given a name.
42
- def initialize &b
43
- @block = b
44
- end
45
- #Creates a lambda function from the exec value and opts hash.
46
- #When the lambda function is called, its arguments become 'args' and
47
- #the block given at initialize is executed.
48
- def new exec, opts={}
49
- lambda do |*args|
50
- @block.call exec, args, opts
51
- end
52
- end
53
- #Creates a named plugin format using the same rules as Plugin::Format.new:
54
- # Plugin::Format.add "name" do |exec, args, opts|
55
- # ...
56
- # return return_data
57
- # end
58
- def self.add name, &b
59
- @@formats[name] = self.new &b
60
- end
61
- #Returns the format called 'name':
62
- # Plugin::Format['name']
63
- def self.[] name
64
- @@formats[name]
65
- end
66
- end
67
-
68
- end
@@ -1,35 +0,0 @@
1
- # command.rb
2
- #
3
- # Copyright 2010 David Green <david4dev@gmail.com>
4
- #
5
- # This program 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
- # This program 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 this program; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
- # MA 02110-1301, USA.
19
- #
20
- #The command format takes '$' to be a command, executes it, and
21
- #returns an array of lines of standard output.
22
- # {
23
- # "format" => "command",
24
- # "$" => "COMMAND"
25
- # }
26
- require 'plugin/format'
27
-
28
- #Depends: libescape-ruby
29
- require 'escape'
30
-
31
- Plugin::Format.add 'command' do |exec, args, opts|
32
- IO.popen Escape.shell_command([exec.to_s, *args]).to_s do |handle|
33
- return handle.read.split("\n")
34
- end
35
- end
@@ -1,32 +0,0 @@
1
- # data.rb
2
- #
3
- # Copyright 2010 David Green <david4dev@gmail.com>
4
- #
5
- # This program 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
- # This program 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 this program; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
- # MA 02110-1301, USA.
19
- #
20
- #data is a very simple plugin format that just returns the data structure
21
- #referred to by the '$' key in the plugin metadata file:
22
- # {
23
- # "format" : "data",
24
- # "$" : ...
25
- # }
26
- #The data can be any type supported by JSON, including number, string, array,
27
- #hash(object) and boolean.
28
- require 'plugin/format'
29
-
30
- Plugin::Format.add 'data' do |exec, args, opts|
31
- exec
32
- end
@@ -1,67 +0,0 @@
1
- # dbus.rb
2
- #
3
- # Defines a plugin format that works by connecting to a dbus service.
4
- #
5
- # Copyright 2010 David Green <david4dev@gmail.com>
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
- # MA 02110-1301, USA.
21
- #
22
- #
23
- #A plugin in dbus format has a general metadata file format of:
24
- # {
25
- # 'format' : 'dbus',
26
- # '$' : [service, object, interface, method]
27
- # }
28
- #For example:
29
- # {
30
- # "format" : "dbus",
31
- # "$" : [
32
- # "org.gnome.Rhythmbox",
33
- # "/org/gnome/Rhythmbox/Player",
34
- # "org.gnome.Rhythmbox.Player",
35
- # "getPlayingUri"
36
- # ]
37
- # }
38
- #describes a plugin that gets the current playing song in Rhythmbox.
39
- #
40
- #The dbus format uses the dbus session bus.
41
- #It only works with dbus services that can be introspected (which is almost all of them).
42
- require 'plugin/format'
43
-
44
- #Depends: libdbus-ruby
45
- require 'dbus'
46
-
47
- module Plugin
48
-
49
- Format.add 'dbus' do |exec, args, opts|
50
- service = exec[0]
51
- object = exec[1]
52
- iface = exec[2]
53
- method = exec[3]
54
-
55
- obj = DBus::SessionBus.
56
- instance.
57
- service(service).
58
- object(object)
59
-
60
- obj.introspect
61
-
62
- obj[iface].
63
- method(method).
64
- call(*args)
65
- end
66
-
67
- end
@@ -1,48 +0,0 @@
1
- # ruby.rb
2
- #
3
- # Copyright 2010 David Green <david4dev@gmail.com>
4
- #
5
- # This program 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
- # This program 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 this program; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
- # MA 02110-1301, USA.
19
- #
20
- #The ruby format allows plugins to be written in ruby.
21
- #The '$' key refers to the ruby file that should be loaded.
22
- #The metadata file has the following format:
23
- # {
24
- # "format" : "ruby",
25
- # "$" : "ruby_lib"
26
- # }
27
- #The ruby file should look something like:
28
- # Plugin.new do |*args|
29
- # #some code
30
- # end
31
- require 'plugin/format'
32
-
33
- module Plugin
34
-
35
- _ruby_plugin = lambda do |*args|
36
- args
37
- end
38
-
39
- def self.new &b
40
- _ruby_plugin = b
41
- end
42
-
43
- Format.add 'ruby' do |exec, args, opts|
44
- load exec
45
- _ruby_plugin.call *args
46
- end
47
-
48
- end
@@ -1,35 +0,0 @@
1
- # script.rb
2
- #
3
- # Copyright 2010 David Green <david4dev@gmail.com>
4
- #
5
- # This program 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
- # This program 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 this program; if not, write to the Free Software
17
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
- # MA 02110-1301, USA.
19
- #
20
- #The script format takes '$' to be an executable in the @script_dir folder.
21
- #The script is executed and an array of lines of standard output is returned.
22
- # {
23
- # "format" => "command",
24
- # "$" => "COMMAND"
25
- # }
26
- require 'plugin/format'
27
-
28
- #Depends: libescape-ruby
29
- require 'escape'
30
-
31
- Plugin::Format.add 'script' do |exec, args, opts|
32
- IO.popen Escape.shell_command(["#{opts[:dir] || '/usr/bin'}/#{exec}", *args]).to_s do |handle|
33
- return handle.read.split("\n")
34
- end
35
- end
data/lib/plugin/loader.rb DELETED
@@ -1,158 +0,0 @@
1
- # loader.rb
2
- #
3
- # Provides a class for loading plugins into a program.
4
- #
5
- # Copyright 2010 David Green <david4dev@gmail.com>
6
- #
7
- # This program is free software; you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation; either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program; if not, write to the Free Software
19
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
- # MA 02110-1301, USA.
21
- require 'plugin/format'
22
-
23
- #Depends: libjson-ruby
24
- require 'json'
25
-
26
- module Plugin
27
-
28
- #Using an instance of this class, you can control what plugins are
29
- #loaded into your program.
30
- class Loader
31
- #OPTIONS:
32
- #
33
- # :allowed_formats => [...] #optional; default is an array of all available plugin formats
34
- #- Defines an array of names (strings) of formats that plugins for this instance can be written in.
35
- #
36
- # :data_dir => '/usr/share/appname' #required
37
- #- Defines the directory in which the data for this instance is stored in the file system.
38
- #
39
- # :plugin_dir => '' #optional; default is "#{@data_dir}/plugins"
40
- #- Defines the directory in which the plugin metadata files are stored
41
- #
42
- # :script_dir => '' #optional; default is "#{@data_dir}/scripts"
43
- #- Defines the directory in which excecutables (potentially used by plugins) are stored.
44
- def initialize opts={}
45
- @available_formats = $:.map do |path|
46
- Dir.glob("#{path}/plugin/format/*.rb").map! do |rbfile|
47
- File.basename(rbfile).sub!(/\.rb$/, '')
48
- end
49
- end
50
- @available_formats.flatten!
51
- @allowed_formats = opts[:allowed_formats] ? (opts[:allowed_formats] & available_formats) : available_formats
52
- @allowed_formats.each do |fmt|
53
- require "plugin/format/#{fmt}"
54
- end
55
- @data_dir = opts[:data_dir]
56
- @plugin_dir = opts[:plugin_dir] || "#{@data_dir}/plugins"
57
- @script_dir = opts[:script_dir] || "#{@data_dir}/scripts"
58
- @plugins = {}
59
- @etc = {}
60
- end
61
- #Loads a plugin called 'name' of type 'type'.
62
- #It does this by reading the metadata file: "#{@plugin_dir}/name.type".
63
- #Plugin types should be considered namespaces for one or more plugins.
64
- #Plugin names are the unique identification of a plugin within a particular type.
65
- #Plugins are similar to functions - they are excecuted with arguments a value (which can be a complex data structure such as hash or array if needed).
66
- #load does not excecute a plugin but it once loaded
67
- #a plugin can be excecuted with the run method.
68
- #
69
- #The metadata files are in a JSON format:
70
- # {
71
- # "format" => "FORMAT_NAME",
72
- # "$" => ...
73
- # }
74
- #The 'format' key is a string that defines the name of the format of the plugin. If load loads a plugin without the 'format' key, an exception is raised.
75
- #If the format is not available or is not allowed (see: initailize), an exception is raised.
76
- #See Plugin::Format for more details on plugin formats.
77
- #The '$' key can be any data structure (most commonly a string) and is used by the format declaration to excecute a plugin. See the individual format declarations in plugin/format/*.rb for details of individual formats. If the '$' key is missing, an exception is raised.
78
- #
79
- #load returns any extra data keys in the JSON file as a hash.
80
- def load name, type
81
- File.open "#{@plugin_dir}/#{name}.#{type}" do |f|
82
- data = JSON.parse(f.read)
83
- unless data.has_key? '$'
84
- raise "#{type} plugin '#{name}' has an invalid metadata file. It is missing the required '$' key!"
85
- end
86
- unless data.has_key? 'format'
87
- raise "#{type} plugin '#{name}' has an invalid metadata file. It is missing the required 'format' key!"
88
- end
89
- unless @plugins.has_key? type
90
- @plugins[type] = {}
91
- end
92
- unless @etc.has_key? type
93
- @etc[type] = {}
94
- end
95
- fmt = data.delete 'format'
96
- format = Format[fmt]
97
- if format.nil?
98
- raise "Plugin format #{fmt} does not exist or is disabled!"
99
- end
100
- @plugins[type][name] = format.new data.delete('$'),
101
- :script_dir => @script_dir,
102
- :data_dir => @data_dir,
103
- :plugin_dir => @plugin_dir
104
- @etc[type][name] = data
105
- end
106
- end
107
- #Returns a hash of any extra keys in the plugin metadata file of
108
- #the plugin with type 'type' and name 'name'. The plugin needs
109
- #to be loaded for this to work.
110
- def etc name, type
111
- @etc[type][name]
112
- end
113
- #Unloads a plugin of name 'name' and type 'type'.
114
- def unload name, type
115
- @etc[type].delete name
116
- @plugins[type].delete name
117
- end
118
- #Loads all plugins of type 'type'.
119
- def load_type type
120
- Dir.glob("#{@plugin_dir}/*.#{type}").each do |file|
121
- self.load file
122
- end
123
- end
124
- #Unloads all plugins of type 'type'.
125
- def unload_type type
126
- @etc.delete type
127
- @plugins.delete type
128
- end
129
- #Loads all plugins.
130
- def load_all
131
- Dir.glob("#{@plugin_dir}/*").each do |file|
132
- self.load file
133
- end
134
- end
135
- #Unloads all plugins.
136
- def unload_all
137
- @etc.clear
138
- @plugins.clear
139
- end
140
- #Excecutes the plugin of type 'type' called 'name', passing in any other arguments.
141
- #The return value of the plugin is returned.
142
- def run name, type, *args
143
- @plugins[type][name].call *args
144
- end
145
- #Returns a list of the loaded types
146
- def types
147
- @plugins.keys
148
- end
149
- #Returns a list of the plugins of type 'type'
150
- def plugins type
151
- unless @plugins.has_key? type
152
- return []
153
- end
154
- @plugins[type].keys
155
- end
156
- end
157
-
158
- end