Skip to content
Snippets Groups Projects
Commit 258ff669 authored by Hugh Dickinson's avatar Hugh Dickinson
Browse files

Initial commit with Zooniverse example plugin.

parent c633b319
Branches
No related tags found
No related merge requests found
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# esap-userprofile-python-client # esap-userprofile-python-client
A Python client for the ESCAPE ESAP User Profile REST API.
from .zooniverse import zooniverse
from .shopping_client import shopping_client
setup.py 0 → 100644
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="esap-userprofile-python-client",
version="0.0.1",
author="Hugh Dickinson",
author_email="hugh.dickinson@open.ac.uk",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://git.astron.nl/astron-sdc/esap-userprofile-python-client",
packages=setuptools.find_packages(),
install_requires=["pandas", "requests", "panoptes-client"],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
python_requires=">=3.6",
)
import pandas as pd
import requests
import json
import os
import urllib.parse
class shopping_client:
endpoint = "esap-api/accounts/user-profiles/"
def __init__(self, username, host="http://localhost:5555/"):
self.username = username
self.host = host
self.basket = None
def get_basket(self, reload=False):
if self.basket is None or reload:
url = urllib.parse.urljoin(self.host, shopping_client.endpoint)
print(url)
response = requests.get(url, dict(user_name=self.username))
print(response.content)
if response.ok:
self.basket = json.loads(response.content)["results"][0]["shopping_cart"]
return self.basket
from .zooniverse import zooniverse
import requests
import json
import io
import getpass
import pandas as pd
from panoptes_client import Panoptes, Project, Workflow
from panoptes_client.panoptes import PanoptesAPIException
class zooniverse:
entity_types = {"workflow": Workflow, "project": Project}
category_converters = {
"subjects": dict(metadata=json.loads, locations=json.loads),
"classifications": dict(metadata=json.loads, annotations=json.loads),
}
def __init__(self, username, password=None):
self.username = username
self.password = password
if self.password is None:
self.password = getpass.getpass()
self.panoptes = Panoptes.connect(username=self.username, password=self.password)
def is_available(self, item, verbose=False):
try:
description = self._get_entity(item).describe_export(
self._get_item_entry(item, "category")
)
if verbose:
print(description)
return True
except PanoptesAPIException as e:
return False
def generate(self, item, wait=False):
print("Generating requested export...")
if wait:
print("\t\tWaiting for generation to complete...")
else:
print("\t\tNot waiting for generation to complete...")
response = self._get_entity(item).get_export(
self._get_item_entry(item, "category"), generate=True, wait=wait
)
if response.ok:
return (
pd.read_csv(
io.BytesIO(response.content),
converters=zooniverse.category_converters[
self._get_item_entry(item, "category")
],
)
if not wait
else response
)
else:
return None
def retrieve(self, item, generate=False, wait=False):
if self.is_available(item) and not generate:
response = self._get_entity(item).get_export(
self._get_item_entry(item, "category"), generate=False, wait=wait
)
else:
if not generate:
print(
"Requested resource is not available and you have specified generate==False"
)
return None
else:
print("Generating requested export...")
if wait:
print("\t\tWaiting for generation to complete...")
else:
print("\t\tNot waiting for generation to complete...")
response = self._get_entity(item).get_export(
self._get_item_entry(item, "category"), generate=True, wait=wait
)
if response.ok:
return (
pd.read_csv(
io.BytesIO(response.content),
converters=zooniverse.category_converters[
self._get_item_entry(item, "category")
],
)
if not wait
else response
)
else:
return None
def _get_entity(self, item):
entity = zooniverse.entity_types[self._get_item_entry(item, "catalog")].find(
int(self._get_item_entry(item, self._catalogue_to_id_string(item)))
)
return entity
def _get_item_entry(self, item, entry):
item_data = json.loads(item["item_data"].replace("'", '"'))
return item_data.get(entry, None)
def _catalogue_to_id_string(self, item):
return self._get_item_entry(item, "catalog") + "_id"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment