Ignore:
Timestamp:
Sep 29, 2017, 9:59:40 AM (20 months ago)
Author:
cameron
Message:

Updates for Python 3; some refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/UCD-scripts/UCD_parser.py

    r5652 r5653  
    2121        if m:
    2222            UCD_config.version = m.group(1)
    23             print "Version %s" % m.group(1)
     23            print("Version %s" % m.group(1))
    2424
    2525trivial_name_char_re = re.compile('[-_\s]')
     
    101101                if m.group(1) != '0000' or m.group(2) != '10FFFF': raise Exception("Bad missing spec: " + s)
    102102                cname = canonicalize(m.group(3))
    103                 if not property_lookup_map.has_key(cname): raise Exception("Bad missing property: " + s)
     103                if not cname in property_lookup_map: raise Exception("Bad missing property: " + s)
    104104                missing_specs[property_lookup_map[cname]] = m.group(4)
    105105            continue  # skip comment and blank lines
     
    107107        if not m: raise Exception("Unknown property value alias syntax: %s" % t)
    108108        prop_code = canonicalize(m.group(1))
    109         if not property_lookup_map.has_key(prop_code): raise Exception("Property code: '%s' is unknown" % prop_code)
     109        if not prop_code in property_lookup_map: raise Exception("Property code: '%s' is unknown" % prop_code)
    110110        else: prop_code = property_lookup_map[prop_code]
    111         if not property_value_list.has_key(prop_code):
     111        if not prop_code in property_value_list:
    112112            property_value_list[prop_code] = []
    113113            property_value_enum_integer[prop_code] = {}
     
    177177# Ensure that the default value for the property is first in the list of property values,
    178178# and that all codepoints not explicitly identified in the file are mapped to this default.
    179 def parse_UCD_enumerated_property_map(property_code, vlist, canon_map, mapfile, default_value = None):
     179def parse_UCD_enumerated_property_map(property_code, vlist, canon_map, mapfile):
    180180    value_map = {}
    181181    for v in vlist: value_map[v] = empty_uset()
    182     if default_value == None:
    183         name_list_order = []
    184     else:
    185         # Default value must always be first in the final enumeration order.
    186         name_list_order = [default_value]
     182    name_list_order = []
     183    default_specs = []
    187184    f = open(UCD_config.UCD_src_dir + "/" + mapfile)
    188185    lines = f.readlines()
     
    191188            m = UCD_missing_regexp1.match(t)
    192189            if m:
    193                 if default_value != None:
    194                     raise Exception("Default value already specified, extraneous @missing spec: %s" % t)
    195190                (missing_lo, missing_hi, default_value) = (int(m.group(1), 16), int(m.group(2), 16), m.group(3))
    196191                default_value = canonicalize(default_value)
    197                 if not canon_map.has_key(default_value):  raise Exception("Unknown default property value name '%s'" % default_value)
     192                if not default_value in canon_map:  raise Exception("Unknown default property value name '%s'" % default_value)
    198193                if missing_lo != 0 or missing_hi != 0x10FFFF: raise Exception("Unexpected missing data range '%x, %x'" % (missing_lo, missing_hi))
    199194                default_value = canon_map[default_value]
     
    202197                if default_value in name_list_order: name_list_order.remove(default_value)
    203198                name_list_order = [default_value] + name_list_order
     199                default_specs.append((missing_lo, missing_hi, default_value))
    204200            continue  # skip comment and blank lines
    205201        m = UCD_point_name_regexp.match(t)
     
    213209            newset = range_uset(cp_lo, cp_hi)
    214210        cname = canonicalize(name)
    215         if not canon_map.has_key(cname):  raise Exception("Unknown property or property value name '%s'" % cname)
     211        if not cname in canon_map:  raise Exception("Unknown property or property value name '%s'" % cname)
    216212        name = canon_map[cname]
    217213        if not name in name_list_order:
    218214            name_list_order.append(name)
    219215        value_map[name] = uset_union(value_map[name], newset)
     216    for (default_lo, default_hi, default_val) in default_specs:
     217        value_map = add_Default_Values(value_map, default_lo, default_hi, default_val)
     218    return (name_list_order, value_map)
     219
     220def add_Default_Values(value_map, default_lo, default_hi, default_val):
     221    default_region = range_uset(default_lo, default_hi)
    220222    explicitly_defined_cps = empty_uset()
    221223    for k in value_map.keys(): explicitly_defined_cps = uset_union(explicitly_defined_cps, value_map[k])
    222     need_default_value = uset_complement(explicitly_defined_cps)
    223     if default_value != None:
    224         value_map[default_value] = uset_union(value_map[default_value], need_default_value)
    225     elif uset_popcount(need_default_value) > 0:
    226         print "Warning no default value, but %i codepoints not specified" % uset_popcount(need_default_value)
    227     return (name_list_order, value_map)
     224    need_default_value = uset_difference(default_region, explicitly_defined_cps)
     225    if default_val in value_map:
     226        value_map[default_val] = uset_union(value_map[default_val], need_default_value)
     227    else:
     228        value_map[default_val] = need_default_value
     229    return value_map
    228230
    229231def parse_ScriptExtensions_txt(scripts, canon_map):
     
    239241            # sc = canonical_property_value_map[canonicalize(scx)]
    240242            sc = scx
    241             if value_map.has_key(sc):
     243            if sc in value_map:
    242244                value_map[sc] = uset_union(value_map[sc], scx_set_map[scx_list])
    243245            else: value_map[sc] = scx_set_map[scx_list]
    244246        explicitly_defined_set = uset_union(explicitly_defined_set, scx_set_map[scx_list])
    245247    for v in scripts:
    246         if value_map.has_key(v):
     248        if v in value_map:
    247249            value_map[v] = uset_union(value_map[v], uset_difference(script_map[v], explicitly_defined_set))
    248         elif script_map.has_key(v):
     250        elif v in script_map:
    249251            value_map[v] = script_map[v]
    250252        else: value_map[v] = empty_uset()
     
    271273        if not canon_map == None:
    272274            cname = canonicalize(name)
    273             if not canon_map.has_key(cname):
     275            if not cname in canon_map:
    274276                raise Exception("Unknown property or property value name '%s'" % cname)
    275277            name = canon_map[cname]
    276         if not value_map.has_key(name):
     278        if not name in value_map:
    277279            value_map[name] = newset
    278280            name_list_order.append(name)
Note: See TracChangeset for help on using the changeset viewer.