Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pyam.iiasa.platforms() function #829

Merged
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Next release

- [#829](https://github.com/IAMconsortium/pyam/pull/829) Add a `pyam.iiasa.list_platforms()` function
- [#826](https://github.com/IAMconsortium/pyam/pull/826) Add `read_ixmp4()` function and extend integration test
- [#825](https://github.com/IAMconsortium/pyam/pull/825) Add support for Python 3.12
- [#824](https://github.com/IAMconsortium/pyam/pull/824) Update ixmp4 requirement to >=0.7.1
Expand Down
11 changes: 8 additions & 3 deletions docs/api/iiasa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ You will be prompted to enter your password.
*Scenario Apps* instances
-------------------------

Coming soon...
The *Scenario Apps* use the |ixmp4| package as a database backend.
You can list all available ixmp4 platforms hosted by IIASA using the following:

*Scenario Explorer* instances
-----------------------------
.. autofunction:: list_platforms
:noindex:


*Scenario Explorer* instances (legacy service)
----------------------------------------------

The *Scenario Explorer* infrastructure developed by the Scenario Services and Scientific
Software team was developed and used for projects from 2018 until 2023.
Expand Down
28 changes: 26 additions & 2 deletions pyam/iiasa.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import pandas as pd
import requests
import yaml
from ixmp4.cli import utils
from ixmp4.conf import settings
from ixmp4.conf.auth import ManagerAuth
from requests.auth import AuthBase

from pyam.core import IamDataFrame
from pyam.logging import deprecation_warning
from pyam.str import is_str
from pyam.str import is_str, shorten
from pyam.utils import (
IAMC_IDX,
META_IDX,
Expand All @@ -35,13 +36,32 @@
You are connected to the {} scenario explorer hosted by IIASA.
If you use this data in any published format, please cite the
data as provided in the explorer guidelines: {}
""".replace("\n", "")
""".replace(
"\n", ""
)
IXMP4_LOGIN = "Please run `ixmp4 login <username>` in a console"

# path to local configuration settings
DEFAULT_IIASA_CREDS = Path("~").expanduser() / ".local" / "pyam" / "iiasa.yaml"


def list_platforms():
"""Print all available ixmp4 platforms hosted by IIASA"""

platforms = ixmp4.conf.settings.manager.list_platforms()
utils.echo("IIASA platform".ljust(20) + "Access".ljust(10) + "Notice\n")

Check warning on line 52 in pyam/iiasa.py

View check run for this annotation

Codecov / codecov/patch

pyam/iiasa.py#L51-L52

Added lines #L51 - L52 were not covered by tests
phackstock marked this conversation as resolved.
Show resolved Hide resolved

for p in platforms:
utils.important(shorten(p.name, 20), nl=False)
utils.echo(str(p.accessibility.value.lower()).ljust(10), nl=False)

Check warning on line 56 in pyam/iiasa.py

View check run for this annotation

Codecov / codecov/patch

pyam/iiasa.py#L54-L56

Added lines #L54 - L56 were not covered by tests

if p.notice is not None:
utils.echo(shorten(p.notice, 55), nl=False)
utils.echo()

Check warning on line 60 in pyam/iiasa.py

View check run for this annotation

Codecov / codecov/patch

pyam/iiasa.py#L58-L60

Added lines #L58 - L60 were not covered by tests

utils.info("\n" + str(len(platforms)) + " total \n")

Check warning on line 62 in pyam/iiasa.py

View check run for this annotation

Codecov / codecov/patch

pyam/iiasa.py#L62

Added line #L62 was not covered by tests


def set_config(user, password, file=None):
raise DeprecationWarning(f"This method is deprecated. {IXMP4_LOGIN}.")

Expand Down Expand Up @@ -197,6 +217,10 @@
@lru_cache()
def valid_connections(self):
"""Return available resources (database API connections)"""
logger.warning(
"IIASA is migrating to a database infrastructure using the ixmp4 package."
phackstock marked this conversation as resolved.
Show resolved Hide resolved
"Use `pyam.iiasa.list_platforms()` to list available ixmp4 databases."
)
return list(self._connection_map.keys())

def connect(self, name):
Expand Down
10 changes: 10 additions & 0 deletions pyam/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@
# test = lambda x: level == x if x is not None else False
def test(x):
return level == x if x is not None else False

elif level[-1] == "-":
level = int(level[:-1])

# test = lambda x: level >= x if x is not None else False
def test(x):
return level >= x if x is not None else False

elif level[-1] == "+":
level = int(level[:-1])

# test = lambda x: level <= x if x is not None else False
def test(x):
return level <= x if x is not None else False

else:
raise ValueError("Unknown level type: `{}`".format(level))

Expand Down Expand Up @@ -141,3 +144,10 @@
def is_str(x):
"""Returns True if x is a string"""
return isinstance(x, six.string_types)


def shorten(value: str, length: int):
"""Shorten a string to a given length adding `...`"""
if len(value) > length - 4:
value = value[: length - 4] + "..."
return value.ljust(length)

Check warning on line 153 in pyam/str.py

View check run for this annotation

Codecov / codecov/patch

pyam/str.py#L151-L153

Added lines #L151 - L153 were not covered by tests
Loading