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

m.(cdo|tnm).download: Replace requests with urllib #977

Open
wants to merge 2 commits into
base: grass8
Choose a base branch
from

Conversation

HuidaeCho
Copy link
Member

This PR fixes #976.

outf.write(inf.read())
except urllib.error.HTTPError as e:
grass.warning(
_("Failed to download %s with status code %d") % (filename, e.code)
Copy link
Member

Choose a reason for hiding this comment

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

.format(filename=..., code=...) is preferred over C-like %s %d. The new code does not have to be outdated just because the surrounding code is.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought C-like formats would be better for cross-language translations, but anyway, we already have Python formats, so not a big deal.

Copy link
Member

Choose a reason for hiding this comment

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

We definitively use .format in new Python code. So far, we weighted the clarity of code and possible order changes in translations over translation reuse. Plus these are not really translated at this point, no? So it is only the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

@wenzeslaus Shouldn't we prefer f-strings (better readability and shorter) in that case when the minimum Python version we support is 3.8?

Copy link
Member

Choose a reason for hiding this comment

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

We didn't adapt the CI yet, but for the main branch, the minimum supported version agreed upon is 3.9. So you are free

Copy link
Member

Choose a reason for hiding this comment

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

I thought this was this GRASS repo, not the GRASS addons repo. For addons I don't know, but hope to follow the main grass repo to allow sharing similar maintenance

Copy link
Member

Choose a reason for hiding this comment

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

The rule now is: f-strings for plain strings, but _("...").format(key=...) for translatable ones. (f-strings are overall better and recommended by linters but don't work with translations.)

Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +169 to +171
def urlopen(url):
url = url.replace(" ", "%20")
return urllib.request.urlopen(url)
Copy link
Member

Choose a reason for hiding this comment

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

There is probably encode-decode function which would do this in more general way.

Copy link
Member Author

@HuidaeCho HuidaeCho Nov 7, 2023

Choose a reason for hiding this comment

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

Found url = urllib.parse.quote(url, safe=":/?=&"), but I liked the above simpler version. Any better suggestion?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was actually surprised that I couldn't find any general URL encode/decode functions that can simply take full URLs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh BTW, TNM only complained about spaces in URL. That was another reason.

Copy link
Member

Choose a reason for hiding this comment

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

Then I don't have a better suggestion. I think a documentation would clarify the existence of the function or a separate function encode_spaces() or encode_X_for_Y() as there seems to be some specificity to this particular case.

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