prisma-api 0.4.0__tar.gz → 0.4.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: prisma_api
3
- Version: 0.4.0
3
+ Version: 0.4.3
4
4
  Summary: A package for interacting with PrISMa Platform APIs. PrISMa is a holistic system for numerical synthesis and assessment of Metal-Organic Frameworks and their applications, for Direct Air Capture of CO2. For more information, see: https://prisma.hw.ac.uk.
5
5
  Author-email: RCCS-CaptureTeam <team@prisma-platform.org>
6
6
  License: GNUv3
@@ -199,6 +199,8 @@ All methods below are available on `api.v2`.
199
199
  - `get_material_psdi(material_id)`
200
200
  - `get_material_property_bundle(mof, sim_or_exp=None, good_structure=None, limit=500, offset=0, query=None)`
201
201
  - `get_material_bundle(mof, sim_or_exp=None, good_structure=None, limit=500, offset=0, query=None, include_cif=False, include_cif_text=False, cif_timeout=60)`
202
+ - `get_material_bundles(names=None, ids=None, sections=None, exclude=None, include_cif_content=False, match='exact', output='json', save_path=None, use_post=False, timeout=300)` — server-side bundle endpoints; accepts one name or a list of names
203
+ - `upsert_material_bundles(bundles, cif_files=None, create_materials=True, strip_ids=False, inline_cifs=False, derive_cif_metadata=True, tag_names=None, method=None, timeout=300)` — write counterpart of `get_material_bundles`; takes the same document the read endpoint returns, with optional CIF upload
202
204
  - `preflight_material_check(name)`
203
205
 
204
206
  ### Case bundles and pack builders
@@ -179,6 +179,8 @@ All methods below are available on `api.v2`.
179
179
  - `get_material_psdi(material_id)`
180
180
  - `get_material_property_bundle(mof, sim_or_exp=None, good_structure=None, limit=500, offset=0, query=None)`
181
181
  - `get_material_bundle(mof, sim_or_exp=None, good_structure=None, limit=500, offset=0, query=None, include_cif=False, include_cif_text=False, cif_timeout=60)`
182
+ - `get_material_bundles(names=None, ids=None, sections=None, exclude=None, include_cif_content=False, match='exact', output='json', save_path=None, use_post=False, timeout=300)` — server-side bundle endpoints; accepts one name or a list of names
183
+ - `upsert_material_bundles(bundles, cif_files=None, create_materials=True, strip_ids=False, inline_cifs=False, derive_cif_metadata=True, tag_names=None, method=None, timeout=300)` — write counterpart of `get_material_bundles`; takes the same document the read endpoint returns, with optional CIF upload
182
184
  - `preflight_material_check(name)`
183
185
 
184
186
  ### Case bundles and pack builders
@@ -1,4 +1,9 @@
1
- __version__ = "0.3.9"
1
+ from importlib.metadata import PackageNotFoundError, version as _version
2
+
3
+ try:
4
+ __version__ = _version("prisma_api")
5
+ except PackageNotFoundError: # running from a source tree without installing
6
+ __version__ = "0+unknown"
2
7
 
3
8
 
4
9
  from prisma_api.prisma_api import prisma_api as init # Main prisma_api class for initialisation
@@ -1,3 +1,5 @@
1
+ import os
2
+ import sys
1
3
  from pathlib import Path
2
4
  import pandas as pd
3
5
  import yaml
@@ -54,6 +56,12 @@ def create_config_file(api_key: str = None):
54
56
  raise ImportError("PyYAML is required. Install with: pip install pyyaml")
55
57
 
56
58
  if not api_key:
59
+ if not sys.stdin or not sys.stdin.isatty():
60
+ raise RuntimeError(
61
+ f"No PrISMa config file at {get_config_path()} and no API key in the "
62
+ f"environment. Set {', '.join(ENV_VARS)} (at least PRISMA_API_KEY), "
63
+ "or run interactively once to create the config file."
64
+ )
57
65
  val = input("Enter your PrISMa API key: ").strip()
58
66
  api_key = val
59
67
 
@@ -67,10 +75,40 @@ def create_config_file(api_key: str = None):
67
75
  yaml.safe_dump(cfg, f, sort_keys=False)
68
76
  return cfg
69
77
 
78
+ # Environment variables read when no config file exists (or use_config_file=False)
79
+ ENV_VARS = ("PRISMA_API_KEY", "PRISMA_API_DEV_API_KEY", "PRISMA_API_DEV_HOST_PORT")
80
+
81
+
82
+ def config_from_env():
83
+ """
84
+ Build a config dict from PRISMA_API_* environment variables.
85
+
86
+ Returns None if none of them is set.
87
+ """
88
+ if not any(os.getenv(name) for name in ENV_VARS):
89
+ return None
90
+ prod_key = os.getenv("PRISMA_API_KEY", "")
91
+ return {
92
+ "api_key": prod_key,
93
+ "dev_api_key": os.getenv("PRISMA_API_DEV_API_KEY", prod_key),
94
+ "dev_host_port": os.getenv("PRISMA_API_DEV_HOST_PORT", "8000"),
95
+ }
96
+
97
+
70
98
  # Get or create config - intended use by initialisation of main prisma_api class
71
99
  def get_or_create_config():
72
- """Return existing config or create it interactively if missing."""
100
+ """
101
+ Return the config, in order of preference:
102
+
103
+ 1. config.yaml, if it exists;
104
+ 2. PRISMA_API_* environment variables (nothing is written to disk);
105
+ 3. an interactive prompt, which creates config.yaml. Without a terminal
106
+ (e.g. CI) this raises instead of blocking on input().
107
+ """
73
108
  cfg = load_config()
109
+ if cfg is not None:
110
+ return cfg
111
+ cfg = config_from_env()
74
112
  if cfg is not None:
75
113
  return cfg
76
114
  return create_config_file()
@@ -25,11 +25,15 @@ def _safe_nan_check(x):
25
25
  # prisma_api main class
26
26
  class prisma_api():
27
27
 
28
- def __init__(self, use_config_file=True, local_dev: bool = False):
28
+ def __init__(self, use_config_file=True, local_dev: bool = False,
29
+ upload_timeout: int = 120):
29
30
 
30
31
  # Initialise `prisma_api` object with api_key location
31
32
  self.verbose = False
32
33
  # Initialise `prisma_api` object with api_key location
34
+ # use_config_file=True: config.yaml, else PRISMA_API_* env vars, else
35
+ # an interactive prompt (raises instead when there is no terminal).
36
+ # use_config_file=False: env vars only.
33
37
  if use_config_file:
34
38
  cfg = get_or_create_config()
35
39
  prod_key = cfg.get('api_key', '')
@@ -40,7 +44,9 @@ class prisma_api():
40
44
  dev_key = os.getenv('PRISMA_API_DEV_API_KEY', prod_key)
41
45
  dev_host_port = os.getenv('PRISMA_API_DEV_HOST_PORT', '8000')
42
46
 
43
- # local_dev now controls target DB explicitly at init time.
47
+ # local_dev controls the target DB at init time:
48
+ # local_dev=False (default) -> PRODUCTION (prisma-platform.org)
49
+ # local_dev=True -> local dev server on localhost:<dev_host_port>
44
50
  self.dev = bool(local_dev)
45
51
  self.dev_host_port = dev_host_port
46
52
  self.key = dev_key if self.dev else prod_key
@@ -50,8 +56,13 @@ class prisma_api():
50
56
  dev=self.dev,
51
57
  dev_host_port=getattr(self, 'dev_host_port', ''),
52
58
  return_format=getattr(self, '_return_format', 'json'),
59
+ upload_timeout=upload_timeout,
53
60
  )
54
61
 
62
+ # Make the target explicit so test uploads don't go to production by mistake.
63
+ target = "LOCAL DEV" if self.dev else "PRODUCTION"
64
+ print(f"prisma_api: connected to {target} API at {self.v2._base_url()}")
65
+
55
66
  def set_return_format(self, fmt: str) -> None:
56
67
  """
57
68
  Set the output format for all v2 list endpoints.