summaryrefslogtreecommitdiffstats
path: root/ipa
blob: 626eb04588024f4d174655ab0ab2dbba04308d52 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python

# Authors:
#   Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Command Line Interface to IPA.

Just proof of concept stuff in here right now.
"""

import sys
from ipalib.startup import api

TAB_WIDTH = 2

def _(msg):
    """
    Dummy gettext function for testing.
    """
    return msg

class row(object):
    def __init__(self, tab, c1, c2=None):
    	assert type(tab) is int
    	assert type(c1) in (str, int)
    	assert type(c2) is str or c2 is None
    	self.tab = tab
    	self.c1 = c1
    	self.c2 = c2

    def __len__(self):
    	return len(str(self.c1))

    def pretty_print(self, just):
    	tab = ' ' * (self.tab * TAB_WIDTH)
    	if self.c2 is None:
    		print '%s%s' % (tab, self.c1)
    	else:
    		if type(self.c1) is int:
    			c1 = str(self.c1).rjust(just)
    		else:
    			c1 = self.c1.ljust(just)
    		print '%s%s %s' % (tab, c1,	self.c2)

def pretty_print(rows):
    rows = tuple(rows)
    def get_lengths():
    	yield 0
    	for r in rows:
    		if r.c2 is not None:
    			yield len(r)
    max_len = max(get_lengths())
    for r in rows:
    	r.pretty_print(max_len)


def print_commands():
    print 'Commands:'
    m = api.max_cmd_len
    for cmd in api.cmd:
    	print '  %s  %s' % (str(cmd).ljust(m), cmd.get_doc(_))

def print_help(cmd):
    print 'Help on %s' % cmd














def print_api():
    def iter_api(tab):
    	for name in api:
    		ns = getattr(api, name)
    		yield row(
    			tab,
    			name,
    			repr(ns),
    		)
    		for i in ns:
    			yield row(
    				tab + 1,
    				i.name,
    				repr(i)
    			)

    def iter_obj(tab):
    	for obj in api.obj:
    		yield row(
    			tab,
    			obj.name,
    			repr(obj),
    		)
    		for (n, f) in [('mthd', '.%s()'), ('prop', '.%s')]:
    			ns = getattr(obj, n)
    			yield row(
    				tab + 1,
    				n,
    				repr(ns),
    			)
    			for attr in ns:
    				yield row(
    					tab + 2,
    					f % attr.name,
    					repr(attr),
    				)

    def iter_summary(tab):
    	for name in api:
    		ns = getattr(api, name)
    		yield row(
    			tab,
    			len(ns),
    			name
    		)

    def print_heading(h):
    	print '\n%s:' % h
    	print '-' * (len(h) + 1)

    tab = 1
    print_heading('API Overview')
    pretty_print(iter_api(tab))

    print_heading('Object Details')
    pretty_print(iter_obj(tab))

    print_heading('Summary')
    pretty_print(iter_summary(tab))





if len(sys.argv) < 2:
    print_commands()
    print 'Usage: ipa COMMAND [OPTIONS]'
    sys.exit(2)
name= sys.argv[1]
if name == '_api_':
    print_api()
    sys.exit()
elif name not in api.cmd:
    print_commands()
    print 'ipa: ERROR: unknown command %r' % name
    sys.exit(2)
api.cmd[name]()