- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / protobuf / python / google / protobuf / descriptor_database.py
1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc.  All rights reserved.
3 # http://code.google.com/p/protobuf/
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 #     * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 """Provides a container for DescriptorProtos."""
32
33 __author__ = 'matthewtoia@google.com (Matt Toia)'
34
35
36 class DescriptorDatabase(object):
37   """A container accepting FileDescriptorProtos and maps DescriptorProtos."""
38
39   def __init__(self):
40     self._file_desc_protos_by_file = {}
41     self._file_desc_protos_by_symbol = {}
42
43   def Add(self, file_desc_proto):
44     """Adds the FileDescriptorProto and its types to this database.
45
46     Args:
47       file_desc_proto: The FileDescriptorProto to add.
48     """
49
50     self._file_desc_protos_by_file[file_desc_proto.name] = file_desc_proto
51     package = file_desc_proto.package
52     for message in file_desc_proto.message_type:
53       self._file_desc_protos_by_symbol.update(
54           (name, file_desc_proto) for name in _ExtractSymbols(message, package))
55     for enum in file_desc_proto.enum_type:
56       self._file_desc_protos_by_symbol[
57           '.'.join((package, enum.name))] = file_desc_proto
58
59   def FindFileByName(self, name):
60     """Finds the file descriptor proto by file name.
61
62     Typically the file name is a relative path ending to a .proto file. The
63     proto with the given name will have to have been added to this database
64     using the Add method or else an error will be raised.
65
66     Args:
67       name: The file name to find.
68
69     Returns:
70       The file descriptor proto matching the name.
71
72     Raises:
73       KeyError if no file by the given name was added.
74     """
75
76     return self._file_desc_protos_by_file[name]
77
78   def FindFileContainingSymbol(self, symbol):
79     """Finds the file descriptor proto containing the specified symbol.
80
81     The symbol should be a fully qualified name including the file descriptor's
82     package and any containing messages. Some examples:
83
84     'some.package.name.Message'
85     'some.package.name.Message.NestedEnum'
86
87     The file descriptor proto containing the specified symbol must be added to
88     this database using the Add method or else an error will be raised.
89
90     Args:
91       symbol: The fully qualified symbol name.
92
93     Returns:
94       The file descriptor proto containing the symbol.
95
96     Raises:
97       KeyError if no file contains the specified symbol.
98     """
99
100     return self._file_desc_protos_by_symbol[symbol]
101
102
103 def _ExtractSymbols(desc_proto, package):
104   """Pulls out all the symbols from a descriptor proto.
105
106   Args:
107     desc_proto: The proto to extract symbols from.
108     package: The package containing the descriptor type.
109
110   Yields:
111     The fully qualified name found in the descriptor.
112   """
113
114   message_name = '.'.join((package, desc_proto.name))
115   yield message_name
116   for nested_type in desc_proto.nested_type:
117     for symbol in _ExtractSymbols(nested_type, message_name):
118       yield symbol
119     for enum_type in desc_proto.enum_type:
120       yield '.'.join((message_name, enum_type.name))