Managers

The manager objects are responsible for managing inputs or outputs. We can have three macro types of managers: database, file and ldap.

Type of managers

Each type of manager is managed by micro types; Below is the complete list:

  1. Database
    1. sqllite (SQLlite)

    2. mssql (Microsoft SQL)

    3. mysql (MySQL or MariaDB)

    4. postgresql (PostgreSQL or EnterpriseDB)

  2. File
    1. file (standard text file or log)

    2. csv (Comma Separated Value file)

    3. json (JSON file)

    4. yaml (YAML file)

    5. xlsx (Microsoft Excel file)

  3. LDAP
    1. ldap (Active Directory Server, OpenLDAP, FreeIPA, etc.)

Note

The connection arguments of a DatabaseManager vary according to the type of database being accessed. Look at the manuals and documentation of each type of database to find out more.

import pyreports

# DatabaseManager object
sqllite_db = pyreports.manager('sqllite', database='/tmp/mydb.db')
mssql_db = pyreports.manager('mssql', server='mssql1.local', database='test', user='dba', password='dba0000')
mysql_db = pyreports.manager('mysql', host='mysql1.local', database='test', user='dba', password='dba0000')
postgresql_db = pyreports.manager('postgresql', host='postgresql1.local', database='test', user='dba', password='dba0000')

# FileManager object
file = pyreports.manager('file', '/tmp/log.log')
csv = pyreports.manager('csv', '/tmp/csv.csv')
json = pyreports.manager('json', '/tmp/json.json')
yaml = pyreports.manager('yaml', '/tmp/yaml.yml')
xlsx = pyreports.manager('xlsx', '/tmp/xlsx.xlsx')

# LdapManager object
ldap = pyreports.manager('ldap', server='ldap.local', username='user', password='password', ssl=False, tls=True)

Managers at work

A Manager object corresponds to each type of manager. And each Manager object has its own methods for writing and reading data.

DatabaseManager

Databasemanager have eight methods that are used to reconnect, query, commit changes and much more. Let’s see these methods in action below.

Note

The following example will be done on a mysql type database, but it can be applied to any database because DB-API 2.0 is used.

import pyreports

# DatabaseManager object
mysql_db = pyreports.manager('mysql', host='mysql1.local', database='test', user='dba', password='dba0000')

# Reconnect to database
mysql_db.reconnect()

# Query: CREATE
mysql_db.execute("CREATE TABLE cars(id SERIAL PRIMARY KEY, name VARCHAR(255), price INT)")
# Query: INSERT
mysql_db.execute("INSERT INTO cars(name, price) VALUES('Audi', 52642)")
# Query: INSERT (many)
new_cars = [
  ('Alfa Romeo', 42123),
  ('Aston Martin', 78324),
  ('Ferrari', 129782),
]
mysql_db.executemany("INSERT INTO cars(name, price) VALUES(%s, %s)", new_cars)
# Commit changes
mysql_db.commit()
# Query: SELECT
mysql_db.execute('SELECT * FROM cars')
# View description and other info of last query
print(mysql_db.description, mysql_db.lastrowid, mysql_db.rowcount)
# Fetch all data
print(mysql_db.fetchall())                  # Dataset object
# Fetch first row
print(mysql_db.fetchone())                  # Dataset object
# Fetch select N row
print(mysql_db.fetchmany(2))                # Dataset object
print(mysql_db.fetchmany())                 # This is same fetchone() method
# Query: SHOW
mysql_db.execute("SHOW TABLES")
print(mysql_db.fetchall())                  # Dataset object

# Call store procedure
mysql_db.callproc('select_cars')
mysql_db.callproc('select_cars', ['Audi'])  # Call with args
print(mysql_db.fetchall())                  # Dataset object

Note

Whatever operation is done, the return value of the fetch* methods return Dataset objects.

FileManager

FileManager has two simple methods: read and write. Let’s see how to use this manager.

import pyreports

# FileManager object
csv = pyreports.manager('csv', '/tmp/cars.csv')

# Read data
cars = csv.read()       # Dataset object

# Write data
cars.append(['Audi', 52642])
csv.write(cars)

LdapManager

LdapManager is an object that allows you to interface and get data from a directory server via the ldap protocol.

import pyreports

# LdapManager object
ldap = pyreports.manager('ldap', server='ldap.local', username='user', password='password', ssl=False, tls=True)

# Rebind connection
ldap.rebind()

# Query: get data
# This is Dataset object
users = ldap.query('DC=test,DC=local', '(&(objectClass=user)(objectCategory=person))', ['name', 'mail', 'phone'])
if users:
    print(users)

# Close connection
ldap.unbind()

Warning

LdapManager should only be used for inputs. An ldap manager has no write methods.