Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
synnefo
Commits
ab896c50
Commit
ab896c50
authored
Apr 02, 2013
by
Christos Stavrakakis
Browse files
Use pprint_table in server-show command
parent
83b203c0
Changes
5
Hide whitespace changes
Inline
Side-by-side
snf-cyclades-app/synnefo/api/management/commands/server-show.py
View file @
ab896c50
...
...
@@ -31,16 +31,17 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from
django.core.management.base
import
BaseCommand
,
CommandError
from
synnefo.webproject.management.
util
import
format_bool
,
format_date
from
django.core.management.base
import
CommandError
from
synnefo.webproject.management.
commands
import
SynnefoCommand
from
synnefo.management.common
import
(
format_vm_state
,
get_vm
,
get_image
)
from
synnefo.lib.astakos
import
UserCache
from
synnefo.settings
import
(
CYCLADES_ASTAKOS_SERVICE_TOKEN
as
ASTAKOS_TOKEN
,
ASTAKOS_URL
)
from
synnefo.webproject.management
import
utils
class
Command
(
Base
Command
):
class
Command
(
Synnefo
Command
):
args
=
"<server ID>"
help
=
"Show server info"
...
...
@@ -61,20 +62,19 @@ class Command(BaseCommand):
image
=
'%s (%s)'
%
(
imageid
,
image_name
)
kv
=
{
'id'
:
server
.
id
,
'name'
:
server
.
name
,
'owner_uuid'
:
userid
,
'owner_name'
:
UserCache
(
ASTAKOS_URL
,
ASTAKOS_TOKEN
).
get_name
(
userid
),
'created'
:
format_date
(
server
.
created
),
'updated'
:
format_date
(
server
.
updated
),
'image'
:
image
,
'host id'
:
server
.
hostid
,
'flavor'
:
flavor
,
'deleted'
:
format_bool
(
server
.
deleted
),
'suspended'
:
format_bool
(
server
.
suspended
),
'state'
:
format_vm_state
(
server
),
'id'
:
server
.
id
,
'name'
:
server
.
name
,
'owner_uuid'
:
userid
,
'owner_name'
:
UserCache
(
ASTAKOS_URL
,
ASTAKOS_TOKEN
).
get_name
(
userid
),
'created'
:
utils
.
format_date
(
server
.
created
),
'updated'
:
utils
.
format_date
(
server
.
updated
),
'image'
:
image
,
'host id'
:
server
.
hostid
,
'flavor'
:
flavor
,
'deleted'
:
utils
.
format_bool
(
server
.
deleted
),
'suspended'
:
utils
.
format_bool
(
server
.
suspended
),
'state'
:
format_vm_state
(
server
),
}
for
key
,
val
in
sorted
(
kv
.
items
()):
line
=
'%s: %s
\n
'
%
(
key
.
rjust
(
16
),
val
)
self
.
stdout
.
write
(
line
.
encode
(
'utf8'
))
utils
.
pprint_table
(
self
.
stdout
,
[
kv
.
values
()],
kv
.
keys
(),
options
[
"output_format"
],
vertical
=
True
)
snf-cyclades-app/synnefo/logic/management/commands/backend-add.py
View file @
ab896c50
...
...
@@ -39,7 +39,7 @@ from synnefo.logic.backend import (get_physical_resources,
create_network_synced
,
connect_network_synced
)
from
synnefo.management.common
import
check_backend_credentials
from
synnefo.webproject.management.util
import
pprint_table
from
synnefo.webproject.management.util
s
import
pprint_table
class
Command
(
BaseCommand
):
...
...
snf-cyclades-app/synnefo/logic/management/commands/backend-modify.py
View file @
ab896c50
...
...
@@ -33,7 +33,7 @@
from
optparse
import
make_option
from
django.core.management.base
import
BaseCommand
,
CommandError
from
synnefo.webproject.management.util
import
parse_bool
from
synnefo.webproject.management.util
s
import
parse_bool
from
synnefo.management.common
import
(
get_backend
,
check_backend_credentials
)
...
...
snf-webproject/synnefo/webproject/management/commands/__init__.py
View file @
ab896c50
...
...
@@ -36,10 +36,23 @@ from optparse import make_option
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.core.exceptions
import
FieldError
from
synnefo.webproject.management
import
util
from
synnefo.webproject.management
import
util
s
from
synnefo.lib.astakos
import
UserCache
class
SynnefoCommand
(
BaseCommand
):
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
"--output-format"
,
dest
=
"output_format"
,
metavar
=
"[pretty, csv, json]"
,
default
=
"pretty"
,
choices
=
[
"pretty"
,
"csv"
,
"json"
],
help
=
"Select the output format: pretty [the default], tabs"
" [tab-separated output], csv [comma-separated output]"
),
)
class
ListCommand
(
BaseCommand
):
"""Generic *-list management command.
...
...
@@ -192,7 +205,7 @@ class ListCommand(BaseCommand):
# --filter-by option
if
options
[
"filter_by"
]:
filters
,
excludes
=
util
.
parse_filters
(
options
[
"filter_by"
])
filters
,
excludes
=
util
s
.
parse_filters
(
options
[
"filter_by"
])
else
:
filters
,
excludes
=
({},
{})
...
...
@@ -270,7 +283,7 @@ class ListCommand(BaseCommand):
output_format
=
options
[
"output_format"
]
if
output_format
!=
"json"
and
not
options
[
"headers"
]:
headers
=
None
util
.
pprint_table
(
self
.
stdout
,
table
,
headers
,
output_format
)
util
s
.
pprint_table
(
self
.
stdout
,
table
,
headers
,
output_format
)
def
handle_args
(
self
,
*
args
,
**
kwargs
):
pass
...
...
@@ -286,7 +299,7 @@ class ListCommand(BaseCommand):
table
=
[]
for
field
,
(
_
,
help_msg
)
in
self
.
FIELDS
.
items
():
table
.
append
((
field
,
help_msg
))
util
.
pprint_table
(
self
.
stdout
,
table
,
headers
)
util
s
.
pprint_table
(
self
.
stdout
,
table
,
headers
)
def
validate_fields
(
self
,
fields
):
for
f
in
fields
:
...
...
@@ -300,4 +313,4 @@ class ListCommand(BaseCommand):
table
=
[]
for
field
in
self
.
object_class
.
_meta
.
fields
:
table
.
append
((
field
.
name
,
field
.
verbose_name
,
field
.
help_text
))
util
.
pprint_table
(
self
.
stdout
,
table
,
headers
)
util
s
.
pprint_table
(
self
.
stdout
,
table
,
headers
)
snf-webproject/synnefo/webproject/management/util.py
→
snf-webproject/synnefo/webproject/management/util
s
.py
View file @
ab896c50
...
...
@@ -117,7 +117,7 @@ def parse_filters(filter_by):
def
pprint_table
(
out
,
table
,
headers
=
None
,
output_format
=
'pretty'
,
separator
=
None
):
separator
=
None
,
vertical
=
False
):
"""Print a pretty, aligned string representation of table.
Works by finding out the max width of each column and padding to data
...
...
@@ -152,20 +152,32 @@ def pprint_table(out, table, headers=None, output_format='pretty',
table
=
map
(
functools
.
partial
(
map
,
uenc
),
table
)
cw
.
writerows
(
table
)
elif
output_format
==
"pretty"
:
# Find out the max width of each column
columns
=
[
headers
]
+
table
if
headers
else
table
widths
=
[
max
(
map
(
len
,
col
))
for
col
in
zip
(
*
(
columns
))]
t_length
=
sum
(
widths
)
+
len
(
sep
)
*
(
len
(
widths
)
-
1
)
if
headers
:
# pretty print the headers
line
=
sep
.
join
(
uenc
(
v
.
rjust
(
w
))
for
v
,
w
in
zip
(
headers
,
widths
))
out
.
write
(
line
+
"
\n
"
)
out
.
write
(
"-"
*
t_length
+
"
\n
"
)
# print the rest table
for
row
in
table
:
line
=
sep
.
join
(
uenc
(
v
.
rjust
(
w
))
for
v
,
w
in
zip
(
row
,
widths
))
out
.
write
(
line
+
"
\n
"
)
if
vertical
:
assert
(
len
(
table
)
==
1
)
row
=
table
[
0
]
max_key
=
max
(
map
(
len
,
headers
))
max_val
=
max
(
map
(
len
,
row
))
for
row
in
table
:
for
(
k
,
v
)
in
zip
(
headers
,
row
):
k
=
uenc
(
k
.
ljust
(
max_key
))
v
=
uenc
(
v
.
ljust
(
max_val
))
out
.
write
(
"%s: %s
\n
"
%
(
k
,
v
))
else
:
# Find out the max width of each column
columns
=
[
headers
]
+
table
if
headers
else
table
widths
=
[
max
(
map
(
len
,
col
))
for
col
in
zip
(
*
(
columns
))]
t_length
=
sum
(
widths
)
+
len
(
sep
)
*
(
len
(
widths
)
-
1
)
if
headers
:
# pretty print the headers
line
=
sep
.
join
(
uenc
(
v
.
rjust
(
w
))
\
for
v
,
w
in
zip
(
headers
,
widths
))
out
.
write
(
line
+
"
\n
"
)
out
.
write
(
"-"
*
t_length
+
"
\n
"
)
# print the rest table
for
row
in
table
:
line
=
sep
.
join
(
uenc
(
v
.
rjust
(
w
))
for
v
,
w
in
zip
(
row
,
widths
))
out
.
write
(
line
+
"
\n
"
)
else
:
raise
ValueError
(
"Unknown output format '%s'"
%
output_format
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment