<s> from __future__ import unicode_literals , absolute_import <EOL> from six import PY2 , PY3 , StringIO , string_types , text_type , integer_types <EOL> from six . moves import filter , map , range <EOL> from collections import namedtuple <EOL> from . utils import get_lines <EOL> Extractor = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> class Component ( object ) : <EOL> _TYPE = "<STR_LIT>" <EOL> @ classmethod <EOL> def _from_container ( cls , container , * args , ** kwargs ) : <EOL> if cls . _TYPE == "<STR_LIT>" : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> k = cls ( ) <EOL> k . _classmethod_args = args <EOL> k . _classmethod_kwargs = kwargs <EOL> k . _populate ( container ) <EOL> return k <EOL> def _populate ( self , container ) : <EOL> if container . name != self . _TYPE : <EOL> raise ValueError ( "<STR_LIT>" . format ( self . _TYPE ) ) <EOL> for extractor in self . _EXTRACTORS : <EOL> lines = get_lines ( container , extractor . type ) <EOL> if not lines and extractor . required : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> . format ( container . name , extractor . type ) ) <EOL> if not extractor . multiple and len ( lines ) > <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> . format ( container . name , extractor . type ) ) <EOL> if extractor . multiple : <EOL> extractor . function ( self , lines ) <EOL> else : <EOL> if len ( lines ) == <NUM_LIT:1> : <EOL> extractor . function ( self , lines [ <NUM_LIT:0> ] ) <EOL> else : <EOL> extractor . function ( self , None ) <EOL> self . _unused = container <EOL> @ classmethod <EOL> def _extracts ( cls , line_type , required = False , multiple = False ) : <EOL> def decorator ( fn ) : <EOL> extractor = Extractor ( <EOL> function = fn , <EOL> type = line_type , <EOL> required = required , <EOL> multiple = multiple ) <EOL> cls . _EXTRACTORS . append ( extractor ) <EOL> return fn <EOL> return decorator <EOL> @ classmethod <EOL> def _outputs ( cls , fn ) : <EOL> cls . _OUTPUTS . append ( fn ) <EOL> return fn <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . __urepr__ ( ) . encode ( '<STR_LIT:utf-8>' ) if PY2 else self . __urepr__ ( ) <EOL> else : <EOL> t = self . __class__ . __name__ <EOL> adress = hex ( id ( self ) ) <EOL> return '<STR_LIT>' . format ( t , adress ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> container = self . _unused . clone ( ) <EOL> for output in self . _OUTPUTS : <EOL> output ( self , container ) <EOL> return str ( container ) </s>
