Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit fe19c56

Browse files
authored
Merge branch 'master' into pr-http-py3
2 parents 1bfc142 + 0ac8733 commit fe19c56

File tree

171 files changed

+25790
-893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+25790
-893
lines changed

.build-bot.json

2.31 KB
Binary file not shown.

.coveragerc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# .coveragerc to control coverage.py
2+
[run]
3+
4+
[report]
5+
include = */site-packages/google/datalab/*

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
*.pyi
33
*.map
44
*.egg-info
5+
*.iml
56
.idea
7+
.DS_Store
68
MANIFEST
79
build
810
.coverage
911
dist
1012
datalab.magics.rst
13+
datalab/notebook/static/*.js
14+
google/datalab/notebook/static/*.js
15+
16+
# Test files
17+
.tox/
18+
.cache/

.travis.yml

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
1-
sudo: required
2-
3-
dist: trusty
4-
51
language: python
6-
7-
python:
8-
- 2.7
9-
- 3.5
2+
dist: trusty
3+
sudo: false
4+
5+
matrix:
6+
include:
7+
- python: 2.7
8+
env: TOX_ENV=py27
9+
- python: 3.5
10+
env: TOX_ENV=py35
11+
- python: 2.7
12+
env: TOX_ENV=flake8
13+
- python: 2.7
14+
env: TOX_ENV=coveralls
1015

1116
before_install:
12-
- sudo apt-get install -y python-setuptools
1317
- npm install -g typescript
1418
- tsc --module amd --noImplicitAny --outdir datalab/notebook/static datalab/notebook/static/*.ts
15-
- pip install -U pip
16-
- pip install -U setuptools
17-
- if [[ $TRAVIS_PYTHON_VERSION == 2* ]]; then pip install -U google-cloud-dataflow==0.5.5; fi
18-
- pip install .
19-
- pip install solutionbox/structured_data/
20-
- pip install solutionbox/image_classification/
21-
- pip install -U tensorflow==1.0.0
22-
- pip install -U protobuf==3.1.0 #v3.2.0 has the "python: double free or corruption" bug.
23-
- pip install flake8
24-
25-
before_script:
26-
- flake8 .
27-
19+
# We use tox for actually running tests.
20+
- pip install --upgrade pip tox
21+
2822
script:
29-
- python ./tests/main.py
30-
- python ./legacy_tests/main.py
31-
23+
# tox reads its configuration from tox.ini.
24+
- tox -e $TOX_ENV
3225

CONTRIBUTING.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ frustration later on.
2020
All submissions, including submissions by project members, require review. We
2121
use Github pull requests for this purpose.
2222

23+
### Running tests
24+
We use [`tox`](https://tox.readthedocs.io/) for running our tests. To run tests
25+
before sending out a pull request, just
26+
[install tox](https://http://tox.readthedocs.io/en/latest/install.html) and run
27+
28+
```shell
29+
$ tox
30+
```
31+
32+
to run tests under all supported environments. (This will skip any environments
33+
for which no interpreter is available.) `tox -l` will provide a list of all
34+
supported environments.
35+
36+
`tox` will run all tests referenced by `tests/main.py` and
37+
`legacy_tests/main.py`.
38+
2339
### The small print
2440
Contributions made by corporations are covered by a different agreement than
2541
the one above, the

README.md

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,80 @@
1-
# datalab
1+
# datalab [![Build Status](https://travis-ci.org/googledatalab/pydatalab.svg?branch=master)](https://travis-ci.org/googledatalab/pydatalab) [![PyPI Package](https://img.shields.io/pypi/v/datalab.svg)](https://pypi.python.org/pypi/datalab)
22

3-
Google datalab Python package. Used in [Google Cloud Datalab]
4-
(https://github.com/GoogleCloudPlatform/datalab) and can be used
5-
in [Jupyter Notebook](http://jupyter.org/).
3+
[Google Cloud Datalab](https://cloud.google.com/datalab/) Python package. Used in
4+
[Google Cloud Datalab](https://github.com/GoogleCloudPlatform/datalab) and can
5+
be used in [Jupyter Notebook](http://jupyter.org/).
66

7-
This adds a number of Python modules such as datalab.bigquery,
8-
datalab.storage, etc, for accessing [Google Cloud Platform services]
9-
(https://cloud.google.com/) as well as adding some new cell magics such as `%chart`,
10-
`%bigquery`, `%storage`, etc.
7+
This adds a number of Python modules such as `google.datalab.bigquery`,
8+
`google.datalab.storage`, etc, for accessing
9+
[Google Cloud Platform services](https://cloud.google.com/) as well as adding
10+
some new cell magics such as `%chart`, `%bigquery`, `%storage`, etc.
1111

12-
See [https://github.com/googledatalab/notebooks](https://github.com/googledatalab/notebooks)
12+
See
13+
[https://github.com/googledatalab/notebooks](https://github.com/googledatalab/notebooks)
1314
for samples of using this package.
1415

15-
## Prerequisites
16-
17-
You will need the [Typscript compiler](https://www.typescriptlang.org/) installed.
18-
In future we should be installable from PyPI.
19-
2016
## Installation
2117

22-
First:
18+
This package is available on PyPI as `datalab`:
2319

24-
git clone https://github.com/googledatalab/pydatalab.git
25-
cd pydatalab
20+
pip install datalab
2621

27-
Then do one of the folowing:
22+
## Using in Jupyter
2823

29-
./install-virtualenv.sh # For use in Python virtual environments
30-
./install-no-virtualenv.sh # For installing in a non-virtual environment
24+
After datalab installation, enable datalab's frontend in Jupyter by running:
3125

32-
You can ignore the message about running `jupyter nbextension enable`; it is not required.
26+
jupyter nbextension install --py datalab.notebook --sys-prefix
3327

34-
## Using in Jupyter
28+
See further details [Jupyter Kernel and Notebook Extensions](https://github.com/googledatalab/pydatalab/wiki/Jupyter-Kernel-and-Notebook-Extensions).
3529

36-
In a notebook cell, enable with:
30+
Then in a notebook cell, enable datalab's magics with:
3731

38-
%load_ext datalab.kernel
32+
%load_ext google.datalab.kernel
33+
34+
(Note: If you hit an error "module traceback cannot be imported", try setting the following environment variable: CLOUDSDK_PYTHON_SITEPACKAGES=1)
3935

4036
Alternatively add this to your `ipython_config.py` file in your profile:
4137

4238
c = get_config()
4339
c.InteractiveShellApp.extensions = [
44-
'datalab.kernel'
40+
'google.datalab.kernel'
4541
]
4642

47-
You will typically put this under `~/.ipython/profile_default`.
48-
See [http://ipython.readthedocs.io/en/stable/development/config.html]
49-
(http://ipython.readthedocs.io/en/stable/development/config.html)
43+
You will typically put this under `~/.ipython/profile_default`. See
44+
[the IPython docs](http://ipython.readthedocs.io/en/stable/development/config.html)
5045
for more about IPython profiles.
5146

52-
If you want to access Google Cloud Platform services such as BigQuery,
53-
you will also need to install [gcloud]
54-
(https://cloud.google.com/sdk/gcloud). You will need to use gcloud
55-
to authenticate; e.g. with:
47+
If you want to access Google Cloud Platform services such as BigQuery, you
48+
will also need to install [gcloud](https://cloud.google.com/sdk/gcloud). You
49+
will need to use `gcloud` to authenticate; e.g. with:
5650

5751
gcloud auth login
5852

5953
You will also need to set the project ID to use; either set a `PROJECT_ID`
60-
environment variable to the project name, or call `set_datalab_project_id(name)`
61-
from within your notebook.
54+
environment variable to the project name, or call
55+
`set_datalab_project_id(name)` from within your notebook.
6256

6357
## Documentation
64-
You can read the Sphinx generated docs at: [http://googledatalab.github.io/pydatalab/](http://googledatalab.github.io/pydatalab/)
58+
59+
You can read the Sphinx generated docs at:
60+
[http://googledatalab.github.io/pydatalab/](http://googledatalab.github.io/pydatalab/)
61+
62+
## Development installation
63+
64+
If you'd like to work on the package, it's useful to be able to install from
65+
source. You will need the
66+
[Typescript compiler](https://www.typescriptlang.org/) installed.
67+
68+
First:
69+
70+
git clone https://github.com/googledatalab/pydatalab.git
71+
cd pydatalab
72+
73+
Then do one of the folowing:
74+
75+
./install-virtualenv.sh # For use in Python virtual environments
76+
./install-no-virtualenv.sh # For installing in a non-virtual environment
77+
78+
You can ignore the message about running `jupyter nbextension enable`; it is
79+
not required.
80+

datalab/context/_utils.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
from __future__ import absolute_import
1818
from __future__ import unicode_literals
19-
import oauth2client.client
2019
import json
2120
import os
2221
import subprocess
2322

23+
import oauth2client.client
24+
import google.auth
25+
import google.auth.exceptions
26+
import google.auth.credentials
27+
import google.auth._oauth2client
28+
2429

2530
# TODO(ojarjur): This limits the APIs against which Datalab can be called
2631
# (when using a service account with a credentials file) to only being those
@@ -51,6 +56,19 @@ def get_config_dir():
5156
return config_dir
5257

5358

59+
def _convert_oauth2client_creds(credentials):
60+
new_credentials = google.oauth2.credentials.Credentials(
61+
token=credentials.access_token,
62+
refresh_token=credentials.refresh_token,
63+
token_uri=credentials.token_uri,
64+
client_id=credentials.client_id,
65+
client_secret=credentials.client_secret,
66+
scopes=credentials.scopes)
67+
68+
new_credentials._expires = credentials.token_expiry
69+
return new_credentials
70+
71+
5472
def get_credentials():
5573
""" Get the credentials to use. We try application credentials first, followed by
5674
user credentials. The path to the application credentials can be overridden
@@ -61,9 +79,8 @@ def get_credentials():
6179
overriding these the defaults should suffice.
6280
"""
6381
try:
64-
credentials = oauth2client.client.GoogleCredentials.get_application_default()
65-
if credentials.create_scoped_required():
66-
credentials = credentials.create_scoped(CREDENTIAL_SCOPES)
82+
credentials, _ = google.auth.default()
83+
credentials = google.auth.credentials.with_scopes_if_required(credentials, CREDENTIAL_SCOPES)
6784
return credentials
6885
except Exception as e:
6986

@@ -75,9 +92,10 @@ def get_credentials():
7592
# Use the first gcloud one we find
7693
for entry in creds['data']:
7794
if entry['key']['type'] == 'google-cloud-sdk':
78-
return oauth2client.client.OAuth2Credentials.from_json(json.dumps(entry['credential']))
95+
creds = oauth2client.client.OAuth2Credentials.from_json(json.dumps(entry['credential']))
96+
return _convert_oauth2client_creds(creds)
7997

80-
if type(e) == oauth2client.client.ApplicationDefaultCredentialsError:
98+
if type(e) == google.auth.exceptions.DefaultCredentialsError:
8199
# If we are in Datalab container, change the message to be about signing in.
82100
if _in_datalab_docker():
83101
raise Exception('No application credentials found. Perhaps you should sign in.')

datalab/stackdriver/monitoring/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ def make_client(project_id=None, context=None):
2626
project=project_id,
2727
credentials=context.credentials,
2828
)
29-
client._connection_class.USER_AGENT = 'pydatalab/v0'
29+
client._connection.USER_AGENT = 'pydatalab/v0'
3030
return client

datalab/utils/_http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import urllib.parse
2727
import urllib.error
2828
import httplib2
29+
import google_auth_httplib2
2930
import logging
3031

3132

@@ -132,7 +133,7 @@ def request(url, args=None, data=None, headers=None, method=None,
132133
if credentials is not None:
133134
# Make a copy of the shared http instance before we modify it.
134135
http = copy.copy(http)
135-
http = credentials.authorize(http)
136+
http = google_auth_httplib2.AuthorizedHttp(credentials)
136137
if stats is not None:
137138
stats['duration'] = datetime.datetime.utcnow()
138139

google/datalab/_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, project_id, credentials, config=None):
3636
"""
3737
self._project_id = project_id
3838
self._credentials = credentials
39-
self._config = config or Context._get_default_config()
39+
self._config = config if config is not None else Context._get_default_config()
4040

4141
@property
4242
def credentials(self):

0 commit comments

Comments
 (0)