Cog Documentation
repository·main·Indexed 19 days ago
https://github.com/nedbat/cogCog is a content generation tool for executing Python snippets in source files to automate the creation or updating of static file content. It provides a command-line interface and a programmatic API via the Cog and CogGenerator classes to extract and execute code within markers, manage output via the cog module, and verify content integrity using HashHandler checksums.
What's inside cog
- Cog is a content generation tool designed for performing small bits of computation for static files. It is used to automate the generation of parts of files that might otherwise need to be updated manually.
What is Cog and how does it work?
mainCog is a content generation tool that allows you to embed small snippets of Python code within static files to generate text dynamically.
How it works:
- Cog identifies chunks of Python code wrapped in specific marker lines.
- It executes the embedded Python code.
- It replaces the space between the markers with the output produced by the Python code.
- All text outside of these markers is passed through unchanged.
This allows you to maintain a single source file that contains both the logic (Python) and the template (the surrounding text), making it easy to generate repetitive code like C++ function declarations, configuration files, or documentation.
Protect output with checksums
mainUse the
-cflag to append a compact base64 checksum to the end of the generated output. This protects the output against accidental manual edits. If a developer edits the generated code, cog will detect the mismatch during the next run and stop to prevent overwriting the manual changes.Cog also supports the older hex checksum format:
[[[end]]] (checksum: <hex_string>)and will automatically upgrade these to the shorter base64 format upon regeneration.--[[[cog -- import cog -- for i in range(10): -- cog.out("%d " % i) -- ]]] 0 1 2 3 4 5 6 7 8 9 --[[[end]]] (sum: vXcVMEUp9m)How to use Cog markers in files
mainTo embed Python code in a file, use triple square bracket markers. The lines between
[[[cogand]]]contain the Python code, and the lines between]]]and[[[end]]]define the output area.Marker Syntax:
- Start block:
[[[cog - End code:
]]] - Start output area:
]]] - End block:
[[[end]]]
Pro-tip: Marker lines can contain additional text (like comment characters) to ensure the file remains valid for other tools (e.g., a C++ compiler or a linter) while the Python code is present.
/*[[[cog import cog fnames = ['DoSomething', 'DoAnotherThing'] for fn in fnames: cog.outl("void %s();" % fn) ]]]*/ //[[[end]]]- Start block:
How to write Cog source files
mainCog source files are primarily plain text files containing chunks of Python code wrapped in specific marker lines. When Cog runs, it executes the Python code and replaces the area between the end-marker and the end-marker with the code's output.
Marker Syntax
Markers are identified by the presence of the
[[[cogand[[[end]]]sequences. The text preceding these sequences is ignored, allowing you to use markers that match your file's comment style (e.g.,#,--,//, or/*).Python Chunk Execution
- Sequential Execution: Each chunk is executed in sequence.
- Shared State: Multiple chunks in the same file share the same globals dictionary, allowing you to define functions or imports in one chunk and use them in subsequent chunks.
- Indentation Handling: Cog automatically manages indentation. It identifies a common whitespace prefix from the output block and re-indents the entire block so that the leftmost non-whitespace character matches the indentation of the
[[[cogmarker line. This allows you to indent your Python code to match the surrounding file structure.
# Example of a standard Python chunk in a Python file [[[cog import cog print("Hello from Cog!") ]]] [[[end]]]How Cog's code generation model works
mainCog is a general-purpose code generation tool that uses Python to transform text files. Instead of using separate input and output files, Cog works by reading a host file (e.g., C++, Java, or any text-based language) and looking for specially-marked sections containing Python code.
When Cog runs, it:
- Identifies sections delimited by
/* [[[cogand]]] */(or similar marker patterns). - Discards any existing generated content between the
]]]marker and the[[[end]]]marker. - Executes the Python code found within the
cogblock. - Captures the output (typically via
cog.outlcalls). - Inserts that captured output into the file, effectively "splicing" the generated code back into the host file.
This allows the generator code and the generated output to live side-by-side in the same file, making the process an edit-time activity rather than a complex build-time step.
- Identifies sections delimited by
How Cog manages source and output files
mainCog uses an in-place update model. Instead of requiring a separate source file and a separate output file, Cog writes its results back into the original file while retaining the code it executed. This allows a single file to serve as both the source of the generation logic and the final output. Because of this design, Cog can be run multiple times on the same file without creating duplicate or separate files.Managing Cog files in source control
mainCog uses marker lines that are designed to accommodate any language syntax. These markers can effectively 'hide' the Cog Python code within your text file. This enables you to check Cog files directly into source control without needing to manage separate source and output files or modify complex build procedures.Using Cog for code generation without a templating engine
mainUnlike traditional code generation tools that rely on templating engines (which can struggle with whitespace and complex logic), Cog uses standard Python code to generate text. This allows you to use the full power of the Python language for text generation, avoiding the mental overhead of converting Python logic into a specific templating syntax.Use Cog markers with file-specific comment styles
mainTo accommodate different file formats, Cog recognizes any line containing the
[[[cogsequence as a start marker. You can prefix the marker with your language's comment syntax.Examples of valid markers:
- Python/Shell:
# [[[cog - C/C++/Java:
// [[[cogor/* [[[cog */ - SQL:
-- [[[cog
If you need custom markers that do not follow these patterns, use the
--markersCLI option to define them.# Example markers for different languages //[[[cog /* [[[cog */ -- [[[cog #if 0 // [[[cog- Python/Shell:
Run cog via CLI or Python module
mainCog can be used as a standalone command-line utility or invoked as a Python module. When using the module approach, the package name is
cogapp.CLI Usage:
$ cog [options] [INFILE | @FILELIST | &FILELIST] ...Module Usage:
$ python3 -m cogapp [options] [arguments]# CLI example $ cog -h # Module example $ python3 -m cogapp -hInstall cogapp via pip
mainCog requires Python 3.10 or higher.
Note that while the package name is
cogapp, it provides acogcommand-line tool after installation.python3 -m pip install cogapp