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

allow overriden defaults in questionnaire #477

Merged
merged 2 commits into from
Nov 20, 2021

Conversation

jacobstr
Copy link
Contributor

@jacobstr jacobstr commented Nov 11, 2021

This works in conjunction with #476, but is independent of it. If partial answers are written out to a file, they can be re-loaded as default overrides to whatever is specified in the template allowing one to resume progress.

Some code that uses the combination of these two MRs:

PARTIAL_ANSWERS = Path('~/.copier_partial.json').expanduser()

kwargs = {}

if Path(PARTIAL_ANSWERS).exists():
    with open(PARTIAL_ANSWERS) as f:
        kwargs['default_overrides'] = json.load(f)

try:
    result = copier.run_auto(TEMPLATES_SRC, os.getcwd(), **kwargs)
except copier.CopierAnswersInterrupt as e:
    with open(PARTIAL_ANSWERS, "w") as f:
        f.write(json.dumps(e.answers.user))
    print("Wrote partial answers to %s" % PARTIAL_ANSWERS)

Demo of these two features working together: https://asciinema.org/a/iVJvC8wAb8s3nSVKkmnhDjeZE

  • First pass, I make some modifications and bail.
  • Second pass, you can see the prior inputs being used.
  • Prior to the third pass I modify the previous answers that were saved ala the snippet above.
  • Third pass it's using the template defaults again because I emptied the javascript object.

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little comment on code.

This feature is cool. With a bit of rework, it could fix #235.

Copier could support loading these custom defaults from $XDG_CONFIG_DIR/copier/user-defaults.yaml. It could also feature a CLI flag --user-defaults to allow the execution environment to override that path. Then, the custom defaults get loaded from there automatically.

Implementation would probably go around

class AnswersMap:

Also, this still needs docs, tests, and a CHANGELOG entry to be merged.

Good work! Thanks 🙂

copier/main.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2021

Codecov Report

Merging #477 (ef54292) into master (600ebdb) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #477      +/-   ##
==========================================
+ Coverage   95.65%   95.71%   +0.05%     
==========================================
  Files          40       40              
  Lines        2580     2616      +36     
==========================================
+ Hits         2468     2504      +36     
  Misses        112      112              
Flag Coverage Δ
unittests 95.71% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
copier/main.py 94.51% <100.00%> (+0.01%) ⬆️
copier/user_data.py 93.83% <100.00%> (+0.11%) ⬆️
tests/test_config.py 99.09% <100.00%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 600ebdb...ef54292. Read the comment docs.

@jacobstr
Copy link
Contributor Author

A little comment on code.

This feature is cool. With a bit of rework, it could fix #235.

Copier could support loading these custom defaults from $XDG_CONFIG_DIR/copier/user-defaults.yaml. It could also feature a CLI flag --user-defaults to allow the execution environment to override that path. Then, the custom defaults get loaded from there automatically.

Implementation would probably go around

class AnswersMap:

Also, this still needs docs, tests, and a CHANGELOG entry to be merged.

Good work! Thanks 🙂

Cheers!

To confirm - you believe this is an incremental step towards getting that functionality? I.E. it simplifies a subsequent MR (likely in cli.py?) that leverages the default-overrides available in the Worker class to merge these rc file defaults?

Walking that out a bit, we'll at some point want to merge (in order):

  • user defaults (e.g. aws_project = teamname-dev)
  • partially completed forms (e.g. aws_project = otherteamname-dev)

I think that logic could live entirely in cli.py - i.e. the caller would be responsible for reconciling these alternate default sources.

@jacobstr jacobstr force-pushed the overriden-defaults branch 2 times, most recently from ca13157 to e1713a3 Compare November 12, 2021 20:23
@jacobstr
Copy link
Contributor Author

jacobstr commented Nov 12, 2021

  • Tests added.
  • Changelog updated.

WRT to docs, the docstrings have been updated with the parameter. Please point me to where we'd want additional docs - it's one of these params that seems suited to the API reference, which is covered by the docstring.

@jacobstr jacobstr force-pushed the overriden-defaults branch 3 times, most recently from f0a32ae to 53f25f3 Compare November 12, 2021 22:24
@yajo
Copy link
Member

yajo commented Nov 14, 2021

I think that logic could live entirely in cli.py - i.e. the caller would be responsible for reconciling these alternate default sources.

In general terms, copier behaves the same through API and CLI. However it's true that this can be an exception because it's more ergonomic for an API call to provide user defaults through data than through a local file. So: yes, this is a valid intermediate step forward.

copier/main.py Outdated Show resolved Hide resolved
@jacobstr jacobstr force-pushed the overriden-defaults branch 2 times, most recently from 8d3e905 to d5981c6 Compare November 16, 2021 01:27
Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks friend! 🙂 The code looks nice, but it has some bugs. That also reveals the test is nice but not enough (as it should have detected those bugs). See code comments below.

copier/main.py Outdated Show resolved Hide resolved
copier/user_data.py Outdated Show resolved Hide resolved
copier/user_data.py Show resolved Hide resolved
copier/user_data.py Outdated Show resolved Hide resolved
tests/test_config.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@jacobstr jacobstr force-pushed the overriden-defaults branch 4 times, most recently from c5606d9 to 72e7166 Compare November 19, 2021 19:41
This works in conjunction with copier-org#476.
If partial answers are written out to a file, they can be re-loaded as
default overrides to whatever is specified in the template allowing one
to resume progress.
@yajo yajo added this to the v6.0.0 milestone Nov 20, 2021
Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@yajo yajo merged commit 126a746 into copier-org:master Nov 20, 2021
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

Successfully merging this pull request may close these issues.

None yet

3 participants