rshell macro facility

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
pythoncoder
Posts: 3543
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

rshell macro facility

Post by pythoncoder » Tue Sep 03, 2019 7:06 am

I have implemented an experimental macro facility in this fork and I'd welcome feedback as to whether anyone else would find this useful. It is documented in README.rst. The idea is that you create Python files on your PC defining text macros. Typically you'd have one defining global macros, and one for your current project defining macros specific to that project. Aims are:
  • Reduce typing of long lines.
  • Implement new functionality (e.g. move/rename).
  • Provide common Linux shortcuts at the rshell > prompt.
  • Use common shortcuts to achieve project-specific effects.
You invoke rshell with -m my_macro_module. rshell imports a Python module my_macro_module on your Python path. This optionally imports your global module. These modules define a set of macros which can be listed at the rshell > prompt with a new lm command.

At the > prompt macros are expanded with a new m command. This session shows the use of two macros mv (move/rename a file) and lf (list the flash drive). After each invocation of a macro its expansion is displayed followed by the outcome of running it.

Code: Select all

/home/adminpete> m mv /flash/test.py /flash/test01.py
cp /flash/test.py /flash/test01.py; rm /flash/test.py
/home/adminpete> m lf
ls -al /flash/
   366 Aug 15 09:14 boot.py
   579 Aug 15 09:44 do_connect.py
    34 Dec 31 2014  main.py
     0 Sep  1 07:41 micropython_ra8875/
  2817 Dec 31 2014  pybcdc.inf
     0 Sep  3 07:32 test01.py
/home/adminpete> 
Macros are defined as entries in a Python dict, e.g.

Code: Select all

macros = {}
macros['..'] = 'cd ..'
macros['...'] = 'cd ../..'
macros['ll'] = 'ls -al {}', 'List arbitrary directory'
macros['lf'] = 'ls -al /flash/{}'
macros['lsd'] = 'ls -al /sd/{}'
macros['lpb'] = 'ls -al /pyboard/{}'
macros['mv'] = 'cp {0} {1}; rm {0}', 'Move/rename'
# A project specific example
macros['cpd'] = 'cp foo/demos/{0}.py /flash/foo/demos/; repl ~ import foo.demos.{0}', 'Copy a demo file and run it'
The proj macro lists the current project directory or a subdirectory (specified with an optional arg):

Code: Select all

/home/adminpete> m proj fonts
ls -l /flash/micropython_ra8875/fonts
     0 Sep  1 07:42 __init__.py
 13912 Sep  1 07:42 font10.py
 19785 Sep  1 07:42 font14.py
/home/adminpete> 
The lack of response to my RFC makes me wonder if I'm alone in thinking this is useful. If there is interest (expressed here or in response to the RFC) I'll submit a PR.
Peter Hinch

Post Reply