Revision 44f510e1 snf-astakos-app/astakos/im/management/commands/resource-modify.py

b/snf-astakos-app/astakos/im/management/commands/resource-modify.py
37 37

  
38 38
from astakos.im.models import Resource
39 39
from astakos.im.resources import update_resource
40
from ._common import show_resource_value, style_options, check_style, units
40 41

  
41 42

  
42 43
class Command(BaseCommand):
......
57 58
                    dest='from_file',
58 59
                    metavar='<limits_file.json>',
59 60
                    help="Read default base quotas from a json file"),
61
        make_option('--unit-style',
62
                    default='mb',
63
                    help=("Specify display unit for resource values "
64
                          "(one of %s); defaults to mb") % style_options),
60 65
    )
61 66

  
62 67
    def handle(self, *args, **options):
......
73 78
                if key in actions and value is not None]
74 79

  
75 80
        if len(opts) != 1:
76
            raise CommandError("Please provide exactly one option.")
81
            raise CommandError("Please provide exactly one of the options: %s."
82
                               % ", ".join(actions.keys()))
83

  
84
        self.unit_style = options['unit_style']
85
        check_style(self.unit_style)
77 86

  
78 87
        key, value = opts[0]
79 88
        action = actions[key]
......
123 132
        for resource in resources:
124 133
            self.stdout.write("Resource '%s' (%s)\n" %
125 134
                              (resource.name, resource.desc))
126
            unit = (" in %s" % resource.unit) if resource.unit else ""
127
            self.stdout.write("Current limit%s: %s\n"
128
                              % (unit, resource.uplimit))
135
            value = show_resource_value(resource.uplimit, resource.name,
136
                                        self.unit_style)
137
            self.stdout.write("Current limit: %s\n" % value)
129 138
            while True:
130
                self.stdout.write("New limit%s (leave blank to keep current): "
131
                                  % (unit))
139
                self.stdout.write("New limit (leave blank to keep current): ")
132 140
                response = raw_input()
133 141
                if response == "":
134 142
                    break
135 143
                else:
136 144
                    try:
137
                        value = int(response)
138
                    except ValueError:
145
                        value = units.parse(response)
146
                    except units.ParseError:
139 147
                        continue
140 148
                    update_resource(resource, value)
141 149
                    break
142 150

  
143 151
    def change_resource_limit(self, resource, limit):
144
        try:
145
            limit = int(limit)
146
        except:
147
            raise CommandError("Limit should be an integer.")
148
        update_resource(resource, limit)
152
        if not isinstance(limit, (int, long)):
153
            try:
154
                limit = units.parse(limit)
155
            except units.ParseError:
156
                m = ("Limit should be an integer, optionally followed "
157
                     "by a unit.")
158
                raise CommandError(m)
159
            update_resource(resource, limit)

Also available in: Unified diff