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

--list flag accepts only text files using mimetypes #414

Merged
merged 3 commits into from
Oct 30, 2018
Merged

--list flag accepts only text files using mimetypes #414

merged 3 commits into from
Oct 30, 2018

Conversation

manveerxyz
Copy link
Contributor

@manveerxyz manveerxyz commented Oct 28, 2018

I ended up implementing the mime-type checking using python's built-in mimetypes. It gets the job done but isn't as accurate as the python-magic library originally proposed.

The reason I substituted away was because python-magic would add another dependency. Although if someone insists I can switch to python-magic instead and add python-magic-bin to setup.py.

EDIT: Solution to #354

spotdl/handle.py Outdated
@@ -263,6 +264,10 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
if parsed.config is not None and to_merge:
parsed = override_config(parsed.config, parser)

file = parsed.list
Copy link
Member

Choose a reason for hiding this comment

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

file function is removed from Python 3 but let's still try to avoid to it. Text editors and GitHub still highlight file as something special and it kinda confuses the eyes. I think we could completely avoid setting this variable here and simply go with parsed.list for the checks below.

(Accessing an attribute on an object may be slower than accessing a local variable but the effect should be negligible, we should probably not worry about it)

spotdl/handle.py Outdated
@@ -263,6 +264,10 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
if parsed.config is not None and to_merge:
parsed = override_config(parsed.config, parser)

file = parsed.list
if mimetypes.MimeTypes().guess_type(file)[0] != "text/plain":
Copy link
Member

Choose a reason for hiding this comment

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

This looks okay but to be consistent with rest of the codebase; let's use if not .. == .. rather than if .. != ...

spotdl/handle.py Outdated
@@ -263,6 +264,10 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
if parsed.config is not None and to_merge:
parsed = override_config(parsed.config, parser)

file = parsed.list
if mimetypes.MimeTypes().guess_type(file)[0] != "text/plain":
parser.error("{0} is not of a valid file format to be used with --list".format(file))
Copy link
Member

Choose a reason for hiding this comment

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

We could make the error message here a bit constructive. Something like mentioning that the file needs to be a plain text file for it to be accepted.

@ritiek
Copy link
Member

ritiek commented Oct 29, 2018

Looks good! I'll take a look what's up with tests and then we could merge this in.

@ritiek ritiek merged commit 20b5e44 into spotDL:master Oct 30, 2018
@ritiek ritiek added this to the v1.1.0 milestone Nov 13, 2018
@ritiek ritiek mentioned this pull request Nov 13, 2018
6 tasks
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

2 participants