File: __init__.py

package info (click to toggle)
datapm 0.10-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 472 kB
  • ctags: 590
  • sloc: python: 2,760; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
'''dpm command line interface.

See dpm help for details.
'''
import os
import sys
import pkg_resources

from base import Command, parser

_commands = {}


## Need this command in here as it uses the list of commands
class HelpCommand(Command):
    name = 'help'
    usage = '%prog'
    summary = 'Show available commands'
    general_usage = \
'''%prog [options] <command>

For an introduction to dpm see the manual:

    $ datapm man

About information (including license details) is available via `datapm about`.
'''
    
    def run(self, options, args):
        if args:
            ## FIXME: handle errors better here
            command = args[0]
            if command not in _commands:
                raise Exception('No command with the name: %s' % command)
            command = _commands[command]
            command.parser.print_help()
            return
        print self.general_usage
        print 'Commands available:'
        commands = list(set(_commands.keys()))
        commands.sort()
        for command in commands:
            print '  %s: %s' % (command, _commands[command].summary)


import pkg_resources
for entry_point in pkg_resources.iter_entry_points('dpm.cli'):
    cmd = entry_point.load()
    cmdinstance = cmd()
    _commands[entry_point.name] = cmdinstance


def main(initial_args=None):
    if initial_args is None:
        initial_args = sys.argv[1:]
    options, args = parser.parse_args(initial_args)
    if not args:
        parser.error('You must give a command (use "datapm help" see a list of commands)')
    command = args[0].lower()
    if command not in _commands:
        parser.error('No command by the name %s %s' % (os.path.basename(sys.argv[0]), command))
    command = _commands[command]
    command.main(initial_args, args[1:], options)