Ignore:
Timestamp:
Sep 12, 2014, 2:35:29 PM (5 years ago)
Author:
cameron
Message:

Generate PropertyValueAliases?.h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/charsetcompiler/UCD/UCD_properties.py

    r4154 r4155  
    5959namespace UCD {
    6060  enum property_t {
    61     %s
     61%s
    6262  };
    6363  const std::string[] property_full_name = {
    64   %s
     64%s
    6565  };
    6666  const std::unordered_map<std::string, property> alias_map = {
    67   %s
     67%s
    6868  };
    6969}
    7070"""
    7171
    72 enums_per_line = 8
     72def multiline_join(item_list, items_per_line, separator = ",", closer='', indent = 4):
     73  lines = ""
     74  sep_with_space = separator + " "
     75  while len(item_list) > items_per_line:
     76    line_items = item_list[:items_per_line]
     77    lines += (" " * indent) + sep_with_space.join(line_items) + separator + "\n"
     78    item_list = item_list[items_per_line:]
     79  lines += (" " * indent) + sep_with_space.join(item_list) + closer
     80  return lines
     81
     82enums_per_line = 4
    7383def generate_PropertyAliases_h():
    7484   (property_enum_name_list, full_name_map, property_lookup_map) = parse_PropertyAlias_txt()
    7585   f = open('PropertyAliases.h', 'w')
    76    enum_text = property_enum_name_list[0]
    77    for i in range(1, len(property_enum_name_list)):
    78      if i % enums_per_line == 0: enum_text += ",\n    "
    79      else: enum_text += ", "
    80      enum_text += property_enum_name_list[i]
    81    full_name_text = ""
    82    for e in property_enum_name_list:
    83      full_name_text += '  "%s",\n' % (full_name_map[e])
    84    map_text = ""
    85    for k in sorted(property_lookup_map.keys()):
    86      map_text += '  {"%s", %s},\n' % (k, property_lookup_map[k])
     86   enum_text = multiline_join(property_enum_name_list, enums_per_line, ',')
     87   full_name_text = multiline_join(['"%s"' % full_name_map[e] for e in property_enum_name_list], 2, ',')
     88   map_text = multiline_join(['{"%s", %s}' % (k, property_lookup_map[k]) for k in sorted(property_lookup_map.keys())], 2, ',')
    8789   f.write(PropertyAliases_template % (enum_text, full_name_text, map_text))
    8890   f.close()
     
    131133          value_preferred_full_name = extra_list[0]
    132134          value_aliases = extra_list[1:]
     135        # Special case for age: second field is numeric, third field is enum
     136        # treat numeric value as an alias string
     137        elif prop_code == 'age':
     138          value_enum = m.group(3)
     139          value_preferred_full_name = m.group(3)
     140          extra = m.group(4)
     141          value_aliases = [m.group(2)] + re.findall("[-A-Za-z_0-9]+", extra)
    133142        else:
    134143          value_enum = m.group(2)
     
    146155
    147156
    148 
     157PropertyValueAliases_template = r"""
     158namespace UCD {
     159  enum %s_value_t {
     160%s
     161  };
     162  const std::string[] %s_value_name = {
     163%s
     164  };
     165  const std::unordered_map<std::string, property> %s_alias_map = {
     166%s
     167  };
     168}
     169"""
     170
     171def generate_PropertyValueAliases_h():
     172   (property_enum_name_list, full_name_map, property_lookup_map) = parse_PropertyAlias_txt()
     173   (property_value_list, property_value_enum_integer, property_value_full_name_map, property_value_lookup_map) = parse_PropertyValueAlias_txt()
     174   f = open('PropertyValueAliases.h', 'w')
     175   for p in property_enum_name_list:
     176     if property_value_list.has_key(p):
     177       if property_value_list[p] == ['N', 'Y']: continue # skip boolean properties
     178       if p == 'ccc':
     179         enum_text = multiline_join(["%s = %s" % (e, property_value_enum_integer[p][e]) for e in property_value_list['ccc']], 4, ',')
     180       else: enum_text = multiline_join(property_value_list[p], 4, ',')
     181       full_name_text = multiline_join(['"%s"' % (property_value_full_name_map[p][e]) for e in property_value_list[p]], 2, ',')
     182       map_text = multiline_join(['{"%s", %s}' % (k, property_value_lookup_map[p][k]) for k in sorted(property_value_lookup_map[p].keys())], 2, ',')
     183       f.write(PropertyValueAliases_template % (p, enum_text, p, full_name_text, p, map_text))
     184   f.close()
     185   
    149186
    150187
Note: See TracChangeset for help on using the changeset viewer.