Skip to content

Latest commit

 

History

History
227 lines (176 loc) · 8.48 KB

README.md

File metadata and controls

227 lines (176 loc) · 8.48 KB

PyPI version Build and upload to PyPI

Code Style Auto Tests/Package Coverage Status

Mergin Maps Python Client

This repository contains a Python client module for access to Mergin Maps service and a command-line tool for easy access to data stored in Mergin Maps.

Join our community and ask questions!

To install the module:

    pip3 install mergin-client

Note: Check also Mergin Maps Cpp Client

Using Python API

To use Mergin Maps from Python, it is only needed to create MerginClient object and then use it:

import mergin

client = mergin.MerginClient(login='john', password='topsecret')
client.download_project('lutraconsulting/Basic survey', '/tmp/basic-survey')

If you have Mergin Maps plugin for QGIS installed and you want to use it from QGIS' Python console

import Mergin.mergin as mergin

client = mergin.MerginClient(login='john', password='topsecret')

Command-line Tool

When the module is installed, it comes with mergin command line tool.

$ mergin --help
Usage: mergin [OPTIONS] COMMAND [ARGS]...

  Command line interface for the Mergin Maps client module. For user
  authentication on server there are two options:

   1. authorization token environment variable (MERGIN_AUTH) is defined, or
   2. username and password need to be given either as environment variables
   (MERGIN_USERNAME, MERGIN_PASSWORD),  or as command options (--username,
   --password).

  Run `mergin --username <your_user> login` to see how to set the token
  variable manually.

Options:
  --url TEXT         Mergin Maps server URL. Default is:
                     https://app.merginmaps.com/
  --auth-token TEXT  Mergin authentication token string
  --username TEXT
  --password TEXT
  --help             Show this message and exit.

Commands:
  clone                Clone project from server.
  create               Create a new project on Mergin Maps server.
  download             Download last version of Mergin Maps project
  download-file        Download project file at specified version.
  list-projects        List projects on the server
  list-files           List files in a project
  login                Login to the service and see how to set the token...
  pull                 Fetch changes from Mergin Maps repository
  push                 Upload local changes into Mergin Maps repository
  remove               Remove project from server.
  rename               Rename project in Mergin Maps repository.
  reset                Reset local changes in project.
  share                Fetch permissions to project
  share-add            Add permissions to [users] to project
  share-remove         Remove [users] permissions from project
  show-file-changeset  Displays information about project changes.
  show-file-history    Displays information about a single version of a...
  show-version         Displays information about a single version of a...
  status               Show all changes in project files - upstream and...

Examples

For example, to download a project:

$ mergin --username john download john/project1 ~/mergin/project1

To download a specific version of a project:

$ mergin --username john download --version v42 john/project1 ~/mergin/project1

To download a specific version of a single file:

  1. First you need to download the project:
mergin --username john download john/myproject
  1. Go to the project directory
cd myproject
  1. Download the version of a file you want:
mergin --username john download-file --version v273 myfile.gpkg /tmp/myfile-v273.gpkg

If you do not want to specify username on the command line and be asked for you password every time, it is possible to set env variables MERGIN_USERNAME and MERGIN_PASSWORD.

When a project is downloaded, mergin commands can be run in the project's working directory:

  1. get status of the project (check if there are any local/remote changes)
    $ mergin status
    
  2. pull changes from Mergin Maps service
    $ mergin pull
    
  3. push local changes to Mergin Maps service
    $ mergin push
    

Using CLI with auth token

If you plan to run mergin command multiple times and you wish to avoid logging in every time, you can use "login" command to get authorization token. It will ask for password and then output environment variable with auth token. The returned token is not permanent - it will expire after several hours.

$ mergin --username john login
Password: topsecret
Login successful!
To set the MERGIN_AUTH variable run in Linux:
export MERGIN_AUTH="Bearer ......."
In Windows:
SET MERGIN_AUTH=Bearer .......

When setting the variable in Windows you do not quote the value.

When the MERGIN_AUTH env variable is set (or passed with --auth-token command line argument), it is possible to run other commands without specifying username/password.

Development

Setting up a development environment

Python 3.10+ required. Install the dependencies and the client in editable mode, so changes in your checkout apply without reinstalling:

    pip install -r requirements.dev.txt
    pip install --editable . --no-deps
    mergin --help

Developing with the QGIS plugin (optional)

To bundle the dependencies into mergin/deps the same way the QGIS plugin does:

    rm -rf mergin/deps
    mkdir -p mergin/deps
    pip wheel --no-deps -r requirements.txt -w mergin/deps
    unzip mergin/deps/pygeodiff-*.whl -d mergin/deps

Remove mergin/deps when going back to the standard setup.

Checking the built package

To check the package exactly as users get it, build a wheel and install that instead:

    pip install build
    python -m build
    pip install --force-reinstall dist/mergin_client-*.whl
    mergin --help

Tests

With the development environment set up, run tests with:

    # TEST_ related vairables are optional
    export TEST_MERGIN_URL=<url> # testing server
    export TEST_API_USERNAME=<username>
    export TEST_API_PASSWORD=<pwd>
    export TEST_API_USERNAME2=<username2>
    export TEST_API_PASSWORD2=<pwd2>
    pytest --cov-report html --cov=mergin mergin/test/

Locked requirements

Dependency ranges live in pyproject.toml; that is what users installing from PyPI get. Two generated files pin exact versions on top of that:

  • requirements.txt: runtime dependencies only. The QGIS plugin bundles exactly these into mergin/deps.
  • requirements.dev.txt: requirements.txt plus development and test dependencies, used by CI and for local development.

After changing dependencies in pyproject.toml, or to pick up newer versions, regenerate both with Python 3.10 (the version the QGIS plugin bundles for and CI uses) and commit them:

    PYTHON=python3.10 scripts/lock_requirements.sh

CI runs pip check after installing from the lock, so it fails if the pins no longer satisfy pyproject.toml.

Code style

Code is formatted with black (line length 120, configured in pyproject.toml), enforced in CI. The black version is pinned only in the dev dependency group in pyproject.toml (CI and the pre-commit hook both take it from there). Black is installed with requirements.dev.txt; to check formatting manually run black --check ./mergin. To run the same check automatically before each commit, install the pre-commit hooks once (the hook runs the black from your active development environment, so commit with that environment activated):

    pip install pre-commit
    pre-commit install