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

Question: how could I join to URL list path parts? #138

Open
kxepal opened this issue Nov 25, 2017 · 11 comments
Open

Question: how could I join to URL list path parts? #138

kxepal opened this issue Nov 25, 2017 · 11 comments
Labels

Comments

@kxepal
Copy link
Member

kxepal commented Nov 25, 2017

Assume I have an URL URL('http://localhost:5984/base') and some list of path segments like ['foo', '/bar', 'baz%']. What's the proper way to join them to url to haveURL(http://localhost:5984/base/foo/%2fbar/baz%25) as result?

So far I came to:

url = URL('http://localhost:5984/base')
segments = ['foo', '/bar', 'baz%']
for seg in sements:
  url /= seg.replace('/', '%2F')

But that doesn't looks like good solution. Or it does?

@asvetlov
Copy link
Member

In [1]: from yarl import URL

In [2]: url = URL('http://localhost:5984/base')

In [3]: segments = ['foo', '/bar', 'baz%']

In [4]: url2 = url / '/'.join(segments)

In [5]: url2
Out[5]: URL('http://localhost:5984/base/foo//bar/baz%25')

@samuelcolvin
Copy link
Member

foo//bar?

@kxepal
Copy link
Member Author

kxepal commented Nov 25, 2017

Out[5]: URL('http://localhost:5984/base/foo//bar/baz%25')

Problem is here: //bar vs /%2Fbar. I didn't found a way so far to avoid quoting on my side /:

@kxepal
Copy link
Member Author

kxepal commented Nov 25, 2017

Also url2 = url / '/'.join(segments) will fail if first segment starts with /, which should be quoted in final path.

@asvetlov
Copy link
Member

yarl has not with_path_segments method because https://docs.python.org/3/library/pathlib.html has no such method.
I not sure if we need to add it -- just for sake of keeping public API simple.
On other side I have no strong objection.

Opinions?

@samuelcolvin
Copy link
Member

I think yarl should match pathlib with respect to double slash //, currently it doesn't:

In [11]: Path('foobar') / 'x//y'
Out[11]: PosixPath('foobar/x/y')

In [12]: URL('http://localhost:5984/base') / 'x//y'
Out[12]: URL('http://localhost:5984/base/x//y')

@kxepal
Copy link
Member Author

kxepal commented Nov 25, 2017

Well, a few. First one is a with_path_segments as you already mentioned. Second is about to allow / work with list / tuple on the right side:

>>> url = URL('http://localhost/base')
>>> url / ['foo', '/bar']
URL('http://localhost/base/foo/%2Fbar)

I believe pathlib doesn't have such issue because it doesn't have quote anything at all, so path segment couldn't have / character in any form. For URL's it's possible.

@kxepal
Copy link
Member Author

kxepal commented Nov 25, 2017

@samuelcolvin
It's not quite the same: for paths, you can't have a empty name object (dir, file, etc.). For URLs, path is an resource identifier. It have no obligations to follow FS rules, so base/x//y - is quite valid path and it's not equals to base/x/y - these are two different resources.

@samuelcolvin
Copy link
Member

makes sense.

@asvetlov
Copy link
Member

asvetlov commented Dec 6, 2017

@kxepal returning to the issue.
Accepting list/tuple as right param for / operator looks very native.
If we decide to implement this -- no need for with_path_segments, with_path can accept just the same list/tuple.

Unfortunately I don't find a time for the issue at least up to New Year.
Do you want to make a PR?

@kxepal
Copy link
Member Author

kxepal commented Dec 6, 2017

Sounds good for me. Will do.

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

No branches or pull requests

3 participants