Revision 7a0aa449 snf-cyclades-app/synnefo/api/management/commands/server-list.py

b/snf-cyclades-app/synnefo/api/management/commands/server-list.py
35 35

  
36 36
from django.core.management.base import BaseCommand, CommandError
37 37
from synnefo.management.common import (format_vm_state, get_backend,
38
                                       filter_results)
38
                                       filter_results, pprint_table)
39 39
from synnefo.api.util import get_image
40 40
from synnefo.db.models import VirtualMachine
41 41

  
......
103 103
        if filter_by:
104 104
            servers = filter_results(servers, filter_by)
105 105

  
106
        labels = ('id', 'name', 'owner', 'flavor', 'image', 'state',
107
                  'backend')
108
        columns = (3, 12, 20, 11, 12, 9, 40)
109

  
110
        if not options['csv']:
111
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
112
            self.stdout.write(line + '\n')
113
            sep = '-' * len(line)
114
            self.stdout.write(sep + '\n')
115

  
116 106
        cache = ImageCache()
117 107

  
108
        headers = ('id', 'name', 'owner', 'flavor', 'image', 'state',
109
                   'backend')
110

  
111
        table = []
118 112
        for server in servers.order_by('id'):
119
            id = str(server.id)
120 113
            try:
121 114
                name = server.name.decode('utf8')
122 115
            except UnicodeEncodeError:
123 116
                name = server.name
117

  
124 118
            flavor = server.flavor.name
119

  
125 120
            try:
126 121
                image = cache.get_image(server.imageid, server.userid)['name']
127 122
            except:
128 123
                image = server.imageid
129 124

  
130 125
            state = format_vm_state(server)
131
            fields = (id, name, server.userid, flavor, image, state,
132
                      str(server.backend))
133 126

  
134
            if options['csv']:
135
                line = '|'.join(fields)
136
            else:
137
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
127
            fields = (str(server.id), name, server.userid, flavor, image,
128
                      state, str(server.backend))
129
            table.append(fields)
138 130

  
139
            self.stdout.write(line.encode('utf8') + '\n')
131
        separator = " | " if options['csv'] else None
132
        pprint_table(self.stdout, table, headers, separator)
140 133

  
141 134

  
142 135
class ImageCache(object):

Also available in: Unified diff