~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/commands/doc/introspection.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# introspection.rb: get informations about what is known to ctioga2
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/commands/commands'
 
16
 
 
17
module CTioga2
 
18
 
 
19
  Version::register_svn_info('$Revision: 238 $', '$Date: 2011-01-23 21:58:08 +0100 (Sun, 23 Jan 2011) $')
 
20
 
 
21
  module Commands
 
22
 
 
23
    module Documentation
 
24
 
 
25
      # This class provides facilities to display information
 
26
      class Introspection
 
27
        
 
28
        # Display all known commands, along with their definition place
 
29
        def list_commands(format = :pretty)
 
30
          cmds = Interpreter::commands
 
31
          names = cmds.keys.sort
 
32
          case format
 
33
          when :list
 
34
            puts names
 
35
          when :yaml
 
36
            require 'yaml'
 
37
            commands = {}
 
38
            for n in names
 
39
              cmd = cmds[n]
 
40
              command = {}
 
41
              command['name'] = n
 
42
              f,l = cmd.context
 
43
              command['file'] = f
 
44
              command['line'] = l.to_i
 
45
              command['long_option'] = cmd.long_option
 
46
              command['short_option'] = cmd.short_option
 
47
              command['short_description'] = cmd.short_description
 
48
              command['long_description'] = cmd.long_description
 
49
              commands[n] = command
 
50
            end
 
51
            puts YAML.dump(commands)
 
52
          else
 
53
            puts "Known commands:" 
 
54
            max = names.inject(0) {|m,x| [m,x.size].max}
 
55
            max2 = names.inject(0) {|m,x| [m,cmds[x].long_option.size].max}
 
56
            for n in names
 
57
              f,l = cmds[n].context
 
58
              puts "\t%-#{max}s\t--%-#{max2}s\t(#{f}: #{l})" % 
 
59
                [n, cmds[n].long_option ]
 
60
            end
 
61
          end
 
62
        end
 
63
 
 
64
        # List known groups
 
65
        def list_groups(raw = false)
 
66
          puts "Known groups:" unless raw
 
67
          groups = Interpreter::groups
 
68
          names = groups.keys.sort
 
69
          if raw
 
70
            puts names
 
71
          else
 
72
            for n in names
 
73
              f,l = groups[n].context
 
74
              puts "\t#{n}\t(#{f}: #{l})"
 
75
            end
 
76
          end
 
77
        end
 
78
 
 
79
        # List known types
 
80
        def list_types(raw = false)
 
81
          puts "Known types:" unless raw
 
82
          types = Interpreter::types
 
83
          names = types.keys.sort
 
84
          if raw
 
85
            puts names
 
86
          else
 
87
            for n in names
 
88
              f,l = types[n].context
 
89
              puts "\t#{n}\t(#{f}: #{l})"
 
90
            end
 
91
          end
 
92
        end
 
93
 
 
94
        # Lauches an editor to edit the given command:
 
95
        def edit_command(cmd)
 
96
          cmd = Interpreter::command(cmd)
 
97
          if cmd
 
98
            edit_file(*cmd.context)
 
99
          end
 
100
        end
 
101
 
 
102
        # Lauches an editor to edit the given command:
 
103
        def edit_group(group)
 
104
          group = Interpreter::group(group)
 
105
          if group
 
106
            edit_file(*group.context)
 
107
          end
 
108
        end
 
109
 
 
110
        # Lauches an editor to edit the given command:
 
111
        def edit_type(type)
 
112
          type = Interpreter::type(type)
 
113
          if type
 
114
            edit_file(*type.context)
 
115
          end
 
116
        end
 
117
 
 
118
 
 
119
        protected 
 
120
 
 
121
        # Launches an editor to edit the given file at the given place.
 
122
        def edit_file(file, line)
 
123
          editor = ENV['EDITOR'] || 'emacs'
 
124
          if ENV['CT2_DEV_HOME']
 
125
            file = "#{ENV['CT2_DEV_HOME']}/#{file}"
 
126
          end
 
127
          system("#{editor} +#{line} #{file} &")
 
128
        end
 
129
 
 
130
      end
 
131
 
 
132
      InternalFormatRE = {
 
133
        /list|raw/i => :list,
 
134
        /default|pretty/i => :pretty,
 
135
        /yaml/i => :yaml
 
136
      }
 
137
      
 
138
      
 
139
      InternalFormatType = CmdType.new('internal-format',
 
140
                                       { :type => :re_list,
 
141
                                         :list => InternalFormatRE}, <<EOD)
 
142
Output format for internals.
 
143
EOD
 
144
 
 
145
 
 
146
      IntrospectionGroup = 
 
147
        CmdGroup.new('introspection', "Introspection",
 
148
                     <<EOD, 100)
 
149
Commands displaying information about the internals of ctioga2, such 
 
150
as known types/commands/backends...
 
151
EOD
 
152
 
 
153
      TypeOption = {'format' => CmdArg.new('internal-format')}
 
154
      RawOption = {'raw' => CmdArg.new('boolean')}
 
155
 
 
156
      ListCommandsCmd = 
 
157
        Cmd.new('list-commands', nil, '--list-commands',
 
158
                [], RawOption.dup.update(TypeOption)) do |p, opts|
 
159
        opts['format'] = :list if opts['raw']
 
160
        
 
161
        Introspection.new.list_commands(opts['format'])
 
162
      end
 
163
 
 
164
      ListCommandsCmd.describe("List known commands",
 
165
                               <<EOH, IntrospectionGroup)
 
166
List all commands known to ctioga2
 
167
EOH
 
168
 
 
169
      ListGroupsCmd = 
 
170
        Cmd.new('list-groups', nil, '--list-groups',
 
171
                [], RawOption) do |p, opts|
 
172
        Introspection.new.list_groups(opts['raw'])
 
173
      end
 
174
 
 
175
      ListGroupsCmd.describe("List known groups",
 
176
                             <<EOH, IntrospectionGroup)
 
177
List all command groups known to ctioga2
 
178
EOH
 
179
 
 
180
      ListTypesCmd = 
 
181
        Cmd.new('list-types', nil, '--list-types',
 
182
                [], RawOption) do |p, opts|
 
183
        Introspection.new.list_types(opts['raw'])
 
184
      end
 
185
 
 
186
      ListTypesCmd.describe("List known types",
 
187
                             <<EOH, IntrospectionGroup)
 
188
List all types known to ctioga2
 
189
EOH
 
190
 
 
191
      EditCommandCmd = 
 
192
        Cmd.new('edit-command', nil, '--edit-command',
 
193
                [ CmdArg.new('text')]) do |plotmaker, cmd|
 
194
        Introspection.new.edit_command(cmd)
 
195
      end
 
196
 
 
197
      EditCommandCmd.describe("Edit the command",
 
198
                               <<EOH, IntrospectionGroup)
 
199
Edit the given command in an editor. It will only work from the 
 
200
top directory of a ctioga2 source tree.
 
201
EOH
 
202
 
 
203
      EditGroupCmd = 
 
204
        Cmd.new('edit-group', nil, '--edit-group',
 
205
                [ CmdArg.new('text')]) do |plotmaker, cmd|
 
206
        Introspection.new.edit_group(cmd)
 
207
      end
 
208
 
 
209
      EditGroupCmd.describe("Edit the group",
 
210
                            <<EOH, IntrospectionGroup)
 
211
Edit the given group in an editor. It will only work from the 
 
212
top directory of a ctioga2 source tree.
 
213
EOH
 
214
 
 
215
      EditTypeCmd = 
 
216
        Cmd.new('edit-type', nil, '--edit-type',
 
217
                [ CmdArg.new('text')]) do |plotmaker, cmd|
 
218
        Introspection.new.edit_type(cmd)
 
219
      end
 
220
 
 
221
      EditTypeCmd.describe("Edit the type",
 
222
                            <<EOH, IntrospectionGroup)
 
223
Edit the given type in an editor. It will only work from the 
 
224
top directory of a ctioga2 source tree.
 
225
EOH
 
226
 
 
227
      VersionRawCmd = 
 
228
        Cmd.new('version-raw', nil, '--version-raw',
 
229
                [ ]) do |plotmaker|
 
230
        print Version::version
 
231
      end
 
232
 
 
233
      VersionRawCmd.describe("Raw version",
 
234
                             <<EOH, IntrospectionGroup)
 
235
Prints the raw version number, without any other decoration and 
 
236
newline.
 
237
EOH
 
238
 
 
239
      
 
240
 
 
241
    end
 
242
 
 
243
  end
 
244
 
 
245
 
 
246
end
 
247