msl.network.database module
Databases that are used by the Network Manager
.
- class msl.network.database.Database(database, **kwargs)[source]
Bases:
object
Base class for connecting to a SQLite database.
Automatically creates the database if it does not already exist.
- Parameters:
database (
str
) – The path to the database file, or':memory:'
to open a connection to a database that resides in RAM instead of on disk.kwargs – Optional keyword arguments to pass to
sqlite3.connect()
.
- property connection
The connection object.
- Type:
- property cursor
The cursor object.
- Type:
- execute(sql, parameters=None)[source]
Wrapper around
sqlite3.Cursor.execute()
.
- table_info(name)[source]
Returns the information about each column in the specified table.
- Parameters:
name (
str
) – The name of the table to get the information of.- Returns:
list
oftuple
– The list of the fields in the table. The indices of each tuple correspond to:0 - id number of the column
1 - the name of the column
2 - the datatype of the column
3 - whether a value in the column can be NULL (0 or 1)
4 - the default value for the column
5 - whether the column is used as a primary key (0 or 1)
- class msl.network.database.ConnectionsTable(*, database=None, as_datetime=False, **kwargs)[source]
Bases:
Database
The database table for devices that have connected to the Network
Manager
.- Parameters:
database (
str
, optional) – The path to the database file, or':memory:'
to open a connection to a database that resides in RAM instead of on disk. IfNone
then loads the default database.as_datetime (
bool
, optional) – Whether to fetch the timestamps from the database asdatetime.datetime
objects. IfFalse
then the timestamps will be of typestr
.kwargs – Optional keyword arguments to pass to
sqlite3.connect()
.
- connections(*, start=None, end=None)[source]
Return the information of the devices that have connected to the Network
Manager
.Changed in version 1.0: Use
T
as the separator between the date and time. Renamed timestamp1 to start. Renamed timestamp2 to end.- Parameters:
start (
datetime.datetime
orstr
, optional) – Include all records that have a timestamp \(\ge\) start. If astr
then in theyyyy-mm-dd
oryyyy-mm-ddTHH:MM:SS
format.end (
datetime.datetime
orstr
, optional) – Include all records that have a timestamp \(\le\) end. If astr
then in theyyyy-mm-dd
oryyyy-mm-ddTHH:MM:SS
format.
- Returns:
- class msl.network.database.HostnamesTable(*, database=None, **kwargs)[source]
Bases:
Database
The database table for trusted hostname’s that are allowed to connect to the Network
Manager
.- Parameters:
database (
str
, optional) – The path to the database file, or':memory:'
to open a connection to a database that resides in RAM instead of on disk. IfNone
then loads the default database.kwargs – Optional keyword arguments to pass to
sqlite3.connect()
.
- insert(hostname)[source]
Insert a hostname.
If the hostname is already in the table then it does not insert it again.
- Parameters:
hostname (
str
) – The trusted hostname.
- delete(hostname)[source]
Delete a hostname.
- Parameters:
hostname (
str
) – A hostname in the table.- Raises:
ValueError – If hostname is not in the table.
- class msl.network.database.UsersTable(*, database=None, **kwargs)[source]
Bases:
Database
The database table for keeping information about a users login credentials for connecting to a Network
Manager
.- Parameters:
database (
str
, optional) – The path to the database file, or':memory:'
to open a connection to a database that resides in RAM instead of on disk. IfNone
then loads the default database.kwargs – Optional keyword arguments to pass to
sqlite3.connect()
.
- insert(username, password, is_admin)[source]
Insert a new user.
The password is encrypted and stored in the database using PBKDF2
To update the values for a user use
update()
.- Parameters:
- Raises:
ValueError – If the username is invalid or if password is empty.
- update(username, *, password=None, is_admin=None)[source]
Update either the salt used for the password and/or the admin rights.
- Parameters:
- Raises:
ValueError – If username is not in the table. If both password and is_admin are not specified. If password is an empty string.
- delete(username)[source]
Delete a user.
- Parameters:
username (
str
) – The name of the user.- Raises:
ValueError – If username is not in the table.
- msl.network.database.convert_datetime(value)[source]
Convert a date and time to a
datetime
object.- Parameters:
value (
bytes
) – The datetime value from an SQLite database.- Returns:
datetime.datetime
– The value as a datetime object.