浏览代码

generic buit ins with params

Johann Woelper 10 年之前
父节点
当前提交
d1d3a8003d
共有 1 个文件被更改,包括 50 次插入18 次删除
  1. 50 18
      pshell/pshell

+ 50 - 18
pshell/pshell

@@ -1,31 +1,63 @@
+#!/usr/bin/env python
 import sys
 import os
 import readline
 import subprocess
+from plumbum import local
 
 def vi():
     subprocess.call('vi')
 
-possibles = globals().copy()
-possibles.update(locals())
+def ll(*args):
+    print local['ls']['-la']()
+
+# is a builtin
+def cd(*args):
+    # TODO: no param, go to home
+    # param is - : go back
+    print args
+    if len(args) == 0:
+        args = '/home/woelper'
+    else:
+        args = args[0]
+    print 'cd to', args[0]
+    local.cwd.chdir(args[0])
+
+def exec_if_builtin(string):
+    print 'STR', string
+    string = string.split()
+    command = string[0]
+    parameters = string[1:]
+    possibles = globals().copy()
+    possibles.update(locals())
+    method = possibles.get(command)
+    if method:
+        method(parameters)
+        return True
+    print 'Did not detect a function by that name'
+
+
+def as_plumbum(string):
+    print 'Running cmd through plumbum'
+    # TODO: until pipe or special char
+    # split the string
+    string = string.split()
+    #print string
+    command = string[0]
+    parameters = string[1:]
+    print command, parameters
+    print local[command][parameters]()
 
 while True:
-    input_string = raw_input('$ ')
-    method = possibles.get(input_string)
-    if not method:
+    input_string = raw_input(local.cwd + ' >> ')
+    
+    # was the string a regular line of python?
+    if exec_if_builtin(input_string) is not True:
         try:
-            
-            print subprocess.check_output(input_string)
+            exec(input_string)
         except:
-            print "Method was not implemented, tryed built-in"
-            print 'Exec failed:', input_string
-        
-    else:
-        method()
-    
-    try:
-        pass
-        #exec(test)
+            try:
+                as_plumbum(input_string)
+            except:
+                print 'command not found:', input_string
         
-    except:
-        print 'Execution error'