@@ -8,10 +8,23 @@ defmodule Pythonx do
88
99 @ moduledoc readme_docs
1010
11+ defstruct [
12+ :python_dl_path ,
13+ :python_home_path ,
14+ :python_executable_path ,
15+ :sys_paths
16+ ]
17+
1118 alias Pythonx.Object
1219
1320 @ install_env_name "PYTHONX_FLAME_INIT_STATE"
1421
22+ @ type init_state :: % __MODULE__ {
23+ python_dl_path: String . t ( ) ,
24+ python_home_path: String . t ( ) ,
25+ python_executable_path: String . t ( ) ,
26+ sys_paths: [ String . t ( ) ]
27+ }
1528 @ type encoder :: ( term ( ) , encoder ( ) -> Object . t ( ) )
1629
1730 @ doc ~s'''
@@ -60,25 +73,20 @@ defmodule Pythonx do
6073 opts = Keyword . validate! ( opts , force: false , uv_version: Pythonx.Uv . default_uv_version ( ) )
6174
6275 Pythonx.Uv . fetch ( pyproject_toml , false , opts )
63- install_paths = Pythonx.Uv . init ( pyproject_toml , false , Keyword . take ( opts , [ :uv_version ] ) )
64-
65- init_state = % {
66- type: :uv_init ,
67- pyproject_toml: pyproject_toml ,
68- opts: Keyword . drop ( opts , [ :force ] ) ,
69- install_paths: install_paths
70- }
71-
76+ init_state = Pythonx.Uv . init ( pyproject_toml , false , Keyword . take ( opts , [ :uv_version ] ) )
7277 :persistent_term . put ( :pythonx_init_state , init_state )
7378 end
7479
75- @ spec init_state ( ) :: map ( )
80+ @ spec init_state ( ) :: init_state ( )
7681 defp init_state ( ) do
7782 :persistent_term . get ( :pythonx_init_state )
7883 end
7984
85+ @ doc ~s'''
86+ Fetches the pythonx init state from the system environment variable.
87+ '''
8088 @ spec init_state_from_env ( ) :: String . t ( ) | nil
81- defp init_state_from_env ( ) , do: System . get_env ( @ install_env_name )
89+ def init_state_from_env ( ) , do: System . get_env ( @ install_env_name )
8290
8391 @ doc ~s'''
8492 Returns a map containing the environment variables required to initialize Pythonx.
@@ -90,7 +98,7 @@ defmodule Pythonx do
9098 |> :erlang . term_to_binary ( )
9199 |> Base . encode64 ( )
92100
93- % { @ install_env_name => init_state }
101+ % { name: @ install_env_name , value: init_state }
94102 end
95103
96104 @ doc ~s'''
@@ -100,29 +108,12 @@ defmodule Pythonx do
100108 def install_paths ( ) do
101109 init_state = init_state ( )
102110
103- init_state . install_paths
104- |> Enum . map ( fn path -> Path . wildcard ( "#{ path } /**" , match_dot: true ) end )
105- |> List . flatten ( )
106- end
107-
108- @ doc false
109- def maybe_uv_init_from_env ( ) do
110- case init_state_from_env ( ) do
111- nil ->
112- :noop
113-
114- init_state_env_value ->
115- % {
116- type: :uv_init ,
117- pyproject_toml: pyproject_toml ,
118- opts: opts
119- } =
120- init_state_env_value
121- |> Base . decode64! ( )
122- |> :erlang . binary_to_term ( )
123-
124- uv_init ( pyproject_toml , opts )
125- end
111+ [
112+ init_state . python_dl_path ,
113+ init_state . python_executable_path
114+ ] ++
115+ init_state . sys_paths ++
116+ Path . wildcard ( Path . join ( init_state . python_home_path , "**" ) , match_dot: true )
126117 end
127118
128119 # Initializes the Python interpreter.
@@ -175,6 +166,16 @@ defmodule Pythonx do
175166 Pythonx.NIF . init ( python_dl_path , python_home_path , python_executable_path , opts [ :sys_paths ] )
176167 end
177168
169+ @ spec init ( init_state ( ) ) :: :ok
170+ def init ( % __MODULE__ {
171+ python_dl_path: python_dl_path ,
172+ python_home_path: python_home_path ,
173+ python_executable_path: python_executable_path ,
174+ sys_paths: sys_paths
175+ } ) do
176+ init ( python_dl_path , python_home_path , python_executable_path , sys_paths: sys_paths )
177+ end
178+
178179 @ doc ~S'''
179180 Evaluates the Python `code`.
180181
0 commit comments