shell.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/python3
  2. import readline
  3. import logging
  4. import sys
  5. import json
  6. import subprocess
  7. def run(arg):
  8. s_dir = "/usr/milis/ayguci/cli.lua"
  9. get = subprocess.Popen([s_dir,arg], stdout = subprocess.PIPE)
  10. get.wait()
  11. get = get.communicate()[0]
  12. get = get.decode("utf-8","strict")
  13. return get
  14. # = i tanımama durumunda delims grubundan siliyoruz.
  15. old_delims = readline.get_completer_delims()
  16. readline.set_completer_delims(old_delims.replace('=', ''))
  17. def call(link,param):
  18. # Send data
  19. message = link
  20. print ('l->"%s"' % message)
  21. print ('p->"%s"' % param)
  22. # parse params
  23. _msg=""
  24. prms={}
  25. temp_p1=""
  26. for par in param:
  27. if "=" in par:
  28. p1=par.split("=")[0]
  29. p2=par.split("=")[1]
  30. prms[p1]=p2
  31. #temp_p1=p1
  32. else:
  33. prms[p1]="'"+prms[p1]+" "+par+"'"
  34. for key in prms:
  35. _msg+=f'"{key}" : "{prms[key]}",'
  36. #_msg+=f'"{p1}" : "{p2}",'
  37. if param:
  38. _msg=_msg[0:-1]
  39. message+="^"+"{"+_msg+"}"
  40. data=run(message)
  41. #print("data->",data.decode())
  42. json_object = json.loads(data)
  43. # json dumps ettikten sonra \u şeklinde yazmaması için ensure_ascii=False
  44. response = json.dumps(json_object, indent=2, ensure_ascii=False)
  45. print(response)
  46. funcs={
  47. "system":lambda x:call("system/info",x),
  48. "rfkill":lambda x:call("system/rfkill",x),
  49. "user":lambda x:call("account/user",x),
  50. "group":lambda x:call("account/group",x),
  51. "datetime":lambda x:call("datetime/manual",x),
  52. "utc":lambda x:call("datetime/utc",x),
  53. "timezone":lambda x:call("datetime/timezone",x),
  54. "kmod":lambda x:call("kernel/module_info",x),
  55. "lang":lambda x:call("locale/language",x),
  56. "kmod":lambda x:call("kernel/module",x),
  57. "disk":lambda x:call("disk/info",x),
  58. }
  59. # https://pymotw.com/3/readline/index.html#completing-text
  60. class SimpleCompleter(object):
  61. def __init__(self, options):
  62. self.options = sorted(options)
  63. return
  64. def complete(self, text, state):
  65. response = None
  66. if state == 0:
  67. # This is the first time for this text, so build a match list.
  68. if text:
  69. self.matches = [s
  70. for s in self.options
  71. if s and s.startswith(text)]
  72. #logging.debug('%s matches: %s', repr(text), self.matches)
  73. else:
  74. self.matches = self.options[:]
  75. #logging.debug('(empty input) matches: %s', self.matches)
  76. # Return the state'th item from the match list,
  77. # if we have that many.
  78. try:
  79. response = self.matches[state]
  80. except IndexError:
  81. response = None
  82. #logging.debug('complete(%s, %s) => %s', repr(text), state, repr(response))
  83. return response
  84. def shell():
  85. line = ''
  86. while line != 'stop':
  87. line = input('ayguci# ')
  88. fun_key=line.split()[0]
  89. fun_par=line.split()[1:]
  90. funcs[fun_key](fun_par) if fun_key in funcs else print ('bulunamadı! %s' % line)
  91. # Register our completer function
  92. readline.set_completer(SimpleCompleter([
  93. "method=get",
  94. "method=post",
  95. "query=info",
  96. "query="
  97. ]+list(funcs.keys())).complete)
  98. # oto tamamlama özelliği
  99. readline.parse_and_bind('tab: complete')
  100. # ayguci shell girdi döngüsü
  101. shell()