| OLD | NEW |
| 1 # Protocol Buffers - Google's data interchange format | 1 # Protocol Buffers - Google's data interchange format |
| 2 # Copyright 2008 Google Inc. All rights reserved. | 2 # Copyright 2008 Google Inc. All rights reserved. |
| 3 # https://developers.google.com/protocol-buffers/ | 3 # https://developers.google.com/protocol-buffers/ |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 from google.protobuf.descriptor import FieldDescriptor | 47 from google.protobuf.descriptor import FieldDescriptor |
| 48 | 48 |
| 49 _TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S' | 49 _TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S' |
| 50 _NANOS_PER_SECOND = 1000000000 | 50 _NANOS_PER_SECOND = 1000000000 |
| 51 _NANOS_PER_MILLISECOND = 1000000 | 51 _NANOS_PER_MILLISECOND = 1000000 |
| 52 _NANOS_PER_MICROSECOND = 1000 | 52 _NANOS_PER_MICROSECOND = 1000 |
| 53 _MILLIS_PER_SECOND = 1000 | 53 _MILLIS_PER_SECOND = 1000 |
| 54 _MICROS_PER_SECOND = 1000000 | 54 _MICROS_PER_SECOND = 1000000 |
| 55 _SECONDS_PER_DAY = 24 * 3600 | 55 _SECONDS_PER_DAY = 24 * 3600 |
| 56 _DURATION_SECONDS_MAX = 315576000000 | |
| 57 | 56 |
| 58 | 57 |
| 59 class Error(Exception): | 58 class Error(Exception): |
| 60 """Top-level module error.""" | 59 """Top-level module error.""" |
| 61 | 60 |
| 62 | 61 |
| 63 class ParseError(Error): | 62 class ParseError(Error): |
| 64 """Thrown in case of parsing error.""" | 63 """Thrown in case of parsing error.""" |
| 65 | 64 |
| 66 | 65 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 240 |
| 242 def ToJsonString(self): | 241 def ToJsonString(self): |
| 243 """Converts Duration to string format. | 242 """Converts Duration to string format. |
| 244 | 243 |
| 245 Returns: | 244 Returns: |
| 246 A string converted from self. The string format will contains | 245 A string converted from self. The string format will contains |
| 247 3, 6, or 9 fractional digits depending on the precision required to | 246 3, 6, or 9 fractional digits depending on the precision required to |
| 248 represent the exact Duration value. For example: "1s", "1.010s", | 247 represent the exact Duration value. For example: "1s", "1.010s", |
| 249 "1.000000100s", "-3.100s" | 248 "1.000000100s", "-3.100s" |
| 250 """ | 249 """ |
| 251 _CheckDurationValid(self.seconds, self.nanos) | |
| 252 if self.seconds < 0 or self.nanos < 0: | 250 if self.seconds < 0 or self.nanos < 0: |
| 253 result = '-' | 251 result = '-' |
| 254 seconds = - self.seconds + int((0 - self.nanos) // 1e9) | 252 seconds = - self.seconds + int((0 - self.nanos) // 1e9) |
| 255 nanos = (0 - self.nanos) % 1e9 | 253 nanos = (0 - self.nanos) % 1e9 |
| 256 else: | 254 else: |
| 257 result = '' | 255 result = '' |
| 258 seconds = self.seconds + int(self.nanos // 1e9) | 256 seconds = self.seconds + int(self.nanos // 1e9) |
| 259 nanos = self.nanos % 1e9 | 257 nanos = self.nanos % 1e9 |
| 260 result += '%d' % seconds | 258 result += '%d' % seconds |
| 261 if (nanos % 1e9) == 0: | 259 if (nanos % 1e9) == 0: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 281 | 279 |
| 282 Raises: | 280 Raises: |
| 283 ParseError: On parsing problems. | 281 ParseError: On parsing problems. |
| 284 """ | 282 """ |
| 285 if len(value) < 1 or value[-1] != 's': | 283 if len(value) < 1 or value[-1] != 's': |
| 286 raise ParseError( | 284 raise ParseError( |
| 287 'Duration must end with letter "s": {0}.'.format(value)) | 285 'Duration must end with letter "s": {0}.'.format(value)) |
| 288 try: | 286 try: |
| 289 pos = value.find('.') | 287 pos = value.find('.') |
| 290 if pos == -1: | 288 if pos == -1: |
| 291 seconds = int(value[:-1]) | 289 self.seconds = int(value[:-1]) |
| 292 nanos = 0 | 290 self.nanos = 0 |
| 293 else: | 291 else: |
| 294 seconds = int(value[:pos]) | 292 self.seconds = int(value[:pos]) |
| 295 if value[0] == '-': | 293 if value[0] == '-': |
| 296 nanos = int(round(float('-0{0}'.format(value[pos: -1])) *1e9)) | 294 self.nanos = int(round(float('-0{0}'.format(value[pos: -1])) *1e9)) |
| 297 else: | 295 else: |
| 298 nanos = int(round(float('0{0}'.format(value[pos: -1])) *1e9)) | 296 self.nanos = int(round(float('0{0}'.format(value[pos: -1])) *1e9)) |
| 299 _CheckDurationValid(seconds, nanos) | |
| 300 self.seconds = seconds | |
| 301 self.nanos = nanos | |
| 302 except ValueError: | 297 except ValueError: |
| 303 raise ParseError( | 298 raise ParseError( |
| 304 'Couldn\'t parse duration: {0}.'.format(value)) | 299 'Couldn\'t parse duration: {0}.'.format(value)) |
| 305 | 300 |
| 306 def ToNanoseconds(self): | 301 def ToNanoseconds(self): |
| 307 """Converts a Duration to nanoseconds.""" | 302 """Converts a Duration to nanoseconds.""" |
| 308 return self.seconds * _NANOS_PER_SECOND + self.nanos | 303 return self.seconds * _NANOS_PER_SECOND + self.nanos |
| 309 | 304 |
| 310 def ToMicroseconds(self): | 305 def ToMicroseconds(self): |
| 311 """Converts a Duration to microseconds.""" | 306 """Converts a Duration to microseconds.""" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 def _NormalizeDuration(self, seconds, nanos): | 352 def _NormalizeDuration(self, seconds, nanos): |
| 358 """Set Duration by seconds and nonas.""" | 353 """Set Duration by seconds and nonas.""" |
| 359 # Force nanos to be negative if the duration is negative. | 354 # Force nanos to be negative if the duration is negative. |
| 360 if seconds < 0 and nanos > 0: | 355 if seconds < 0 and nanos > 0: |
| 361 seconds += 1 | 356 seconds += 1 |
| 362 nanos -= _NANOS_PER_SECOND | 357 nanos -= _NANOS_PER_SECOND |
| 363 self.seconds = seconds | 358 self.seconds = seconds |
| 364 self.nanos = nanos | 359 self.nanos = nanos |
| 365 | 360 |
| 366 | 361 |
| 367 def _CheckDurationValid(seconds, nanos): | |
| 368 if seconds < -_DURATION_SECONDS_MAX or seconds > _DURATION_SECONDS_MAX: | |
| 369 raise Error( | |
| 370 'Duration is not valid: Seconds {0} must be in range ' | |
| 371 '[-315576000000, 315576000000].'.format(seconds)) | |
| 372 if nanos <= -_NANOS_PER_SECOND or nanos >= _NANOS_PER_SECOND: | |
| 373 raise Error( | |
| 374 'Duration is not valid: Nanos {0} must be in range ' | |
| 375 '[-999999999, 999999999].'.format(nanos)) | |
| 376 | |
| 377 | |
| 378 def _RoundTowardZero(value, divider): | 362 def _RoundTowardZero(value, divider): |
| 379 """Truncates the remainder part after division.""" | 363 """Truncates the remainder part after division.""" |
| 380 # For some languanges, the sign of the remainder is implementation | 364 # For some languanges, the sign of the remainder is implementation |
| 381 # dependent if any of the operands is negative. Here we enforce | 365 # dependent if any of the operands is negative. Here we enforce |
| 382 # "rounded toward zero" semantics. For example, for (-5) / 2 an | 366 # "rounded toward zero" semantics. For example, for (-5) / 2 an |
| 383 # implementation may give -3 as the result with the remainder being | 367 # implementation may give -3 as the result with the remainder being |
| 384 # 1. This function ensures we always return -2 (closer to zero). | 368 # 1. This function ensures we always return -2 (closer to zero). |
| 385 result = value // divider | 369 result = value // divider |
| 386 remainder = value % divider | 370 remainder = value % divider |
| 387 if result < 0 and remainder > 0: | 371 if result < 0 and remainder > 0: |
| 388 return result + 1 | 372 return result + 1 |
| 389 else: | 373 else: |
| 390 return result | 374 return result |
| 391 | 375 |
| 392 | 376 |
| 393 class FieldMask(object): | 377 class FieldMask(object): |
| 394 """Class for FieldMask message type.""" | 378 """Class for FieldMask message type.""" |
| 395 | 379 |
| 396 def ToJsonString(self): | 380 def ToJsonString(self): |
| 397 """Converts FieldMask to string according to proto3 JSON spec.""" | 381 """Converts FieldMask to string according to proto3 JSON spec.""" |
| 398 camelcase_paths = [] | 382 return ','.join(self.paths) |
| 399 for path in self.paths: | |
| 400 camelcase_paths.append(_SnakeCaseToCamelCase(path)) | |
| 401 return ','.join(camelcase_paths) | |
| 402 | 383 |
| 403 def FromJsonString(self, value): | 384 def FromJsonString(self, value): |
| 404 """Converts string to FieldMask according to proto3 JSON spec.""" | 385 """Converts string to FieldMask according to proto3 JSON spec.""" |
| 405 self.Clear() | 386 self.Clear() |
| 406 for path in value.split(','): | 387 for path in value.split(','): |
| 407 self.paths.append(_CamelCaseToSnakeCase(path)) | 388 self.paths.append(path) |
| 408 | 389 |
| 409 def IsValidForDescriptor(self, message_descriptor): | 390 def IsValidForDescriptor(self, message_descriptor): |
| 410 """Checks whether the FieldMask is valid for Message Descriptor.""" | 391 """Checks whether the FieldMask is valid for Message Descriptor.""" |
| 411 for path in self.paths: | 392 for path in self.paths: |
| 412 if not _IsValidPath(message_descriptor, path): | 393 if not _IsValidPath(message_descriptor, path): |
| 413 return False | 394 return False |
| 414 return True | 395 return True |
| 415 | 396 |
| 416 def AllFieldsFromDescriptor(self, message_descriptor): | 397 def AllFieldsFromDescriptor(self, message_descriptor): |
| 417 """Gets all direct fields of Message Descriptor to FieldMask.""" | 398 """Gets all direct fields of Message Descriptor to FieldMask.""" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 | 465 |
| 485 def _CheckFieldMaskMessage(message): | 466 def _CheckFieldMaskMessage(message): |
| 486 """Raises ValueError if message is not a FieldMask.""" | 467 """Raises ValueError if message is not a FieldMask.""" |
| 487 message_descriptor = message.DESCRIPTOR | 468 message_descriptor = message.DESCRIPTOR |
| 488 if (message_descriptor.name != 'FieldMask' or | 469 if (message_descriptor.name != 'FieldMask' or |
| 489 message_descriptor.file.name != 'google/protobuf/field_mask.proto'): | 470 message_descriptor.file.name != 'google/protobuf/field_mask.proto'): |
| 490 raise ValueError('Message {0} is not a FieldMask.'.format( | 471 raise ValueError('Message {0} is not a FieldMask.'.format( |
| 491 message_descriptor.full_name)) | 472 message_descriptor.full_name)) |
| 492 | 473 |
| 493 | 474 |
| 494 def _SnakeCaseToCamelCase(path_name): | |
| 495 """Converts a path name from snake_case to camelCase.""" | |
| 496 result = [] | |
| 497 after_underscore = False | |
| 498 for c in path_name: | |
| 499 if c.isupper(): | |
| 500 raise Error('Fail to print FieldMask to Json string: Path name ' | |
| 501 '{0} must not contain uppercase letters.'.format(path_name)) | |
| 502 if after_underscore: | |
| 503 if c.islower(): | |
| 504 result.append(c.upper()) | |
| 505 after_underscore = False | |
| 506 else: | |
| 507 raise Error('Fail to print FieldMask to Json string: The ' | |
| 508 'character after a "_" must be a lowercase letter ' | |
| 509 'in path name {0}.'.format(path_name)) | |
| 510 elif c == '_': | |
| 511 after_underscore = True | |
| 512 else: | |
| 513 result += c | |
| 514 | |
| 515 if after_underscore: | |
| 516 raise Error('Fail to print FieldMask to Json string: Trailing "_" ' | |
| 517 'in path name {0}.'.format(path_name)) | |
| 518 return ''.join(result) | |
| 519 | |
| 520 | |
| 521 def _CamelCaseToSnakeCase(path_name): | |
| 522 """Converts a field name from camelCase to snake_case.""" | |
| 523 result = [] | |
| 524 for c in path_name: | |
| 525 if c == '_': | |
| 526 raise ParseError('Fail to parse FieldMask: Path name ' | |
| 527 '{0} must not contain "_"s.'.format(path_name)) | |
| 528 if c.isupper(): | |
| 529 result += '_' | |
| 530 result += c.lower() | |
| 531 else: | |
| 532 result += c | |
| 533 return ''.join(result) | |
| 534 | |
| 535 | |
| 536 class _FieldMaskTree(object): | 475 class _FieldMaskTree(object): |
| 537 """Represents a FieldMask in a tree structure. | 476 """Represents a FieldMask in a tree structure. |
| 538 | 477 |
| 539 For example, given a FieldMask "foo.bar,foo.baz,bar.baz", | 478 For example, given a FieldMask "foo.bar,foo.baz,bar.baz", |
| 540 the FieldMaskTree will be: | 479 the FieldMaskTree will be: |
| 541 [_root] -+- foo -+- bar | 480 [_root] -+- foo -+- bar |
| 542 | | | 481 | | |
| 543 | +- baz | 482 | +- baz |
| 544 | | 483 | |
| 545 +- bar --- baz | 484 +- bar --- baz |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 | 715 |
| 777 | 716 |
| 778 WKTBASES = { | 717 WKTBASES = { |
| 779 'google.protobuf.Any': Any, | 718 'google.protobuf.Any': Any, |
| 780 'google.protobuf.Duration': Duration, | 719 'google.protobuf.Duration': Duration, |
| 781 'google.protobuf.FieldMask': FieldMask, | 720 'google.protobuf.FieldMask': FieldMask, |
| 782 'google.protobuf.ListValue': ListValue, | 721 'google.protobuf.ListValue': ListValue, |
| 783 'google.protobuf.Struct': Struct, | 722 'google.protobuf.Struct': Struct, |
| 784 'google.protobuf.Timestamp': Timestamp, | 723 'google.protobuf.Timestamp': Timestamp, |
| 785 } | 724 } |
| OLD | NEW |