Created
April 4, 2012 00:48
-
-
Save tsprlng/2296774 to your computer and use it in GitHub Desktop.
The Pysh Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a Pysh script based on an 'idealized' partial re-implementation of my uta utility. | |
Pysh will basically be a language (or set of ugly macros) for quickly hacking shell-scripty tasks in Python. | |
Theoretically you get the luxury of Python idioms like generators, context managers, and meaningful whitespace, with the flexibility to hurl data between other programs in various convenient ways that don't involve Popen. | |
All the best languages have quirky special characters, right? (It's not like Perl, I promise.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/pysh --v1.02 --shell /bin/bash --workin ./.pysh | |
# Interpreter arg. ideas (maybe not): | |
# --v: A compatibility option (preserve old syntax? or at least warn of changes if script broken) | |
# --shell: Which shell to use for expansion of $ calls? | |
# --workin: Specify alternate working directory (create one to use for FIFOs n stuff. test permissions at start! no broken transactions like) | |
# This attempts to demonstrate some of the ideas I've had for Pysh by way of an example script. | |
# File extension will actually be .pysh, maybe cross-compiling (or regex-abusing for a start) to .py, but I'm using .py at the moment to at least get basic highlighting. | |
# I suppose I'll have to eventually provide a vim color scheme at least! | |
# http://www.jperla.com/blog/post/a-clean-python-shell-script <-- Implementation | |
# http://stackoverflow.com/questions/4840580/how-do-i-use-python-to-easily-expand-variables-to-strings | |
students = { 'aa1111' : 'Alice', | |
'rr2211' : 'Bob' } | |
funs = { 'print' => print } | |
# include easy python argument parsing (global argv + ???) | |
def main(args): # args structures can be parsed into vars or passed to shell functions with &>> | |
# must provide casts to string/list in case people want to make their own parsing systems | |
args &>> [ | |
('purpose', pos=true, select=funs._keys, help='What do you want to do?'), | |
('verbose', switch='-v') | |
] | |
funs[purpose]() | |
def print: | |
=for= student in ./*: # == means 'do a progress bar' or otherwise indicate progress/activity as well as possible (get some semi-meaningful output from quick hack scripts without silly effort). May need persistent and non-persistent output versions. | |
if student not in students.keys: continue | |
enscripts = [] | |
for f in ('./{student}' $ diffFiles): # get lines from command output as generator | |
pipe |<< './{student}' $ enscript -T '{students[student]}/{f}' --blablaseveralopts '{f}' | |
# quoted strings automatically get pre-expanded... | |
enscripts.append(pipe) | |
with cd('./{student}'): | |
'./print.pdf' << concat(enscripts) |>> $ ps2pdf | |
def$ diffFiles: # def$ means SHELL FUNCTION -- a virtual shell alias accessible from anywhere in this script... a bit hackish but I can't think of a more useful way to do it. Shell functions are an abstraction around program execution. You can pass in arguments and/or pipe abstractions, specify a working directory (which establishes a base for relative paths inside the function) and so on... | |
$ git diff --name-only `firstCommit` ./src | |
# -- ($) call without dir runs in cwd (in this case the one passed to fn) by default | |
# -- multiple calls get composed so the entire function's calls essentially share IO pipes... | |
# -- shell funs can return lists (lines) or pipe abstractions... determined by caller. | |
# -- in this case we just alias this other shell fun (which is actually a representation of a real program...) | |
def$ firstCommit: | |
$ git show-commits blablabla | tail -n 1 | |
def diff: | |
for student in ./*: | |
if student not in students.keys: continue | |
pipe |<< './{student}' $ diffFiles | |
# Following all that will be the reference pysh-compiler implementation in pysh itself, which essentially calls sed a billion times on semi-translated versions of the code sitting in anonymous pipes... It will expose all the bugs beautifully and I'll die of shame. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xonsh is better