MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / node_to_value

Function node_to_value

src/packagedcode/pypi_setup_py.py:116–146  ·  view source on GitHub ↗

Return the extracted and converted value of a node or None

(node, body)

Source from the content-addressed store, hash-verified

114
115
116def node_to_value(node, body):
117 """
118 Return the extracted and converted value of a node or None
119 """
120 if node is None:
121 return
122
123 if isinstance(node, ast.Constant):
124 return node.value
125
126 if isinstance(node, (ast.List, ast.Tuple, ast.Set,)):
127 return [node_to_value(subnode, body) for subnode in node.elts]
128
129 if isinstance(node, ast.Dict):
130 result = {}
131 for key, value in zip(node.keys, node.values):
132 result[node_to_value(key, body)] = node_to_value(value, body)
133 return result
134
135 if isinstance(node, ast.Name):
136 variable = find_variable_in_body(body, node.id)
137 if variable is not None:
138 return node_to_value(variable, body)
139
140 if isinstance(node, ast.Call):
141 if not isinstance(node.func, ast.Name):
142 return
143 if node.func.id != 'dict':
144 return
145 return get_call_kwargs(node, body)
146 return
147
148
149def find_variable_in_body(body, name):

Callers 1

get_call_kwargsFunction · 0.85

Calls 2

find_variable_in_bodyFunction · 0.85
get_call_kwargsFunction · 0.85

Tested by

no test coverage detected