<s> import inspect <EOL> import types <EOL> from mamba . example_group import ExampleGroup , PendingExampleGroup <EOL> from mamba . example import Example , PendingExample <EOL> from mamba . infrastructure import is_python3 <EOL> class Loader ( object ) : <EOL> def load_examples_from ( self , module ) : <EOL> loaded = [ ] <EOL> example_groups = self . _example_groups_for ( module ) <EOL> for klass in example_groups : <EOL> example_group = self . _create_example_group ( klass ) <EOL> self . _add_hooks_examples_and_nested_example_groups_to ( klass , example_group ) <EOL> loaded . append ( example_group ) <EOL> return loaded <EOL> def _example_groups_for ( self , module ) : <EOL> return [ klass for name , klass in inspect . getmembers ( module , inspect . isclass ) if self . _is_example_group ( name ) ] <EOL> def _is_example_group ( self , class_name ) : <EOL> return class_name . endswith ( '<STR_LIT>' ) <EOL> def _create_example_group ( self , klass , execution_context = None ) : <EOL> if '<STR_LIT>' in klass . __name__ : <EOL> return PendingExampleGroup ( self . _subject ( klass ) , execution_context = execution_context ) <EOL> return ExampleGroup ( self . _subject ( klass ) , execution_context = execution_context ) <EOL> def _subject ( self , example_group ) : <EOL> subject = getattr ( example_group , '<STR_LIT>' , example_group . __name__ . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if isinstance ( subject , str ) : <EOL> return subject [ <NUM_LIT:10> : ] <EOL> else : <EOL> return subject <EOL> def _add_hooks_examples_and_nested_example_groups_to ( self , klass , example_group ) : <EOL> self . _load_hooks ( klass , example_group ) <EOL> self . _load_examples ( klass , example_group ) <EOL> self . _load_nested_example_groups ( klass , example_group ) <EOL> self . _load_helper_methods_to_execution_context ( klass , example_group . execution_context ) <EOL> def _load_hooks ( self , klass , example_group ) : <EOL> for hook in self . _hooks_in ( klass ) : <EOL> example_group . hooks [ hook . __name__ ] . append ( hook ) <EOL> def _hooks_in ( self , example_group ) : <EOL> return [ method for name , method in self . _methods_for ( example_group ) if self . _is_hook ( name ) ] <EOL> def _is_hook ( self , method_name ) : <EOL> return method_name . startswith ( '<STR_LIT>' ) or method_name . startswith ( '<STR_LIT>' ) <EOL> def _load_examples ( self , klass , example_group ) : <EOL> for example in self . _examples_in ( klass ) : <EOL> if self . _is_pending_example ( example ) or self . _is_pending_example_group ( example_group ) : <EOL> example_group . append ( PendingExample ( example ) ) <EOL> else : <EOL> example_group . append ( Example ( example ) ) <EOL> def _examples_in ( self , example_group ) : <EOL> return [ method for name , method in self . _methods_for ( example_group ) if self . _is_example ( method ) ] <EOL> def _methods_for ( self , klass ) : <EOL> return inspect . getmembers ( klass , inspect . isfunction if is_python3 ( ) else inspect . ismethod ) <EOL> def _is_example ( self , method ) : <EOL> return method . __name__ [ <NUM_LIT:10> : ] . startswith ( '<STR_LIT>' ) or self . _is_pending_example ( method ) <EOL> def _is_pending_example ( self , example ) : <EOL> return example . __name__ [ <NUM_LIT:10> : ] . startswith ( '<STR_LIT>' ) <EOL> def _is_pending_example_group ( self , example_group ) : <EOL> return isinstance ( example_group , PendingExampleGroup ) <EOL> def _load_nested_example_groups ( self , klass , example_group ) : <EOL> for nested in self . _example_groups_for ( klass ) : <EOL> if isinstance ( example_group , PendingExampleGroup ) : <EOL> nested_example_group = PendingExampleGroup ( self . _subject ( nested ) , execution_context = example_group . execution_context ) <EOL> else : <EOL> nested_example_group = self . _create_example_group ( nested , execution_context = example_group . execution_context ) <EOL> self . _add_hooks_examples_and_nested_example_groups_to ( nested , nested_example_group ) <EOL> example_group . append ( nested_example_group ) <EOL> def _load_helper_methods_to_execution_context ( self , klass , execution_context ) : <EOL> helper_methods = [ method for name , method in self . _methods_for ( klass ) if not self . _is_example ( method ) ] <EOL> for method in helper_methods : <EOL> if is_python3 ( ) : <EOL> setattr ( execution_context , method . __name__ , types . MethodType ( method , execution_context ) ) <EOL> else : <EOL> setattr ( execution_context , method . __name__ , types . MethodType ( method . im_func , execution_context , execution_context . __class__ ) ) </s>
