Mercurial > repos > gga > apollo_iframe
comparison webapollo.py @ 5:207c1c281360 draft
planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/apollo commit 85194fa009ead2c34720faab61a4143fc29d17c2
author | gga |
---|---|
date | Fri, 31 Aug 2018 09:33:49 -0400 |
parents | a10003dc6c85 |
children | 466f03bef714 |
comparison
equal
deleted
inserted
replaced
4:a10003dc6c85 | 5:207c1c281360 |
---|---|
3 import argparse | 3 import argparse |
4 import collections | 4 import collections |
5 import json | 5 import json |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import random | |
8 import time | 9 import time |
9 from abc import abstractmethod | 10 from abc import abstractmethod |
10 | 11 |
11 from BCBio import GFF | 12 from BCBio import GFF |
12 | 13 |
476 return True | 477 return True |
477 else: | 478 else: |
478 raise Exception("User is not an administrator. Permission denied") | 479 raise Exception("User is not an administrator. Permission denied") |
479 | 480 |
480 | 481 |
482 def PermissionCheck(user, org_cn, permission_type): | |
483 return any(org["organism"] == org_cn and permission_type in org["permissions"] for org in user.organismPermissions) | |
484 | |
485 | |
486 def PasswordGenerator(length): | |
487 chars = list('qwrtpsdfghjklzxcvbnm') | |
488 return ''.join(random.choice(chars) for _ in range(length)) | |
489 | |
490 | |
491 def IsRemoteUser(): | |
492 if 'GALAXY_WEBAPOLLO_REMOTE_USER' not in os.environ: | |
493 return False | |
494 value = os.environ['GALAXY_WEBAPOLLO_REMOTE_USER'] | |
495 if value.lower() in ('true', 't', '1'): | |
496 return True | |
497 else: | |
498 return False | |
499 | |
500 | |
481 class WebApolloInstance(object): | 501 class WebApolloInstance(object): |
482 | 502 |
483 def __init__(self, url, username, password): | 503 def __init__(self, url, username, password): |
484 self.apollo_url = url | 504 self.apollo_url = url |
485 self.username = username | 505 self.username = username |
1252 data['species'] = species | 1272 data['species'] = species |
1253 | 1273 |
1254 return self.request('addOrganism', data) | 1274 return self.request('addOrganism', data) |
1255 | 1275 |
1256 def findAllOrganisms(self): | 1276 def findAllOrganisms(self): |
1257 return self.request('findAllOrganisms', {}) | 1277 orgs = self.request('findAllOrganisms', {}) |
1278 if not isinstance(orgs, (list,)): | |
1279 orgs = [] | |
1280 return orgs | |
1258 | 1281 |
1259 def findOrganismByCn(self, cn): | 1282 def findOrganismByCn(self, cn): |
1260 orgs = self.findAllOrganisms() | 1283 orgs = self.findAllOrganisms() |
1261 orgs = [x for x in orgs if x['commonName'] == cn] | 1284 orgs = [x for x in orgs if x['commonName'] == cn] |
1262 if len(orgs) == 0: | 1285 if len(orgs) == 0: |
1350 | 1373 |
1351 def removeUserFromGroup(self, group, user): | 1374 def removeUserFromGroup(self, group, user): |
1352 data = {'group': group.name, 'userId': user.userId} | 1375 data = {'group': group.name, 'userId': user.userId} |
1353 return self.request('removeUserFromGroup', data) | 1376 return self.request('removeUserFromGroup', data) |
1354 | 1377 |
1355 def createUser(self, email, firstName, lastName, newPassword, role="user", groups=None): | 1378 def createUser(self, email, firstName, lastName, newPassword, role="user", groups=None, addToHistory=False): |
1356 data = { | 1379 data = { |
1357 'firstName': firstName, | 1380 'firstName': firstName, |
1358 'lastName': lastName, | 1381 'lastName': lastName, |
1359 'email': email, | 1382 'email': email, |
1360 'role': role, | 1383 'role': role, |
1361 'groups': [] if groups is None else groups, | 1384 'groups': [] if groups is None else groups, |
1362 # 'availableGroups': [], | 1385 # 'availableGroups': [], |
1363 'newPassword': newPassword, | 1386 'newPassword': newPassword, |
1364 # 'organismPermissions': [], | 1387 # 'organismPermissions': [], |
1365 } | 1388 } |
1366 return self.request('createUser', data) | 1389 returnData = self.request('createUser', data) |
1390 if addToHistory and not IsRemoteUser(): | |
1391 f = open("Apollo_credentials.txt", "w") | |
1392 f.write('Username: %s\tPassword: %s' % (email, newPassword)) | |
1393 return returnData | |
1394 | |
1395 def assertOrCreateUser(self, email): | |
1396 try: | |
1397 gx_user = AssertUser(self.loadUsers(email)) | |
1398 except Exception: | |
1399 self.createUser(email, email, email, PasswordGenerator(12), role='user', addToHistory=True) | |
1400 gx_user = AssertUser(self.loadUsers(email)) | |
1401 return gx_user | |
1367 | 1402 |
1368 def deleteUser(self, user): | 1403 def deleteUser(self, user): |
1369 return self.request('deleteUser', {'userId': user.userId}) | 1404 return self.request('deleteUser', {'userId': user.userId}) |
1370 | 1405 |
1371 def updateUser(self, user, email, firstName, lastName, newPassword): | 1406 def updateUser(self, user, email, firstName, lastName, newPassword): |
1530 wa = WebApolloInstance( | 1565 wa = WebApolloInstance( |
1531 os.environ['GALAXY_WEBAPOLLO_URL'], | 1566 os.environ['GALAXY_WEBAPOLLO_URL'], |
1532 os.environ['GALAXY_WEBAPOLLO_USER'], | 1567 os.environ['GALAXY_WEBAPOLLO_USER'], |
1533 os.environ['GALAXY_WEBAPOLLO_PASSWORD'] | 1568 os.environ['GALAXY_WEBAPOLLO_PASSWORD'] |
1534 ) | 1569 ) |
1535 # Assert that the email exists in apollo | |
1536 try: | |
1537 gx_user = wa.requireUser(email) | |
1538 except UnknownUserException: | |
1539 return [] | |
1540 | 1570 |
1541 # Key for cached data | 1571 # Key for cached data |
1542 cacheKey = 'groups-' + email | 1572 cacheKey = 'groups-' + email |
1543 # We don't want to trust "if key in cache" because between asking and fetch | 1573 # We don't want to trust "if key in cache" because between asking and fetch |
1544 # it might through key error. | 1574 # it might through key error. |
1545 if cacheKey not in cache: | 1575 if cacheKey not in cache: |
1546 # However if it ISN'T there, we know we're safe to fetch + put in | 1576 # However if it ISN'T there, we know we're safe to fetch + put in |
1547 # there. | 1577 # there. |
1548 data = _galaxy_list_groups(wa, gx_user, *args, **kwargs) | 1578 data = _galaxy_list_groups(wa, *args, **kwargs) |
1549 cache[cacheKey] = data | 1579 cache[cacheKey] = data |
1550 return data | 1580 return data |
1551 try: | 1581 try: |
1552 # The cache key may or may not be in the cache at this point, it | 1582 # The cache key may or may not be in the cache at this point, it |
1553 # /likely/ is. However we take no chances that it wasn't evicted between | 1583 # /likely/ is. However we take no chances that it wasn't evicted between |
1556 data = cache[cacheKey] | 1586 data = cache[cacheKey] |
1557 return data | 1587 return data |
1558 except KeyError: | 1588 except KeyError: |
1559 # If access fails due to eviction, we will fail over and can ensure that | 1589 # If access fails due to eviction, we will fail over and can ensure that |
1560 # data is inserted. | 1590 # data is inserted. |
1561 data = _galaxy_list_groups(wa, gx_user, *args, **kwargs) | 1591 data = _galaxy_list_groups(wa, *args, **kwargs) |
1562 cache[cacheKey] = data | 1592 cache[cacheKey] = data |
1563 return data | 1593 return data |
1564 | 1594 |
1565 | 1595 |
1566 def _galaxy_list_groups(wa, gx_user, *args, **kwargs): | 1596 def _galaxy_list_groups(wa, *args, **kwargs): |
1567 # Fetch the groups. | 1597 # Fetch the groups. |
1568 group_data = [] | 1598 group_data = [] |
1569 for group in wa.groups.loadGroups(): | 1599 for group in wa.groups.loadGroups(): |
1570 # Reformat | 1600 # Reformat |
1571 group_data.append((group.name, group.name, False)) | 1601 group_data.append((group.name, group.name, False)) |