Organising xonsh files

167 views
Skip to first unread message

andrea crotti

unread,
Nov 6, 2017, 5:18:43 PM11/6/17
to xonsh
Hi everyone,
I just switched to Xonsh last weekend, and loving it so far.

What I would like to do now is to rewrite some crappy bash functions into .xsh files, write a few completers and so on and so forth.

I had a look at the documentation however, and I don't see any suggested way to organise multiple files with functions spread around.

Any best practices for that?
I could just place .xsh files in the same directory as the xonshrc and "import *" from them?
Doesn't sound so great but could work, any other suggestions?

The other obvious thing I can't find documented is how to reload the ".xonshrc" configuration file, what is the right way to do that?
Thanks a lot,
Andrea

Dieter Van Eessen

unread,
Nov 7, 2017, 12:39:21 PM11/7/17
to andrea crotti, xonsh
Hello Andrea,

Also recently started using xonsh.

A. About importing, I can provide you 2 alternatives:
1) Add 'sys.path.append("/home/<username>/.config/xonsh/")' somewhere in ~/.xonshrc
    Add __init__.py, write and test modules there and import them in .xonshrc.
   -PRO: This is a default directory created and used by xonsh (check in docs)
   -CON: Directory name is xonsh. Explicit relative imports within this package fail since 'xonsh' package now exists 2 times.

2) (My preference) Add 'sys.path.append("/home/<username>/.config/")' in ~/.xonshrc
    Create directory/package xonsh_config inside .config and import from there
    -CON: This it NOT default. Normally I always prefer to keep default behavior instead.
    -CON: subdirectories in .config are normally always <PROGRAM/LIBRARY NAME>. This fucks up (an unspoken) convention.
    -PRO: I think there's an environment variable in xonsh which defines it's configuration directory (which is .config/xonsh by default)
    -PRO: Now you don't encounter issues when importing from xonsh package. xonsh and xonsh_config are now clearly distinguished.

3) (Would be nice if it worked :). It would be nice if .config was by convention not a hidden directory. It would then be possible to use for every python program:  'from config import ...'. But this doesn't work for hidden dirs... and since .config  and rc files are kinda very stable conventions, I don't want to change it.

I prefer 2) because I prefer adding tests on whatever I write. Got a 'test.py' and 'log.py' which import modules from both 'xonsh' and 'xonsh_config' package. Testing before implementation is useful when using xonsh as primary shell :)

B. About reloading:
Use 'from importlib import reload'. Use it as you wish! As an example, I got the following piece in .xonshrc to reload an 'on_precommand' function every time before I issue a command
import xonsh_config.ntwuk
def ntwuk_wrapper(cmd, **kwargs):
    reload(xonsh_config.ntwuk)
    return xonsh_config.ntwuk.on_precommand(cmd, **kwargs)
events.on_precommand(ntwuk_wrapper)

Hopefully this may provide some useful insights
Gtz,
Dieter



--
You received this message because you are subscribed to the Google Groups "xonsh" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+unsubscribe@googlegroups.com.
To post to this group, send email to xo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xonsh/220ee692-9d43-473f-9c7c-89834ceac3c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

andrea crotti

unread,
Nov 7, 2017, 3:06:40 PM11/7/17
to xonsh
Well I can also have a separate directory altogether and use just import everything from there, doesn't matter too much if it's not standard.

My .xonshrc file is also a symlink anyway to another directory.
However I'm a bit surprised that in the docs there is no mention of this at all, maybe would be worth to add a paragraph about that with some example configurations?

And about the reload sure that works, except that I had the same issue you had with hidden files (.xonshrc) so again just have to place things in other modules instead.
To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+un...@googlegroups.com.

adam j hartz

unread,
Nov 7, 2017, 9:53:48 PM11/7/17
to andrea crotti, xonsh
As an alternative to Pythonic imports, there is also the "source" built-in. For example, running:

$ source ~/.xonshrc

is a way to reload your config.


And it would be possible to programmatically source from, for example, all the files in a directory by adding something like the following to your ~/.xonshrc:

cd ~/my_xonsh_config_files
for fname in !(ls .):
source @(fname.strip())
cd -

Arguably a hack, but I think it could get the job done.


-Adam
> >> email to xonsh+un...@googlegroups.com <javascript:>.
> >> To post to this group, send email to xo...@googlegroups.com <javascript:>
> >> .
> >> <https://groups.google.com/d/msgid/xonsh/220ee692-9d43-473f-9c7c-89834ceac3c0%40googlegroups.com?utm_medium=email&utm_source=footer>
> >> .
> >> For more options, visit https://groups.google.com/d/optout.
> >>
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups "xonsh" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to xonsh+un...@googlegroups.com.
> To post to this group, send email to xo...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/xonsh/d4046ba1-1bd5-42a5-a722-5c13f12acecf%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages