Mercurial > repos > stevecassidy > alveoimport
annotate util.py @ 18:5e1b7d922ea3 draft default tip
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
author | stevecassidy |
---|---|
date | Mon, 15 Jan 2018 18:34:57 -0500 |
parents | a38315ecf593 |
children |
rev | line source |
---|---|
14
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
1 """ |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
2 Utility functions and settings for API tools |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
3 """ |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
4 import pyalveo |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
5 import csv |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
6 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
7 # this should be in a config file |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
8 API_URL = 'https://app.alveo.edu.au' |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
9 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
10 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
11 def write_key(api_key, output_path, client_module=pyalveo): |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
12 """Tests whether an API key is valid and writes it to a file. |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
13 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
14 :type api_key: String |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
15 :param api_key: Alveo API key |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
16 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
17 :type output_path: String |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
18 :param output_path: Path to the file to store the API key in |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
19 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
20 :type client_module: pyalveo.Client |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
21 :param client_module: Module providing the client (used for testing purposes), |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
22 defaults to pyalveo |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
23 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
24 :raises: pyalveo.APIError if the API request is not successful |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
25 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
26 """ |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
27 # validate the client key, raises an exception if it is not valid |
18
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
14
diff
changeset
|
28 client_module.Client(api_key=api_key, api_url=API_URL, use_cache=False) |
14
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
29 outfile = open(output_path, 'w') |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
30 outfile.write(api_key) |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
31 outfile.close() |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
32 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
33 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
34 def get_item_lists(api_key): |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
35 client = pyalveo.Client(api_key=api_key, api_url=API_URL, use_cache=False) |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
36 return client.get_item_lists() |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
37 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
38 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
39 def read_item_list(filename, client): |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
40 """Read an item list from a file |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
41 which should be a tabular formatted file |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
42 with one column header ItemURL. |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
43 Return an instance of ItemGroup""" |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
44 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
45 with open(filename) as fd: |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
46 csvreader = csv.DictReader(fd, dialect='excel-tab') |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
47 if 'ItemURL' not in csvreader.fieldnames: |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
48 return None |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
49 itemurls = [] |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
50 for row in csvreader: |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
51 itemurls.append(row['ItemURL']) |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
52 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
53 itemlist = pyalveo.ItemGroup(itemurls, client) |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
54 |
a38315ecf593
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit b5b26e9118f2ad8af109d606746b39a5588f0511-dirty
stevecassidy
parents:
diff
changeset
|
55 return itemlist |
18
5e1b7d922ea3
planemo upload for repository https://github.com/Alveo/alveo-galaxy-tools commit 53cde5cc9b8c1adcccdc3cfa52d8ca82079aeda7
stevecassidy
parents:
14
diff
changeset
|
56 |