Skip to content

Commit 313a6c0

Browse files
committed
Merge pull request #1 from LabKey/experiment
Introduce _netrc, experiment api, and support for python 2 & 3
2 parents eef7719 + 7b95563 commit 313a6c0

14 files changed

+1733
-589
lines changed

README.md

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# About
2-
Python client API for LabKey Server. To get started, please see the [full documentation for this library](https://www.labkey.org/wiki/home/Documentation/page.view?name=python).
2+
The Python client API for LabKey Server lets you query, insert and update data on a LabKey Server from a Python client.
3+
4+
# Release Notes
5+
6+
Changes in the current release:
7+
8+
- Support for Python 3
9+
- Support for netrc files (.labkeycredentials.txt files are now deprecated)
10+
- New methods for working with assay data:
11+
- [load_batch](https://github.com/LabKey/labkey-api-python/tree/master/labkey/experment.py)
12+
- [save_batch](https://github.com/LabKey/labkey-api-python/tree/master/labkey/experment.py)
13+
- server_context parameter added to all methods
14+
- PEP standards - the latest version follows PEP code styling standards
15+
- New [samples](https://github.com/LabKey/labkey-api-python/tree/master/samples)
316

417
# Installation
518
To install, simply use `pip`:
@@ -9,27 +22,71 @@ $ pip install labkey
922
```
1023

1124
# Credentials
12-
In order to the use the Python client API for LabKey Server, you will need to specify your login credentials in a credential file. The package assumes that this file will be located either:
25+
As of v0.4.0 this API no longer supports using a ``.labkeycredentials.txt`` file, and now uses the .netrc files similar to the other labkey APIs. Additional .netrc [setup instructions](https://www.labkey.org/wiki/home/Documentation/page.view?name=netrc) can be found at the link.
26+
27+
## Set Up a netrc File
28+
29+
On a Mac, UNIX, or Linux system the netrc file should be named ``.netrc`` (dot netrc) and on Windows it should be named ``_netrc`` (underscore netrc). The file should be located in your home directory and the permissions on the file must be set so that you are the only user who can read it, i.e. it is unreadable to everyone else.
1330

14-
1. ``$HOME/.labkeycredentials.txt``
15-
2. The location will be specified in the ``LABKEY_CREDENTIALS`` environment variable.
31+
To create the netrc on a Windows machine, first create an environment variable called ’HOME’ that is set to your home directory (for example, C:/Users/johndoe) or any directory you want to use.
32+
33+
In that directory, create a text file with the prefix appropriate to your system, either an underscore or dot.
34+
35+
The following three lines must be included in the file. The lines must be separated by either white space (spaces, tabs, or newlines) or commas:
36+
```
37+
machine <remote-instance-of-labkey-server>
38+
login <user-email>
39+
password <user-password>
40+
```
1641

17-
The ``labkeycredentials`` file must be in the following format. (3 separate lines):
42+
For example:
1843
```
19-
machine https://hosted.labkey.com
20-
login labkeypython@gmail.com
21-
password python
44+
machine mymachine.labkey.org
45+
login user@labkey.org
46+
password mypassword
2247
```
23-
where:
24-
- machine: URL of your LabKey Server
25-
- login: email address to be used to login to the LabKey Server
26-
- password: password associated with the login
48+
Note that the netrc file only deals with connections at the machine level and should not include a port or protocol designation, meaning both "mymachine.labkey.org:8888" and "https://mymachine.labkey.org" are incorrect.
2749

28-
A sample ``labkeycredentials`` file has been shipped with the source and named ``.labkeycredentials.sample``.
50+
# Supported Functions
51+
52+
- **labkey.query.select_rows()** - Query and get results sets from LabKey Server.
53+
- **labkey.query.execute_sql()** - Execute SQL (LabKey SQL dialect) through the query module on LabKey Server.
54+
- **labkey.query.insert_rows()** - Insert rows into a table on LabKey Server.
55+
- **labkey.query.update_rows()** - Update rows in a table on LabKey Server.
56+
- **labkey.query.delete_rows()** - Delete records in a table on LabKey Server.
57+
- **labkey.experiment.load_batch()** - Retreive assay data (batch level) from LabKey Server.
58+
- **labkey.experiment.save_batch()** - Save assay data (batch level) on LabKey Server.
59+
60+
# Examples
61+
62+
Sample code is available in the [samples](https://github.com/LabKey/labkey-api-python/tree/experiment/samples) directory.
63+
64+
The following gets data from the Users table on your local machine:
65+
66+
```python
67+
from labkey.utils import create_server_context
68+
from labkey.query import select_rows
69+
70+
print("Create a server context")
71+
labkey_server = 'localhost:8080'
72+
project_name = 'ModuleAssayTest' # Project folder name
73+
contextPath = 'labkey'
74+
schema = 'core'
75+
table = 'Users'
76+
77+
server_context = create_server_context(labkey_server, project_name, contextPath, use_ssl=False)
78+
79+
result = select_rows(server_context, schema, table)
80+
if result is not None:
81+
print(result['rows'][0])
82+
print("select_rows: Number of rows returned: " + str(result['rowCount']))
83+
else:
84+
print('select_rows: Failed to load results from ' + schema + '.' + table)
85+
```
2986

3087
# Supported Versions
31-
Python 2.6 or 2.7 are fully supported.
32-
LabKey Server v11.1 and later.
88+
Python 2.6+ and 3.4+ are fully supported.
89+
LabKey Server v13.3 and later.
3390

3491
# Contributing
3592
This library and the LabKey Server are maintained by the LabKey Software Foundation. If you have any questions or need support, please use the [LabKey Server support forum](https://www.labkey.org/wiki/home/page.view?name=support).

labkey/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
__title__ = 'labkey'
17-
__version__ = '0.3.0'
18-
__author__ = 'LabKey Software'
19-
__license__ = 'Apache 2.0'
16+
from labkey import query, experiment, utils # wiki, messageboard
17+
from pkg_resources import get_distribution
2018

21-
from labkey import query, wiki, messageboard
19+
__title__ = get_distribution('labkey').project_name
20+
__version__ = get_distribution('labkey').version
21+
__author__ = 'LabKey Software'
22+
__license__ = 'Apache License 2.0'

labkey/exceptions.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# Copyright (c) 2015 LabKey Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
from requests import exceptions
17+
18+
19+
# base exception class for server responses
20+
class RequestError(exceptions.HTTPError):
21+
default_msg = 'Server Error'
22+
23+
def __init__(self, server_response=None):
24+
if server_response is not None:
25+
try:
26+
decoded = server_response.json()
27+
if 'exception' in decoded:
28+
# use labkey server error message if available
29+
msg = decoded['exception']
30+
self.server_exception = decoded
31+
else:
32+
msg = self.default_msg
33+
except ValueError:
34+
# no valid json to decode
35+
raise ServerNotFoundError(server_response)
36+
37+
self.message = '{0}: {1}'.format(server_response.status_code, msg)
38+
39+
self.response = server_response
40+
41+
42+
class QueryNotFoundError(RequestError):
43+
default_msg = 'Query Resource Not Found'
44+
45+
46+
class RequestAuthorizationError(RequestError):
47+
default_msg = 'Authorization Failed'
48+
49+
50+
class ServerNotFoundError(RequestError):
51+
SERVER_NOT_FOUND_MSG = 'Server resource not found. Please verify context path and project path are valid'
52+
53+
def __init__(self, server_response=None):
54+
self.message = '{0}: {1}'.format(server_response.status_code, self.SERVER_NOT_FOUND_MSG)
55+
self.response = server_response
56+
57+
58+
class ServerContextError(exceptions.HTTPError):
59+
def __init__(self, inner_exception=None):
60+
self.message = self._get_message(inner_exception)
61+
self.exception = inner_exception
62+
63+
def _get_message(self, e):
64+
switcher = {
65+
exceptions.SSLError:
66+
'Failed to match server SSL configuration. Ensure the server_context is configured correctly.'
67+
}
68+
return switcher.get(type(e), 'Please verify server_context is configured correctly')

0 commit comments

Comments
 (0)