tf_extend.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os,sys,network,socket,time,machine,gc,tf,btree
  2. # these helper classes let us use the tf.transfer() iterator,
  3. # by intercepting the .write()
  4. class wc():
  5. def __init__(self):
  6. self.words=self.lines=self.bytes_=0
  7. def write(self,text):
  8. self.bytes_ += len(text)
  9. self.lines += 1
  10. self.words += len(text.split())
  11. class lessor():
  12. def __init__(self,nums=False):
  13. self.i=0
  14. self.nums=nums
  15. def write(self,text):
  16. if self.i==-1: return
  17. self.i += 1
  18. if self.nums: sys.stdout.write(str(self.i)+' ')
  19. sys.stdout.write(text)
  20. if self.i%30==0:
  21. sys.stdout.write("====> press <enter> to see more, N or Q to quit <====\n")
  22. g=sys.stdin.read(1)
  23. if g=='\n': g=sys.stdin.read(1)
  24. if g in "nNqQ":
  25. self.i=-1
  26. # the main entry point for the extension
  27. # returns True if the command was interpreted
  28. def cmd(args):
  29. cmd=args[0]
  30. if cmd in ('wc','more','less','exec') and len(args)<2:
  31. print("syntax: "+cmd+" <filename>")
  32. return True
  33. if cmd in ('wc','more','less'):
  34. nums=False
  35. if cmd=='wc':
  36. w = wc()
  37. else:
  38. if args[1]=='-n':
  39. nums=True
  40. del(args[1])
  41. w = lessor(nums)
  42. try:
  43. tf.transfer(args[1],w)
  44. if cmd=='wc':
  45. print("lines: {}\twords: {}\tbytes: {}".format(w.lines, w.words, w.bytes_))
  46. except:
  47. print("file not found: "+args[1])
  48. elif cmd in ('ifconfig','ip'):
  49. ifc=network.WLAN().ifconfig()
  50. print("IP: {}\tmask: {}\tgateway: {}\tDNS: {}".format(*ifc))
  51. elif cmd in ('host','nslookup','dig'):
  52. if len(args)<2:
  53. print("syntax: host <domain.name>")
  54. else:
  55. print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
  56. elif cmd=='connect':
  57. if len(args)<3:
  58. print("syntax: connect <ssid> <password>")
  59. else:
  60. w=network.WLAN(network.STA_IF)
  61. w.connect(args[1],args[2])
  62. print("connecting...",end=' ')
  63. time.sleep(3)
  64. print(w.ifconfig() if w.isconnected() else "not yet connected; try 'ifconfig' in a few seconds")
  65. elif cmd=='scan':
  66. w=network.WLAN(network.STA_IF)
  67. print("scanning...")
  68. s=w.scan()
  69. if len(s)==0:
  70. print("no AP found")
  71. return True
  72. for i in s:
  73. print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
  74. elif cmd=='freq':
  75. # identify esp32 or esp8266
  76. try: # is this esp8266
  77. machine.TouchPad
  78. freqs=("160","80","240")
  79. except AttributeError:
  80. freqs=("160","80")
  81. if len(args)==1 or args[1] in freqs:
  82. if len(args)>1:
  83. machine.freq(int(args[1])*1000000)
  84. print("master cpu frequency {}MHz".format(machine.freq()//1000000))
  85. else:
  86. print("syntax: freq [ 160 | 80 | 240 ] ")
  87. elif cmd=='btree':
  88. try:
  89. f=open(args[1])
  90. b=btree.open(f)
  91. print("Key\t\tValue")
  92. i=0
  93. for w in b:
  94. print("{}\t{}".format(w,b[w]))
  95. i+=1
  96. if i%30==0:
  97. r=input("continue? ")
  98. if r=='n': break
  99. except OSError:
  100. print("file not found or is not a btree database")
  101. elif cmd=='exec':
  102. try:
  103. exec(open(args[1]).read(),globals(),globals())
  104. except OSError:
  105. print("file not found")
  106. elif cmd=='free':
  107. print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
  108. elif cmd=='help':
  109. print("==Extended commands")
  110. print(" connect <essid> <password> \tscan")
  111. print(" ifconfig/ip \t\thost/dig/nslookup <domain.name>")
  112. print(" freq [160 | 80 | 240]\texec <python-filename>")
  113. print(" free \t\t\twc <filename>")
  114. print(" less/more [-n] <filename>")
  115. print(" bt-list <filename>")
  116. else: # command not found
  117. return False
  118. return True