server.resources package

This package handles various dynamic data that is stored in memory. The data mainly constitutes of entity objects that are emitted by devices and need to be forwarded to clients.

  • each entity
    • is a JSON object (dict) conforming one of the JSON Schemas

    • has a unique ID as one of its properties

    • apart from text/number/bool values, it may also contain arrays with

      ID references to other entities (see device.json)

  • all entities of the same type are grouped together in a single dict
    • the keys are their IDs

    • this grouped representation is then synchronized to clients

Note that in some cases the device emits a slightly different object that is then tranformed by the server to the actual entity, which conforms the schema.

Other non-entity dynamic resources include:

  • mapping of device socketio connection

  • mapping of client (user) socketio connection

  • management of temporary directories (temporary storage for datafiles)

Submodules

server.resources.resources module

class server.resources.resources.EntityContainer(validator_new, validator_update)[source]

Bases: object

Container for entity objects (JSON compatible). Each entity has to contain ‘id’ field with a unique id of type str.

add(entity)[source]

Add new entity, which does not yet exist in the container.

to_dict()[source]
update(entity)[source]

Update some or all properties of an existing entity.

class server.resources.resources.ParentEntityContainer(validator_new, validator_update, children={})[source]

Bases: EntityContainer

Entity container with lists of id references to its children. When an entity is added or removed from the child-container, the list of references in the appropriate parent-entity is updated.

add(entity)[source]

Add new entity, which does not yet exist in the container.

class server.resources.resources.SocketioConnections(multiple_connections=False)[source]

Bases: object

Track active socket.io connections. Each account (user or device) is allowed only 1 active connection. This structure can be used to

  1. query id_public (account) by sid (connection)

  2. and vice versa - query sid by id_public

Modification of added entries are not allowed (only add, get, remove) and unique constraint is enforced on both sid and id_public to prevent errors.

add(id_public, sid, host=None)[source]
property by_pubid

Public id of account -> sid of connection.

property by_sid

Sid of connection -> public id of account.

get_hosts(id_public)[source]

Public id of account -> remote host addresses.

remove(*, id_public=None, sid=None)[source]
class server.resources.resources.TempdirManager[source]

Bases: object

add(dir_id)[source]

Create a new temporary directory for given id (e.g. device_id). Id must be unique otherwise UniqueIdConflict is raised.

delete_files_over_limit(dir_id, limit)[source]

Check number of files in temp directory. If limit was exceeded, delete old files (based on file-modified timestamp).

Returns list of deleted filenames.

update_file(dir_id, filename, contents)[source]

Create new or overwrite given file in the temporary directory.