15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Protocol Buffers - Google's data interchange format
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Copyright 2008 Google Inc.  All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# http://code.google.com/p/protobuf/
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# modification, are permitted provided that the following conditions are
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# met:
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Redistributions of source code must retain the above copyright
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# notice, this list of conditions and the following disclaimer.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Redistributions in binary form must reproduce the above
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# in the documentation and/or other materials provided with the
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# distribution.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# contributors may be used to endorse or promote products derived from
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# this software without specific prior written permission.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"""
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)This module is the central entity that determines which implementation of the
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)API is used.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"""
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)__author__ = 'petar@google.com (Petar Petrov)'
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import os
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# This environment variable can be used to switch to a certain implementation
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# of the Python API. Right now only 'python' and 'cpp' are valid values. Any
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# other value will be ignored.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)_implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION',
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 'python')
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if _implementation_type != 'python':
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  # For now, by default use the pure-Python implementation.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  # The code below checks if the C extension is available and
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  # uses it if it is available.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _implementation_type = 'cpp'
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ## Determine automatically which implementation to use.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #try:
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #  from google.protobuf.internal import cpp_message
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #  _implementation_type = 'cpp'
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #except ImportError, e:
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #  _implementation_type = 'python'
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
59ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch# This environment variable can be used to switch between the two
60ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch# 'cpp' implementations. Right now only 1 and 2 are valid values. Any
61ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch# other value will be ignored.
62ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch_implementation_version_str = os.getenv(
63ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION',
64ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    '1')
65ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
66ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
67ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochif _implementation_version_str not in ('1', '2'):
68ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  raise ValueError(
69ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "unsupported PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION: '" +
70ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      _implementation_version_str + "' (supported versions: 1, 2)"
71ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      )
72ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
73ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
74ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch_implementation_version = int(_implementation_version_str)
75ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
76ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
77ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Usage of this function is discouraged. Clients shouldn't care which
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# implementation of the API is in use. Note that there is no guarantee
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# that differences between APIs will be maintained.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Please don't use this function if possible.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)def Type():
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return _implementation_type
84ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
85ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch# See comment on 'Type' above.
86ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochdef Version():
87ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  return _implementation_version
88