<s> import re <EOL> from . comments import CommentCache , Comments <EOL> from . settings import Documentation , MetadataList <EOL> class Populator ( object ) : <EOL> """<STR_LIT>""" <EOL> def add ( self , row ) : <EOL> raise NotImplementedError <EOL> def populate ( self ) : <EOL> raise NotImplementedError <EOL> class NullPopulator ( Populator ) : <EOL> def add ( self , row ) : <EOL> pass <EOL> def populate ( self ) : <EOL> pass <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> class _TablePopulator ( Populator ) : <EOL> def __init__ ( self , table ) : <EOL> self . _table = table <EOL> self . _populator = NullPopulator ( ) <EOL> self . _comment_cache = CommentCache ( ) <EOL> def add ( self , row ) : <EOL> if self . _is_cacheable_comment_row ( row ) : <EOL> self . _comment_cache . add ( row ) <EOL> else : <EOL> self . _add ( row ) <EOL> def _is_cacheable_comment_row ( self , row ) : <EOL> return row . is_commented ( ) <EOL> def _add ( self , row ) : <EOL> if self . _is_continuing ( row ) : <EOL> self . _consume_comments ( ) <EOL> else : <EOL> self . _populator . populate ( ) <EOL> self . _populator = self . _get_populator ( row ) <EOL> self . _consume_standalone_comments ( ) <EOL> self . _populator . add ( row ) <EOL> def _is_continuing ( self , row ) : <EOL> return row . is_continuing ( ) and self . _populator <EOL> def _get_populator ( self , row ) : <EOL> raise NotImplementedError <EOL> def _consume_comments ( self ) : <EOL> self . _comment_cache . consume_with ( self . _populator . add ) <EOL> def _consume_standalone_comments ( self ) : <EOL> self . _consume_comments ( ) <EOL> def populate ( self ) : <EOL> self . _consume_comments ( ) <EOL> self . _populator . populate ( ) <EOL> class SettingTablePopulator ( _TablePopulator ) : <EOL> def _get_populator ( self , row ) : <EOL> row . handle_old_style_metadata ( ) <EOL> setter = self . _table . get_setter ( row . head ) <EOL> if not setter : <EOL> return NullPopulator ( ) <EOL> if setter . im_class is Documentation : <EOL> return DocumentationPopulator ( setter ) <EOL> if setter . im_class is MetadataList : <EOL> return MetadataPopulator ( setter ) <EOL> return SettingPopulator ( setter ) <EOL> class VariableTablePopulator ( _TablePopulator ) : <EOL> def _get_populator ( self , row ) : <EOL> return VariablePopulator ( self . _table . add , row . head ) <EOL> def _consume_standalone_comments ( self ) : <EOL> self . _comment_cache . consume_with ( self . _populate_standalone_comment ) <EOL> def _populate_standalone_comment ( self , comment ) : <EOL> populator = self . _get_populator ( comment ) <EOL> populator . add ( comment ) <EOL> populator . populate ( ) <EOL> def populate ( self ) : <EOL> self . _populator . populate ( ) <EOL> self . _consume_standalone_comments ( ) <EOL> class _StepContainingTablePopulator ( _TablePopulator ) : <EOL> def _is_continuing ( self , row ) : <EOL> return row . is_indented ( ) and self . _populator or row . is_commented ( ) <EOL> def _is_cacheable_comment_row ( self , row ) : <EOL> return row . is_commented ( ) and not self . _populator <EOL> class TestTablePopulator ( _StepContainingTablePopulator ) : <EOL> def _get_populator ( self , row ) : <EOL> return TestCasePopulator ( self . _table . add ) <EOL> class KeywordTablePopulator ( _StepContainingTablePopulator ) : <EOL> def _get_populator ( self , row ) : <EOL> return UserKeywordPopulator ( self . _table . add ) <EOL> class ForLoopPopulator ( Populator ) : <EOL> def __init__ ( self , for_loop_creator ) : <EOL> self . _for_loop_creator = for_loop_creator <EOL> self . _loop = None <EOL> self . _populator = NullPopulator ( ) <EOL> self . _declaration = [ ] <EOL> self . _declaration_comments = [ ] <EOL> def add ( self , row ) : <EOL> dedented_row = row . dedent ( ) <EOL> if not self . _loop : <EOL> declaration_ready = self . _populate_declaration ( row ) <EOL> if not declaration_ready : <EOL> return <EOL> self . _create_for_loop ( ) <EOL> if not row . is_continuing ( ) : <EOL> self . _populator . populate ( ) <EOL> self . _populator = StepPopulator ( self . _loop . add_step ) <EOL> self . _populator . add ( dedented_row ) <EOL> def _populate_declaration ( self , row ) : <EOL> if row . starts_for_loop ( ) or row . is_continuing ( ) : <EOL> self . _declaration . extend ( row . dedent ( ) . data ) <EOL> self . _declaration_comments . extend ( row . comments ) <EOL> return False <EOL> return True <EOL> def _create_for_loop ( self ) : <EOL> self . _loop = self . _for_loop_creator ( self . _declaration , <EOL> self . _declaration_comments ) <EOL> def populate ( self ) : <EOL> if not self . _loop : <EOL> self . _create_for_loop ( ) <EOL> self . _populator . populate ( ) <EOL> class _TestCaseUserKeywordPopulator ( Populator ) : <EOL> def __init__ ( self , test_or_uk_creator ) : <EOL> self . _test_or_uk_creator = test_or_uk_creator <EOL> self . _test_or_uk = None <EOL> self . _populator = NullPopulator ( ) <EOL> self . _comment_cache = CommentCache ( ) <EOL> def add ( self , row ) : <EOL> if row . is_commented ( ) : <EOL> self . _comment_cache . add ( row ) <EOL> return <EOL> if not self . _test_or_uk : <EOL> self . _test_or_uk = self . _test_or_uk_creator ( row . head ) <EOL> dedented_row = row . dedent ( ) <EOL> if dedented_row : <EOL> self . _handle_data_row ( dedented_row ) <EOL> def _handle_data_row ( self , row ) : <EOL> if not self . _continues ( row ) : <EOL> self . _populator . populate ( ) <EOL> self . _populator = self . _get_populator ( row ) <EOL> self . _comment_cache . consume_with ( self . _populate_comment_row ) <EOL> else : <EOL> self . _comment_cache . consume_with ( self . _populator . add ) <EOL> self . _populator . add ( row ) <EOL> def _populate_comment_row ( self , crow ) : <EOL> populator = StepPopulator ( self . _test_or_uk . add_step ) <EOL> populator . add ( crow ) <EOL> populator . populate ( ) <EOL> def populate ( self ) : <EOL> self . _populator . populate ( ) <EOL> self . _comment_cache . consume_with ( self . _populate_comment_row ) <EOL> def _get_populator ( self , row ) : <EOL> if row . starts_test_or_user_keyword_setting ( ) : <EOL> setter = self . _setting_setter ( row ) <EOL> if not setter : <EOL> return NullPopulator ( ) <EOL> if setter . im_class is Documentation : <EOL> return DocumentationPopulator ( setter ) <EOL> return SettingPopulator ( setter ) <EOL> if row . starts_for_loop ( ) : <EOL> return ForLoopPopulator ( self . _test_or_uk . add_for_loop ) <EOL> return StepPopulator ( self . _test_or_uk . add_step ) <EOL> def _continues ( self , row ) : <EOL> return row . is_continuing ( ) and self . _populator or ( isinstance ( self . _populator , ForLoopPopulator ) and row . is_indented ( ) ) <EOL> def _setting_setter ( self , row ) : <EOL> setting_name = row . test_or_user_keyword_setting_name ( ) <EOL> return self . _test_or_uk . get_setter ( setting_name ) <EOL> class TestCasePopulator ( _TestCaseUserKeywordPopulator ) : <EOL> _item_type = '<STR_LIT>' <EOL> class UserKeywordPopulator ( _TestCaseUserKeywordPopulator ) : <EOL> _item_type = '<STR_LIT>' <EOL> class _PropertyPopulator ( Populator ) : <EOL> def __init__ ( self , setter ) : <EOL> self . _setter = setter <EOL> self . _value = [ ] <EOL> self . _comments = Comments ( ) <EOL> self . _data_added = False <EOL> def add ( self , row ) : <EOL> if not row . is_commented ( ) : <EOL> self . _add ( row ) <EOL> self . _comments . add ( row ) <EOL> def _add ( self , row ) : <EOL> self . _value . extend ( row . tail if not self . _data_added else row . data ) <EOL> self . _data_added = True <EOL> class VariablePopulator ( _PropertyPopulator ) : <EOL> def __init__ ( self , setter , name ) : <EOL> _PropertyPopulator . __init__ ( self , setter ) <EOL> self . _name = name <EOL> def populate ( self ) : <EOL> self . _setter ( self . _name , self . _value , self . _comments . value ) <EOL> class SettingPopulator ( _PropertyPopulator ) : <EOL> def populate ( self ) : <EOL> self . _setter ( self . _value , self . _comments . value ) <EOL> class DocumentationPopulator ( _PropertyPopulator ) : <EOL> _end_of_line_escapes = re . compile ( r'<STR_LIT>' ) <EOL> def populate ( self ) : <EOL> self . _setter ( self . _value , self . _comments . value ) <EOL> def _add ( self , row ) : <EOL> self . _add_to_value ( row . dedent ( ) . data ) <EOL> def _add_to_value ( self , data ) : <EOL> joiner = self . _row_joiner ( ) <EOL> if joiner : <EOL> self . _value . append ( joiner ) <EOL> self . _value . append ( '<STR_LIT:U+0020>' . join ( data ) ) <EOL> def _row_joiner ( self ) : <EOL> if self . _is_empty ( ) : <EOL> return None <EOL> return self . _joiner_based_on_eol_escapes ( ) <EOL> def _is_empty ( self ) : <EOL> return not self . _value or ( len ( self . _value ) == <NUM_LIT:1> and self . _value [ <NUM_LIT:0> ] == '<STR_LIT>' ) <EOL> def _joiner_based_on_eol_escapes ( self ) : <EOL> match = self . _end_of_line_escapes . search ( self . _value [ - <NUM_LIT:1> ] ) <EOL> if not match or len ( match . group ( <NUM_LIT:1> ) ) % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> if not match . group ( <NUM_LIT:0> ) . endswith ( '<STR_LIT:n>' ) : <EOL> return '<STR_LIT:U+0020>' <EOL> return None <EOL> class MetadataPopulator ( DocumentationPopulator ) : <EOL> def __init__ ( self , setter ) : <EOL> _PropertyPopulator . __init__ ( self , setter ) <EOL> self . _name = None <EOL> def populate ( self ) : <EOL> self . _setter ( self . _name , self . _value , self . _comments . value ) <EOL> def _add ( self , row ) : <EOL> data = row . dedent ( ) . data <EOL> if self . _name is None : <EOL> self . _name = data [ <NUM_LIT:0> ] if data else '<STR_LIT>' <EOL> data = data [ <NUM_LIT:1> : ] <EOL> self . _add_to_value ( data ) <EOL> class StepPopulator ( _PropertyPopulator ) : <EOL> def _add ( self , row ) : <EOL> self . _value . extend ( row . data ) <EOL> def populate ( self ) : <EOL> if self . _value or self . _comments : <EOL> self . _setter ( self . _value , self . _comments . value ) </s>
