Commit fc347244 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Georgios D. Tsoukalas

pep8 improvements

parent 802e5855
......@@ -40,7 +40,7 @@ from collections import namedtuple
from django.utils.translation import ugettext as _
from astakos.im.settings import (
QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL)
QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL)
if QUOTAHOLDER_URL:
from kamaki.clients.quotaholder import QuotaholderClient
......@@ -58,6 +58,8 @@ inf = float('inf')
clientkey = 'astakos'
_client = None
def get_client():
global _client
if _client:
......@@ -67,6 +69,7 @@ def get_client():
_client = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN)
return _client
def set_quota(payload):
c = get_client()
if not c:
......@@ -75,6 +78,7 @@ def set_quota(payload):
logger.debug('set_quota: %s rejected: %s' % (payload, result))
return result
def get_entity(payload):
c = get_client()
if not c:
......@@ -83,6 +87,7 @@ def get_entity(payload):
logger.debug('get_entity: %s reply: %s' % (payload, result))
return result
def get_holding(payload):
c = get_client()
if not c:
......@@ -91,6 +96,7 @@ def get_holding(payload):
logger.debug('get_holding: %s reply: %s' % (payload, result))
return result
def qh_get_holdings(users, resources):
payload = []
append = payload.append
......@@ -100,6 +106,7 @@ def qh_get_holdings(users, resources):
result = get_holding(payload)
return result
def quota_limits_per_user_from_get(lst):
quotas = {}
for holder, resource, q, c, il, el, imp, exp, ret, rel, flags in lst:
......@@ -109,6 +116,7 @@ def quota_limits_per_user_from_get(lst):
quotas[holder] = userquotas
return quotas
def quotas_per_user_from_get(lst):
limits = {}
counters = {}
......@@ -139,14 +147,17 @@ def qh_get_quota(users, resources):
logger.debug('get_quota: %s rejected: %s' % (payload, result))
return result
def qh_get_quota_limits(users, resources):
result = qh_get_quota(users, resources)
return quota_limits_per_user_from_get(result)
def qh_get_quotas(users, resources):
result = qh_get_quota(users, resources)
return quotas_per_user_from_get(result)
def create_entity(payload):
c = get_client()
if not c:
......@@ -190,11 +201,14 @@ class QuotaValues(namedtuple('QuotaValues', ('quantity',
'import_limit',
'export_limit'))):
__slots__ = ()
def __dir__(self):
return ['quantity', 'capacity', 'import_limit', 'export_limit']
def __str__(self):
return '\t'.join(['%s=%s' % (f, strbigdec(getattr(self, f))) for f in dir(self)])
return '\t'.join(['%s=%s' % (f, strbigdec(getattr(self, f)))
for f in dir(self)])
def add_quota_values(q1, q2):
return QuotaValues(
......@@ -203,25 +217,29 @@ def add_quota_values(q1, q2):
import_limit = q1.import_limit + q2.import_limit,
export_limit = q1.export_limit + q2.export_limit)
def qh_register_user_with_quotas(user):
return register_users_with_quotas([user])
def register_users_with_quotas(users):
rejected = register_users(users)
if not rejected:
register_quotas(users)
def register_users(users):
if not users:
return
payload = list(CreateEntityPayload(
entity=u.uuid,
owner='system',
key=ENTITY_KEY,
ownerkey='') for u in users)
entity=u.uuid,
owner='system',
key=ENTITY_KEY,
ownerkey='') for u in users)
return create_entity(payload)
def register_quotas(users):
if not users:
return
......@@ -230,7 +248,7 @@ def register_quotas(users):
append = payload.append
for u in users:
for resource, q in u.all_quotas().iteritems():
append( SetQuotaPayload(
append(SetQuotaPayload(
holder=u.uuid,
resource=resource,
key=ENTITY_KEY,
......@@ -241,6 +259,7 @@ def register_quotas(users):
flags=0))
return set_quota(payload)
def send_quotas(userquotas):
if not userquotas:
return
......@@ -249,7 +268,7 @@ def send_quotas(userquotas):
append = payload.append
for holder, quotas in userquotas.iteritems():
for resource, q in quotas.iteritems():
append( SetQuotaPayload(
append(SetQuotaPayload(
holder=holder,
resource=resource,
key=ENTITY_KEY,
......@@ -260,6 +279,7 @@ def send_quotas(userquotas):
flags=0))
return set_quota(payload)
def register_services(services):
def payload(services):
return list(CreateEntityPayload(
......@@ -282,6 +302,7 @@ def register_services(services):
m = "Failed to register services: %s" % (failed,)
raise RuntimeError(m)
def register_resources(resources):
try:
QH_PRACTICALLY_INFINITE
......@@ -299,6 +320,7 @@ def register_resources(resources):
flags=0) for resource in resources)
return set_quota(payload)
def qh_check_users(users):
payload = [(u.uuid, ENTITY_KEY) for u in users]
result = get_entity(payload)
......@@ -313,6 +335,7 @@ def qh_check_users(users):
nonexisting.append(u)
return (existing, nonexisting)
def qh_add_quota(serial, sub_list, add_list):
if not QUOTAHOLDER_URL:
return ()
......@@ -343,6 +366,7 @@ def qh_add_quota(serial, sub_list, add_list):
return result
def qh_query_serials(serials):
if not QUOTAHOLDER_URL:
return ()
......@@ -354,6 +378,7 @@ def qh_query_serials(serials):
serials=serials)
return result
def qh_ack_serials(serials):
if not QUOTAHOLDER_URL:
return ()
......@@ -415,22 +440,22 @@ def _usage_units(timeline, after, before, details=0):
target = point['target']
if details:
yield (target,
point['resource'],
point['name'],
issue_time,
uu_cost,
uu_total)
yield (target,
point['resource'],
point['name'],
issue_time,
uu_cost,
uu_total)
if not t_total:
return
yield (target,
'total',
point['resource'],
issue_time,
uu_total / t_total,
uu_total)
yield (target,
'total',
point['resource'],
issue_time,
uu_total / t_total,
uu_total)
def usage_units(timeline, after, before, details=0):
......@@ -454,22 +479,22 @@ def traffic_units(timeline, after, before, details=0):
tu_total += tu
if details:
yield (target,
point['resource'],
point['name'],
issue_time,
tu,
tu_total)
yield (target,
point['resource'],
point['name'],
issue_time,
tu,
tu_total)
if not tu_total:
return
yield (target,
'total',
point['resource'],
issue_time,
tu_total // len(timeline),
tu_total)
yield (target,
'total',
point['resource'],
issue_time,
tu_total // len(timeline),
tu_total)
def timeline_charge(entity, resource, after, before, details, charge_type):
......
......@@ -66,6 +66,7 @@ def format_date(d):
else:
return 'in ' + timeuntil(d)
def format_dict(d, level=1, ident=22):
iteritems = d.iteritems()
if not isinstance(d, OrderedDict):
......@@ -76,9 +77,11 @@ def format_dict(d, level=1, ident=22):
l.insert(0, '\n')
return ''.join(l)
def format_set(s):
return list(s)
def format(obj, level=1, ident=22):
if isinstance(obj, bool):
return format_bool(obj)
......@@ -91,6 +94,7 @@ def format(obj, level=1, ident=22):
else:
return obj
def get_astakosuser_content_type():
try:
return ContentType.objects.get(app_label='im',
......@@ -148,6 +152,7 @@ def remove_group_permission(group, pname):
except Permission.DoesNotExist:
return -1
def shortened(s, limit, suffix=True):
length = len(s)
if length <= limit:
......
......@@ -106,7 +106,7 @@ class Command(NoArgsCommand):
dest='csv',
default=False,
help="Use pipes to separate values"),
)
)
def handle_noargs(self, **options):
allow_shorten = not options['full']
......@@ -151,7 +151,7 @@ class Command(NoArgsCommand):
(info['email'], True),
(info['status'], False),
(info['appid'], False),
]
]
fields = [(format(elem), flag) for (elem, flag) in fields]
......@@ -169,6 +169,7 @@ class Command(NoArgsCommand):
self.stdout.write(line + '\n')
def filter_by_state(chain_dict, states):
d = {}
for chain, (state, project, app) in chain_dict.iteritems():
......@@ -176,6 +177,7 @@ def filter_by_state(chain_dict, states):
d[chain] = (state, project, app)
return d
def chain_info(chain_dict):
l = []
for chain, (state, project, app) in chain_dict.iteritems():
......@@ -186,12 +188,12 @@ def chain_info(chain_dict):
appid = ""
d = {
'projectid' : str(chain),
'name' : project.application.name if project else app.name,
'applicant' : app.applicant.realname,
'email' : app.applicant.email,
'projectid': str(chain),
'name': project.application.name if project else app.name,
'applicant': app.applicant.realname,
'email': app.applicant.email,
'status': status,
'appid' : appid,
}
'appid': appid,
}
l.append(d)
return l
......@@ -41,6 +41,7 @@ from ._common import format
import uuid
class Command(BaseCommand):
args = "<user ID or email>"
help = "Show user info"
......
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