<s> import re <EOL> import random <EOL> import utils <EOL> class Manacost : <EOL> '''<STR_LIT>''' <EOL> def get_colors ( self ) : <EOL> colors = '<STR_LIT>' <EOL> for sym in self . symbols : <EOL> if self . symbols [ sym ] > <NUM_LIT:0> : <EOL> symcolors = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , sym ) <EOL> for symcolor in symcolors : <EOL> if symcolor not in colors : <EOL> colors += symcolor <EOL> return '<STR_LIT>' . join ( sorted ( colors ) ) <EOL> def check_colors ( self , symbolstring ) : <EOL> for sym in symbolstring : <EOL> if not sym in self . colors : <EOL> return False <EOL> return True <EOL> def __init__ ( self , src , fmt = '<STR_LIT>' ) : <EOL> self . raw = None <EOL> self . json = None <EOL> self . parsed = True <EOL> self . valid = True <EOL> self . none = False <EOL> self . inner = None <EOL> self . cmc = <NUM_LIT:0> <EOL> self . colorless = <NUM_LIT:0> <EOL> self . sequence = [ ] <EOL> self . symbols = { sym : <NUM_LIT:0> for sym in utils . mana_syms } <EOL> self . allsymbols = { sym : <NUM_LIT:0> for sym in utils . mana_symall } <EOL> self . colors = '<STR_LIT>' <EOL> if fmt == '<STR_LIT>' : <EOL> self . json = src <EOL> text = utils . mana_translate ( self . json . upper ( ) ) <EOL> else : <EOL> self . raw = src <EOL> text = self . raw <EOL> if text == '<STR_LIT>' : <EOL> self . inner = '<STR_LIT>' <EOL> self . none = True <EOL> elif not ( len ( text ) >= <NUM_LIT:2> and text [ <NUM_LIT:0> ] == '<STR_LIT:{>' and text [ - <NUM_LIT:1> ] == '<STR_LIT:}>' ) : <EOL> self . parsed = False <EOL> self . valid = False <EOL> else : <EOL> self . inner = text [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> idx = <NUM_LIT:0> <EOL> while idx < len ( self . inner ) : <EOL> if ( len ( utils . mana_unary_marker ) > <NUM_LIT:0> and <EOL> self . inner [ idx : idx + len ( utils . mana_unary_marker ) ] == utils . mana_unary_marker ) : <EOL> idx += len ( utils . mana_unary_marker ) <EOL> self . sequence += [ utils . mana_unary_marker ] <EOL> elif self . inner [ idx : idx + len ( utils . mana_unary_counter ) ] == utils . mana_unary_counter : <EOL> idx += len ( utils . mana_unary_counter ) <EOL> self . sequence += [ utils . mana_unary_counter ] <EOL> self . colorless += <NUM_LIT:1> <EOL> self . cmc += <NUM_LIT:1> <EOL> else : <EOL> old_idx = idx <EOL> for symlen in range ( utils . mana_symlen_min , utils . mana_symlen_max + <NUM_LIT:1> ) : <EOL> encoded_sym = self . inner [ idx : idx + symlen ] <EOL> if encoded_sym in utils . mana_symall_decode : <EOL> idx += symlen <EOL> self . sequence += [ encoded_sym ] <EOL> sym = utils . mana_symall_decode [ encoded_sym ] <EOL> self . allsymbols [ sym ] += <NUM_LIT:1> <EOL> if sym in utils . mana_symalt : <EOL> self . symbols [ utils . mana_alt ( sym ) ] += <NUM_LIT:1> <EOL> else : <EOL> self . symbols [ sym ] += <NUM_LIT:1> <EOL> if sym == utils . mana_X : <EOL> self . cmc += <NUM_LIT:0> <EOL> elif utils . mana_2 in sym : <EOL> self . cmc += <NUM_LIT:2> <EOL> else : <EOL> self . cmc += <NUM_LIT:1> <EOL> break <EOL> if idx == old_idx : <EOL> idx += <NUM_LIT:1> <EOL> self . valid = False <EOL> self . colors = self . get_colors ( ) <EOL> def __str__ ( self ) : <EOL> if self . none : <EOL> return '<STR_LIT>' <EOL> return utils . mana_untranslate ( utils . mana_open_delimiter + '<STR_LIT>' . join ( self . sequence ) <EOL> + utils . mana_close_delimiter ) <EOL> def format ( self , for_forum = False , for_html = False ) : <EOL> if self . none : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return utils . mana_untranslate ( utils . mana_open_delimiter + '<STR_LIT>' . join ( self . sequence ) <EOL> + utils . mana_close_delimiter , for_forum , for_html ) <EOL> def encode ( self , randomize = False ) : <EOL> if self . none : <EOL> return '<STR_LIT>' <EOL> elif randomize : <EOL> return ( utils . mana_open_delimiter <EOL> + '<STR_LIT>' . join ( random . sample ( self . sequence , len ( self . sequence ) ) ) <EOL> + utils . mana_close_delimiter ) <EOL> else : <EOL> return utils . mana_open_delimiter + '<STR_LIT>' . join ( self . sequence ) + utils . mana_close_delimiter <EOL> def vectorize ( self , delimit = False ) : <EOL> if self . none : <EOL> return '<STR_LIT>' <EOL> elif delimit : <EOL> ld = '<STR_LIT:(>' <EOL> rd = '<STR_LIT:)>' <EOL> else : <EOL> ld = '<STR_LIT>' <EOL> rd = '<STR_LIT>' <EOL> return '<STR_LIT:U+0020>' . join ( map ( lambda s : ld + s + rd , self . sequence ) ) <EOL> class Manatext : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , src , fmt = '<STR_LIT>' ) : <EOL> self . raw = None <EOL> self . json = None <EOL> self . valid = True <EOL> self . text = src <EOL> self . costs = [ ] <EOL> if fmt == '<STR_LIT>' : <EOL> self . json = src <EOL> manastrs = re . findall ( utils . mana_json_regex , src ) <EOL> else : <EOL> self . raw = src <EOL> manastrs = re . findall ( utils . mana_regex , src ) <EOL> for manastr in manastrs : <EOL> cost = Manacost ( manastr , fmt ) <EOL> if not cost . valid : <EOL> self . valid = False <EOL> self . costs += [ cost ] <EOL> self . text = self . text . replace ( manastr , utils . reserved_mana_marker , <NUM_LIT:1> ) <EOL> if ( utils . mana_open_delimiter in self . text <EOL> or utils . mana_close_delimiter in self . text <EOL> or utils . mana_json_open_delimiter in self . text <EOL> or utils . mana_json_close_delimiter in self . text ) : <EOL> self . valid = False <EOL> def __str__ ( self ) : <EOL> text = self . text <EOL> for cost in self . costs : <EOL> text = text . replace ( utils . reserved_mana_marker , str ( cost ) , <NUM_LIT:1> ) <EOL> return text <EOL> def format ( self , for_forum = False , for_html = False ) : <EOL> text = self . text <EOL> for cost in self . costs : <EOL> text = text . replace ( utils . reserved_mana_marker , cost . format ( for_forum = for_forum , for_html = for_html ) , <NUM_LIT:1> ) <EOL> if for_html : <EOL> text = text . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> return text <EOL> def encode ( self , randomize = False ) : <EOL> text = self . text <EOL> for cost in self . costs : <EOL> text = text . replace ( utils . reserved_mana_marker , cost . encode ( randomize = randomize ) , <NUM_LIT:1> ) <EOL> return text <EOL> def vectorize ( self ) : <EOL> text = self . text <EOL> special_chars = [ utils . reserved_mana_marker , <EOL> utils . dash_marker , <EOL> utils . bullet_marker , <EOL> utils . this_marker , <EOL> utils . counter_marker , <EOL> utils . choice_open_delimiter , <EOL> utils . choice_close_delimiter , <EOL> utils . newline , <EOL> utils . tap_marker , <EOL> utils . untap_marker , <EOL> utils . newline , <EOL> '<STR_LIT:;>' , '<STR_LIT::>' , '<STR_LIT:">' , '<STR_LIT:U+002C>' , '<STR_LIT:.>' ] <EOL> for char in special_chars : <EOL> text = text . replace ( char , '<STR_LIT:U+0020>' + char + '<STR_LIT:U+0020>' ) <EOL> text = text . replace ( '<STR_LIT:/>' , '<STR_LIT>' ) <EOL> for cost in self . costs : <EOL> text = text . replace ( utils . reserved_mana_marker , cost . vectorize ( ) , <NUM_LIT:1> ) <EOL> return '<STR_LIT:U+0020>' . join ( text . split ( ) ) </s>
