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

cp.get_file_str: do not fail if file not found #36936

Merged
merged 1 commit into from
Oct 12, 2016
Merged

cp.get_file_str: do not fail if file not found #36936

merged 1 commit into from
Oct 12, 2016

Conversation

vutny
Copy link
Contributor

@vutny vutny commented Oct 12, 2016

What does this PR do?

It removes misleading exception message when cp.get_file_str function tries to display non-existent file:

Passed invalid arguments: coercing to Unicode: need string or buffer, bool found.

Instead, the function will simply return the result given by cache_file function which does the real job.

This becomes especially useful in Jinja templates.

Previous Behavior

# salt-call cp.get_file_str salt://files/test2
[ERROR   ] Unable to cache file u'salt://files/test2' from saltenv 'base'.

Passed invalid arguments: coercing to Unicode: need string or buffer, bool found.

Usage:

    Return the contents of a file from a URL

    CLI Example:

    .. code-block:: bash

        salt '*' cp.get_file_str salt://my/file

New Behavior

# salt-call cp.get_file_str salt://files/test2
[ERROR   ] Unable to cache file u'salt://files/test2' from saltenv 'base'.
local:
    False

Tests written?

No

with salt.utils.fopen(fn_, 'r') as fp_:
data = fp_.read()
return data
if isinstance(fn_, str):
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be using six.string_type instead of str here.

Any reason you're not using os.path.exists or something similar instead of checking for a str type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rallytime Fixed, thanks!

os.path.exists(fn_) doesn't suit here, because it will throw TypeError if cache_file() function would return False (file cannot be cached for some reason). Too much logic to wrap around this simple check.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right. That makes sense. Thanks!

@rallytime rallytime added the pending-changes The pull request needs additional changes before it can be merged label Oct 12, 2016
@rallytime rallytime merged commit e2bc94b into saltstack:2015.8 Oct 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-changes The pull request needs additional changes before it can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants