2021-10-04 18:59:41 +02:00
|
|
|
defmodule Mobilizon.PythonPort do
|
|
|
|
@moduledoc """
|
|
|
|
Port to use Python modules from Elixir
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Export.Python
|
|
|
|
|
2021-11-08 12:14:13 +01:00
|
|
|
@python_path "/usr/bin/python3"
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Whether Python3 is installed
|
|
|
|
"""
|
|
|
|
@spec python_exists? :: boolean
|
|
|
|
def python_exists? do
|
|
|
|
File.exists?(python_path())
|
|
|
|
end
|
|
|
|
|
2021-10-04 18:59:41 +02:00
|
|
|
@doc """
|
|
|
|
## Parameters
|
|
|
|
- path: directory to include in python path
|
|
|
|
"""
|
|
|
|
@spec python_instance(String.t()) :: pid
|
|
|
|
def python_instance(path) do
|
2021-11-08 12:14:13 +01:00
|
|
|
{:ok, pid} = Python.start(python: python_path(), python_path: path)
|
2021-10-04 18:59:41 +02:00
|
|
|
|
|
|
|
pid
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Call python function using MFA format
|
|
|
|
"""
|
|
|
|
@spec call_python(pid, binary, binary, list) :: any
|
|
|
|
def call_python(pid, module, function, arguments \\ []) do
|
|
|
|
Python.call(pid, module, function, arguments)
|
|
|
|
end
|
2021-11-08 12:14:13 +01:00
|
|
|
|
|
|
|
@spec python_path :: String.t()
|
|
|
|
defp python_path do
|
|
|
|
case get_in(Application.get_env(:mobilizon, __MODULE__), [:path]) do
|
|
|
|
path when is_binary(path) -> path
|
|
|
|
nil -> @python_path
|
|
|
|
end
|
|
|
|
end
|
2021-10-04 18:59:41 +02:00
|
|
|
end
|