> For the complete documentation index, see [llms.txt](https://0xlava.gitbook.io/ducky-morph/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xlava.gitbook.io/ducky-morph/python-api/codegeneratorsfactory.md).

# CodeGeneratorsFactory

## Code Generators Factory :&#x20;

The `CodeGeneratorsFactory` is responsible for managing and loading all available code generators in DuckyMorph.

It implements a **Factory Pattern** to dynamically discover, load, and instantiate transpiler modules at runtime.

This allows the framework to remain extensible without modifying core logic when adding new generators.

***

### Design Overview :&#x20;

The factory follows a simple workflow:

1. Scan the generators directory
2. Identify valid generator modules
3. Dynamically import them
4. Register generator classes internally
5. Provide access through a unified interface

This design enables **plug-and-play transpilers**.

***

### Generator File Convention

Each generator must follow this structure:

#### File naming

```
<name>_generator.py
```

#### Class naming

```
<Name>Generator
```

#### Example

```
arduino_generator.py
```

```python
class ArduinoGenerator(CodeGenerator):    ...
```

The factory uses this convention to automatically resolve and load generators.

***

## Core Methods :&#x20;

### <mark style="color:yellow;">load\_all</mark>()

Scans the generators directory and loads all available transpiler modules.

#### Behavior

* Iterates through `duckymorph/core/code_generators/`
* Filters files ending with `_generator.py`
* Loads each generator dynamically

#### Usage

```python
CodeGeneratorsFactory.load_all()
```

***

### <mark style="color:yellow;">get\_generator</mark>(name)

Returns an instantiated generator object by name.

#### Behavior

* Looks up the generator in the registry
* If not found, attempts to load it dynamically
* Returns an instance of the generator class

#### Example

```python
generator = CodeGeneratorsFactory.get_generator("arduino")
```

***

### <mark style="color:yellow;">get\_available\_generators</mark>()

Returns all currently loaded generators.

#### Example

```python
CodeGeneratorsFactory.get_avaliable_generators()
```

#### Output

Dictionary of:

```
{name: generator_class}
```

***

### <mark style="color:yellow;">get\_generators\_modules\_path</mark>()

Returns the file system path where generator modules are stored.

#### Use Case

* Adding new generator files
* Debugging module loading
* Extending framework manually

***

### <mark style="color:yellow;">register\_generator</mark>(name, generator\_cls)

Manually registers a custom generator at runtime.

#### Use Case

* External plugins
* Dynamically created generators
* Testing or mocking

#### Example

```python
CodeGeneratorsFactory.register_generator("custom", CustomGenerator)
```

***

## Error Handling :&#x20;

The factory raises <mark style="color:cyan;">`CodeGeneratorImportError`</mark> in cases such as:

* Module file not found
* Class not defined correctly
* Naming convention mismatch

This ensures strict consistency across all generators.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://0xlava.gitbook.io/ducky-morph/python-api/codegeneratorsfactory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
