Last active
August 29, 2015 14:21
-
-
Save altnight/8107f38ce58689b21221 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
def parse_input_string(strs): | |
import shlex | |
parsed = shlex.split(cmd) | |
print(parsed) | |
return parsed | |
cmd = 'hoge | moge 1 | insert_delimiter ! | whitespace' | |
parsed = parse_input_string(cmd) | |
def parse_command(parsed): | |
ret = [] | |
current = [] | |
while parsed: | |
c = parsed.pop(0) | |
if c == "|": | |
ret.append(current) | |
current = [] | |
else: | |
current.append(c) | |
ret.append(current) | |
return ret | |
cmds = parse_command(parsed) | |
print(cmds) | |
def register_functions(): | |
def echo(barg, arg=''): | |
if arg: | |
ret = arg | |
elif barg: | |
ret = barg | |
else: | |
ret = '' | |
return ret | |
def moge(barg, arg='?'): | |
return '!moge' + barg + arg + 'moge!' | |
def whitespace(barg): | |
return ' '.join([c for c in barg]) | |
def insert_delimiter(barg, delimiter=','): | |
return '{}'.format(delimiter).join([c for c in barg]) | |
fns = [echo, moge, whitespace, insert_delimiter] | |
return fns | |
def get_fn(fn_str): | |
for fn in register_functions(): | |
if fn.__name__ == fn_str: | |
return fn | |
return None | |
def emit(cmds): | |
default_func = get_fn('echo') | |
ret ='' | |
while len(cmds) > 0: | |
_c = cmds.pop(0) | |
fn_str = _c[0] | |
args = _c[1:] | |
if callable(get_fn(fn_str)): | |
ret = get_fn(fn_str)(ret, *args) | |
else: | |
ret = default_func(''.join(_c)) | |
print('no commands') | |
print(ret) | |
emit(cmds) |
Author
altnight
commented
May 24, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment