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

Add destination folder #14

Closed
wants to merge 4 commits into from
Closed

Add destination folder #14

wants to merge 4 commits into from

Conversation

jawrainey
Copy link
Collaborator

No description provided.

Used type annotation and docstrings for new method.
csv2docx/cli.py Outdated
import pathlib


def create_path_if_not_exists(path: str) -> pathlib.Path:
Copy link
Collaborator Author

@jawrainey jawrainey May 3, 2020

Choose a reason for hiding this comment

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

I don't like this method name ... open to suggestions. It's nice to keep it separate though as we write separate tests for it. IMHO, do not merge these changes until tests exist 👍

Copy link
Collaborator Author

@jawrainey jawrainey May 3, 2020

Choose a reason for hiding this comment

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

Also, the problem is that the parameter path can be a directory output, an absolute path /user/bin/output or relative path ../../output and we should accept all those conditions.

Rather than deal with edge cases, I used pathlib as it converts the given path to a concrete path that is user platform specific 👍

Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps go with
def concrete_path(path: str) -> pathlib.Path:
with a note that this method will create the directory if non-existent.

Ps. does it handle a nested non-existent directory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great suggestion re:name 👍

Yes -- it handles nested non-existent directories thanks to the parents option.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about absolute_path as it's more common terminology?

csv2docx/cli.py Outdated
print ("Getting .docx template and .csv data files ...")
help='Delimiter used in your csv. Default is \';\'')
@click.option(
'--path', '-p',
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe destination is a better name?

Copy link
Owner

Choose a reason for hiding this comment

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

I would go with destination yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@davidverweij -- one minor issue with destination is that the data option has the -d flag ...

Copy link
Contributor

Choose a reason for hiding this comment

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

i would go with -o output it feels standard

@jawrainey jawrainey self-assigned this May 3, 2020
@jawrainey jawrainey linked an issue May 3, 2020 that may be closed by this pull request
@jawrainey
Copy link
Collaborator Author

jawrainey commented May 3, 2020

Testing

Default argument

poetry run convert -t tests/data/example.docx -c tests/data/example.csv

Default argument (folder now exists):

poetry run convert -t tests/data/example.docx -c tests/data/example.csv -p output

Nested folders: (folder does not exist):

poetry run convert -t tests/data/example.docx -c tests/data/example.csv -p how/deep/can/we/go/output

Shortcuts (folder does not exist):

poetry run convert -t tests/data/example.docx -c tests/data/example.csv -p ~/Desktop/output

Relative:

poetry run convert -t tests/data/example.docx -c tests/data/example.csv -p ./output

We should NOT merge this until we have written tests to validate the above and whatever other edge cases we can think of -- hence making this a draft. This is my first time using pathlib, so there may be gotchas I am unaware of, but it seems pretty neat.

@jawrainey jawrainey marked this pull request as draft May 3, 2020 21:58
csv2docx/cli.py Outdated
return

print("All fields are present in your csv. Generating Word docs ...")

path = create_path_if_not_exists(path)
Copy link
Collaborator Author

@jawrainey jawrainey May 3, 2020

Choose a reason for hiding this comment

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

As above, but to reiterate: can you think of a better method name?

csv2docx/cli.py Outdated
docx.merge_templates([single_document], separator='page_break')
# TODO: write to user-defined subfolder
docx.write(f"{counter}.docx")
docx.write(f"{path}/{counter}.docx")
Copy link
Collaborator Author

@jawrainey jawrainey May 3, 2020

Choose a reason for hiding this comment

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

Just had a thought that this might cause issues on windows given we are using /. However, because we convert path to type pathlib.Path it means that once we reach this docx.write that it is now a pathlib.path and not a string. This is very important because if you use the backslash operator (/) after a pathlib.path object then it will join whatever is right (in this case, the filename) with the path 👍

Copy link
Owner

Choose a reason for hiding this comment

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

So that means the "/" will work in this case right? Also, the counter would ideally be a filename based on the .csv - see also #16 - but that might be for another PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it would work in this case, but I feel that we should probably instead create a variable before adding it to docx.write to make it more explicit, e.g. write(f"{path}.docx") where path is defined before 👍

@jawrainey
Copy link
Collaborator Author

jawrainey commented May 4, 2020

@davidverweij -- I have made the changes you've suggested above and merged master into this PR. Could you test it and let me know your thoughts? Once happy, I can write unit tests for the method (or all methods?!) to put the infrastructure in place for #11, #12 , etc prior to merging this with master. Tests in place will make validating future PR/changes easier -- unless you want to write the tests? 🥇

@davidverweij
Copy link
Owner

Pulled into 'output_destination' branch to work on further. Please --set-upstream-to origin/output_destination to track updates

@jawrainey jawrainey marked this pull request as ready for review May 4, 2020 16:16
@jawrainey jawrainey marked this pull request as draft May 4, 2020 16:17
@davidverweij
Copy link
Owner

Continuing this on the output branch, see PR #18

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.

Add destination folder
3 participants