forked from livebook-dev/pythonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.ex
More file actions
26 lines (20 loc) · 786 Bytes
/
Copy pathast.ex
File metadata and controls
26 lines (20 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
defmodule Pythonx.AST do
@moduledoc false
@scan_globals_py "lib/pythonx/ast/scan_globals.py"
@external_resource @scan_globals_py
@scan_globals_code File.read!(@scan_globals_py)
@doc """
Analyzes globals in the given Python code.
Returns a map with two entries:
* `:referenced` - names of referenced globals that are not defined
prior in the code
* `:defined` - names of new globals defined in the code
"""
@spec scan_globals(String.t()) :: %{referenced: names, defined: names}
when names: MapSet.t(String.t())
def scan_globals(code) when is_binary(code) do
{result, %{}} = Pythonx.eval(@scan_globals_code, %{"code" => code})
{referenced, defined} = Pythonx.decode(result)
%{referenced: referenced, defined: defined}
end
end