Skip to content

Replace path_prefix with vars mapping#46

Merged
yarikoptic merged 5 commits intomasterfrom
gh-43
May 14, 2021
Merged

Replace path_prefix with vars mapping#46
yarikoptic merged 5 commits intomasterfrom
gh-43

Conversation

@jwodder
Copy link
Copy Markdown
Contributor

@jwodder jwodder commented May 13, 2021

Closes #43.

@jwodder jwodder added the minor Increment the minor version when merged label May 13, 2021
Comment thread src/tinuous/__main__.py Outdated
from pathlib import Path
import re
from shutil import rmtree
from string import Template
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ho ho -- I think I never used that yet another way to format strings in Python. I only wonder -- why you decided to go for "Template + format" instead of e.g. just making vars to also participate in "format"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because there's no clean way to pull off nested {format} templates.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

not sure I am following on "nested". What I meant is instead of

        template2 = Template(path_template).substitute(vars)
        return template2.format_map(self.path_fields())

have smth like

       return template2.format_map(dict(vars, **self.path_fields())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've changed the implementation to allow using vars via {format} syntax.

Comment thread src/tinuous/__main__.py Outdated
fields = self.path_fields()
expanded_vars: Dict[str, str] = {}
for name, template in vars.items():
expanded_vars[name] = template.format(**fields, **expanded_vars)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

right -- I "dreamed" about the bug in my suggested code ;-) this should mitigate it. Thanks!

Comment thread src/tinuous/__main__.py
class Config(NoExtraModel):
repo: str
path_prefix: Optional[str] = None
vars: Dict[str, str] = Field(default_factory=dict)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

note to myself -- dict is ordered in any sensible python3 version so order of vars specification matters and could be "relied upon" I guess.

@yarikoptic
Copy link
Copy Markdown
Member

@jwodder would you be so kind to add a few tests to ensure that vars are correctly "format"ed etc, in particular may be with the fancy reuse of one var defined earlier in the next one?

@jwodder
Copy link
Copy Markdown
Contributor Author

jwodder commented May 14, 2021

@yarikoptic Test added.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 14, 2021

Codecov Report

❗ No coverage uploaded for pull request base (master@cd3be4a). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##             master     #46   +/-   ##
========================================
  Coverage          ?   2.60%           
========================================
  Files             ?       3           
  Lines             ?     499           
  Branches          ?      74           
========================================
  Hits              ?      13           
  Misses            ?     486           
  Partials          ?       0           

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 cd3be4a...ed58c34. Read the comment docs.

@yarikoptic
Copy link
Copy Markdown
Member

looks good, thank you @jwodder

@yarikoptic yarikoptic merged commit 4e7af32 into master May 14, 2021
@yarikoptic yarikoptic deleted the gh-43 branch May 14, 2021 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generalize {path_prefix} into vars: (or consts: or anything better)

3 participants