Cogapp - Content Generator
Cogapp, currently at version 3.6.0, is a content generator that executes Python snippets embedded within source files (like Python, C, HTML, or plain text) to produce updated output. It helps keep documentation, code examples, and derived content in sync with their source, primarily operating as a command-line tool. Releases are generally stable, with new versions addressing bug fixes and minor enhancements.
Common errors
-
cog: command not found
cause The `cogapp` package is either not installed, or the directory containing its `cog` executable is not included in your system's PATH environment variable.fixFirst, ensure `pip install cogapp` was successful. If the problem persists, run `python -m cogapp ...` instead of `cog ...` to explicitly invoke the module. -
TypeError: main() got an unexpected keyword argument 'argv'
cause You are attempting to call `cogapp.main()` programmatically using the `argv` keyword argument, which was removed in version 3.0.0.fixPass command-line arguments as a list of strings directly to the `main()` function, for example: `from cogapp.cogapp import main; main(['-r', 'my_file.txt'])`. -
FileNotFoundError: [Errno 2] No such file or directory: 'non_existent_file.txt'
cause The input file specified in the `cog` command does not exist at the given path or the path is incorrect.fixVerify that the file path and name provided to `cog` are correct and the file exists. Use an absolute path or ensure you are running the command from the correct directory.
Warnings
- breaking The `main()` function signature changed significantly in version 3.0.0. It no longer accepts `sys.argv` implicitly or via an `argv` keyword argument.
- gotcha By default, cogapp writes its generated output to standard output (stdout). This means if you simply run `cog my_file.txt`, the changes will be printed to your console, not written back to the file.
- gotcha Cogapp's default delimiters `[[[cog ... ]]]` and `[[[end]]]` are strict. Incorrect nesting, unmatched delimiters, or Python syntax errors within the `cog` block can lead to silent failures, unexpected output, or incomplete processing.
Install
-
pip install cogapp
Imports
- main
from cogapp.cogapp import main
Quickstart
import subprocess
import os
import datetime
# Create a dummy input file
file_content = """
Hello, cogapp user!
This line will be replaced by cog.
[[[cog
print(f"The current UTC time is: {datetime.datetime.utcnow().isoformat(timespec='seconds')}")
]]]
This line will remain.
"""
input_filename = "cog_example.txt"
with open(input_filename, "w") as f:
f.write(file_content)
print(f"Created {input_filename} with initial content:")
with open(input_filename, "r") as f:
print(f.read())
# Run cog in-place
print(f"\nRunning 'python -m cogapp -r {input_filename}'...")
try:
result = subprocess.run([
"python", "-m", "cogapp", "-r", input_filename
], capture_output=True, text=True, check=True)
if result.stdout:
print("Cog output (stdout):")
print(result.stdout)
if result.stderr:
print("Cog errors (stderr):")
print(result.stderr)
print(f"\nUpdated {input_filename} content:")
with open(input_filename, "r") as f:
print(f.read())
except subprocess.CalledProcessError as e:
print(f"Error running cogapp: {e}")
print(f"Stdout: {e.stdout}")
print(f"Stderr: {e.stderr}")
finally:
# Clean up
if os.path.exists(input_filename):
os.remove(input_filename)
print(f"\nCleaned up {input_filename}.")