Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / service-list.py @ 66c90acd

History | View | Annotate | Download (2.8 kB)

1 6b03a847 Sofia Papagiannaki
# Copyright 2012 GRNET S.A. All rights reserved.
2 6b03a847 Sofia Papagiannaki
#
3 64cd4730 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 64cd4730 Antony Chazapis
# without modification, are permitted provided that the following
5 64cd4730 Antony Chazapis
# conditions are met:
6 6b03a847 Sofia Papagiannaki
#
7 64cd4730 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 64cd4730 Antony Chazapis
#      disclaimer.
10 6b03a847 Sofia Papagiannaki
#
11 64cd4730 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 64cd4730 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 64cd4730 Antony Chazapis
#      provided with the distribution.
15 6b03a847 Sofia Papagiannaki
#
16 64cd4730 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 64cd4730 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 64cd4730 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 64cd4730 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 64cd4730 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 64cd4730 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 64cd4730 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 64cd4730 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 64cd4730 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 64cd4730 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 64cd4730 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 64cd4730 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 6b03a847 Sofia Papagiannaki
#
29 64cd4730 Antony Chazapis
# The views and conclusions contained in the software and
30 64cd4730 Antony Chazapis
# documentation are those of the authors and should not be
31 64cd4730 Antony Chazapis
# interpreted as representing official policies, either expressed
32 64cd4730 Antony Chazapis
# or implied, of GRNET S.A.
33 64cd4730 Antony Chazapis
34 6b03a847 Sofia Papagiannaki
from optparse import make_option
35 64cd4730 Antony Chazapis
36 6b03a847 Sofia Papagiannaki
from django.core.management.base import BaseCommand, CommandError
37 64cd4730 Antony Chazapis
38 6b03a847 Sofia Papagiannaki
from astakos.im.models import Service
39 64cd4730 Antony Chazapis
40 6b03a847 Sofia Papagiannaki
class Command(BaseCommand):
41 6b03a847 Sofia Papagiannaki
    help = "List g"
42 9597fc83 Kostas Papadimitriou
43 6b03a847 Sofia Papagiannaki
    option_list = BaseCommand.option_list + (
44 6b03a847 Sofia Papagiannaki
        make_option('-c',
45 6b03a847 Sofia Papagiannaki
            action='store_true',
46 6b03a847 Sofia Papagiannaki
            dest='csv',
47 6b03a847 Sofia Papagiannaki
            default=False,
48 6b03a847 Sofia Papagiannaki
            help="Use pipes to separate values"),
49 6b03a847 Sofia Papagiannaki
    )
50 9597fc83 Kostas Papadimitriou
51 6b03a847 Sofia Papagiannaki
    def handle(self, *args, **options):
52 6b03a847 Sofia Papagiannaki
        if args:
53 6b03a847 Sofia Papagiannaki
            raise CommandError("Command doesn't accept any arguments")
54 9597fc83 Kostas Papadimitriou
55 6b03a847 Sofia Papagiannaki
        services = Service.objects.all()
56 9597fc83 Kostas Papadimitriou
57 9597fc83 Kostas Papadimitriou
        labels = ('id', 'name', 'url', 'auth_token', 'icon')
58 9597fc83 Kostas Papadimitriou
        columns = (3, 12, 40, 20, 20)
59 9597fc83 Kostas Papadimitriou
60 6b03a847 Sofia Papagiannaki
        if not options['csv']:
61 6b03a847 Sofia Papagiannaki
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
62 6b03a847 Sofia Papagiannaki
            self.stdout.write(line + '\n')
63 6b03a847 Sofia Papagiannaki
            sep = '-' * len(line)
64 6b03a847 Sofia Papagiannaki
            self.stdout.write(sep + '\n')
65 9597fc83 Kostas Papadimitriou
66 6b03a847 Sofia Papagiannaki
        for service in services:
67 9597fc83 Kostas Papadimitriou
            fields = (str(service.id), service.name, service.url,
68 9597fc83 Kostas Papadimitriou
                    service.auth_token,
69 9597fc83 Kostas Papadimitriou
                    service.icon)
70 9597fc83 Kostas Papadimitriou
71 6b03a847 Sofia Papagiannaki
            if options['csv']:
72 6b03a847 Sofia Papagiannaki
                line = '|'.join(fields)
73 6b03a847 Sofia Papagiannaki
            else:
74 6b03a847 Sofia Papagiannaki
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
75 9597fc83 Kostas Papadimitriou
76 6b03a847 Sofia Papagiannaki
            self.stdout.write(line.encode('utf8') + '\n')