cwbrowser.cw_connection.CWInstanceConnection

class cwbrowser.cw_connection.CWInstanceConnection(url, login, password, realm=None, port=22, server_root='/')[source]

Tool to dump the data stored in a cw instance.

# Create dummy rqls
rql1 = ("Any C, G Where X is Subject, X code_in_study C, "
        "X handedness 'ambidextrous', X gender G")
rql2 = ("Any S WHERE S is Scan, S has_data A, A field '3T', "
        "S in_assessment B, B timepoint 'V1', S format 'GIS', "
        "S in_assessment C, C concerns D, D code_in_study 'ab100207'")

# HTTP test
url = @HTTPURL; login = @LOGIN; password = @PWD
connection = CWInstanceConnection(url, login, password)
connection.execute(rql1, export_type="json")
connection.execute_with_fuse(rql2, "/tmp/fuse", timer=1)

# HTTPS test
url = @HTTPSURL; login = @LOGIN; password = @PWD
connection = CWInstanceConnection(url, login, password, realm="Imagen")
connection.execute(rql)

Attributes

url str the url to the cw instance.
login str the cw login.
opener: OpenerDirector   object that contains the connexion to the cw instance.

Methods

__init__(url, login, password[, realm, ...]) Initilize the HTTPConnection class.
execute(rql[, export_type, nb_tries, timeout]) Method that loads the rset from a rql request.
execute_with_sync(rql, sync_dir[, timer, ...]) Method that loads the rset from a rql request through sftp protocol using the CWSearch mechanism.

Attributes

importers
_EXPORT_TYPES = ['json', 'csv', 'cw']
__module__ = 'cwbrowser.cw_connection'
_connect(password)[source]

Method to create an object that handle opening of HTTP/HTTPS URLs.

Parameters:

password: str (mandatory) :

the cw user password.

_create_cwsearch(rql, export_type='cwsearch')[source]

Method that creates a CWSearch entity from a rql.

Note

The CWSearch title has to be unique, build automatically title of the form ‘auto_generated_title_x’ where x is incremented each time an element is inserted in the data base.

Parameters:

rql: str (mandatory) :

the rql rquest that will be executed on the cw instance.

_get_server_dataset(sync_dir, cwsearch_title, cw_params)[source]

Download the CWSearch result trough a sftp connection.

Note

If a folder ‘sync_dir’ + ‘cwsearch_title’ is detected on the local machine, no download is run. We assume that the CWSearch has already been downloaded properly.

Parameters:

sync_dir: str (mandatory) :

the destination folder where the rql data are synchronized.

cwsearch_title: str (mandatory) :

the title of the CWSearch that will be downloaded.

cw_params: dict (mandatory) :

a dictionary containing cw/fuse parameters.

_sftp_get_recursive(path, dest, sftp)[source]

Recursive download of the data through a sftp connection.

Parameters:

path: str (mandatory) :

the sftp path to download.

dest: str (mandatory) :

the destination folder on the local machine.

sftp: paramiko sftp connection (mandatory) :

_sftp_isdir(path, sftp)[source]

Check if a distant path is a directory through a sftp connection.

Parameters:

path: str (mandatory) :

the sftp path to download.

sftp: paramiko sftp connection (mandatory) :

execute(rql, export_type='json', nb_tries=2, timeout=300)[source]

Method that loads the rset from a rql request.

Parameters:

rql: str (mandatory) :

the rql rquest that will be executed on the cw instance.

export_type: str (optional default ‘json’) :

the result set export format: one defined in ‘_EXPORT_TYPES’.

nb_tries: int (optional default 2) :

number of times a request will be repeated if it fails.

timeout: int (optional, default 300) :

number of seconds to wait for server response before considering that the request failed.

Returns:

rset: list of list of str :

a list that contains the requested entity parameters.

execute_with_sync(rql, sync_dir, timer=3, nb_tries=3)[source]

Method that loads the rset from a rql request through sftp protocol using the CWSearch mechanism.

Parameters:

rql: str (mandatory) :

the rql rquest that will be executed on the cw instance.

sync_dir: str (mandatory) :

the destination folder where the rql data are synchronized.

timer: int (optional default 3) :

the time in seconds we are waiting for the fuse or twisted server update.

nb_tries: int (optional default 3) :

if the update has not been detected after ‘nb_of_try’ trials raise an exception.

Returns:

rset: list of list or list of dict :

a list that contains the requested cubicweb database parameters when a json rset is generated, a list of dictionaries if a csv rset is generated.

importers = {'json': <function load at 0x2b1996b5c578>, 'csv': <function load_csv at 0x2b1998dda668>, 'cw': <function load at 0x2b1996b5c578>}