<s> """<STR_LIT>""" <EOL> from collections import OrderedDict <EOL> import warnings <EOL> import vimdoc <EOL> from vimdoc import error <EOL> from vimdoc import paragraph <EOL> from vimdoc import regex <EOL> class Block ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type = None , is_secondary = False , is_default = False ) : <EOL> self . locals = { } <EOL> if type is not None : <EOL> self . SetType ( type ) <EOL> self . globals = { } <EOL> self . header = None <EOL> self . paragraphs = paragraph . Paragraphs ( ) <EOL> self . _required_args = [ ] <EOL> self . _optional_args = [ ] <EOL> self . _closed = False <EOL> self . _is_secondary = is_secondary <EOL> self . _is_default = is_default <EOL> def AddLine ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if self . paragraphs . IsType ( paragraph . CodeBlock ) : <EOL> if line . startswith ( '<STR_LIT:<>' ) : <EOL> self . paragraphs . Close ( ) <EOL> line = line [ <NUM_LIT:1> : ] . lstrip ( ) <EOL> if line : <EOL> self . AddLine ( line ) <EOL> return <EOL> if line [ : <NUM_LIT:1> ] not in '<STR_LIT>' : <EOL> self . paragraphs . Close ( ) <EOL> self . AddLine ( line ) <EOL> return <EOL> self . paragraphs . AddLine ( line ) <EOL> return <EOL> self . _ParseArgs ( line ) <EOL> if not line . strip ( ) : <EOL> self . paragraphs . SetType ( paragraph . BlankLine ) <EOL> return <EOL> match = regex . list_item . match ( line or '<STR_LIT>' ) <EOL> if match : <EOL> leader = match . group ( <NUM_LIT:1> ) <EOL> self . paragraphs . Close ( ) <EOL> line = regex . list_item . sub ( '<STR_LIT>' , line ) <EOL> self . paragraphs . SetType ( paragraph . ListItem , leader ) <EOL> self . paragraphs . AddLine ( line ) <EOL> return <EOL> if line and line [ : <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> if self . paragraphs . IsType ( paragraph . ListItem ) : <EOL> self . paragraphs . AddLine ( line . lstrip ( ) ) <EOL> return <EOL> elif self . paragraphs . IsType ( paragraph . ListItem ) : <EOL> self . paragraphs . Close ( ) <EOL> self . paragraphs . SetType ( paragraph . TextParagraph ) <EOL> if line == '<STR_LIT:>>' or line . endswith ( '<STR_LIT>' ) : <EOL> line = line [ : - <NUM_LIT:1> ] . rstrip ( ) <EOL> if line : <EOL> self . paragraphs . AddLine ( line ) <EOL> self . paragraphs . SetType ( paragraph . CodeBlock ) <EOL> return <EOL> self . paragraphs . AddLine ( line ) <EOL> def Global ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . SetType ( True ) <EOL> for key , value in kwargs . items ( ) : <EOL> if key in self . globals : <EOL> raise error . RedundantControl ( key ) <EOL> self . globals [ key ] = value <EOL> def Local ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . SetType ( True ) <EOL> for key , value in kwargs . items ( ) : <EOL> if key in self . locals and self . locals [ key ] != value : <EOL> raise error . InconsistentControl ( key , self . locals [ key ] , value ) <EOL> self . locals [ key ] = value <EOL> def SetType ( self , newtype ) : <EOL> """<STR_LIT>""" <EOL> ourtype = self . locals . get ( '<STR_LIT:type>' ) <EOL> if newtype is True or newtype == ourtype : <EOL> self . locals [ '<STR_LIT:type>' ] = ourtype or newtype <EOL> return <EOL> if ourtype is None or ourtype is True : <EOL> self . locals [ '<STR_LIT:type>' ] = newtype <EOL> else : <EOL> raise error . TypeConflict ( ourtype , newtype ) <EOL> def SetHeader ( self , directive ) : <EOL> """<STR_LIT>""" <EOL> if self . header : <EOL> raise error . MultipleErrors <EOL> self . header = directive <EOL> self . paragraphs . Close ( ) <EOL> def AddSubHeader ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . paragraphs . SetType ( paragraph . SubHeaderLine , name ) <EOL> def Default ( self , arg , value ) : <EOL> """<STR_LIT>""" <EOL> self . _ParseArgs ( value ) <EOL> if arg not in self . _optional_args : <EOL> self . _optional_args . append ( arg ) <EOL> self . paragraphs . SetType ( paragraph . DefaultLine , arg , value ) <EOL> def Except ( self , typ , description ) : <EOL> """<STR_LIT>""" <EOL> description = description or '<STR_LIT>' <EOL> self . _ParseArgs ( description ) <EOL> self . paragraphs . SetType ( paragraph . ExceptionLine , typ , description ) <EOL> def Close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _closed : <EOL> return <EOL> self . _closed = True <EOL> if self . locals . get ( '<STR_LIT:type>' ) is True and '<STR_LIT>' in self . locals : <EOL> self . SetType ( vimdoc . DICTIONARY ) <EOL> if ( self . locals . get ( '<STR_LIT:type>' ) in [ vimdoc . FUNCTION , vimdoc . COMMAND ] <EOL> and '<STR_LIT>' not in self . locals ) : <EOL> if not self . header : <EOL> from vimdoc . docline import Usage <EOL> self . header = Usage ( '<STR_LIT>' ) <EOL> self . locals [ '<STR_LIT>' ] = self . header . GenerateUsage ( self ) <EOL> if '<STR_LIT>' in self . locals and self . locals . get ( '<STR_LIT:type>' ) != vimdoc . FUNCTION : <EOL> raise error . InvalidBlock ( '<STR_LIT>' ) <EOL> return self <EOL> def RequiredArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . locals . get ( '<STR_LIT:type>' ) == vimdoc . FUNCTION : <EOL> sigargs = [ a for a in self . locals . get ( '<STR_LIT:args>' ) if a != '<STR_LIT>' ] <EOL> if not self . _required_args : <EOL> return sigargs <EOL> if not set ( self . _required_args ) . difference ( sigargs ) : <EOL> return sigargs <EOL> if len ( self . _required_args ) == len ( sigargs ) : <EOL> return self . _required_args <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( sigargs , self . _required_args ) , <EOL> error . ArgumentMismatch ) <EOL> return self . _required_args <EOL> def OptionalArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . locals . get ( '<STR_LIT:type>' ) == vimdoc . FUNCTION <EOL> and self . _optional_args <EOL> and '<STR_LIT>' not in self . locals . get ( '<STR_LIT:args>' ) ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . FullName ( ) ) , <EOL> error . DocumentationWarning ) <EOL> return ( ) <EOL> return self . _optional_args <EOL> def LocalName ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . locals . get ( '<STR_LIT:type>' ) == vimdoc . DICTIONARY : <EOL> return self . locals [ '<STR_LIT>' ] <EOL> if '<STR_LIT:name>' not in self . locals : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> return self . locals [ '<STR_LIT:name>' ] <EOL> def FullName ( self ) : <EOL> """<STR_LIT>""" <EOL> typ = self . locals . get ( '<STR_LIT:type>' ) <EOL> if typ == vimdoc . FUNCTION : <EOL> if '<STR_LIT>' in self . locals : <EOL> attribute = self . locals . get ( '<STR_LIT>' , self . LocalName ( ) ) <EOL> return '<STR_LIT>' . format ( self . locals [ '<STR_LIT>' ] , attribute ) <EOL> if '<STR_LIT>' in self . locals : <EOL> return '<STR_LIT>' . format ( self . locals [ '<STR_LIT>' ] or self . LocalName ( ) ) <EOL> return self . locals . get ( '<STR_LIT>' , '<STR_LIT>' ) + self . LocalName ( ) <EOL> return self . LocalName ( ) <EOL> def TagName ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_secondary : <EOL> return None <EOL> typ = self . locals . get ( '<STR_LIT:type>' ) <EOL> if typ == vimdoc . FUNCTION : <EOL> if '<STR_LIT>' not in self . locals : <EOL> return '<STR_LIT>' . format ( self . FullName ( ) ) <EOL> if typ == vimdoc . COMMAND : <EOL> return '<STR_LIT>' . format ( self . FullName ( ) ) <EOL> return self . FullName ( ) <EOL> def IsDefault ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _is_default <EOL> def _ParseArgs ( self , args ) : <EOL> self . _required_args = list ( OrderedDict . fromkeys ( <EOL> self . _required_args + regex . required_arg . findall ( args ) ) ) <EOL> self . _optional_args = list ( OrderedDict . fromkeys ( <EOL> self . _optional_args + regex . optional_arg . findall ( args ) ) ) <EOL> def __repr__ ( self ) : <EOL> try : <EOL> name = self . FullName ( ) <EOL> except KeyError : <EOL> name = '<STR_LIT:?>' <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , name ) <EOL> def __lt__ ( self , other ) : <EOL> return self . FullName ( ) < other . FullName ( ) </s>
