Skip to content

Commit 9260ae9

Browse files
committed
Expose quasi-enum types for string values w/ choices.
Users who want to avoid tripping over string literal mismatches can use class constants instead. Addresses: https://github.com/GoogleCloudPlatform/gcloud-python/pull/1056/files#r37029533
1 parent 6549219 commit 9260ae9

1 file changed

Lines changed: 56 additions & 26 deletions

File tree

gcloud/bigquery/job.py

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@
2323
from gcloud.bigquery.table import _parse_schema_resource
2424

2525

26+
class _Enum(object):
27+
"""Psedo-enumeration class.
28+
29+
Subclasses must define ``ALLOWED`` as a class-level constant: it must
30+
be a sequence of strings.
31+
"""
32+
@classmethod
33+
def validate(cls, value):
34+
"""Check that ``value`` is one of the allowed values.
35+
36+
:raises: ValueError if value is not allowed.
37+
"""
38+
if value not in cls.ALLOWED:
39+
raise ValueError('Pass one of: %s' ', '.join(cls.ALLOWED))
40+
41+
42+
class CreateDisposition(_Enum):
43+
"""Pseudo-enum for allowed values for ``create_disposition`` properties.
44+
"""
45+
CREATE_IF_NEEDED = 'CREATE_IF_NEEDED'
46+
CREATE_NEVER = 'CREATE_NEVER'
47+
ALLOWED = (CREATE_IF_NEEDED, CREATE_NEVER)
48+
49+
50+
class Encoding(_Enum):
51+
"""Pseudo-enum for allowed values for ``encoding`` properties."""
52+
UTF_8 = 'UTF-8'
53+
ISO_8559_1 = 'ISO-8559-1'
54+
ALLOWED = (UTF_8, ISO_8559_1)
55+
56+
57+
class SourceFormat(_Enum):
58+
"""Pseudo-enum for allowed values for ``source_format`` properties."""
59+
CSV = 'CSV'
60+
DATASTORE_BACKUP = 'DATASTORE_BACKUP'
61+
NEWLINE_DELIMITED_JSON = 'NEWLINE_DELIMITED_JSON'
62+
ALLOWED = (CSV, DATASTORE_BACKUP, NEWLINE_DELIMITED_JSON)
63+
64+
65+
class WriteDisposition(_Enum):
66+
"""Pseudo-enum for allowed values for ``write_disposition`` properties."""
67+
WRITE_APPEND = 'WRITE_APPEND'
68+
WRITE_TRUNCATE = 'WRITE_TRUNCATE'
69+
WRITE_EMPTY = 'WRITE_EMPTY'
70+
ALLOWED = (WRITE_APPEND, WRITE_TRUNCATE, WRITE_EMPTY)
71+
72+
2673
class _LoadConfiguration(object):
2774
"""User-settable configuration options for load jobs."""
2875
# None -> use server default.
@@ -327,14 +374,10 @@ def create_disposition(self):
327374
def create_disposition(self, value):
328375
"""Update create_disposition.
329376
330-
:type value: boolean
331-
:param value: new create_disposition: one of "CREATE_IF_NEEDED" or
332-
"CREATE_NEVER"
333-
334-
:raises: ValueError for invalid value.
377+
:type value: string
378+
:param value: allowed values for :class:`CreateDisposition`.
335379
"""
336-
if value not in ('CREATE_IF_NEEDED', 'CREATE_NEVER'):
337-
raise ValueError("Pass 'CREATE_IF_NEEDED' or 'CREATE_NEVER'")
380+
CreateDisposition.validate(value) # raises ValueError if invalid
338381
self._configuration._create_disposition = value
339382

340383
@create_disposition.deleter
@@ -356,12 +399,9 @@ def encoding(self, value):
356399
"""Update encoding.
357400
358401
:type value: string
359-
:param value: new encoding: one of 'UTF-8' or 'ISO-8859-1'.
360-
361-
:raises: ValueError for invalid value.
402+
:param value: allowed values for :class:`Encoding`.
362403
"""
363-
if value not in ('UTF-8', 'ISO-8559-1'):
364-
raise ValueError("Pass 'UTF-8' or 'ISO-8559-1'")
404+
Encoding.validate(value) # raises ValueError if invalid
365405
self._configuration._encoding = value
366406

367407
@encoding.deleter
@@ -518,14 +558,9 @@ def source_format(self, value):
518558
"""Update source_format.
519559
520560
:type value: string
521-
:param value: new source_format: one of "CSV", "DATASTORE_BACKUP",
522-
or "NEWLINE_DELIMITED_JSON"
523-
524-
:raises: ValueError for invalid values.
561+
:param value: valid values for :class:`SourceFormat`.
525562
"""
526-
if value not in ('CSV', 'DATASTORE_BACKUP', 'NEWLINE_DELIMITED_JSON'):
527-
raise ValueError(
528-
"Pass 'CSV', 'DATASTORE_BACKUP' or 'NEWLINE_DELIMITED_JSON'")
563+
SourceFormat.validate(value) # raises ValueError if invalid
529564
self._configuration._source_format = value
530565

531566
@source_format.deleter
@@ -547,14 +582,9 @@ def write_disposition(self, value):
547582
"""Update write_disposition.
548583
549584
:type value: string
550-
:param value: new write_disposition: one of "WRITE_APPEND",
551-
"WRITE_TRUNCATE", or "WRITE_EMPTY"
552-
553-
:raises: ValueError for invalid value types.
585+
:param value: valid values for :class:`WriteDisposition`.
554586
"""
555-
if value not in ('WRITE_APPEND', 'WRITE_TRUNCATE', 'WRITE_EMPTY'):
556-
raise ValueError(
557-
"Pass 'WRITE_APPEND', 'WRITE_TRUNCATE' or 'WRITE_EMPTY'")
587+
WriteDisposition.validate(value) # raises ValueError if invalid
558588
self._configuration._write_disposition = value
559589

560590
@write_disposition.deleter

0 commit comments

Comments
 (0)