Skip to content

Commit e713e2f

Browse files
committed
Make unsupported separate python module
- change version recognition to allow local import - wiki, messageboard changes: PEP 8 standards, switch to using server_context session rather than requests directly, change documentation to match current python pattern
1 parent 4ac02bd commit e713e2f

File tree

5 files changed

+74
-158
lines changed

5 files changed

+74
-158
lines changed

labkey/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from labkey import query, experiment, utils
17-
import labkey.unsupported.wiki, labkey.unsupported.messageboard
18-
from pkg_resources import get_distribution
1917

20-
__title__ = get_distribution('labkey').project_name
21-
__version__ = get_distribution('labkey').version
18+
__title__ = 'labkey'
19+
__version__ = '0.4.2'
2220
__author__ = 'LabKey'
2321
__license__ = 'Apache License 2.0'

labkey/unsupported/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# No content required

labkey/unsupported/messageboard.py

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,88 +14,43 @@
1414
# limitations under the License.
1515
#
1616
"""
17+
unsupported.messageboard
18+
~~~~~~~~~~~~~~~~
1719
WARNING: This module is not officially supported! Use at your own risk.
1820
19-
############################################################################
20-
NAME:
21-
LabKey Message Board API
22-
23-
SUMMARY:
2421
This module provides functions for interacting with Message Boards on the
2522
LabKey Server.
26-
27-
DESCRIPTION:
28-
This module is designed to simply programmatic editing of wikis and posting messages
29-
to Message Boards/Forums on the LabKey Server
30-
31-
Documentation:
32-
LabKey Python API:
33-
https://www.labkey.org/wiki/home/Documentation/page.view?name=python
34-
35-
Setup, configuration of the LabKey Python API:
36-
https://www.labkey.org/wiki/home/Documentation/page.view?name=setupPython
37-
38-
Using the LabKey Python API:
39-
https://www.labkey.org/wiki/home/Documentation/page.view?name=usingPython
40-
41-
Documentation for the LabKey client APIs:
42-
https://www.labkey.org/wiki/home/Documentation/page.view?name=viewAPIs
43-
44-
Support questions should be directed to the LabKey forum:
45-
https://www.labkey.org/announcements/home/Server/Forum/list.view?
46-
47-
48-
############################################################################
4923
"""
50-
51-
from labkey.utils import build_url, handle_response
52-
import requests
5324
from requests.exceptions import SSLError
25+
from labkey.utils import build_url
5426

5527

56-
def postMessage(server_context, messageTitle, messageBody, renderAs, container_path=None):
28+
def post_message(server_context, message_title, message_body, render_as, container_path=None):
5729
"""
58-
############################################################################
59-
postMessage()
60-
61-
postMessage() can be used to post a message to a message board on the LabKey Server
62-
63-
The following are the minimum required params:
64-
65-
myresults = labkeyApi.postMessage(
66-
baseUrl = 'https://hosted.labkey.com',
67-
containerPath = 'PythonProject',
68-
messageTitle = 'Message Title',
69-
messageBody = 'This is the content of my message ....',
70-
renderAs = 'HTML')
71-
72-
The function will return the integer 1 for success and the integer 0 if the message post fails
73-
74-
----------------------------------------------------------------------------
75-
Test Code:
76-
77-
[NOT AVAILABLE]
78-
79-
############################################################################
30+
Post a message to a message board on a LabKey instance.
31+
:param server_context: A LabKey server context. See utils.create_server_context.
32+
:param message_title: The title of the message.
33+
:param message_body: The content of the message.
34+
:param render_as: The content of the message.
35+
:param container_path: Optional container path that can be used to override the server_context container path
36+
:return: Returns 1 if successful, 0 is post failed.
8037
"""
81-
8238
# Build the URL for querying LabKey Server
8339
message_url = build_url(server_context, 'announcements', 'insert.api', container_path=container_path)
8440

8541
message_data = {
86-
'title': messageTitle,
87-
'body': messageBody,
88-
'rendererType': renderAs
42+
'title': message_title,
43+
'body': message_body,
44+
'rendererType': render_as
8945
}
9046

9147
session = server_context['session']
92-
data = None
9348

9449
try:
95-
message_response = requests.post(message_url, message_data, headers=None) # seems to be happy with Python dict directly
50+
message_response = session.post(message_url, message_data)
9651
except SSLError as e:
9752
print("There was problem while attempting to submit the message to " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode()))
98-
print("The HTTP client error was: "+ format(e))
99-
return(0)
53+
print("The HTTP client error was: " + format(e))
54+
return 0
10055

101-
return(1)
56+
return 1

labkey/unsupported/wiki.py

Lines changed: 44 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -14,143 +14,97 @@
1414
# limitations under the License.
1515
#
1616
"""
17+
unsupported.wiki
18+
~~~~~~~~~~~~~~~~
1719
WARNING: This module is not officially supported! Use at your own risk.
1820
19-
############################################################################
20-
NAME:
21-
LabKey Collaboration API
22-
23-
SUMMARY:
24-
This module provides functions for interacting with Wikis on LabKey Server.
25-
26-
DESCRIPTION:
27-
This module is designed to simply programmatic editing of wikis on the LabKey Server
28-
29-
Documentation:
30-
LabKey Python API:
31-
https://www.labkey.org/wiki/home/Documentation/page.view?name=python
32-
33-
Setup, configuration of the LabKey Python API:
34-
https://www.labkey.org/wiki/home/Documentation/page.view?name=setupPython
35-
36-
Using the LabKey Python API:
37-
https://www.labkey.org/wiki/home/Documentation/page.view?name=usingPython
38-
39-
Documentation for the LabKey client APIs:
40-
https://www.labkey.org/wiki/home/Documentation/page.view?name=viewAPIs
41-
42-
Support questions should be directed to the LabKey forum:
43-
https://www.labkey.org/announcements/home/Server/Forum/list.view?
44-
45-
############################################################################
21+
This module provides functions for interacting with Wiki's on the
22+
LabKey Server.
4623
"""
47-
4824
import json
4925
from labkey.utils import build_url, handle_response
50-
import requests
5126
from requests.exceptions import SSLError
5227

53-
def updateWiki(server_context, wikiName, wikiBody, container_path=None):
54-
"""
55-
############################################################################
56-
updateWiki()
57-
58-
updateWiki() can be used to update an existing wiki page
59-
60-
The following are the minimum required params:
61-
62-
myresults = labkeyApi.updateWiki(
63-
baseUrl = 'https://hosted.labkey.com',
64-
containerPath = 'PythonProject',
65-
wikiName = 'MyWiki',
66-
wikiBody = 'New Content for my wiki')
6728

68-
The following are optional:
69-
70-
debug = True #will result in a more verbose output
71-
72-
73-
This API does not support the ability to change the Render Type for the wiki to be updated.
74-
75-
This API returns a dictionary containing the response from the server. The 'success' key
76-
in the dictionary will be true when the wiki was successfully updated. It will be false
77-
in the case of a failure. In the case of a failure, the 'error' key contains the error
78-
message returned by the server.
79-
80-
----------------------------------------------------------------------------
81-
Test Code:
82-
83-
[NOT AVAILABLE]
84-
85-
############################################################################
86-
"""
87-
29+
def update_wiki(server_context, wiki_name, wiki_body, container_path=None):
30+
"""
31+
Used to update an existing wiki page
32+
:param server_context: A LabKey server context. See labkey.utils.create_server_context.
33+
:param wiki_name: The name of the wiki.
34+
:param wiki_body: The body of the wiki.
35+
:param container_path: Optional container path that can be used to override the server_context container path
36+
:return: returns a dictionary containing the response from the server. The 'success' key
37+
in the dictionary will be true when the wiki was successfully updated. It will be false
38+
in the case of a failure. In the case of a failure, the 'error' key contains the error
39+
message returned by the server.
40+
"""
8841
# Build the URL for reading the wiki page
8942
read_wiki_url = build_url(server_context, 'wiki', 'editWiki.api', container_path=container_path)
90-
payload = {'name': wikiName}
43+
payload = {
44+
'name': wiki_name
45+
}
9146
headers = {
9247
'Content-type': 'application/json'
9348
}
9449

95-
data = None
50+
session = server_context['session']
9651

9752
try:
98-
read_response = requests.get(read_wiki_url, params=payload, headers=headers) # editWiki action only takes URL parameters, not JSON (JSON is not bound to form)
53+
read_response = session.get(read_wiki_url, params=payload, headers=headers)
9954
except SSLError as e:
100-
print("There was a problem while attempting to submit the read for the wiki page " + str(wikiName) + " via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode()))
101-
print("The HTTP client error was: "+ format(e))
102-
return(1) # TODO: this is incorrect, should return 'success'/'error' properly like the docs say
55+
print("There was a problem while attempting to submit the read for the wiki page " + str(wiki_name) + " via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode()))
56+
print("The HTTP client error was: " + format(e))
57+
return 1 # TODO: this is incorrect, should return 'success'/'error' properly like the docs say
10358

10459
data = read_response.text
10560

10661
# Search HTML response for required information on wiki. This is stored in the javascript
10762
# variable named
10863
# - _wikiProps: for 14.3 and earlier
10964
# - LABKEY._wiki.setProps for 15.1 and later
110-
dataList = data.split('\n')
65+
data_list = data.split('\n')
11166

11267
# If LabKey Server is v14.3 or earlier find line containing '_wikiProp'
113-
v = next((i for i in range(len(dataList)) if '_wikiProp' in dataList[i]), None)
68+
v = next((i for i in range(len(data_list)) if '_wikiProp' in data_list[i]), None)
11469

11570
# If v = None, then server is running 15.1 or later and find the line
11671
# containing 'LABKEY._wiki.setProps'
117-
if v == None:
118-
v = next((i for i in range(len(dataList)) if 'LABKEY._wiki.setProps' in dataList[i]), None)
72+
if v is None:
73+
v = next((i for i in range(len(data_list)) if 'LABKEY._wiki.setProps' in data_list[i]), None)
11974

12075
# Verify that we found the variable in the HTML response. If not
12176
# do not proceed
122-
if v == None:
123-
print("There was a problem while attempting to read the data for the wiki page '" + str(wikiName) + "'.")
77+
if v is None:
78+
print("There was a problem while attempting to read the data for the wiki page '" + str(wiki_name) + "'.")
12479
print("The script is unable to find the wiki properties in the HTML response")
125-
return(1) # TODO: this is incorrect, should return 'success'/'error' properly like the docs say
80+
return 1 # TODO: this is incorrect, should return 'success'/'error' properly like the docs say
12681

127-
wikiVars = {}
82+
wiki_vars = {}
12883
for j in range(100):
12984
# Read each line, until find a javascript closing bracket.
130-
if '};' in dataList[v+j+1]:
85+
if '};' in data_list[v+j+1]:
13186
break
132-
if '});' in dataList[v+j+1]:
87+
if '});' in data_list[v+j+1]:
13388
break
134-
wvar = dataList[v+j+1].rstrip().lstrip().replace('\'','').replace(',','')
135-
wikiVars[wvar.split(':')[0]] = wvar.split(':')[1]
89+
wvar = data_list[v+j+1].rstrip().lstrip().replace('\'', '').replace(',', '')
90+
wiki_vars[wvar.split(':')[0]] = wvar.split(':')[1]
13691

13792
# Build the URL for updating the wiki page
13893
update_wiki_url = build_url(server_context, 'wiki', 'saveWiki.api', container_path=container_path)
13994
headers = {
14095
'Content-type': 'application/json'
14196
}
142-
data = None
14397

144-
# Update wikiVars to use the new wiki content.
145-
wikiVars['name'] = wikiName
146-
wikiVars['body'] = wikiBody
98+
# Update wiki_vars to use the new wiki content.
99+
wiki_vars['name'] = wiki_name
100+
wiki_vars['body'] = wiki_body
147101

148102
try:
149-
response = requests.post(update_wiki_url, data=json.dumps(wikiVars, sort_keys=True), headers=headers)
103+
response = session.post(update_wiki_url, data=json.dumps(wiki_vars, sort_keys=True), headers=headers)
150104
data = handle_response(response)
151105
except SSLError as e:
152-
print("There was a problem while attempting to submit the read for the wiki page '" + str(wikiName) + "' via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode()))
153-
print("The HTTP client error was: "+ format(e))
154-
return(1) # TODO: this is incorrect, should return 'success'/'error' properly like the docs say
106+
print("There was a problem while attempting to submit the read for the wiki page '" + str(wiki_name) + "' via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode()))
107+
print("The HTTP client error was: " + format(e))
108+
return 1 # TODO: this is incorrect, should return 'success'/'error' properly like the docs say
155109

156-
return(data)
110+
return data

setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@
1919
Also installs included versions of third party libraries, if those libraries
2020
are not already installed.
2121
"""
22+
import re
2223
from setuptools import setup
2324

2425
packages = [
2526
'labkey'
2627
]
2728

29+
with open('labkey/__init__.py', 'r') as fd:
30+
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
31+
fd.read(), re.MULTILINE).group(1)
32+
33+
if not version:
34+
raise RuntimeError('Cannot find version information')
35+
2836
long_desc = "Python client API for LabKey Server. Supports query and experiment APIs."
2937

3038
setup(
3139
name='labkey',
32-
version='0.4.2',
40+
version=version,
3341
description='Python client API for LabKey Server',
3442
long_description=long_desc,
3543
license="Apache License 2.0",

0 commit comments

Comments
 (0)