using cog without having to include any python in the cog_enabled_src_file
Context
python nedbat cog
most recent stable version as of 2023-03-11
Scenario
developer MurrayStella wants to invoke cog on a cog_enabled_src_file
MurrayStella wants to remove the inline python from the cog_enabled_src_file and use indirect python instead
Example
this simple example illustrates the desired outcome
MurrayStella wants to convert BEFORE into AFTER
BEFORE
## Simple demo just to show what we want
* just print a hello world message
* a random number of times for variability
* include a random integer for more variability
##@ [[[cog cog.out('Hello {vxx:03d} World!\n'.format(vxx=random.randint(0,999))*random.randint(1,3)) ]]]
Hello 009 World!
Hello 009 World!
##@ [[[end]]]
##@ [[[cog cog.out('Hello {vxx:03d} World!\n'.format(vxx=random.randint(0,999))*random.randint(1,3)) ]]]
Hello 371 World!
##@ [[[end]]]
AFTER
## Simple demo just to show what we want
* just print a hello world message
* a random number of times for variability
* include a random integer for more variability
##@ <cogbeg coggid="uu190hello1678463240" coguri="py003010" >
Hello 772 World!
Hello 772 World!
Hello 772 World!
##@ <cogend>
##@ <cogbeg coggid="uu195hello1678463240" coguri="py003010" >
Hello 371 World!
##@ <cogend>
Feature request
MurrayStella would like to use the syntax style in the AFTER example instead of BEFORE
This could be doable with a new cmd_line flag such as --idmarker= or --outputmarker=
this flag is similar to the --marker cmd_line flag
instead of changing the cog_markup_syntax as --marker does, it would instead remove the need to specify inline python with cog.out("PythonCodeHere")
it would allow the user to specify any arbitrary prefix and suffix to delimit a cog_code_id
the cog_code_id could simply reference a variable specified with the -D name=val cmd_line flag
this would tell cog to get the auto-generated code from the variable instead of inline python code
it would also allow arbitrary syntax in the cog_enabled_src_file that does not have to involve any visible python
Workaround
MurrayStella has already implemented a hacky sadness-inducing (but functional) workaround to provide this functionality
Workaround Steps:
Step001: invoke cog from within python instead of from the cmd_line
this allows us to do our hacky sadness-inducing postproc and preproc
indirect-vs-inline-python
Overview
Context
Scenario
Example
BEFORE
AFTER
Feature request
--idmarker=
or--outputmarker=
--marker
cmd_line flag--marker
does, it would instead remove the need to specify inline python withcog.out("PythonCodeHere")
-D name=val
cmd_line flagWorkaround
--marker
option[[[cog ]]] [[[end]]]
to become<cogbeg coguri= <cogend>
-D name=val
cmd_line flag-D name=val
cmd_line flag is not friendly to python-generated output that spans more than one linecoggid="unique_id_here"
becomescog.out(MyPyOutputEngine.unique_id_here())
cog.out(..)
changes back tocoggid="..."
See also