Commit 83dfe64a authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Use `with' to open file in management command

parent a0f09a72
...@@ -49,6 +49,7 @@ AddResourceArgs = namedtuple('AddQuotaArgs', ('resource', ...@@ -49,6 +49,7 @@ AddResourceArgs = namedtuple('AddQuotaArgs', ('resource',
'capacity', 'capacity',
)) ))
class Command(BaseCommand): class Command(BaseCommand):
help = """Import user quota limits from file or set quota help = """Import user quota limits from file or set quota
for a single user from the command line for a single user from the command line
...@@ -171,36 +172,30 @@ for a single user from the command line ...@@ -171,36 +172,30 @@ for a single user from the command line
qh_sync_user(user.id) qh_sync_user(user.id)
def import_from_file(self, location): def import_from_file(self, location):
try:
f = open(location, 'r')
except IOError, e:
raise CommandError(e)
users = set() users = set()
for line in f.readlines(): with open(location) as f:
try: for line in f.readlines():
t = line.rstrip('\n').split(' ')
user = t[0]
args = AddResourceArgs(*t[1:])
except(IndexError, TypeError):
self.stdout.write('Invalid line format: %s:\n' % t)
continue
else:
try: try:
user = AstakosUser.objects.get(uuid=user) t = line.rstrip('\n').split(' ')
users.add(user.id) user = t[0]
except AstakosUser.DoesNotExist: args = AddResourceArgs(*t[1:])
self.stdout.write('Not found user having uuid: %s\n' % user) except(IndexError, TypeError):
self.stdout.write('Invalid line format: %s:\n' % t)
continue continue
else: else:
try: try:
user.add_resource_policy(*args) user = AstakosUser.objects.get(uuid=user)
except Exception, e: users.add(user.id)
self.stdout.write('Failed to policy: %s\n' % e) except AstakosUser.DoesNotExist:
self.stdout.write('Not found user having uuid: %s\n'
% user)
continue continue
else:
finally: try:
f.close() user.add_resource_policy(*args)
except Exception, e:
self.stdout.write('Failed to policy: %s\n' % e)
continue
qh_sync_users(users) qh_sync_users(users)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment