|
| 1 | +# Copyright (c) 2025 pandas-gbq Authors All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style |
| 3 | +# license that can be found in the LICENSE file. |
| 4 | + |
| 5 | + |
| 6 | +class Context(object): |
| 7 | + """Storage for objects to be used throughout a session. |
| 8 | +
|
| 9 | + A Context object is initialized when the ``pandas_gbq`` module is |
| 10 | + imported, and can be found at :attr:`pandas_gbq.context`. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self._credentials = None |
| 15 | + self._project = None |
| 16 | + # dialect defaults to None so that read_gbq can stop warning if set. |
| 17 | + self._dialect = None |
| 18 | + |
| 19 | + @property |
| 20 | + def credentials(self): |
| 21 | + """ |
| 22 | + Credentials to use for Google APIs. |
| 23 | +
|
| 24 | + These credentials are automatically cached in memory by calls to |
| 25 | + :func:`pandas_gbq.read_gbq` and :func:`pandas_gbq.to_gbq`. To |
| 26 | + manually set the credentials, construct an |
| 27 | + :class:`google.auth.credentials.Credentials` object and set it as |
| 28 | + the context credentials as demonstrated in the example below. See |
| 29 | + `auth docs`_ for more information on obtaining credentials. |
| 30 | +
|
| 31 | + .. _auth docs: http://google-auth.readthedocs.io |
| 32 | + /en/latest/user-guide.html#obtaining-credentials |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + google.auth.credentials.Credentials |
| 37 | +
|
| 38 | + Examples |
| 39 | + -------- |
| 40 | +
|
| 41 | + Manually setting the context credentials: |
| 42 | +
|
| 43 | + >>> import pandas_gbq |
| 44 | + >>> from google.oauth2 import service_account |
| 45 | + >>> credentials = service_account.Credentials.from_service_account_file( |
| 46 | + ... '/path/to/key.json', |
| 47 | + ... ) |
| 48 | + >>> pandas_gbq.context.credentials = credentials |
| 49 | + """ |
| 50 | + return self._credentials |
| 51 | + |
| 52 | + @credentials.setter |
| 53 | + def credentials(self, value): |
| 54 | + self._credentials = value |
| 55 | + |
| 56 | + @property |
| 57 | + def project(self): |
| 58 | + """Default project to use for calls to Google APIs. |
| 59 | +
|
| 60 | + Returns |
| 61 | + ------- |
| 62 | + str |
| 63 | +
|
| 64 | + Examples |
| 65 | + -------- |
| 66 | +
|
| 67 | + Manually setting the context project: |
| 68 | +
|
| 69 | + >>> import pandas_gbq |
| 70 | + >>> pandas_gbq.context.project = 'my-project' |
| 71 | + """ |
| 72 | + return self._project |
| 73 | + |
| 74 | + @project.setter |
| 75 | + def project(self, value): |
| 76 | + self._project = value |
| 77 | + |
| 78 | + @property |
| 79 | + def dialect(self): |
| 80 | + """ |
| 81 | + Default dialect to use in :func:`pandas_gbq.read_gbq`. |
| 82 | +
|
| 83 | + Allowed values for the BigQuery SQL syntax dialect: |
| 84 | +
|
| 85 | + ``'legacy'`` |
| 86 | + Use BigQuery's legacy SQL dialect. For more information see |
| 87 | + `BigQuery Legacy SQL Reference |
| 88 | + <https://cloud.google.com/bigquery/docs/reference/legacy-sql>`__. |
| 89 | + ``'standard'`` |
| 90 | + Use BigQuery's standard SQL, which is |
| 91 | + compliant with the SQL 2011 standard. For more information |
| 92 | + see `BigQuery Standard SQL Reference |
| 93 | + <https://cloud.google.com/bigquery/docs/reference/standard-sql/>`__. |
| 94 | +
|
| 95 | + Returns |
| 96 | + ------- |
| 97 | + str |
| 98 | +
|
| 99 | + Examples |
| 100 | + -------- |
| 101 | +
|
| 102 | + Setting the default syntax to standard: |
| 103 | +
|
| 104 | + >>> import pandas_gbq |
| 105 | + >>> pandas_gbq.context.dialect = 'standard' |
| 106 | + """ |
| 107 | + return self._dialect |
| 108 | + |
| 109 | + @dialect.setter |
| 110 | + def dialect(self, value): |
| 111 | + self._dialect = value |
| 112 | + |
| 113 | + |
| 114 | +# Create an empty context, used to cache credentials. |
| 115 | +context = Context() |
| 116 | +"""A :class:`pandas_gbq.Context` object used to cache credentials. |
| 117 | +
|
| 118 | +Credentials automatically are cached in-memory by :func:`pandas_gbq.read_gbq` |
| 119 | +and :func:`pandas_gbq.to_gbq`. |
| 120 | +""" |
0 commit comments