Edit Source

ldlite

LDLite is a lightweight reporting tool for FOLIO services. It is part of the Library Data Platform project and provides basic LDP functions without requiring the platform to be installed.

LDLite functions include retrieving data from a FOLIO instance, transforming the data, and storing the data in a reporting database for further querying.

To install LDLite or upgrade to the latest version:

python -m pip install --upgrade ldlite

Example:

# Import and initialize LDLite.
import ldlite
ld = ldlite.LDLite()

# Connect to a database.
db = ld.connect_db(filename='ldlite.db')

# Connect to FOLIO.
ld.connect_folio(url='https://folio-etesting-snapshot-kong.ci.folio.org',
                 tenant='diku',
                 user='diku_admin',
                 password='admin')

# Send a CQL query and store the results in table "g", "g_j", etc.
ld.query(table='g', path='/groups', query='cql.allRecords=1 sortby id')

# Print the result tables.
ld.select(table='g')
ld.select(table='g_j')
# etc.
class LDLite:
LDLite()

Creates an instance of LDLite.

Example:

import ldlite

ld = ldlite.LDLite()
def connect_db(self, filename=None):

Connects to an embedded database for storing data.

The optional filename designates a local file containing the database or where the database will be created if it does not exist. If filename is not specified, the database will be stored in memory and will not be persisted to disk.

This method returns a connection to the database which can be used to submit SQL queries.

Example:

db = ld.connect_db(filename='ldlite.db')
def connect_db_postgresql(self, dsn):

Connects to a PostgreSQL database for storing data.

The data source name is specified by dsn. This method returns a connection to the database which can be used to submit SQL queries. The returned connection defaults to autocommit mode.

Example:

db = ld.connect_db_postgresql(dsn='dbname=ldlite host=localhost user=ldlite')
def experimental_connect_db_sqlite(self, filename=None):

Connects to an embedded SQLite database for storing data.

This method is experimental and may not be supported in future versions.

The optional filename designates a local file containing the SQLite database or where the database will be created if it does not exist. If filename is not specified, the database will be stored in memory and will not be persisted to disk.

This method returns a connection to the database which can be used to submit SQL queries.

Example:

db = ld.connect_db_sqlite(filename='ldlite.db')
def connect_folio(self, url, tenant, user, password):

Connects to a FOLIO instance with a user name and password.

The url, tenant, user, and password settings are FOLIO-specific connection parameters.

Example:

ld.connect_folio(url='https://folio-etesting-snapshot-kong.ci.folio.org',
                 tenant='diku',
                 user='diku_admin',
                 password='admin')
def connect_okapi(self, url, tenant, user, password, legacy_auth=False):

Deprecated; use connect_folio(). This will be removed for the Sunflower release.

def connect_okapi_token(self, url, tenant, token):

Deprecated; use connect_folio(). This will be removed for the Sunflower release.

def drop_tables(self, table):

Drops a specified table and any accompanying tables that were output from JSON transformation.

A table called table_jtable is used to retrieve the names of the tables created by JSON transformation.

Example:

ld.drop_tables('g')
def set_folio_max_retries(self, max_retries):

Sets the maximum number of retries for FOLIO requests.

This method changes the configured maximum number of retries which is initially set to 2. The max_retries parameter is the new value.

Note that a request is only retried if a timeout occurs.

Example:

ld.set_folio_max_retries(5)
def set_okapi_max_retries(self, max_retries):

Deprecated; use set_folio_max_retries(). This will be removed for the Sunflower release.

def set_folio_timeout(self, timeout):

Sets the timeout for connections to FOLIO.

This method changes the configured timeout which is initially set to 60 seconds. The timeout parameter is the new timeout in seconds.

Example:

ld.set_folio_timeout(300)
def set_okapi_timeout(self, timeout):

Deprecated; use set_folio_timeout(). This will be removed for the Sunflower release.

def query( self, table, path, query=None, json_depth=3, limit=None, transform=None):

Submits a query to a FOLIO module, and transforms and stores the result.

The retrieved result is stored in table within the reporting database. the table name may include a schema name; however, if the database is SQLite, which does not support schemas, the schema name will be added to the table name as a prefix.

The path parameter is the request path.

If query is a string, it is assumed to be a CQL or similar query and is encoded as query=query. If query is a dictionary, it is interpreted as a set of query parameters. Each value of the dictionary must be either a string or a list of strings. If a string, it is encoded as key=value. If a list of strings, it is encoded as key=value1&key=value2&...

By default JSON data are transformed into one or more tables that are created in addition to table. New tables overwrite any existing tables having the same name. If json_depth is specified within the range 0 < json_depth < 5, this determines how far into nested JSON data the transformation will descend. (The default is 3.) If json_depth is specified as 0, JSON data are not transformed.

If limit is specified, then only up to limit records are retrieved.

The transform parameter is no longer supported and will be removed in the future. Instead, specify json_depth as 0 to disable JSON transformation.

This method returns a list of newly created tables, or raises ValueError or RuntimeError.

Example:

ld.query(table='g', path='/groups', query='cql.allRecords=1 sortby id')
def quiet(self, enable):

Configures suppression of progress messages.

If enable is True, progress messages are suppressed; if False, they are not suppressed.

Example:

ld.quiet(True)
def select(self, table, columns=None, limit=None):

Prints rows of a table in the reporting database.

By default all rows and columns of table are printed to standard output. If columns is specified, then only the named columns are printed. If limit is specified, then only up to limit rows are printed.

Examples:

ld.select(table='loans', limit=10)
ld.select(table='loans', columns=['id', 'item_id', 'loan_date'])
def export_csv(self, filename, table, header=True):

Export a table in the reporting database to a CSV file.

All rows of table are exported to filename, or filename.csv if filename does not have an extension.

If header is True (the default), the CSV file will begin with a header line containing the column names.

Example:

ld.to_csv(table='g', filename='g.csv')
def to_csv(self, filename, table, header=True):

Deprecated; use export_csv().

def export_excel(self, filename, table, header=True):

Export a table in the reporting database to an Excel file.

All rows of table are exported to filename, or filename.xlsx if filename does not have an extension.

If header is True (the default), the worksheet will begin with a row containing the column names.

Example:

ld.export_excel(table='g', filename='g')
def to_xlsx(self, filename, table, header=True):

Deprecated; use export_excel().