nedbat / cog

Small bits of Python computation for static files
MIT License
340 stars 26 forks source link

Generate multiple output files with cog #19

Open karolzlot opened 2 years ago

karolzlot commented 2 years ago

Is it possible to generate multiple output files with cog?

For example I have python dictionary:

MODULE_ACTIONS_DICT = { "contractors": ["add", "delete", "edit", "find", "get"], "invoices": ["add", "delete", "download", "edit", "find"], "expenses": ["find","get"], ........ }

I would like to use cog to create multiple source files, one file per dictionary key and using dictionary values to parametrize those files.

I wonder if cog is right tool for this task... Alternatively I can use jinja2

nedbat commented 1 year ago

Hi, sorry I didn't get to this. Cog doesn't have a way to do this now, but I might be adding an explicit API to cog, which would make this possible. Did you find another way to do it?

dreftymac commented 1 year ago

using-cog-from-within-python

Overview

Context

Scenario

Problem

Solution

Example


    import cogapp
    cogscript = cogapp.Cog()
    pass

    ddcogginfo = dict()
    ddcogginfo['prelines']  = 'import random;'
    ddcogginfo['fileinpp']  = 'QXP:/path/to/my/file/friantexport.java'
    ddcogginfo['outname']   = 'friantexport_new.java'
    ddcogginfo['markers']   = '[[cog ]] [[end]]' ## (using the default cog_markup_syntax)
    bbselfreplace   = True
    pass

    aacoggopts  = list()
    aacoggopts +=  [ 'cog' ]                                      ## specify cog
    aacoggopts +=  [ '-nutf-8' ]                                  ## specify utf-8
    aacoggopts +=  [ '--markers={markers}'.format(**ddcogginfo) ] ## specify cog delimiter syntax
    aacoggopts +=  [ '-p{prelines}'.format(**ddcogginfo) ]        ## prepend_generator_source
    if(bbselfreplace):
      aacoggopts +=  [ '-r' ]                                     ## self-replace file content
      pass
    elif(True):
      aacoggopts +=  [ '-o{outname}'.format(**ddcogginfo) ]       ## specify outputfile separate from cog_input_file
    pass
    aacoggopts +=  [ '--verbosity=2' ]                            ## 2verbose 1quiet 0silent
    aacoggopts +=  [ ddcogginfo['fileinpp'] ]                     ## specify cog_input_file
    pass

    vg3557flesoutt = cogscript.main(aacoggopts)
    pass