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

Environment Variable Expansion #576

Merged
merged 25 commits into from Dec 20, 2015
Merged

Environment Variable Expansion #576

merged 25 commits into from Dec 20, 2015

Conversation

adqm
Copy link
Member

@adqm adqm commented Dec 19, 2015

This changeset:

I think it is all working, and I hope I didn't break anything else.

Also, separately, I'm not quite sure how the Travis and Appveyor autobuild things work, but I am trying here to make them run in quiet mode to avoid printing a dot for each of the test cases (there are really a lot of them now, after the last PR was merged, and printing the dots really seems to slow things down).

@adqm
Copy link
Member Author

adqm commented Dec 19, 2015

Woops this broke things on Windows, it seems. I'll ping when fixed.

@scopatz scopatz added this to the v0.3 milestone Dec 19, 2015
@scopatz
Copy link
Member

scopatz commented Dec 19, 2015

Thanks @adqm. Did a quick review and it seemed good. Didn't see the win error. Just ping.

@adqm
Copy link
Member Author

adqm commented Dec 20, 2015

Okay, @scopatz, ready for another look, I think. Changes from last time:

  • included a modified version of expandvars that uses xonsh's environment instead of os.environ to do replacement in strings
  • fixed the regexes that check for which ensurer to use, to avoid DIRSTACK_SIZE being interpreted as a \w*DIRS variable
  • skipped several test cases on Windows, where spoofed environments were necessary
  • made the environment variable lookups always fall back to the default values so that just checking, e.g., the value of $XONSH_STORE_STDOUT works now (shows the default, rather than raising an IndexError). Is there a reason things didn't work this way before?

i, j = m.span(0)
name = m.group(1)
if name.startswith(start) and name.endswith(end):
name = eval(name[1:-1]) # change for non-POSIX ${...}
Copy link
Member

Choose a reason for hiding this comment

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

ast.literal_eval() maybe? Or is this a full eval? Also do you need the current locals and globals? Seems like an empty execution context is desirable here.

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 think this has to be a full eval, but I think it should happen in the current locals and globals.

My thought is that if we have x = "HOME", both of the following should work:

>>> ${x}
>>> echo "${x}"

literal eval wouldn't handle that case.

Copy link
Member

Choose a reason for hiding this comment

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

Understood. Got it. That will be super cool to have.

Copy link
Member

Choose a reason for hiding this comment

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

But I think you then want to use the xonsh eval or at least the __xonsh_ctx__ as the execution context. As written this will use the function's globals and locals.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, sorry; that was what I meant by the comment above; we will want to eval in the xonsh context.

add slashes to quoted strings with, e.g., ~ in them
return path
if not _varprog:
import re
_varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't handle %name% completion on windows, which someone will probably complain about :) https://docs.python.org/3.5/library/os.path.html#os.path.expandvars Shouldn't be too hard to modify.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yes. That's what I get for copying from posixpath.py and not even looking at the windows equivalent. I can work that in.

@scopatz
Copy link
Member

scopatz commented Dec 20, 2015

made the environment variable lookups always fall back to the default values so that just checking, e.g., the value of $XONSH_STORE_STDOUT works now (shows the default, rather than raising an IndexError). Is there a reason things didn't work this way before?

Nope that was an oversight! Thanks for fixing.

@scopatz
Copy link
Member

scopatz commented Dec 20, 2015

Thanks for all of the hard work here!

@adqm
Copy link
Member Author

adqm commented Dec 20, 2015

Alright, ready for another look. This time I:

  • modified expandvars to do the %name% expansion on Windows
  • removed replace_env and undo_replace_env
  • modified the eval to evaluate in the right context

if self._orig_env is not None:
os.environ.clear()
os.environ.update(self._orig_env)
self._orig_env = None
Copy link
Member

Choose a reason for hiding this comment

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

I think that these are still potentially useful to users. Do you mind putting them back in?

@scopatz
Copy link
Member

scopatz commented Dec 20, 2015

Looks great to me, other than the method removal. Basically ready to merge.

@scopatz
Copy link
Member

scopatz commented Dec 20, 2015

I can put them back in if you'd like too.

@adqm
Copy link
Member Author

adqm commented Dec 20, 2015

I just put them back in, so we should be good to go, I think (assuming the tests pass). Let me know if you have any other thoughts/suggestions.

I'll also take a look at your history PR tonight.

scopatz added a commit that referenced this pull request Dec 20, 2015
Environment Variable Expansion
@scopatz scopatz merged commit 9e39b3b into xonsh:master Dec 20, 2015
@scopatz
Copy link
Member

scopatz commented Dec 20, 2015

Awesome!

@adqm adqm deleted the env_expand branch December 31, 2015 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants