Ejemplo n.º 1
0
def text_unpass_1_choice(s, delimit=False):
    choice_regex = (re.escape(choice_open_delimiter) +
                    re.escape(unary_marker) + r'.*' +
                    re.escape(bullet_marker) + r'.*' +
                    re.escape(choice_close_delimiter))
    choices = re.findall(choice_regex, s)
    for choice in sorted(choices,
                         lambda x, y: cmp(len(x), len(y)),
                         reverse=True):
        fragments = choice[1:-1].split(bullet_marker)
        countfrag = fragments[0]
        optfrags = fragments[1:]
        choicecount = int(
            utils.from_unary(
                re.findall(utils.number_unary_regex, countfrag)[0]))
        newchoice = ''

        if choicecount == 0:
            if len(countfrag) == 2:
                newchoice += 'choose one or both '
            else:
                newchoice += 'choose one or more '
        elif choicecount == 1:
            newchoice += 'choose one '
        elif choicecount == 2:
            newchoice += 'choose two '
        else:
            newchoice += 'choose ' + utils.to_unary(str(choicecount)) + ' '
        newchoice += dash_marker

        for option in optfrags:
            option = option.strip()
            if option:
                newchoice += newline + bullet_marker + ' ' + option

        if delimit:
            s = s.replace(
                choice,
                choice_open_delimiter + newchoice + choice_close_delimiter)
            s = s.replace('an opponent ' + choice_open_delimiter + 'choose ',
                          'an opponent ' + choice_open_delimiter + 'chooses ')
        else:
            s = s.replace(choice, newchoice)
            s = s.replace('an opponent choose ', 'an opponent chooses ')

    return s
Ejemplo n.º 2
0
def text_unpass_1_choice(s, delimit = False):
    choice_regex = (re.escape(choice_open_delimiter) + re.escape(unary_marker)
                    + r'.*' + re.escape(bullet_marker) + r'.*' + re.escape(choice_close_delimiter))
    choices = re.findall(choice_regex, s)
    for choice in sorted(choices, lambda x,y: cmp(len(x), len(y)), reverse = True):
        fragments = choice[1:-1].split(bullet_marker)
        countfrag = fragments[0]
        optfrags = fragments[1:]
        choicecount = int(utils.from_unary(re.findall(utils.number_unary_regex, countfrag)[0]))
        newchoice = ''

        if choicecount == 0:
            if len(countfrag) == 2:
                newchoice += 'choose one or both '
            else:
                newchoice += 'choose one or more '
        elif choicecount == 1:
            newchoice += 'choose one '
        elif choicecount == 2:
            newchoice += 'choose two '
        else:
            newchoice += 'choose ' + utils.to_unary(str(choicecount)) + ' '
        newchoice += dash_marker
        
        for option in optfrags:
            option = option.strip()
            if option:
                newchoice += newline + bullet_marker + ' ' + option

        if delimit:
            s = s.replace(choice, choice_open_delimiter + newchoice + choice_close_delimiter)
            s = s.replace('an opponent ' + choice_open_delimiter + 'choose ', 
                          'an opponent ' + choice_open_delimiter + 'chooses ')
        else:
            s = s.replace(choice, newchoice)
            s = s.replace('an opponent choose ', 'an opponent chooses ')
    
    return s
Ejemplo n.º 3
0
def fields_from_json(src_json):
    parsed = True
    valid = True
    fields = {}

    # we hardcode in what the things are called in the mtgjson format
    if 'name' in src_json:
        name_val = src_json['name'].lower()
        name_orig = name_val
        name_val = transforms.name_pass_1_sanitize(name_val)
        name_val = utils.to_ascii(name_val)
        fields[field_name] = [(-1, name_val)]
    else:
        name_orig = ''
        parsed = False

    # return the actual Manacost object
    if 'manaCost' in src_json:
        cost =  Manacost(src_json['manaCost'], fmt = 'json')
        valid = valid and cost.valid
        parsed = parsed and cost.parsed
        fields[field_cost] = [(-1, cost)]

    if 'supertypes' in src_json:
        fields[field_supertypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), 
                                             src_json['supertypes']))]

    if 'types' in src_json:
        fields[field_types] = [(-1, map(lambda s: utils.to_ascii(s.lower()), 
                                        src_json['types']))]
    else:
        parsed = False

    if 'subtypes' in src_json:
        fields[field_subtypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), 
                                           src_json['subtypes']))]

    if 'rarity' in src_json:
        if src_json['rarity'] in utils.json_rarity_map:
            fields[field_rarity] = [(-1, utils.json_rarity_map[src_json['rarity']])]
        else:
            fields[field_rarity] = [(-1, src_json['rarity'])]
            parsed = False
    else:
        parsed = False

    if 'loyalty' in src_json:
        fields[field_loyalty] = [(-1, utils.to_unary(str(src_json['loyalty'])))]

    p_t = ''
    parsed_pt = True
    if 'power' in src_json:
        p_t = utils.to_ascii(utils.to_unary(src_json['power'])) + '/' # hardcoded
        parsed_pt = False
        if 'toughness' in src_json:
            p_t = p_t + utils.to_ascii(utils.to_unary(src_json['toughness']))
            parsed_pt = True
    elif 'toughness' in src_json:
        p_t = '/' + utils.to_ascii(utils.to_unary(src_json['toughness'])) # hardcoded
        parsed_pt = False
    if p_t:
        fields[field_pt] = [(-1, p_t)]
    parsed = parsed and parsed_pt
        
    # similarly, return the actual Manatext object
    if 'text' in src_json:
        text_val = src_json['text'].lower()
        text_val = transforms.text_pass_1_strip_rt(text_val)
        text_val = transforms.text_pass_2_cardname(text_val, name_orig)
        text_val = transforms.text_pass_3_unary(text_val)
        text_val = transforms.text_pass_4a_dashes(text_val)
        text_val = transforms.text_pass_4b_x(text_val)
        text_val = transforms.text_pass_5_counters(text_val)
        text_val = transforms.text_pass_6_uncast(text_val)
        text_val = transforms.text_pass_7_choice(text_val)
        text_val = transforms.text_pass_8_equip(text_val)
        text_val = transforms.text_pass_9_newlines(text_val)
        text_val = transforms.text_pass_10_symbols(text_val)
        text_val = utils.to_ascii(text_val)
        text_val = text_val.strip()
        mtext = Manatext(text_val, fmt = 'json')
        valid = valid and mtext.valid
        fields[field_text] = [(-1, mtext)]
    
    # we don't need to worry about bsides because we handle that in the constructor
    return parsed, valid and fields_check_valid(fields), fields
Ejemplo n.º 4
0
def text_pass_3_unary(s):
    return utils.to_unary(s)
Ejemplo n.º 5
0
def fields_from_json(src_json, linetrans=True):
    parsed = True
    valid = True
    fields = {}

    # we hardcode in what the things are called in the mtgjson format
    if 'name' in src_json:
        name_val = src_json['name'].lower()
        name_orig = name_val
        name_val = transforms.name_pass_1_sanitize(name_val)
        name_val = utils.to_ascii(name_val)
        fields[field_name] = [(-1, name_val)]
    else:
        name_orig = ''
        parsed = False

    # return the actual Manacost object
    if 'manaCost' in src_json:
        cost = Manacost(src_json['manaCost'], fmt='json')
        valid = valid and cost.valid
        parsed = parsed and cost.parsed
        fields[field_cost] = [(-1, cost)]

    if 'supertypes' in src_json:
        fields[field_supertypes] = [(-1,
                                     map(lambda s: utils.to_ascii(s.lower()),
                                         src_json['supertypes']))]

    if 'types' in src_json:
        fields[field_types] = [(-1,
                                map(lambda s: utils.to_ascii(s.lower()),
                                    src_json['types']))]
    else:
        parsed = False

    if 'subtypes' in src_json:
        fields[field_subtypes] = [(
            -1,
            map(
                lambda s: utils.to_ascii(s.lower())
                # urza's lands...
                .replace('"', "'").replace('-', utils.dash_marker),
                src_json['subtypes']))]

    if 'rarity' in src_json:
        if src_json['rarity'] in utils.json_rarity_map:
            fields[field_rarity] = [
                (-1, utils.json_rarity_map[src_json['rarity']])
            ]
        else:
            fields[field_rarity] = [(-1, src_json['rarity'])]
            parsed = False
    else:
        parsed = False

    if 'loyalty' in src_json:
        fields[field_loyalty] = [(-1, utils.to_unary(str(src_json['loyalty'])))
                                 ]

    p_t = ''
    parsed_pt = True
    if 'power' in src_json:
        p_t = utils.to_ascii(utils.to_unary(
            src_json['power'])) + '/'  # hardcoded
        parsed_pt = False
        if 'toughness' in src_json:
            p_t = p_t + utils.to_ascii(utils.to_unary(src_json['toughness']))
            parsed_pt = True
    elif 'toughness' in src_json:
        p_t = '/' + utils.to_ascii(utils.to_unary(
            src_json['toughness']))  # hardcoded
        parsed_pt = False
    if p_t:
        fields[field_pt] = [(-1, p_t)]
    parsed = parsed and parsed_pt

    # similarly, return the actual Manatext object
    if 'text' in src_json:
        text_val = src_json['text'].lower()
        text_val = transforms.text_pass_1_strip_rt(text_val)
        text_val = transforms.text_pass_2_cardname(text_val, name_orig)
        text_val = transforms.text_pass_3_unary(text_val)
        text_val = transforms.text_pass_4a_dashes(text_val)
        text_val = transforms.text_pass_4b_x(text_val)
        text_val = transforms.text_pass_5_counters(text_val)
        text_val = transforms.text_pass_6_uncast(text_val)
        text_val = transforms.text_pass_7_choice(text_val)
        text_val = transforms.text_pass_8_equip(text_val)
        text_val = transforms.text_pass_9_newlines(text_val)
        text_val = transforms.text_pass_10_symbols(text_val)
        if linetrans:
            text_val = transforms.text_pass_11_linetrans(text_val)
        text_val = utils.to_ascii(text_val)
        text_val = text_val.strip()
        mtext = Manatext(text_val, fmt='json')
        valid = valid and mtext.valid
        fields[field_text] = [(-1, mtext)]

    # we don't need to worry about bsides because we handle that in the constructor
    return parsed, valid and fields_check_valid(fields), fields
Ejemplo n.º 6
0
def fields_from_json(src_json, linetrans=True):
    parsed = True
    valid = True
    fields = {}

    # we hardcode in what the things are called in the mtgjson format
    if "name" in src_json:
        name_val = src_json["name"].lower()
        name_orig = name_val
        name_val = transforms.name_pass_1_sanitize(name_val)
        name_val = utils.to_ascii(name_val)
        fields[field_name] = [(-1, name_val)]
    else:
        name_orig = ""
        parsed = False

    # return the actual Manacost object
    if "manaCost" in src_json:
        cost = Manacost(src_json["manaCost"], fmt="json")
        valid = valid and cost.valid
        parsed = parsed and cost.parsed
        fields[field_cost] = [(-1, cost)]

    if "supertypes" in src_json:
        fields[field_supertypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json["supertypes"]))]

    if "types" in src_json:
        fields[field_types] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json["types"]))]
    else:
        parsed = False

    if "subtypes" in src_json:
        fields[field_subtypes] = [
            (
                -1,
                map(
                    lambda s: utils.to_ascii(s.lower())
                    # urza's lands...
                    .replace('"', "'").replace("-", utils.dash_marker),
                    src_json["subtypes"],
                ),
            )
        ]

    if "rarity" in src_json:
        if src_json["rarity"] in utils.json_rarity_map:
            fields[field_rarity] = [(-1, utils.json_rarity_map[src_json["rarity"]])]
        else:
            fields[field_rarity] = [(-1, src_json["rarity"])]
            parsed = False
    else:
        parsed = False

    if "loyalty" in src_json:
        fields[field_loyalty] = [(-1, utils.to_unary(str(src_json["loyalty"])))]

    p_t = ""
    parsed_pt = True
    if "power" in src_json:
        p_t = utils.to_ascii(utils.to_unary(src_json["power"])) + "/"  # hardcoded
        parsed_pt = False
        if "toughness" in src_json:
            p_t = p_t + utils.to_ascii(utils.to_unary(src_json["toughness"]))
            parsed_pt = True
    elif "toughness" in src_json:
        p_t = "/" + utils.to_ascii(utils.to_unary(src_json["toughness"]))  # hardcoded
        parsed_pt = False
    if p_t:
        fields[field_pt] = [(-1, p_t)]
    parsed = parsed and parsed_pt

    # similarly, return the actual Manatext object
    if "text" in src_json:
        text_val = src_json["text"].lower()
        text_val = transforms.text_pass_1_strip_rt(text_val)
        text_val = transforms.text_pass_2_cardname(text_val, name_orig)
        text_val = transforms.text_pass_3_unary(text_val)
        text_val = transforms.text_pass_4a_dashes(text_val)
        text_val = transforms.text_pass_4b_x(text_val)
        text_val = transforms.text_pass_5_counters(text_val)
        text_val = transforms.text_pass_6_uncast(text_val)
        text_val = transforms.text_pass_7_choice(text_val)
        text_val = transforms.text_pass_8_equip(text_val)
        text_val = transforms.text_pass_9_newlines(text_val)
        text_val = transforms.text_pass_10_symbols(text_val)
        if linetrans:
            text_val = transforms.text_pass_11_linetrans(text_val)
        text_val = utils.to_ascii(text_val)
        text_val = text_val.strip()
        mtext = Manatext(text_val, fmt="json")
        valid = valid and mtext.valid
        fields[field_text] = [(-1, mtext)]

    # we don't need to worry about bsides because we handle that in the constructor
    return parsed, valid and fields_check_valid(fields), fields
Ejemplo n.º 7
0
def fields_from_json(src_json, linetrans = True, addspaces = False):
    parsed = True
    valid = True
    fields = {}

    # we hardcode in what the things are called in the mtgjson format
    if 'name' in src_json:
        name_val = src_json['name'].lower()
        name_orig = name_val
        name_val = transforms.name_pass_1_sanitize(name_val)
        name_val = utils.to_ascii(name_val)
        
        #RMM: Testing out the idea of chopping up names.
        if addspaces:
            def intersperse(lst, item):
                result = [item] * (len(lst) * 2 - 1)
                result[0::2] = lst
                return result
            name_val = intersperse(name_val, ' ')
        fields[field_name] = [(-1, name_val)]
    else:
        name_orig = ''
        parsed = False

    # return the actual Manacost object
    if 'manaCost' in src_json:
        cost =  Manacost(src_json['manaCost'], fmt = 'json')
        valid = valid and cost.valid
        parsed = parsed and cost.parsed
        fields[field_cost] = [(-1, cost)]

    if 'supertypes' in src_json:
        fields[field_supertypes] = [(-1, [utils.to_ascii(s.lower()) for s in src_json['supertypes']])]

    if 'types' in src_json:
        fields[field_types] = [(-1, [utils.to_ascii(s.lower()) for s in src_json['types']])]
    else:
        parsed = False

    if 'subtypes' in src_json:
        fields[field_subtypes] = [(-1, [utils.to_ascii(s.lower())
                                           # urza's lands...
                                           .replace('"', "'").replace('-', utils.dash_marker) for s in src_json['subtypes']])]
        

    if 'rarity' in src_json:
        if src_json['rarity'] in utils.json_rarity_map:
            fields[field_rarity] = [(-1, utils.json_rarity_map[src_json['rarity']])]
        else:
            fields[field_rarity] = [(-1, src_json['rarity'])]
            parsed = False
    else:
        parsed = False

    if 'loyalty' in src_json:
        fields[field_loyalty] = [(-1, utils.to_unary(str(src_json['loyalty'])))]

    p_t = ''
    parsed_pt = True
    if 'power' in src_json:
        p_t = utils.to_ascii(utils.to_unary(src_json['power'])) + ' / ' # hardcoded
        parsed_pt = False
        if 'toughness' in src_json:
            p_t = p_t + utils.to_ascii(utils.to_unary(src_json['toughness']))
            parsed_pt = True
    elif 'toughness' in src_json:
        p_t = ' / ' + utils.to_ascii(utils.to_unary(src_json['toughness'])) # hardcoded
        parsed_pt = False
    if p_t:
        fields[field_pt] = [(-1, p_t)]
    parsed = parsed and parsed_pt
        
    # similarly, return the actual Manatext object
    if 'text' in src_json:
        text_val = src_json['text'].lower()
        text_val = transforms.text_pass_1_strip_rt(text_val)
        text_val = transforms.text_pass_2_cardname(text_val, name_orig)
        text_val = transforms.text_pass_3_unary(text_val)
        text_val = transforms.text_pass_4a_dashes(text_val)
        text_val = transforms.text_pass_4b_x(text_val)
        text_val = transforms.text_pass_5_counters(text_val)
        text_val = transforms.text_pass_6_uncast(text_val)
        text_val = transforms.text_pass_7_choice(text_val)
        text_val = transforms.text_pass_8_equip(text_val)
        text_val = transforms.text_pass_9_newlines(text_val)
        text_val = transforms.text_pass_10_symbols(text_val)
        if linetrans:
            text_val = transforms.text_pass_11_linetrans(text_val)
        if addspaces:
            text_val = transforms.text_pass_12_addspaces(text_val)
        text_val = utils.to_ascii(text_val)
        text_val = text_val.strip()
        
        mtext = Manatext(text_val, fmt = 'json')
        valid = valid and mtext.valid
        fields[field_text] = [(-1, mtext)]
    
    # we don't need to worry about bsides because we handle that in the constructor
    return parsed, valid and fields_check_valid(fields), fields
Ejemplo n.º 8
0
def text_pass_3_unary(s):
    return utils.to_unary(s)