86 lines
2.3 KiB
Python
Executable File
86 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
import cmd
|
|
import pkg_resources as pkg
|
|
__name__="cli"
|
|
|
|
print()
|
|
#init the array only if needed
|
|
try:listed=listed
|
|
except NameError:listed=[]
|
|
init=True
|
|
|
|
def ls(where=""):
|
|
where=where.lstrip("/")
|
|
#doesn't appear to have effect practically, but still better to have it than not, might be needed when script used in a directory
|
|
|
|
#just fancy bars to visually make clear where script output is
|
|
# if where != "": print("\n","─"*5+"separator"+"─"*5,"\n")
|
|
|
|
#just fancy bars to visually make clear where script output is
|
|
if where != "": print("\n","─"*5+(where if where != "" else "/")+"─"*5,"\n")
|
|
|
|
# print("listing",where if where != "" else "/","\n")
|
|
global listed
|
|
try:
|
|
if pkg.resource_isdir(__name__,where):
|
|
for i in pkg.resource_listdir(__name__,where):
|
|
if i != "":
|
|
if pkg.resource_isdir(__name__, where+"/"+i):
|
|
if i != "": print("DIR : ",i+"/")
|
|
else: print("FILE: ",where+"/"+i if where != "" else i)
|
|
else:
|
|
print("not a dir") #should only be called when trying to ls(file), which should not happen unless called manually
|
|
|
|
|
|
except RecursionError: print("\nTOO MUCH RECURSION!\n") #better than the bunch of ugly traceback python does by default
|
|
|
|
class test_cli(cmd.Cmd):
|
|
def __init__(self, intro="Self-Resource CLI\n© jan6 & Co. 2020-3030\n",
|
|
prompt="(self/DIR) "):
|
|
cmd.Cmd.__init__(self)
|
|
self.intro=intro
|
|
self.prompt=prompt
|
|
self.doc_header="Test Cli (type 'help <topic>'):"
|
|
def emptyline(self): pass
|
|
def do_end(self, arg):
|
|
"""
|
|
just
|
|
end it...
|
|
"""
|
|
return True
|
|
#def help_end(arg):
|
|
# print("End session")
|
|
do_EOF = do_eof = do_quit = do_end
|
|
def do_quit(self, args):
|
|
if args: print(args)
|
|
return True
|
|
def precmd(self, line):
|
|
global init
|
|
newline=line.strip()
|
|
#is_cmt=newline.startswith('#')
|
|
if newline.startswith('#'):
|
|
return ('')
|
|
elif newline.startswith('/'):
|
|
if init: return 'EOF'
|
|
else: return 'quit bye'
|
|
elif newline=='':
|
|
return 'echo null'
|
|
init=False
|
|
return (line)
|
|
def do_echo(self, args): print(args)
|
|
def do_ls(self, args):
|
|
print("arguments:",args)
|
|
if not args: args=""
|
|
args=str(args)
|
|
#print("my name is "+__name__)
|
|
ls(args)
|
|
#args=str(args)
|
|
#if pkg.resource_isdir(__name__,args): pkg.resource_listdir(__name__,args)
|
|
#else: print(args)
|
|
|
|
if True:
|
|
cli=test_cli()
|
|
cli.cmdloop()
|
|
|