## Revision 79a04823

b/autotools/convert-constants
84 84
```    return None
```
85 85

86 86

87
```def ConvertVariable(prefix, name, value):
```
87
```def IdentifyOrigin(all_items, value):
```
88
```  """Tries to identify a constant name from a constant's value.
```
89

90
```  This uses a simple algorithm: is there a constant (and only one)
```
91
```  with the same value? If so, then it returns that constants' name.
```
92

93
```  @note: it is recommended to use this only for tuples/lists/sets, and
```
94
```      not for individual (top-level) values
```
95
```  @param all_items: a dictionary of name/values for the current module
```
96
```  @param value: the value for which we try to find an origin
```
97

98
```  """
```
99
```  found = [name for (name, v) in all_items.items() if v is value]
```
100
```  if len(found) == 1:
```
101
```    return found[0]
```
102
```  else:
```
103
```    return None
```
104

105

106
```def FormatListElems(all_items, pfx_name, ovals, tvals):
```
107
```  """Formats a list's elements.
```
108

109
```  This formats the elements as either values or, if we find all
```
110
```  origins, as names.
```
111

112
```  @param all_items: a dictionary of name/values for the current module
```
113
```  @param pfx_name: the prefix name currently used
```
114
```  @param ovals: the list of actual (Python) values
```
115
```  @param tvals: the list of values we want to format in the Haskell form
```
116

117
```  """
```
118
```  origins = [IdentifyOrigin(all_items, v) for v in ovals]
```
119
```  if compat.all(x is not None for x in origins):
```
120
```    values = [NameRules(pfx_name + origin) for origin in origins]
```
121
```  else:
```
122
```    values = tvals
```
123
```  return ", ".join(values)
```
124

125

126
```def ConvertVariable(prefix, name, value, all_items):
```
88 127
```  """Converts a given variable to Haskell code.
```
89 128

90 129
```  @param prefix: a prefix for the Haskell name (useful for module
```
91 130
```      identification)
```
92 131
```  @param name: the Python name
```
93 132
```  @param value: the value
```
133
```  @param all_items: a dictionary of name/value for the module being
```
134
```      processed
```
94 135
```  @return: a list of Haskell code lines
```
95 136

96 137
```  """
```
......
121 162
```    if value:
```
122 163
```      lines.append("-- Following lines come from dictionary %s" % fqn)
```
123 164
```      for k in sorted(value.keys()):
```
124
```        lines.extend(ConvertVariable(prefix, DictKeyName(name, k), value[k]))
```
165
```        lines.extend(ConvertVariable(prefix, DictKeyName(name, k),
```
166
```                                     value[k], all_items))
```
125 167
```  elif isinstance(value, tuple):
```
126 168
```    tvs = [HaskellTypeVal(elem) for elem in value]
```
127 169
```    if compat.all(e is not None for e in tvs):
```
128 170
```      ttypes = ", ".join(e[0] for e in tvs)
```
129
```      tvals = ", ".join(e[1] for e in tvs)
```
171
```      tvals = FormatListElems(all_items, pfx_name, value, [e[1] for e in tvs])
```
130 172
```      lines.append("-- | Converted from Python tuple %s" % fqn)
```
131 173
```      lines.append("%s :: (%s)" % (hs_name, ttypes))
```
132 174
```      lines.append("%s = (%s)" % (hs_name, tvals))
```
......
145 187
```      ttypes, tvals = zip(*tvs)
```
146 188
```      uniq_types = set(ttypes)
```
147 189
```      if len(uniq_types) == 1:
```
190
```        values = FormatListElems(all_items, pfx_name, value, tvals)
```
148 191
```        lines.append("-- | Converted from Python list or set %s" % fqn)
```
149 192
```        lines.append("%s :: [%s]" % (hs_name, uniq_types.pop()))
```
150
```        lines.append("%s = [%s]" % (hs_name, ", ".join(tvals)))
```
193
```        lines.append("%s = [%s]" % (hs_name, values))
```
151 194
```      else:
```
152 195
```        lines.append("-- | Skipped list/set %s, is not homogeneous" % fqn)
```
153 196
```    else:
```
......
169 212
```  """
```
170 213
```  lines = [""]
```
171 214

172
```  all_names = dir(module)
```
215
```  all_items = dict((name, getattr(module, name)) for name in dir(module))
```
173 216

174
```  for name in all_names:
```
175
```    value = getattr(module, name)
```
176
```    new_lines = ConvertVariable(prefix, name, value)
```
217
```  for name in sorted(all_items.keys()):
```
218
```    value = all_items[name]
```
219
```    new_lines = ConvertVariable(prefix, name, value, all_items)
```
177 220
```    if new_lines:
```
178 221
```      lines.extend(new_lines)
```
179 222
```      lines.append("")
```

Also available in: Unified diff