SnakeBridge.Generator.PathMapper (SnakeBridge v0.16.0)

Copy Markdown View Source

Maps Python module paths to Elixir file paths for split layout generation.

This module provides deterministic path mapping from Python module paths to Elixir source file paths, mirroring Python's package structure.

Convention

  • Package/submodule directories use __init__.ex (like Python's __init__.py)
  • Class modules use lowercase_name.ex files
  • All paths are lowercase with underscores

Examples

iex> PathMapper.module_to_path("examplelib", "lib/gen")
"lib/gen/examplelib/__init__.ex"

iex> PathMapper.module_to_path("examplelib.predict", "lib/gen")
"lib/gen/examplelib/predict/__init__.ex"

iex> PathMapper.class_file_path("examplelib.predict", "Widget", "lib/gen")
"lib/gen/examplelib/predict/widget.ex"

Summary

Functions

Computes all file paths needed for a library's functions and classes.

Returns all ancestor module paths for a given Python module.

Computes the file path for a class module.

Returns the directory path for a Python module.

Computes file path for a Python module's functions.

Converts a Python module path to an Elixir module atom.

Functions

all_files_for_library(library_python_name, functions, classes, base_dir)

@spec all_files_for_library(String.t(), [map()], [map()], String.t()) ::
  {[String.t()], [String.t()]}

Computes all file paths needed for a library's functions and classes.

Returns a tuple of {module_files, class_files}. Module files are generated for the library root and any python modules that have functions or classes.

Parameters

  • library_python_name - The library's Python module name
  • functions - List of function info maps with "python_module" key
  • classes - List of class info maps with "python_module" and "name" keys
  • base_dir - Base directory for generated files

ancestor_modules(python_module)

@spec ancestor_modules(String.t()) :: [String.t()]

Returns all ancestor module paths for a given Python module.

Examples

iex> ancestor_modules("examplelib")
[]

iex> ancestor_modules("examplelib.predict")
["examplelib"]

iex> ancestor_modules("examplelib.predict.chain.widget")
["examplelib", "examplelib.predict", "examplelib.predict.chain"]

class_file_path(python_module, class_name, base_dir)

@spec class_file_path(String.t(), String.t(), String.t()) :: String.t()

Computes the file path for a class module.

Classes are placed as direct .ex files named after the class.

Examples

iex> class_file_path("examplelib.predict", "Widget", "lib/gen")
"lib/gen/examplelib/predict/widget.ex"

module_to_dir(python_module, base_dir)

@spec module_to_dir(String.t(), String.t()) :: String.t()

Returns the directory path for a Python module.

Examples

iex> module_to_dir("examplelib", "lib/gen")
"lib/gen/examplelib"

iex> module_to_dir("examplelib.predict", "lib/gen")
"lib/gen/examplelib/predict"

module_to_path(python_module, base_dir, type \\ :package)

@spec module_to_path(String.t(), String.t(), :package | :leaf) :: String.t()

Computes file path for a Python module's functions.

By default, modules are treated as packages and get __init__.ex. Use type: :leaf for leaf modules that should get direct .ex files.

Parameters

  • python_module - The Python module path (e.g., "examplelib.predict")
  • base_dir - Base directory for generated files
  • type - :package (default) or :leaf

Examples

iex> module_to_path("examplelib", "lib/gen")
"lib/gen/examplelib/__init__.ex"

iex> module_to_path("examplelib.predict.widget", "lib/gen", :leaf)
"lib/gen/examplelib/predict/widget.ex"

python_module_to_elixir_module(python_module, library_module)

@spec python_module_to_elixir_module(String.t(), module()) :: module()

Converts a Python module path to an Elixir module atom.

Parameters

  • python_module - The Python module path
  • library_module - The base Elixir module for the library

Examples

iex> python_module_to_elixir_module("examplelib", Examplelib)
Examplelib

iex> python_module_to_elixir_module("examplelib.predict", Examplelib)
Examplelib.Predict