<s> from exceptions import NemoException <EOL> PERMISSIVE = True <EOL> class Node ( object ) : <EOL> is_root = False <EOL> follows_indentation_rules = True <EOL> def __init__ ( self , value , depth , line_number ) : <EOL> self . value = value <EOL> self . depth = depth <EOL> self . line_number = line_number <EOL> self . parent = None <EOL> self . children = [ ] <EOL> self . siblings = [ ] <EOL> def add_child ( self , node ) : <EOL> raise NotImplemented ( ) <EOL> def check_as_closer ( self , node , active_node ) : <EOL> """<STR_LIT>""" <EOL> raise NemoException ( '<STR_LIT>' + '<STR_LIT>' % node + '<STR_LIT>' % self + '<STR_LIT>' % active_node ) <EOL> def write ( self , buffer ) : <EOL> raise NotImplemented ( ) <EOL> def __str__ ( self ) : <EOL> return str ( unicode ( self ) ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . depth , self . line_number , self . value ) <EOL> class NemoNode ( Node ) : <EOL> @ property <EOL> def value ( self ) : <EOL> return '<STR_LIT>' % ( self . _keyword , self . _arguments ) <EOL> @ value . setter <EOL> def value ( self , value ) : <EOL> self . _keyword , self . _arguments = value <EOL> def add_child ( self , node ) : <EOL> self . children . append ( node ) <EOL> node . parent = self <EOL> def _padding ( self ) : <EOL> return [ '<STR_LIT:U+0020>' for i in xrange ( <NUM_LIT:1> , self . depth ) ] <EOL> def write ( self , buffer ) : <EOL> buffer . write ( '<STR_LIT:\n>' ) <EOL> buffer . writelines ( self . _padding ( ) ) <EOL> buffer . writelines ( [ '<STR_LIT:<>' , self . _keyword , '<STR_LIT:U+0020>' , self . _arguments ] ) <EOL> if len ( self . children ) is <NUM_LIT:0> : <EOL> buffer . write ( '<STR_LIT>' ) <EOL> else : <EOL> buffer . write ( '<STR_LIT:>>' ) <EOL> self . _write_children ( buffer ) <EOL> buffer . write ( '<STR_LIT:\n>' ) <EOL> buffer . writelines ( self . _padding ( ) ) <EOL> buffer . writelines ( [ '<STR_LIT>' , self . _keyword , '<STR_LIT:>>' ] ) <EOL> def check_indentation_rules ( self , children ) : <EOL> depth_seen = None <EOL> for child in children : <EOL> if child . follows_indentation_rules and not PERMISSIVE : <EOL> if depth_seen is None : <EOL> depth_seen = child . depth <EOL> elif child . depth is not depth_seen : <EOL> raise NemoException ( '<STR_LIT>' + '<STR_LIT>' % child + '<STR_LIT>' % self + '<STR_LIT>' % depth_seen ) <EOL> yield child <EOL> def check_open_close_on_mako_nodes ( self , children ) : <EOL> open_mako_context = None <EOL> for child in children : <EOL> child_type = type ( child ) <EOL> if child_type is MakoNode and open_mako_context is None : <EOL> open_mako_context = child <EOL> if child_type is MakoEndTag : <EOL> if open_mako_context is None : <EOL> raise NemoException ( '<STR_LIT>' + '<STR_LIT>' % child + '<STR_LIT>' % self ) <EOL> open_mako_context = None <EOL> yield child <EOL> if open_mako_context is not None : <EOL> raise NemoException ( '<STR_LIT>' + '<STR_LIT>' % open_mako_context + '<STR_LIT>' % self ) <EOL> def _write_children ( self , buffer ) : <EOL> """<STR_LIT>""" <EOL> children = self . check_open_close_on_mako_nodes ( <EOL> self . check_indentation_rules ( <EOL> self . children ) ) <EOL> for child in children : <EOL> child . write ( buffer ) <EOL> class MakoNode ( NemoNode ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , depth , line_number ) : <EOL> super ( MakoNode , self ) . __init__ ( value = ( value , '<STR_LIT>' ) , depth = depth , line_number = line_number ) <EOL> def add_child ( self , node ) : <EOL> self . children . append ( node ) <EOL> node . parent = self <EOL> def write ( self , buffer ) : <EOL> buffer . write ( "<STR_LIT:\n>" ) <EOL> buffer . write ( self . value ) <EOL> self . _write_children ( buffer ) <EOL> def check_as_closer ( self , node , active_node ) : <EOL> """<STR_LIT>""" <EOL> if self is active_node : <EOL> return <EOL> potentially_closed = active_node . parent <EOL> while potentially_closed is not None : <EOL> if potentially_closed . depth == node . depth : <EOL> return <EOL> elif potentially_closed . depth < node . depth : <EOL> raise NemoException ( '<STR_LIT>' + '<STR_LIT>' % node + '<STR_LIT>' % self + '<STR_LIT>' % active_node ) <EOL> potentially_closed = potentially_closed . parent <EOL> class NemoRoot ( NemoNode ) : <EOL> """<STR_LIT>""" <EOL> is_root = True <EOL> def __init__ ( self ) : <EOL> super ( NemoRoot , self ) . __init__ ( ( '<STR_LIT>' , None ) , - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def write ( self , buffer ) : <EOL> self . _write_children ( buffer ) <EOL> def _write_children ( self , buffer ) : <EOL> """<STR_LIT>""" <EOL> children = self . check_open_close_on_mako_nodes ( <EOL> self . children ) <EOL> for child in children : <EOL> child . write ( buffer ) <EOL> class Leaf ( Node ) : <EOL> """<STR_LIT>""" <EOL> follows_indentation_rules = False <EOL> def write ( self , buffer = None ) : <EOL> buffer . write ( "<STR_LIT:\n>" ) <EOL> buffer . write ( self . value ) <EOL> def add_child ( self , node ) : <EOL> raise NemoException ( '<STR_LIT>' % ( node , self ) ) <EOL> class MakoEndTag ( Leaf ) : <EOL> """<STR_LIT>""" <EOL> follows_indentation_rules = True <EOL> pass </s>
