| OLD | NEW |
| 1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import fnmatch | 5 import fnmatch |
| 6 import inspect | 6 import inspect |
| 7 import os | 7 import os |
| 8 import re | 8 import re |
| 9 import sys | 9 import sys |
| 10 | 10 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 if obj.__module__ != module.__name__: | 150 if obj.__module__ != module.__name__: |
| 151 continue | 151 continue |
| 152 | 152 |
| 153 if index_by_class_name: | 153 if index_by_class_name: |
| 154 key_name = camel_case.ToUnderscore(obj.__name__) | 154 key_name = camel_case.ToUnderscore(obj.__name__) |
| 155 else: | 155 else: |
| 156 key_name = module.__name__.split('.')[-1] | 156 key_name = module.__name__.split('.')[-1] |
| 157 if not directly_constructable or IsDirectlyConstructable(obj): | 157 if not directly_constructable or IsDirectlyConstructable(obj): |
| 158 if key_name in classes and index_by_class_name: | 158 if key_name in classes and index_by_class_name: |
| 159 assert classes[key_name] is obj, ( | 159 assert classes[key_name] is obj, ( |
| 160 'Duplicate key_name with different objs detected: ' | 160 'Duplicate key_name with different objs detected: ' |
| 161 'key=%s, obj1=%s, obj2=%s' % (key_name, classes[key_name], obj)) | 161 'key=%s, obj1=%s, obj2=%s' % (key_name, classes[key_name], obj)) |
| 162 else: | 162 else: |
| 163 classes[key_name] = obj | 163 classes[key_name] = obj |
| 164 | 164 |
| 165 return classes | 165 return classes |
| 166 | 166 |
| 167 | 167 |
| 168 def IsDirectlyConstructable(cls): | 168 def IsDirectlyConstructable(cls): |
| 169 """Returns True if instance of |cls| can be construct without arguments.""" | 169 """Returns True if instance of |cls| can be construct without arguments.""" |
| 170 assert inspect.isclass(cls) | 170 assert inspect.isclass(cls) |
| 171 if not hasattr(cls, '__init__'): | 171 if not hasattr(cls, '__init__'): |
| 172 # Case |class A: pass|. | 172 # Case |class A: pass|. |
| 173 return True | 173 return True |
| 174 if cls.__init__ is object.__init__: | 174 if cls.__init__ is object.__init__: |
| 175 # Case |class A(object): pass|. | 175 # Case |class A(object): pass|. |
| 176 return True | 176 return True |
| 177 # Case |class (object):| with |__init__| other than |object.__init__|. | 177 # Case |class (object):| with |__init__| other than |object.__init__|. |
| 178 args, _, _, defaults = inspect.getargspec(cls.__init__) | 178 args, _, _, defaults = inspect.getargspec(cls.__init__) |
| 179 if defaults is None: | 179 if defaults is None: |
| 180 defaults = () | 180 defaults = () |
| 181 # Return true if |self| is only arg without a default. | 181 # Return true if |self| is only arg without a default. |
| 182 return len(args) == len(defaults) + 1 | 182 return len(args) == len(defaults) + 1 |
| 183 | 183 |
| 184 | 184 |
| 185 _counter = [0] | 185 _COUNTER = [0] |
| 186 | 186 |
| 187 | 187 |
| 188 def _GetUniqueModuleName(): | 188 def _GetUniqueModuleName(): |
| 189 _counter[0] += 1 | 189 _COUNTER[0] += 1 |
| 190 return "module_" + str(_counter[0]) | 190 return "module_" + str(_COUNTER[0]) |
| OLD | NEW |