<s> import re <EOL> import string <EOL> import random <EOL> choice = random . choice <EOL> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> opentag = '<STR_LIT>' <EOL> closetag = '<STR_LIT>' <EOL> DEBUG = <NUM_LIT:0> <EOL> FILTERWHITESPACE = <NUM_LIT:1> <EOL> randint = random . randint <EOL> vowels = '<STR_LIT>' <EOL> class BlurbWriter ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , content , debug = False ) : <EOL> self . DEBUG = debug <EOL> self . data = { } <EOL> self . _cache = { } <EOL> self . formatdict = None <EOL> self . formatfunc = None <EOL> self . lasttag = '<STR_LIT>' <EOL> self . choicetree = [ ] <EOL> p = '<STR_LIT>' <EOL> self . tag = re . compile ( p , re . IGNORECASE ) <EOL> p = '<STR_LIT>' <EOL> self . pstatement = re . compile ( p , re . IGNORECASE ) <EOL> self . allkeys = self . keys ( ) <EOL> self . importcontent ( content ) <EOL> def importcontent ( self , contentdict ) : <EOL> self . data . update ( contentdict ) <EOL> dk = self . data . keys ( ) <EOL> dk . sort ( ) <EOL> self . keywords = dk <EOL> def keyindex ( self , key ) : <EOL> if key in self . allkeys : <EOL> return self . allkeys . index ( key ) <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def keys ( self ) : <EOL> k = self . data . keys ( ) <EOL> k . sort ( ) <EOL> return self . data . keys ( ) <EOL> def has_key ( self , key ) : <EOL> if self . _cache . has_key ( key ) : <EOL> return <NUM_LIT:1> <EOL> if self . data . has_key ( key ) : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def alternatives ( self , key ) : <EOL> return self [ key ] <EOL> def choice ( self , cached , key ) : <EOL> if self . has_key ( cached ) : <EOL> return <NUM_LIT:1> , self [ cached ] [ <NUM_LIT:0> ] <EOL> items = self [ key ] <EOL> if len ( items ) > <NUM_LIT:0> : <EOL> i = randint ( <NUM_LIT:0> , len ( items ) - <NUM_LIT:1> ) <EOL> self . choicetree . append ( ( self . keyindex ( key ) , i ) ) <EOL> return i , items [ i ] <EOL> else : <EOL> self . choicetree . append ( ( - <NUM_LIT:1> , - <NUM_LIT:1> ) ) <EOL> return - <NUM_LIT:1> , '<STR_LIT>' <EOL> def getvalue ( self , key ) : <EOL> return self [ key ] <EOL> def __getitem__ ( self , key ) : <EOL> if self . _cache . has_key ( key ) : <EOL> return self . _cache [ key ] <EOL> if self . data . has_key ( key ) : <EOL> return self . data [ key ] <EOL> return '<STR_LIT>' + key + '<STR_LIT>' <EOL> def define ( self , key , value ) : <EOL> if DEBUG : <EOL> print '<STR_LIT>' , key , value <EOL> self . _cache [ key ] = [ value ] <EOL> def softdefine ( self , key , value ) : <EOL> if DEBUG : <EOL> print '<STR_LIT>' , key , value <EOL> if not self . _cache . has_key ( key ) : <EOL> self . _cache [ key ] = [ value ] <EOL> def clearcache ( self , key = None ) : <EOL> if not key : <EOL> self . _cache = { } <EOL> else : <EOL> if self . _cache . has_key ( key ) : <EOL> del self . _cache [ key ] <EOL> def parsetag ( self , tag ) : <EOL> hard = <NUM_LIT:0> <EOL> parts = string . split ( tag , '<STR_LIT:U+002C>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> name = string . strip ( parts [ - <NUM_LIT:1> ] ) <EOL> cmd = parts [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> variable = None <EOL> return name , cmd , variable , hard <EOL> parts = string . split ( tag , '<STR_LIT:=>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> hard = <NUM_LIT:1> <EOL> name = string . strip ( parts [ - <NUM_LIT:1> ] ) <EOL> cmd = [ ] <EOL> variable = string . join ( parts [ <NUM_LIT:0> : - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> return name , cmd , variable , hard <EOL> parts = string . split ( tag , '<STR_LIT::>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> hard = <NUM_LIT:0> <EOL> name = string . strip ( parts [ - <NUM_LIT:1> ] ) <EOL> cmd = [ ] <EOL> variable = string . join ( parts [ <NUM_LIT:0> : - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> return name , cmd , variable , hard <EOL> else : <EOL> hard = <NUM_LIT:0> <EOL> name = tag <EOL> cmd = [ ] <EOL> variable = None <EOL> return name , cmd , variable , hard <EOL> def write ( self , tag , formatdict = None , formatfunc = None ) : <EOL> '''<STR_LIT>''' <EOL> return self . writetag ( opentag + tag + closetag , formatdict , formatfunc ) <EOL> def writetag ( self , tag , formatdict = None , formatfunc = None ) : <EOL> '''<STR_LIT>''' <EOL> self . allkeys = self . keys ( ) <EOL> self . choicetree = [ ] <EOL> if not formatdict : <EOL> self . formatdict = { } <EOL> else : <EOL> self . formatdict = formatdict <EOL> self . formatfunc = formatfunc <EOL> _ , item = self . replacetag ( <NUM_LIT:0> , tag ) <EOL> _ , item = self . replacecode ( item ) <EOL> if FILTERWHITESPACE : <EOL> return string . join ( string . split ( item ) , '<STR_LIT:U+0020>' ) <EOL> return item <EOL> def __write ( self , tag , formatdict = None , formatfunc = None ) : <EOL> '''<STR_LIT>''' <EOL> if tag [ : <NUM_LIT:2> ] < > opentag : <EOL> tag = opentag + tag + closetag <EOL> print '<STR_LIT>' , tag <EOL> name , cmd , var , hard = self . parsetag ( tag ) <EOL> print name , cmd , var , hard <EOL> self . allkeys = self . keys ( ) <EOL> self . choicetree = [ ] <EOL> if DEBUG > <NUM_LIT:2> : <EOL> print '<STR_LIT>' , name , cmd <EOL> if not formatdict : <EOL> self . formatdict = { } <EOL> else : <EOL> self . formatdict = formatdict <EOL> self . formatfunc = formatfunc <EOL> _ , item = self . choice ( var , name ) <EOL> self . pos = <NUM_LIT:0> <EOL> _ , item = self . replacetag ( <NUM_LIT:0> , item ) <EOL> _ , item = self . replacecode ( item ) <EOL> if var and hard : <EOL> self . define ( var , item ) <EOL> elif var and <NUM_LIT:1> : <EOL> self . softdefine ( var , item ) <EOL> if FILTERWHITESPACE : <EOL> return string . join ( string . split ( item ) , '<STR_LIT:U+0020>' ) <EOL> return item <EOL> def replacecode ( self , text ) : <EOL> m = <NUM_LIT:1> <EOL> pend = <NUM_LIT:0> <EOL> while m < > None : <EOL> m = self . pstatement . search ( text , pend ) <EOL> if m == None : <EOL> return <NUM_LIT:0> , text <EOL> tag = m . group ( '<STR_LIT>' ) <EOL> if len ( tag ) == <NUM_LIT:0> : <EOL> raise '<STR_LIT>' <EOL> return <NUM_LIT:0> , '<STR_LIT>' <EOL> try : <EOL> result = eval ( tag ) <EOL> except : <EOL> result = '<STR_LIT>' + tag + '<STR_LIT>' <EOL> if not isinstance ( result , basestring ) : <EOL> result = str ( result ) <EOL> parts = string . split ( text , '<STR_LIT>' + tag + '<STR_LIT>' ) <EOL> text = parts [ <NUM_LIT:0> ] + result + string . join ( parts [ <NUM_LIT:1> : ] , '<STR_LIT>' + tag + '<STR_LIT>' ) <EOL> return <NUM_LIT:1> , text <EOL> def capsentence ( self , s ) : <EOL> ss = string . split ( s , '<STR_LIT>' ) <EOL> new = [ ] <EOL> for i in ss : <EOL> new . append ( string . upper ( i [ <NUM_LIT:0> ] ) + i [ <NUM_LIT:1> : ] ) <EOL> return string . join ( new , '<STR_LIT>' ) <EOL> def nextopen ( self , pos , text ) : <EOL> return string . find ( text , opentag , pos ) <EOL> def nextclosed ( self , pos , text ) : <EOL> return string . find ( text , closetag , pos ) <EOL> def nexttag ( self , pos , text ) : <EOL> start = self . nextopen ( pos , text ) <EOL> stop = self . nextclosed ( pos , text ) <EOL> if start == - <NUM_LIT:1> : <EOL> if stop == - <NUM_LIT:1> : <EOL> return - <NUM_LIT:1> , - <NUM_LIT:1> <EOL> return <NUM_LIT:0> , stop <EOL> if start < stop and start < > - <NUM_LIT:1> : <EOL> return <NUM_LIT:1> , start <EOL> else : <EOL> return <NUM_LIT:0> , stop <EOL> def findtag ( self , text ) : <EOL> p = - <NUM_LIT:1> <EOL> last = None , None <EOL> while <NUM_LIT:1> : <EOL> kind , p = self . nexttag ( p + <NUM_LIT:1> , text ) <EOL> if last [ <NUM_LIT:0> ] == <NUM_LIT:1> and kind == <NUM_LIT:0> : <EOL> if self . DEBUG : <EOL> print text [ last [ <NUM_LIT:1> ] + len ( opentag ) : p ] <EOL> return last [ <NUM_LIT:1> ] + len ( opentag ) , p <EOL> if ( kind , p ) == ( - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> break <EOL> last = kind , p <EOL> return None , None <EOL> def replacetag ( self , level , text ) : <EOL> level = level + <NUM_LIT:1> <EOL> if level > <NUM_LIT:100> : <EOL> raise '<STR_LIT>' , self . lasttag <EOL> m = <NUM_LIT:1> <EOL> while m < > None : <EOL> start , stop = self . findtag ( text ) <EOL> if start == None : <EOL> return <NUM_LIT:0> , text <EOL> tag = text [ start : stop ] <EOL> if len ( tag ) == <NUM_LIT:0> : <EOL> raise '<STR_LIT>' <EOL> return <NUM_LIT:0> , '<STR_LIT>' <EOL> metatext = text <EOL> metatag = tag <EOL> while string . find ( tag , opentag ) < > - <NUM_LIT:1> : <EOL> _ , metatag = self . replacetag ( level , tag ) <EOL> parts = string . split ( metatext , tag ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> tag = string . join ( parts , metatag ) <EOL> else : <EOL> tag = metatag <EOL> return self . replacetag ( level , tag ) <EOL> tagname , cmd , variable , hard = self . parsetag ( tag ) <EOL> cachethis = <NUM_LIT:0> <EOL> setarticle = <NUM_LIT:0> <EOL> uppered = <NUM_LIT:0> <EOL> formatcmds = [ ] <EOL> for i in cmd : <EOL> si = string . strip ( i ) <EOL> if not si : <EOL> continue <EOL> if si == '<STR_LIT>' : <EOL> setarticle = <NUM_LIT:1> <EOL> elif si [ <NUM_LIT:0> ] == '<STR_LIT:!>' : <EOL> formatcmds . append ( si [ <NUM_LIT:1> : ] ) <EOL> elif si [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> uppered = <NUM_LIT:1> <EOL> parts = string . split ( text , opentag + tag + closetag ) <EOL> if self . has_key ( tagname ) : <EOL> _ , selection = self . choice ( variable , tagname ) <EOL> _ , c = self . replacetag ( level , selection ) <EOL> else : <EOL> _ , c = self . replacetag ( level , '<STR_LIT>' + tagname + '<STR_LIT>' ) <EOL> self . lasttag = c <EOL> art = '<STR_LIT>' <EOL> if setarticle : <EOL> if c [ <NUM_LIT:0> ] in vowels : <EOL> art = '<STR_LIT>' <EOL> else : <EOL> art = '<STR_LIT>' <EOL> if uppered and c : <EOL> c = string . upper ( c [ <NUM_LIT:0> ] ) + c [ <NUM_LIT:1> : ] <EOL> if len ( formatcmds ) > <NUM_LIT:0> and self . formatfunc : <EOL> for fc in formatcmds : <EOL> if DEBUG : <EOL> print '<STR_LIT>' , fc , c <EOL> if not self . formatdict . has_key ( fc ) : <EOL> continue <EOL> c = self . formatfunc ( c , self . formatdict [ fc ] ) <EOL> if DEBUG : <EOL> print '<STR_LIT>' , fc , c <EOL> vardef = c <EOL> if variable : <EOL> c = '<STR_LIT>' <EOL> try : <EOL> text = parts [ <NUM_LIT:0> ] + art + c + string . join ( parts [ <NUM_LIT:1> : ] , opentag + tag + closetag ) <EOL> except : <EOL> print '<STR_LIT>' <EOL> pass <EOL> if variable or cachethis : <EOL> if variable : <EOL> tg = variable <EOL> else : <EOL> tg = tagname <EOL> if hard : <EOL> self . define ( tg , vardef ) <EOL> else : <EOL> self . softdefine ( tg , vardef ) <EOL> return <NUM_LIT:0> , text </s>
