tf_extend.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.c = [0,0,0] # lines, words & bytes
  7. def write(self,text):
  8. self.c[2] += len(text)
  9. self.c[0] += 1
  10. self.c[1] += 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.c))
  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. try:
  56. print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
  57. except:
  58. print("network/DNS not available")
  59. elif cmd=='connect':
  60. if len(args)<3:
  61. print("syntax: connect <ssid> <password>")
  62. else:
  63. w=network.WLAN(network.STA_IF)
  64. w.connect(args[1],args[2])
  65. print("connecting...",end=' ')
  66. time.sleep(3)
  67. print(w.ifconfig() if w.isconnected() else "not yet connected; try 'ifconfig' in a few seconds")
  68. elif cmd=='scan':
  69. w=network.WLAN(network.STA_IF)
  70. print("scanning...")
  71. s=w.scan()
  72. if len(s)==0:
  73. print("no AP found")
  74. return True
  75. for i in s:
  76. print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
  77. elif cmd=='freq':
  78. # identify esp32 or esp8266
  79. try: # is this esp8266
  80. machine.TouchPad
  81. freqs=("160","80","240")
  82. except AttributeError:
  83. freqs=("160","80")
  84. if len(args)==1 or args[1] in freqs:
  85. if len(args)>1:
  86. machine.freq(int(args[1])*1000000)
  87. print("master cpu frequency {}MHz".format(machine.freq()//1000000))
  88. else:
  89. print("syntax: freq [ 160 | 80 | 240 ] ")
  90. elif cmd=='btree':
  91. try:
  92. f=open(args[1])
  93. b=btree.open(f)
  94. print("Key\t\tValue")
  95. i=0
  96. for k,v in b.items():
  97. print("{:10}\t{}".format(k,v))
  98. i+=1
  99. if i%30==0:
  100. r=input("continue? ")
  101. if r=='n': break
  102. except OSError:
  103. print("file not found or is not a btree database")
  104. elif cmd=='exec':
  105. try:
  106. exec(open(args[1]).read(),globals(),globals())
  107. except OSError:
  108. print("file not found")
  109. elif cmd=='free':
  110. print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
  111. elif cmd=='help':
  112. print("==Extended commands")
  113. print(" connect <essid> <password> \tscan")
  114. print(" ifconfig/ip \t\thost/dig/nslookup <domain.name>")
  115. print(" freq [160 | 80 | 240]\t\texec <python-filename>")
  116. print(" free \t\t\twc <filename>")
  117. print(" less/more [-n] <filename>")
  118. print(" btree <filename>")
  119. else: # command not found
  120. return False
  121. return True