4141
4242
4343_NOW = datetime .datetime .utcnow # To be replaced by tests.
44- _RFC3339_MICROS = ' %Y-%m-%dT%H:%M:%S.%fZ'
45- _RFC3339_NO_FRACTION = ' %Y-%m-%dT%H:%M:%S'
46- _TIMEONLY_W_MICROS = ' %H:%M:%S.%f'
47- _TIMEONLY_NO_FRACTION = ' %H:%M:%S'
44+ _RFC3339_MICROS = " %Y-%m-%dT%H:%M:%S.%fZ"
45+ _RFC3339_NO_FRACTION = " %Y-%m-%dT%H:%M:%S"
46+ _TIMEONLY_W_MICROS = " %H:%M:%S.%f"
47+ _TIMEONLY_NO_FRACTION = " %H:%M:%S"
4848# datetime.strptime cannot handle nanosecond precision: parse w/ regex
49- _RFC3339_NANOS = re .compile (r"""
49+ _RFC3339_NANOS = re .compile (
50+ r"""
5051 (?P<no_fraction>
5152 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} # YYYY-MM-DDTHH:MM:SS
5253 )
5556 (?P<nanos>\d{1,9}) # nanoseconds, maybe truncated
5657 )?
5758 Z # Zulu
58- """ , re .VERBOSE )
59+ """ ,
60+ re .VERBOSE ,
61+ )
5962# NOTE: Catching this ImportError is a workaround for GAE not supporting the
6063# "pwd" module which is imported lazily when "expanduser" is called.
6164try :
62- _USER_ROOT = os .path .expanduser ('~' )
65+ _USER_ROOT = os .path .expanduser ("~" )
6366except ImportError : # pragma: NO COVER
6467 _USER_ROOT = None
65- _GCLOUD_CONFIG_FILE = os .path .join (
66- 'gcloud' , 'configurations' , 'config_default' )
67- _GCLOUD_CONFIG_SECTION = 'core'
68- _GCLOUD_CONFIG_KEY = 'project'
68+ _GCLOUD_CONFIG_FILE = os .path .join ("gcloud" , "configurations" , "config_default" )
69+ _GCLOUD_CONFIG_SECTION = "core"
70+ _GCLOUD_CONFIG_KEY = "project"
6971
7072
7173class _LocalStack (Local ):
@@ -74,6 +76,7 @@ class _LocalStack(Local):
7476 Intended for use in :class:`google.cloud.datastore.batch.Batch.__enter__`,
7577 :class:`google.cloud.storage.batch.Batch.__enter__`, etc.
7678 """
79+
7780 def __init__ (self ):
7881 super (_LocalStack , self ).__init__ ()
7982 self ._stack = []
@@ -115,7 +118,7 @@ class _UTC(datetime.tzinfo):
115118 """
116119
117120 _dst = datetime .timedelta (0 )
118- _tzname = ' UTC'
121+ _tzname = " UTC"
119122 _utcoffset = _dst
120123
121124 def dst (self , dt ): # pylint: disable=unused-argument
@@ -137,7 +140,7 @@ def utcoffset(self, dt): # pylint: disable=unused-argument
137140 return self ._utcoffset
138141
139142 def __repr__ (self ):
140- return ' <%s>' % (self ._tzname ,)
143+ return " <%s>" % (self ._tzname ,)
141144
142145 def __str__ (self ):
143146 return self ._tzname
@@ -160,8 +163,10 @@ def _ensure_tuple_or_list(arg_name, tuple_or_list):
160163 :raises TypeError: if the ``tuple_or_list`` is not a tuple or list.
161164 """
162165 if not isinstance (tuple_or_list , (tuple , list )):
163- raise TypeError ('Expected %s to be a tuple or list. '
164- 'Received %r' % (arg_name , tuple_or_list ))
166+ raise TypeError (
167+ "Expected %s to be a tuple or list. "
168+ "Received %r" % (arg_name , tuple_or_list )
169+ )
165170 return list (tuple_or_list )
166171
167172
@@ -247,7 +252,7 @@ def _date_from_iso8601_date(value):
247252 :returns: A datetime date object created from the string
248253
249254 """
250- return datetime .datetime .strptime (value , ' %Y-%m-%d' ).date ()
255+ return datetime .datetime .strptime (value , " %Y-%m-%d" ).date ()
251256
252257
253258def _time_from_iso8601_time_naive (value ):
@@ -278,8 +283,7 @@ def _rfc3339_to_datetime(dt_str):
278283 :rtype: :class:`datetime.datetime`
279284 :returns: The datetime object created from the string.
280285 """
281- return datetime .datetime .strptime (
282- dt_str , _RFC3339_MICROS ).replace (tzinfo = UTC )
286+ return datetime .datetime .strptime (dt_str , _RFC3339_MICROS ).replace (tzinfo = UTC )
283287
284288
285289def _rfc3339_nanos_to_datetime (dt_str ):
@@ -301,11 +305,13 @@ def _rfc3339_nanos_to_datetime(dt_str):
301305 with_nanos = _RFC3339_NANOS .match (dt_str )
302306 if with_nanos is None :
303307 raise ValueError (
304- 'Timestamp: %r, does not match pattern: %r' % (
305- dt_str , _RFC3339_NANOS .pattern ))
308+ "Timestamp: %r, does not match pattern: %r"
309+ % (dt_str , _RFC3339_NANOS .pattern )
310+ )
306311 bare_seconds = datetime .datetime .strptime (
307- with_nanos .group ('no_fraction' ), _RFC3339_NO_FRACTION )
308- fraction = with_nanos .group ('nanos' )
312+ with_nanos .group ("no_fraction" ), _RFC3339_NO_FRACTION
313+ )
314+ fraction = with_nanos .group ("nanos" )
309315 if fraction is None :
310316 micros = 0
311317 else :
@@ -335,7 +341,7 @@ def _datetime_to_rfc3339(value, ignore_zone=True):
335341 return value .strftime (_RFC3339_MICROS )
336342
337343
338- def _to_bytes (value , encoding = ' ascii' ):
344+ def _to_bytes (value , encoding = " ascii" ):
339345 """Converts a string value to bytes, if necessary.
340346
341347 Unfortunately, ``six.b`` is insufficient for this task since in
@@ -357,12 +363,11 @@ def _to_bytes(value, encoding='ascii'):
357363 in if it started out as bytes.
358364 :raises TypeError: if the value could not be converted to bytes.
359365 """
360- result = (value .encode (encoding )
361- if isinstance (value , six .text_type ) else value )
366+ result = value .encode (encoding ) if isinstance (value , six .text_type ) else value
362367 if isinstance (result , six .binary_type ):
363368 return result
364369 else :
365- raise TypeError (' %r could not be converted to bytes' % (value ,))
370+ raise TypeError (" %r could not be converted to bytes" % (value ,))
366371
367372
368373def _bytes_to_unicode (value ):
@@ -377,12 +382,11 @@ def _bytes_to_unicode(value):
377382
378383 :raises ValueError: if the value could not be converted to unicode.
379384 """
380- result = (value .decode ('utf-8' )
381- if isinstance (value , six .binary_type ) else value )
385+ result = value .decode ("utf-8" ) if isinstance (value , six .binary_type ) else value
382386 if isinstance (result , six .text_type ):
383387 return result
384388 else :
385- raise ValueError (' %r could not be converted to unicode' % (value ,))
389+ raise ValueError (" %r could not be converted to unicode" % (value ,))
386390
387391
388392def _from_any_pb (pb_type , any_pb ):
@@ -402,8 +406,10 @@ def _from_any_pb(pb_type, any_pb):
402406 msg = pb_type ()
403407 if not any_pb .Unpack (msg ):
404408 raise TypeError (
405- 'Could not convert {} to {}' .format (
406- any_pb .__class__ .__name__ , pb_type .__name__ ))
409+ "Could not convert {} to {}" .format (
410+ any_pb .__class__ .__name__ , pb_type .__name__
411+ )
412+ )
407413
408414 return msg
409415
@@ -417,12 +423,8 @@ def _pb_timestamp_to_datetime(timestamp_pb):
417423 :rtype: :class:`datetime.datetime`
418424 :returns: A UTC datetime object converted from a protobuf timestamp.
419425 """
420- return (
421- _EPOCH +
422- datetime .timedelta (
423- seconds = timestamp_pb .seconds ,
424- microseconds = (timestamp_pb .nanos / 1000.0 ),
425- )
426+ return _EPOCH + datetime .timedelta (
427+ seconds = timestamp_pb .seconds , microseconds = (timestamp_pb .nanos / 1000.0 )
426428 )
427429
428430
@@ -449,8 +451,8 @@ def _datetime_to_pb_timestamp(when):
449451 :returns: A timestamp protobuf corresponding to the object.
450452 """
451453 ms_value = _microseconds_from_datetime (when )
452- seconds , micros = divmod (ms_value , 10 ** 6 )
453- nanos = micros * 10 ** 3
454+ seconds , micros = divmod (ms_value , 10 ** 6 )
455+ nanos = micros * 10 ** 3
454456 return timestamp_pb2 .Timestamp (seconds = seconds , nanos = nanos )
455457
456458
@@ -488,8 +490,7 @@ def _duration_pb_to_timedelta(duration_pb):
488490 :returns: The converted timedelta object.
489491 """
490492 return datetime .timedelta (
491- seconds = duration_pb .seconds ,
492- microseconds = (duration_pb .nanos / 1000.0 ),
493+ seconds = duration_pb .seconds , microseconds = (duration_pb .nanos / 1000.0 )
493494 )
494495
495496
@@ -521,17 +522,19 @@ def _name_from_project_path(path, project, template):
521522 match = template .match (path )
522523
523524 if not match :
524- raise ValueError ('path "%s" did not match expected pattern "%s"' % (
525- path , template .pattern ,))
525+ raise ValueError (
526+ 'path "%s" did not match expected pattern "%s"' % (path , template .pattern )
527+ )
526528
527529 if project is not None :
528- found_project = match .group (' project' )
530+ found_project = match .group (" project" )
529531 if found_project != project :
530532 raise ValueError (
531- 'Project from client (%s) should agree with '
532- 'project from resource(%s).' % (project , found_project ))
533+ "Project from client (%s) should agree with "
534+ "project from resource(%s)." % (project , found_project )
535+ )
533536
534- return match .group (' name' )
537+ return match .group (" name" )
535538
536539
537540def make_secure_channel (credentials , user_agent , host , extra_options = ()):
@@ -556,20 +559,17 @@ def make_secure_channel(credentials, user_agent, host, extra_options=()):
556559 :rtype: :class:`grpc._channel.Channel`
557560 :returns: gRPC secure channel with credentials attached.
558561 """
559- target = ' %s:%d' % (host , http_client .HTTPS_PORT )
562+ target = " %s:%d" % (host , http_client .HTTPS_PORT )
560563 http_request = google .auth .transport .requests .Request ()
561564
562- user_agent_option = (' grpc.primary_user_agent' , user_agent )
565+ user_agent_option = (" grpc.primary_user_agent" , user_agent )
563566 options = (user_agent_option ,) + extra_options
564567 return google .auth .transport .grpc .secure_authorized_channel (
565- credentials ,
566- http_request ,
567- target ,
568- options = options )
568+ credentials , http_request , target , options = options
569+ )
569570
570571
571- def make_secure_stub (credentials , user_agent , stub_class , host ,
572- extra_options = ()):
572+ def make_secure_stub (credentials , user_agent , stub_class , host , extra_options = ()):
573573 """Makes a secure stub for an RPC service.
574574
575575 Uses / depends on gRPC.
@@ -594,8 +594,9 @@ def make_secure_stub(credentials, user_agent, stub_class, host,
594594 :rtype: object, instance of ``stub_class``
595595 :returns: The stub object used to make gRPC requests to a given API.
596596 """
597- channel = make_secure_channel (credentials , user_agent , host ,
598- extra_options = extra_options )
597+ channel = make_secure_channel (
598+ credentials , user_agent , host , extra_options = extra_options
599+ )
599600 return stub_class (channel )
600601
601602
@@ -621,7 +622,7 @@ def make_insecure_stub(stub_class, host, port=None):
621622 target = host
622623 else :
623624 # NOTE: This assumes port != http_client.HTTPS_PORT:
624- target = ' %s:%d' % (host , port )
625+ target = " %s:%d" % (host , port )
625626 channel = grpc .insecure_channel (target )
626627 return stub_class (channel )
627628
0 commit comments