Metadata-Version: 2.4
Name: cmsdials
Version: 3.0.0
Summary: The Python api client interface to DIALS service
Author-email: Gabriel Moreira <gabriel.moreira@cern.ch>
Project-URL: Repository, https://gitlab.cern.ch/cms-dqmdc/libraries/dials-py
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests<3.0.0,>=2.31.0
Requires-Dist: urllib3<2.0.0,>=1.26.16
Requires-Dist: pydantic<3.0.0,>=2.8.2
Provides-Extra: pandas
Requires-Dist: pandas>=1.5.3; extra == "pandas"
Provides-Extra: tqdm
Requires-Dist: tqdm>=4.66.1; extra == "tqdm"
Dynamic: license-file

![Build Status](https://gitlab.cern.ch/cms-ppd/technical-support/libraries/dials-py/badges/develop/pipeline.svg)
![Coverage](https://gitlab.cern.ch/cms-ppd/technical-support/libraries/dials-py/badges/develop/coverage.svg)
![Latest Release](https://gitlab.cern.ch/cms-ppd/technical-support/libraries/dials-py/-/badges/release.svg)
[![PyPI version](https://badge.fury.io/py/cmsdials.png)](https://badge.fury.io/py/cmsdials)

# dials-py

The Python client of the [DIALS](https://cms-ppd.docs.cern.ch/ts/software/DIALS/index.html) API.

The full documentation, with every endpoint, filters, pandas and progress bar support, retrying and resuming long queries, ML certification JSONs and the development instance, is in the [dials-py user guide](https://cms-ppd.docs.cern.ch/ts/software/DIALS/user_guide/dials_py.html).

## Installation

```bash
pip install cmsdials
```

The `pandas` and `tqdm` extras enable conversion to DataFrames and progress bars:

```bash
pip install "cmsdials[pandas,tqdm]"
```

## Usage

Tokens are issued by CERN's Keycloak directly, dials-py never sends your password or a client secret to DIALS.

To log in as yourself, with the device flow (interactive, the credentials are cached in `.cache/` and renewed automatically):

```python
from cmsdials.auth.device import Credentials

creds = Credentials.from_creds_file()
```

To log in as an application, with its client id and secret (unattended scripts, the application must be granted [API access](https://cms-ppd.docs.cern.ch/ts/software/DIALS/user_guide/api_access.html)):

```python
import os

from cmsdials.auth.client_credentials import Credentials

creds = Credentials(client_id="my-application", client_secret=os.environ["CLIENT_SECRET"])
```

Then query DIALS, optionally in another workspace than the one picked from your e-groups:

```python
from cmsdials import Dials
from cmsdials.filters import LumisectionHistogram1DFilters

dials = Dials(creds, workspace="tracker")

data = dials.h1d.list_all(
    LumisectionHistogram1DFilters(me="PixelPhase1/Tracks/PXBarrel/charge_PXLayer_2"),
    max_pages=5,
)
```

## Development

Install the dependencies and the package using `uv`:

```shell
uv sync --all-groups --extra pandas --extra tqdm
uv run pre-commit install
uv pip install -e .
```

### Running tests

The repository has some tests written to make sure DIALS responses are compatible with pydantic metaclasses, you can use pytest to run all tests but you need to specify the client id and secret of an application to authenticate non-interactively against DIALS api:

```bash
CLIENT_SECRET=... CLIENT_ID=... uv run pytest tests
```

Any application granted API access in DIALS can be used: `CLIENT_ID` is its client id and `CLIENT_SECRET` its client secret, both obtained from the applications portal. Tests target the dev Keycloak applications by default, overwrite them with `AUTH_CLIENT_ID` and `AUTH_AUDIENCE`. The interactive authentication flow should be tested manually, an example for this can be found in [test_auth_client.py](/tests/integration/test_auth_client.py).

If testing against a local version of DIALS you need to specify the BASE_URL:

```bash
CLIENT_SECRET=... CLIENT_ID=... BASE_URL=http://localhost:8000 uv run pytest tests
```

#### Tox

Tox is pre-configured in `tox.ini`, so you can run the following to test against multiple python versions locally:

```bash
CLIENT_SECRET=... CLIENT_ID=... uv run tox
```

**[asdf](https://asdf-vm.com/) users**

tox requires multiple versions of Python to be installed. Using `asdf`, you have multiple versions installed, but they aren’t normally exposed to the current shell. You can use the following command to expose multiple versions of Python in the current directory:

```bash
asdf set python 3.12.9 3.11.10 3.10.13 3.9.19
```

This will use `3.12.9` by default (if you just run `python`), but it will also put `python3.11`, `python3.10` and `python3.9` symlinks in your path so you can run those too (which is exactly what tox is looking for).

### Releasing the package on PyPI

The package is available in PyPI at [cmsdials](https://pypi.org/project/cmsdials/), under the [cmsdqm](https://pypi.org/org/cms-dqm/) organization. You'll need at leat Mantainer rights to be able to push new versions.

#### CI

Do not worry. The GitLab CI is configured to automatically publish the package on PyPI and the release notes in GitLab whever a tag is pushed to the repo.

> [!NOTE]
> For this to work the CI/CD variables named `UV_PUBLISH_TOKEN`, `GITLAB_TOKEN` should be registered in gitlab. The `UV_PUBLISH_TOKEN` is a api token access of CMSDQM organization and the `GITLAB_TOKEN` is a Project Access Token with api read/write rights, which is needed to read merge requests using the `glab-cli`.
> https://gitlab.cern.ch/cms-dqmdc/libraries/dials-py/-/settings/access_tokens

#### Manual

If you want to follow the manual approach, you need to first build and then publish.

##### Build

You can use uv to build the package using:

```bash
uv build
```

The build system will automatically update the package version based on the git tag of the current commit.

##### Publish

Provided you have already generate a PyPI api token in your account or in CMDQM org, you can publish using:

```bash
UV_PUBLISH_TOKEN=... uv publish
```
