Metadata-Version: 2.4
Name: shillelagh
Version: 1.4.4
Summary: Making it easy to query APIs via SQL
Home-page: https://github.com/betodealmeida/shillelagh/
Author: Beto Dealmeida
Author-email: roberto@dealmeida.net
License: mit
Project-URL: Documentation, https://shillelagh.readthedocs.io/
Platform: any
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: SQL
Requires-Python: >=3.9
Description-Content-Type: text/x-rst; charset=UTF-8
License-File: LICENSE.txt
License-File: AUTHORS.rst
Requires-Dist: importlib-metadata; python_version < "3.10"
Requires-Dist: apsw>=3.43.2.0
Requires-Dist: python_dateutil>=2.8.1
Requires-Dist: requests>=2.31.0
Requires-Dist: requests-cache>=0.7.1
Requires-Dist: sqlalchemy>=1.3
Requires-Dist: greenlet>=2.0.2
Requires-Dist: typing_extensions>=3.7.4.3
Requires-Dist: packaging
Provides-Extra: testing
Requires-Dist: PyYAML>=5.4; extra == "testing"
Requires-Dist: appdirs>=1.4.4; extra == "testing"
Requires-Dist: beautifulsoup4>=4.11.1; extra == "testing"
Requires-Dist: boto3>=1.24.28; extra == "testing"
Requires-Dist: codespell>=2.1.0; extra == "testing"
Requires-Dist: defusedxml>=0.7.1; extra == "testing"
Requires-Dist: dill>=0.3.6; extra == "testing"
Requires-Dist: flake8-pyproject>=1.2.3; extra == "testing"
Requires-Dist: freezegun>=1.1.0; extra == "testing"
Requires-Dist: google-auth>=1.23.0; extra == "testing"
Requires-Dist: holidays>=0.23; extra == "testing"
Requires-Dist: html5lib>=1.1; extra == "testing"
Requires-Dist: pandas>=1.2.2; extra == "testing"
Requires-Dist: pip-tools>=6.4.0; extra == "testing"
Requires-Dist: pre-commit>=2.13.0; extra == "testing"
Requires-Dist: pip-compile-multi>=2.6.3; extra == "testing"
Requires-Dist: prison>=0.2.1; extra == "testing"
Requires-Dist: prompt_toolkit>=3; extra == "testing"
Requires-Dist: psutil>=5.8.0; extra == "testing"
Requires-Dist: psycopg2-binary>=2.9.9; extra == "testing"
Requires-Dist: pyarrow>=14.0.1; extra == "testing"
Requires-Dist: pyfakefs>=4.3.3; extra == "testing"
Requires-Dist: pygments>=2.8; extra == "testing"
Requires-Dist: pylint>=2.16.2; extra == "testing"
Requires-Dist: pytest-cov>=2.11.1; extra == "testing"
Requires-Dist: pytest-integration==0.2.2; extra == "testing"
Requires-Dist: pytest-mock>=3.5.1; extra == "testing"
Requires-Dist: pytest>=7.2.0; extra == "testing"
Requires-Dist: python-graphql-client>=0.4.3; extra == "testing"
Requires-Dist: python-jsonpath>=0.10.3; extra == "testing"
Requires-Dist: requests-mock>=1.8.0; extra == "testing"
Requires-Dist: ruff>=0.3.7; extra == "testing"
Requires-Dist: sqlglot>=23.0.2; extra == "testing"
Requires-Dist: tabulate>=0.8.9; extra == "testing"
Requires-Dist: yarl>=1.8.1; extra == "testing"
Provides-Extra: all
Requires-Dist: PyYAML>=5.4; extra == "all"
Requires-Dist: appdirs>=1.4.4; extra == "all"
Requires-Dist: beautifulsoup4>=4.11.1; extra == "all"
Requires-Dist: boto3>=1.24.28; extra == "all"
Requires-Dist: defusedxml>=0.7.1; extra == "all"
Requires-Dist: google-auth>=1.23.0; extra == "all"
Requires-Dist: holidays>=0.23; extra == "all"
Requires-Dist: html5lib>=1.1; extra == "all"
Requires-Dist: pandas>=1.2.2; extra == "all"
Requires-Dist: prison>=0.2.1; extra == "all"
Requires-Dist: prompt_toolkit>=3; extra == "all"
Requires-Dist: psutil>=5.8.0; extra == "all"
Requires-Dist: psycopg2-binary>=2.9.9; extra == "all"
Requires-Dist: pyarrow>=14.0.1; extra == "all"
Requires-Dist: pygments>=2.8; extra == "all"
Requires-Dist: python-graphql-client>=0.4.3; extra == "all"
Requires-Dist: python-jsonpath>=0.10.3; extra == "all"
Requires-Dist: sqlglot>=23.0.2; extra == "all"
Requires-Dist: tabulate>=0.8.9; extra == "all"
Requires-Dist: yarl>=1.8.1; extra == "all"
Provides-Extra: docs
Requires-Dist: sphinx>=4.0.1; extra == "docs"
Provides-Extra: console
Requires-Dist: PyYAML>=5.4; extra == "console"
Requires-Dist: appdirs>=1.4.4; extra == "console"
Requires-Dist: prompt_toolkit>=3; extra == "console"
Requires-Dist: pygments>=2.8; extra == "console"
Requires-Dist: tabulate>=0.8.9; extra == "console"
Provides-Extra: dbtmetricflowapi
Requires-Dist: pyarrow>=14.0.1; extra == "dbtmetricflowapi"
Requires-Dist: python-graphql-client>=0.4.3; extra == "dbtmetricflowapi"
Requires-Dist: sqlglot>=23.0.2; extra == "dbtmetricflowapi"
Provides-Extra: genericjsonapi
Requires-Dist: prison>=0.2.1; extra == "genericjsonapi"
Requires-Dist: python-jsonpath>=0.10.3; extra == "genericjsonapi"
Requires-Dist: yarl>=1.8.1; extra == "genericjsonapi"
Provides-Extra: genericxmlapi
Requires-Dist: defusedxml>=0.7.1; extra == "genericxmlapi"
Requires-Dist: prison>=0.2.1; extra == "genericxmlapi"
Requires-Dist: yarl>=1.8.1; extra == "genericxmlapi"
Provides-Extra: githubapi
Requires-Dist: python-jsonpath>=0.10.3; extra == "githubapi"
Provides-Extra: gsheetsapi
Requires-Dist: google-auth>=1.23.0; extra == "gsheetsapi"
Requires-Dist: pyopenssl>=24.0.0; extra == "gsheetsapi"
Provides-Extra: holidaysmemory
Requires-Dist: holidays>=0.23; extra == "holidaysmemory"
Provides-Extra: htmltableapi
Requires-Dist: beautifulsoup4>=4.11.1; extra == "htmltableapi"
Requires-Dist: html5lib>=1.1; extra == "htmltableapi"
Requires-Dist: pandas>=1.2.2; extra == "htmltableapi"
Provides-Extra: multicorn
Requires-Dist: psycopg2-binary>=2.9.9; extra == "multicorn"
Provides-Extra: pandasmemory
Requires-Dist: pandas>=1.2.2; extra == "pandasmemory"
Provides-Extra: s3selectapi
Requires-Dist: boto3>=1.24.28; extra == "s3selectapi"
Provides-Extra: systemapi
Requires-Dist: psutil>=5.8.0; extra == "systemapi"
Dynamic: license-file

==========
Shillelagh
==========

.. image:: https://coveralls.io/repos/github/betodealmeida/shillelagh/badge.svg?branch=master
   :target: https://coveralls.io/github/betodealmeida/shillelagh?branch=master
.. image:: https://readthedocs.org/projects/shillelagh/badge/?version=latest
   :target: https://shillelagh.readthedocs.io/en/latest/?badge=latest
   :alt: Documentation Status
.. image:: https://badge.fury.io/py/shillelagh.svg
   :target: https://badge.fury.io/py/shillelagh
.. image:: https://img.shields.io/pypi/pyversions/shillelagh
   :alt: PyPI - Python Version

.. image:: docs/logo.png
   :width: 25 %

Shillelagh (ʃɪˈleɪlɪ) is a Python library and CLI that allows you to query many resources (APIs, files, in memory objects) using SQL. It's both user and developer friendly, making it trivial to access resources and easy to add support for new ones.

Learn more on the `documentation <https://shillelagh.readthedocs.io/en/latest/>`_.

The library is an implementation of the `Python DB API 2.0 <https://www.python.org/dev/peps/pep-0249/>`_ based on `SQLite <https://sqlite.org/index.html>`_ (using the `APSW <https://rogerbinns.github.io/apsw/>`_ library):

.. code-block:: python

    from shillelagh.backends.apsw.db import connect

    connection = connect(":memory:")
    cursor = connection.cursor()

    query = "SELECT * FROM a_table"
    for row in cursor.execute(query):
        print(row)

There is also a `SQLAlchemy <https://www.sqlalchemy.org/>`_ dialect:

.. code-block:: python

    from sqlalchemy import text
    from sqlalchemy.engine import create_engine

    engine = create_engine("shillelagh://")
    connection = engine.connect()

    query = "SELECT * FROM a_table"
    for row in connection.execute(text(query)):
        print(row)

And a command-line utility:

.. code-block:: bash

    $ shillelagh
    🍀> SELECT * FROM a_table

There is also an `experimental backend <https://shillelagh.readthedocs.io/en/latest/postgres.html>`_ that uses Postgres with the `Multicorn2 <http://multicorn2.org/>`_ extension, and another one using `sqlglot <https://sqlglot.com/sqlglot.html>`_.

To run the Postgres backend, first install the additional dependencies:

.. code-block:: bash

    $ pip install 'shillelagh[multicorn]'
    $ pip install 'multicorn @ git+https://github.com/pgsql-io/multicorn2.git@v2.5'

Then run:

.. code-block:: python

    from shillelagh.backends.multicorn.db import connect

    connection = connect(
        user="username",
        password="password",
        host="localhost",
        port=5432,
        database="examples",
    )

Or:

.. code-block:: python

    from sqlalchemy import create_engine
    engine = create_engine("shillelagh+multicorn2://username:password@localhost:5432/examples")

For the sqlglot backend you can use ``shillelagh+sqlglot://`` as the connection string, or import ``connect()`` from ``shillelagh.backends.sqlglot.db``.

Why SQL?
========

Sharks have been around for a long time. They're older than trees and the rings of Saturn, actually! The reason they haven't changed that much in hundreds of millions of years is because they're really good at what they do.

SQL has been around for some 50 years for the same reason: it's really good at what it does.

Why "Shillelagh"?
=================

Picture a leprechaun hitting APIs with a big stick so that they accept SQL.

How is it different?
====================

Shillelagh allows you to easily query non-SQL resources. For example, if you have a `Google Spreadsheet <https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0>`_ you can query it directly as if it were a table in a database:

.. code-block:: sql

    SELECT country, SUM(cnt)
    FROM "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0"
    WHERE cnt > 0
    GROUP BY country

You can even run ``INSERT``/``DELETE``/``UPDATE`` queries against the spreadsheet:

.. code-block:: sql

    UPDATE "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0"
    SET cnt = cnt + 1
    WHERE country != 'BR'

Queries like this are supported by `adapters <https://shillelagh.readthedocs.io/en/latest/adapters.html>`_. Currently Shillelagh has the following adapters:

============= ============ ========================================================================== =====================================================================================================
 Name          Type         URI pattern                                                                Example URI
============= ============ ========================================================================== =====================================================================================================
 CSV           File/API     ``/path/to/file.csv``; ``http(s)://*``                                     ``/home/user/sample_data.csv``
 Datasette     API          ``http(s)://*``                                                            ``https://global-power-plants.datasettes.com/global-power-plants/global-power-plants``
 Generic JSON  API          ``http(s)://*``                                                            ``https://api.stlouisfed.org/fred/series?series_id=GNPCA&api_key=XXX&file_type=json#$.seriess[*]``
 Generic XML   API          ``http(s)://*``                                                            ``https://api.congress.gov/v3/bill/118?format=xml&offset=0&limit=2&api_key=XXX#.//bill``
 GitHub        API          ``https://api.github.com/repos/${owner}/{$repo}/pulls``                    ``https://api.github.com/repos/apache/superset/pulls``
 GSheets       API          ``https://docs.google.com/spreadsheets/d/${id}/edit#gid=${sheet_id}``      ``https://docs.google.com/spreadsheets/d/1LcWZMsdCl92g7nA-D6qGRqg1T5TiHyuKJUY1u9XAnsk/edit#gid=0``
 HTML table    API          ``http(s)://*``                                                            ``https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population``
 Pandas        In memory    Any variable name (local or global)                                        ``my_df``
 S3            API          ``s3://bucket/path/to/file``                                               ``s3://shillelagh/sample_data.csv``
 Socrata       API          ``https://${domain}/resource/${dataset-id}.json``                          ``https://data.cdc.gov/resource/unsk-b7fc.json``
 System        API          ``system://${resource}``                                                   ``system://cpu?interval=2``
 WeatherAPI    API          ``https://api.weatherapi.com/v1/history.json?key=${key}&q=${location}``    ``https://api.weatherapi.com/v1/history.json?key=XXX&q=London``
============= ============ ========================================================================== =====================================================================================================

There are also 3rd-party adapters:

- `Airtable <https://github.com/cancan101/airtable-db-api>`_
- `GraphQL <https://github.com/cancan101/graphql-db-api>`_
- `OData v4 <https://github.com/brandonjjon/sqlalchemy-odata>`_

A query can combine data from multiple adapters:

.. code-block:: sql

    INSERT INTO "/tmp/file.csv"
    SELECT time, chance_of_rain
    FROM "https://api.weatherapi.com/v1/history.json?q=London"
    WHERE time IN (
      SELECT datetime
      FROM "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=1648320094"
    )

The query above reads timestamps from a Google sheet, uses them to filter weather data from `WeatherAPI <https://www.weatherapi.com/>`_, and writes the chance of rain into a (pre-existing) CSV file.

New adapters are relatively easy to implement. There's a `step-by-step tutorial <https://shillelagh.readthedocs.io/en/latest/development.html>`_ that explains how to create a new adapter to an API or filetype.

Installation
============

Install Shillelagh with ``pip``:

.. code-block:: bash

    $ pip install 'shillelagh'

You also need to install optional dependencies, depending on the adapter you want to use:

.. code-block:: bash

    $ pip install 'shillelagh[console]'        # to use the CLI
    $ pip install 'shillelagh[genericjsonapi]' # for Generic JSON
    $ pip install 'shillelagh[genericxmlapi]'  # for Generic XML
    $ pip install 'shillelagh[githubapi]'      # for GitHub
    $ pip install 'shillelagh[gsheetsapi]'     # for GSheets
    $ pip install 'shillelagh[htmltableapi]'   # for HTML tables
    $ pip install 'shillelagh[pandasmemory]'   # for Pandas in memory
    $ pip install 'shillelagh[s3selectapi]'    # for S3 files
    $ pip install 'shillelagh[systemapi]'      # for CPU information

Alternatively, you can install everything with:

.. code-block:: bash

    $ pip install 'shillelagh[all]'
