Skip to content

Question about asking questions and generation in a loop #381

@amitjindal

Description

@amitjindal

Hi, I want to understand how I would attempt to do the following. I tried to find answers in documentation, and other reference implementations and even tests. But they don't all seem to add up.

Here's what I want to do

I want to generate source code for all the models in a django application along with their templates, views and other files. If you are not familiar with django, that's okay. I will explain.
So given I have 2 models, I want to ask questions related to each model and then generate code. The models can also be 3/5 or any number.

For example, if I have first and second as models, I would expect to get:

  1. forms.py (contains definitions for both first and second form).
  2. templates/first.html
  3. templates/second.html

In essence, I need to loop inside the file as well as on the directory itself.

Here's how I thought I would approach this

I wanted to write the model structure in a JSON or YML (list of models and their associated properties.

project_name:
  type: str
  help: Your project name

author_username:
  type: str
  help: Your username (e.g. on GitHub)

models:
  - first:
      type: str
      help: First model name
  - second:
      type: str
      help: Second model name

When I run this the output is:

author_username? Format: str
🎤 [None]: dsf

models? Format: yaml
🎤 [[{'first': {'type': 'str', 'help': 'First model name'}}, {'second': {'type': 'str', 'help': 'Second model name'}}]]: 

    create  README.md
    create  .copier-answers.yml
    create  Makefile

I wanted copier to recurse and ask questions about first and second.

Other possibilities

Invoking multiple times

As an alternative I am thinking about writing a python program where I would read the models data from a JSON or YML and merge that with questions somehow to get the desired outcome. Maybe even run the copy command multiple times with each model.

So I want to know:

  1. Is there a way to do this?
  2. How can I invoke the copy command with additional data that would start asking questions?
  3. Will it append to answers?
  4. Will it append to generated files?

Asking questions in a loop

Second alternative may be to ask questions in a loop, collect answers and then pass them to copy command as DATA and that could do the trick. But here I don't know how to:

  1. How to invoke the question asking routines that would return me answers to current model.
  2. Generate multiple files for each model.
  3. Store answers

I am sort of lost and maybe copier cannot do what I am trying.

Please guide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions