Skip to content

Commit

Permalink
Black + various pre-commit hooks (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch authored Aug 13, 2019
1 parent 34be613 commit 80cf6fe
Show file tree
Hide file tree
Showing 16 changed files with 702 additions and 570 deletions.
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-json
- id: check-added-large-files
- id: check-yaml
- id: debug-statements

- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.1
hooks:
- id: flake8
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,12 @@ TABLES
> BYE;
GoodBye!
```

# Contributing

Contributions are welcomed of course. We like to use `black` and `flake8`.

```bash
pip install -r requirements-dev.txt # installs useful dev deps
pre-commit install # installs useful commit hooks
```
14 changes: 9 additions & 5 deletions pydruid/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from tornado import gen
from tornado.httpclient import AsyncHTTPClient, HTTPError
except ImportError:
print('Warning: unable to import Tornado. The asynchronous client will not work.')
print("Warning: unable to import Tornado. The asynchronous client will not work.")


class AsyncPyDruid(BaseDruidClient):
Expand Down Expand Up @@ -110,7 +110,8 @@ def _post(self, query):
try:
headers, querystr, url = self._prepare_url_headers_and_body(query)
response = yield http_client.fetch(
url, method='POST', headers=headers, body=querystr)
url, method="POST", headers=headers, body=querystr
)
except HTTPError as e:
self.__handle_http_error(e, query)
else:
Expand All @@ -127,9 +128,12 @@ def __handle_http_error(e, query):
except ValueError:
pass
else:
err = err.get('error', None)
raise IOError('{0} \n Druid Error: {1} \n Query is: {2}'.format(
e, err, json.dumps(query.query_dict, indent=4)))
err = err.get("error", None)
raise IOError(
"{0} \n Druid Error: {1} \n Query is: {2}".format(
e, err, json.dumps(query.query_dict, indent=4)
)
)

@gen.coroutine
def topn(self, **kwargs):
Expand Down
33 changes: 21 additions & 12 deletions pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


# extract error from the <PRE> tag inside the HTML response
HTML_ERROR = re.compile('<pre>\s*(.*?)\s*</pre>', re.IGNORECASE)
HTML_ERROR = re.compile("<pre>\\s*(.*?)\\s*</pre>", re.IGNORECASE)


class BaseDruidClient(object):
Expand All @@ -49,16 +49,16 @@ def set_proxies(self, proxies):
urllib.request.install_opener(opener)

def _prepare_url_headers_and_body(self, query):
querystr = json.dumps(query.query_dict).encode('utf-8')
if self.url.endswith('/'):
querystr = json.dumps(query.query_dict).encode("utf-8")
if self.url.endswith("/"):
url = self.url + self.endpoint
else:
url = self.url + '/' + self.endpoint
headers = {'Content-Type': 'application/json'}
url = self.url + "/" + self.endpoint
headers = {"Content-Type": "application/json"}
if (self.username is not None) and (self.password is not None):
authstring = '{}:{}'.format(self.username, self.password)
authstring = "{}:{}".format(self.username, self.password)
b64string = b64encode(authstring.encode()).decode()
headers['Authorization'] = 'Basic {}'.format(b64string)
headers["Authorization"] = "Basic {}".format(b64string)

return headers, querystr, url

Expand Down Expand Up @@ -449,7 +449,8 @@ def export_tsv(self, dest_path):
"""
if self.query_builder.last_query is None:
raise AttributeError(
"There was no query executed by this client yet. Can't export!")
"There was no query executed by this client yet. Can't export!"
)
else:
return self.query_builder.last_query.export_tsv(dest_path)

Expand All @@ -462,7 +463,8 @@ def export_pandas(self):
"""
if self.query_builder.last_query is None:
raise AttributeError(
"There was no query executed by this client yet. Can't export!")
"There was no query executed by this client yet. Can't export!"
)
else:
return self.query_builder.last_query.export_pandas()

Expand Down Expand Up @@ -539,6 +541,7 @@ class PyDruid(BaseDruidClient):
0 7 2013-10-04T00:00:00.000Z user_1
1 6 2013-10-04T00:00:00.000Z user_2
"""

def __init__(self, url, endpoint):
super(PyDruid, self).__init__(url, endpoint)

Expand All @@ -561,12 +564,18 @@ def _post(self, query):
except (ValueError, AttributeError, KeyError):
pass

raise IOError('{0} \n Druid Error: {1} \n Query is: {2}'.format(
e, err, json.dumps(
raise IOError(
"{0} \n Druid Error: {1} \n Query is: {2}".format(
e,
err,
json.dumps(
query.query_dict,
indent=4,
sort_keys=True,
separators=(',', ': '))))
separators=(",", ": "),
),
)
)
else:
query.parse(data)
return query
Expand Down
Loading

0 comments on commit 80cf6fe

Please sign in to comment.