| 2222 | raise ValueError |
| 2223 | |
| 2224 | class RewriteSymbolics(ast.NodeTransformer): |
| 2225 | def visit_Attribute(self, node): |
| 2226 | a = [] |
| 2227 | n = node |
| 2228 | while isinstance(n, ast.Attribute): |
| 2229 | a.append(n.attr) |
| 2230 | n = n.value |
| 2231 | if not isinstance(n, ast.Name): |
| 2232 | raise ValueError |
| 2233 | a.append(n.id) |
| 2234 | value = ".".join(reversed(a)) |
| 2235 | return wrap_value(value) |
| 2236 | |
| 2237 | def visit_Name(self, node): |
| 2238 | if not isinstance(node.ctx, ast.Load): |
| 2239 | raise ValueError() |
| 2240 | return wrap_value(node.id) |
| 2241 | |
| 2242 | def visit_BinOp(self, node): |
| 2243 | # Support constant folding of a couple simple binary operations |
| 2244 | # commonly used to define default values in text signatures |
| 2245 | left = self.visit(node.left) |
| 2246 | right = self.visit(node.right) |
| 2247 | if not isinstance(left, ast.Constant) or not isinstance(right, ast.Constant): |
| 2248 | raise ValueError |
| 2249 | if isinstance(node.op, ast.Add): |
| 2250 | return ast.Constant(left.value + right.value) |
| 2251 | elif isinstance(node.op, ast.Sub): |
| 2252 | return ast.Constant(left.value - right.value) |
| 2253 | elif isinstance(node.op, ast.BitOr): |
| 2254 | return ast.Constant(left.value | right.value) |
| 2255 | raise ValueError |
| 2256 | |
| 2257 | def p(name_node, default_node, default=empty): |
| 2258 | name = parse_name(name_node) |