registry

package
v0.0.0-...-fee3e51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2025 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package registry provides module, type, and attribute registry functionality for Python code analysis.

This package handles:

  • Module discovery and path resolution
  • Python builtin type registry
  • Class attribute tracking
  • Python version detection

Module Registry

BuildModuleRegistry walks a directory tree to discover all Python files and build a mapping from module paths to file paths:

registry, err := registry.BuildModuleRegistry("/path/to/project")
if err != nil {
    log.Fatal(err)
}
filePath, ok := registry.GetFilePath("myapp.views")

The registry automatically skips common directories like venv, __pycache__, .git, etc.

Builtin Registry

BuiltinRegistry provides type information for Python builtin types and functions:

builtins := registry.NewBuiltinRegistry()
typeInfo := builtins.GetBuiltinType("str")
// Returns: &core.TypeInfo{TypeFQN: "builtins.str", ...}

It includes comprehensive coverage of:

  • Builtin types (str, int, list, dict, etc.)
  • Builtin functions (len, range, enumerate, etc.)
  • Type methods (str.upper, list.append, etc.)

Attribute Registry

AttributeRegistry tracks class attributes discovered during analysis:

attrReg := registry.NewAttributeRegistry()
attrReg.AddAttribute("myapp.User", &core.ClassAttribute{
    Name: "email",
    Type: &core.TypeInfo{TypeFQN: "builtins.str"},
})

Thread-safe for concurrent access during multi-file analysis.

Python Version Detection

The package can detect Python version from project files:

  • .python-version files
  • pyproject.toml dependencies
  • Defaults to latest stable version

This informs which builtin types and methods are available.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildModuleRegistry

func BuildModuleRegistry(rootPath string) (*core.ModuleRegistry, error)

BuildModuleRegistry walks a directory tree and builds a complete module registry. It discovers all Python files and maps them to their corresponding module paths.

The registry enables:

  • Resolving fully qualified names (FQNs) for functions
  • Mapping import statements to actual files
  • Detecting ambiguous module names

Algorithm:

  1. Walk directory tree recursively
  2. Skip common non-source directories (venv, __pycache__, etc.)
  3. Convert file paths to Python module paths
  4. Index both full module paths and short names

Parameters:

  • rootPath: absolute path to the project root directory

Returns:

  • *core.ModuleRegistry: populated registry with all discovered modules
  • error: if root path doesn't exist or is inaccessible

Example:

registry, err := BuildModuleRegistry("/path/to/myapp")
// Discovers:
//   /path/to/myapp/views.py → "myapp.views"
//   /path/to/myapp/utils/helpers.py → "myapp.utils.helpers"

Types

type AttributeRegistry

type AttributeRegistry struct {
	Classes map[string]*core.ClassAttributes // Map from class FQN to class attributes
	// contains filtered or unexported fields
}

AttributeRegistry is the global registry of class attributes It provides thread-safe access to class attribute information.

func NewAttributeRegistry

func NewAttributeRegistry() *AttributeRegistry

NewAttributeRegistry creates a new empty AttributeRegistry.

func (*AttributeRegistry) AddAttribute

func (ar *AttributeRegistry) AddAttribute(classFQN string, attr *core.ClassAttribute)

AddAttribute adds a single attribute to a class Creates the ClassAttributes entry if it doesn't exist.

func (*AttributeRegistry) AddClassAttributes

func (ar *AttributeRegistry) AddClassAttributes(classAttrs *core.ClassAttributes)

AddClassAttributes adds or updates attributes for a class Thread-safe for concurrent modifications.

func (*AttributeRegistry) GetAllClasses

func (ar *AttributeRegistry) GetAllClasses() []string

GetAllClasses returns a list of all registered class FQNs.

func (*AttributeRegistry) GetAttribute

func (ar *AttributeRegistry) GetAttribute(classFQN, attrName string) *core.ClassAttribute

GetAttribute retrieves a specific attribute from a class Returns nil if class or attribute is not found.

func (*AttributeRegistry) GetClassAttributes

func (ar *AttributeRegistry) GetClassAttributes(classFQN string) *core.ClassAttributes

GetClassAttributes retrieves attributes for a given class FQN Returns nil if class is not in registry.

func (*AttributeRegistry) HasClass

func (ar *AttributeRegistry) HasClass(classFQN string) bool

HasClass checks if a class is registered.

func (*AttributeRegistry) Size

func (ar *AttributeRegistry) Size() int

Size returns the number of registered classes.

type BuiltinMethod

type BuiltinMethod struct {
	Name       string         // Method name (e.g., "upper", "append")
	ReturnType *core.TypeInfo // Return type of the method
}

BuiltinMethod represents a method available on a builtin type.

type BuiltinRegistry

type BuiltinRegistry struct {
	Types map[string]*BuiltinType // Type FQN -> builtin type info
}

BuiltinRegistry maintains information about Python builtin types and their methods. This enables type inference for literal values and builtin method calls.

func NewBuiltinRegistry

func NewBuiltinRegistry() *BuiltinRegistry

NewBuiltinRegistry creates and initializes a registry with Python builtin types. The registry is pre-populated with common types: str, list, dict, set, tuple, int, float, bool, bytes, and their associated methods.

Returns:

  • Initialized BuiltinRegistry with all builtin types

func (*BuiltinRegistry) GetMethod

func (br *BuiltinRegistry) GetMethod(typeFQN, methodName string) *BuiltinMethod

GetMethod retrieves method information for a builtin type.

Parameters:

  • typeFQN: fully qualified type name
  • methodName: name of the method

Returns:

  • BuiltinMethod if found, nil otherwise

func (*BuiltinRegistry) GetType

func (br *BuiltinRegistry) GetType(typeFQN string) *BuiltinType

GetType retrieves builtin type information by its fully qualified name.

Parameters:

  • typeFQN: fully qualified type name (e.g., "builtins.str")

Returns:

  • BuiltinType if found, nil otherwise

func (*BuiltinRegistry) InferLiteralType

func (br *BuiltinRegistry) InferLiteralType(literal string) *core.TypeInfo

InferLiteralType infers the type of a Python literal value. Supports: strings, integers, floats, booleans, lists, dicts, sets, tuples.

Parameters:

  • literal: the literal value as a string

Returns:

  • TypeInfo with confidence 1.0 if recognized, nil otherwise

type BuiltinType

type BuiltinType struct {
	FQN     string                    // Fully qualified name (e.g., "builtins.str")
	Methods map[string]*BuiltinMethod // Method name -> method info
}

BuiltinType represents a Python builtin type with its available methods.

type StdlibRegistryLoader

type StdlibRegistryLoader struct {
	RegistryPath string // Path to registries directory (e.g., "registries/python3.14/stdlib/v1")
}

StdlibRegistryLoader loads stdlib registries from local filesystem.

func (*StdlibRegistryLoader) LoadRegistry

func (l *StdlibRegistryLoader) LoadRegistry() (*core.StdlibRegistry, error)

LoadRegistry loads manifest and all modules from local directory.

type StdlibRegistryRemote

type StdlibRegistryRemote struct {
	BaseURL       string                        // CDN base URL (e.g., "https://codepathfinder.dev/registries")
	PythonVersion string                        // Python version (e.g., "3.14")
	Manifest      *core.Manifest                // Loaded manifest
	ModuleCache   map[string]*core.StdlibModule // In-memory cache of loaded modules
	CacheMutex    sync.RWMutex                  // Mutex for thread-safe cache access
	HTTPClient    *http.Client                  // HTTP client for downloads
}

StdlibRegistryRemote loads Python stdlib registries from a remote CDN. It uses lazy loading (downloads modules on-demand) and in-memory caching.

func NewStdlibRegistryRemote

func NewStdlibRegistryRemote(baseURL, pythonVersion string) *StdlibRegistryRemote

NewStdlibRegistryRemote creates a new remote registry loader.

Parameters:

Returns:

  • Initialized StdlibRegistryRemote

func (*StdlibRegistryRemote) CacheSize

func (r *StdlibRegistryRemote) CacheSize() int

CacheSize returns the number of modules currently cached in memory.

Returns:

  • Number of cached modules

func (*StdlibRegistryRemote) ClearCache

func (r *StdlibRegistryRemote) ClearCache()

ClearCache clears the in-memory module cache. Useful for testing or memory management.

func (*StdlibRegistryRemote) GetClass

func (r *StdlibRegistryRemote) GetClass(moduleName, className string) *core.StdlibClass

GetClass retrieves a class from a module, downloading the module if needed.

Parameters:

  • moduleName: name of the module
  • className: name of the class

Returns:

  • StdlibClass if found, nil otherwise

func (*StdlibRegistryRemote) GetFunction

func (r *StdlibRegistryRemote) GetFunction(moduleName, functionName string) *core.StdlibFunction

GetFunction retrieves a function from a module, downloading the module if needed.

Parameters:

  • moduleName: name of the module (e.g., "os")
  • functionName: name of the function (e.g., "getcwd")

Returns:

  • StdlibFunction if found, nil otherwise

func (*StdlibRegistryRemote) GetModule

func (r *StdlibRegistryRemote) GetModule(moduleName string) (*core.StdlibModule, error)

GetModule retrieves a module by name, downloading it if not cached. This implements lazy loading - modules are only downloaded when needed.

Parameters:

  • moduleName: name of the module (e.g., "os", "sys")

Returns:

  • StdlibModule if found, nil otherwise
  • error if download or parsing fails

func (*StdlibRegistryRemote) HasModule

func (r *StdlibRegistryRemote) HasModule(moduleName string) bool

HasModule checks if a module exists in the manifest (without downloading it).

Parameters:

  • moduleName: name of the module

Returns:

  • true if module exists in manifest, false otherwise

func (*StdlibRegistryRemote) LoadManifest

func (r *StdlibRegistryRemote) LoadManifest() error

LoadManifest downloads and parses the manifest.json file from the CDN.

Returns:

  • error if download or parsing fails

func (*StdlibRegistryRemote) ModuleCount

func (r *StdlibRegistryRemote) ModuleCount() int

ModuleCount returns the number of modules in the manifest.

Returns:

  • Number of modules, or 0 if manifest not loaded

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL