|
| 1 | +import os |
| 2 | +import requests |
| 3 | +import arff |
| 4 | + |
| 5 | +from . import config |
| 6 | + |
| 7 | + |
| 8 | +""" |
| 9 | +Provides an interface to the OpenML server. |
| 10 | +
|
| 11 | +All parameters of the APIConnector can be either specified in a config |
| 12 | +file or when creating this object. The config file must be placed in a |
| 13 | +directory ``.openml`` inside the users home directory and have the name |
| 14 | +``config``. If one of the parameters is specified by passing it to the |
| 15 | +constructor of this class, it will override the value specified in the |
| 16 | +configuration file. |
| 17 | +
|
| 18 | +Parameters |
| 19 | +---------- |
| 20 | +cache_directory : string, optional (default=None) |
| 21 | + A local directory which will be used for caching. If this is not set, a |
| 22 | + directory '.openml/cache' in the users home directory will be used. |
| 23 | + If either directory does not exist, it will be created. |
| 24 | +
|
| 25 | +apikey : string, optional (default=None) |
| 26 | + Your OpenML API key which will be used to authenticate you at the OpenML |
| 27 | + server. |
| 28 | +
|
| 29 | +server : string, optional (default=None) |
| 30 | + The OpenML server to connect to. |
| 31 | +
|
| 32 | +verbosity : int, optional (default=None) |
| 33 | +
|
| 34 | +configure_logger : bool (default=True) |
| 35 | + Whether the python logging module should be configured by the openml |
| 36 | + package. If set to true, this is a very basic configuration, |
| 37 | + which only prints to the standard output. This is only recommended |
| 38 | + for testing or small problems. It is set to True to adhere to the |
| 39 | + `specifications of the OpenML client API |
| 40 | + <https://github.com/openml/OpenML/wiki/Client-API>`_. |
| 41 | + When the openml module is used as a library, it is recommended that |
| 42 | + the main application controls the logging level, e.g. see |
| 43 | + `here <http://pieces.openpolitics.com |
| 44 | + /2012/04/python-logging-best-practices/>`_. |
| 45 | +
|
| 46 | +private_directory : str, optional (default=None) |
| 47 | + A local directory which can be accessed through the OpenML package. |
| 48 | + Useful to access private datasets through the same interface. |
| 49 | +
|
| 50 | +Raises |
| 51 | +------ |
| 52 | +ValueError |
| 53 | + If apikey is neither specified in the config nor given as an argument. |
| 54 | +OpenMLServerError |
| 55 | + If the OpenML server returns an unexptected response. |
| 56 | +
|
| 57 | +Notes |
| 58 | +----- |
| 59 | +Testing the API calls in Firefox is possible with the Firefox AddOn |
| 60 | +HTTPRequestor. |
| 61 | +
|
| 62 | +""" |
| 63 | + |
| 64 | + |
| 65 | +def _perform_api_call(call, data=None, file_dictionary=None, |
| 66 | + file_elements=None, add_authentication=True): |
| 67 | + """ |
| 68 | + Perform an API call at the OpenML server. |
| 69 | + return self._read_url(url, data=data, filePath=filePath, |
| 70 | + def _read_url(self, url, add_authentication=False, data=None, filePath=None): |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + call : str |
| 75 | + The API call. For example data/list |
| 76 | + data : dict (default=None) |
| 77 | + Dictionary containing data which will be sent to the OpenML |
| 78 | + server via a POST request. |
| 79 | + **kwargs |
| 80 | + Further arguments which are appended as GET arguments. |
| 81 | +
|
| 82 | + Returns |
| 83 | + ------- |
| 84 | + return_code : int |
| 85 | + HTTP return code |
| 86 | + return_value : str |
| 87 | + Return value of the OpenML server |
| 88 | + """ |
| 89 | + url = config.server |
| 90 | + if not url.endswith("/"): |
| 91 | + url += "/" |
| 92 | + url += call |
| 93 | + if file_dictionary is not None or file_elements is not None: |
| 94 | + return _read_url_files(url, data=data, file_dictionary=file_dictionary, |
| 95 | + file_elements=file_elements) |
| 96 | + return _read_url(url, data=data) |
| 97 | + |
| 98 | + |
| 99 | +def _read_url_files(url, data=None, file_dictionary=None, file_elements=None): |
| 100 | + """do a post request to url with data None, file content of |
| 101 | + file_dictionary and sending file_elements as files""" |
| 102 | + if data is None: |
| 103 | + data = {} |
| 104 | + data['api_key'] = config.apikey |
| 105 | + if file_elements is None: |
| 106 | + file_elements = {} |
| 107 | + if file_dictionary is not None: |
| 108 | + for key, path in file_dictionary.items(): |
| 109 | + path = os.path.abspath(path) |
| 110 | + if os.path.exists(path): |
| 111 | + try: |
| 112 | + if key is 'dataset': |
| 113 | + # check if arff is valid? |
| 114 | + decoder = arff.ArffDecoder() |
| 115 | + with open(path) as fh: |
| 116 | + decoder.decode(fh, encode_nominal=True) |
| 117 | + except: |
| 118 | + raise ValueError("The file you have provided is not a valid arff file") |
| 119 | + |
| 120 | + file_elements[key] = open(path, 'rb') |
| 121 | + |
| 122 | + else: |
| 123 | + raise ValueError("File doesn't exist") |
| 124 | + response = requests.post(url, data=data, files=file_elements) |
| 125 | + return response.status_code, response.text |
| 126 | + |
| 127 | + |
| 128 | +def _read_url(url, data=None): |
| 129 | + if data is None: |
| 130 | + data = {} |
| 131 | + data['api_key'] = config.apikey |
| 132 | + |
| 133 | + response = requests.post(url, data=data) |
| 134 | + return response.status_code, response.text |
0 commit comments