Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate multiple output files with cog #19

Open
karolzlot opened this issue Feb 17, 2022 · 2 comments
Open

Generate multiple output files with cog #19

karolzlot opened this issue Feb 17, 2022 · 2 comments

Comments

@karolzlot
Copy link

karolzlot commented Feb 17, 2022

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
Copy link
Owner

nedbat commented Jan 30, 2023

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
Copy link

using-cog-from-within-python

Overview

  • using cog from within python

Context

  • nedbat cog for python
  • most recent stable version as of 2023-03-11

Scenario

  • developer karolzlot wants to
    • invoke cog from the cmd_line
    • use cog to create multiple source files
    • one file per dictionary key and using dictionary values to parametrize those files

Problem

  • as of this writing, this option is not available from the cmd_line
  • Solution -- run cog from within python

Solution

  • using cog from within python
  • cog can be invoked from within a python program
  • this approach supports any workflow you want to use
  • (eg) use cog + cookiecutter + jinja2 for arbitrary workflows

Example

  • the following code excerpt shows how to initialize and run cog inside a python program
  • you could wrap this code inside a function or a classmethod and pass in the input file as a parameter
  • alternatively call the function and pass in the entire config dictionary as an argument
    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants