main.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # this is as the PYTHONSTARTUP target file
  2. async def __main__(argc,argv):
  3. while use.lives:
  4. print('atic-atac')
  5. await aio.sleep(10)
  6. import uio as io
  7. import sys
  8. try:
  9. use.srv = open('srv','r').read().strip()
  10. use.host = open('machine','r').read().strip()
  11. except OSError:
  12. print('machine','config','not','set')
  13. import aio
  14. def sha256(data):
  15. import hashlib as uhashlib
  16. import ubinascii
  17. h = uhashlib.sha256()
  18. if isinstance(data,str):
  19. h.update(data.encode('utf-8'))
  20. else:
  21. h.update(data)
  22. return ubinascii.hexlify(h.digest()).decode()
  23. def mkdir(p):
  24. p = p.rstrip("/\n\r\t")
  25. try:
  26. if __UPY__:
  27. __import__("uos").mkdir(p)
  28. else:
  29. __import__("os").mkdir(p)
  30. return p
  31. except OSError as e:
  32. if e.args[0] == 17:
  33. return
  34. mkdir('cache')
  35. def fopen(f, no_html=False):
  36. if f.startswith(':'):
  37. f = f[1:].strip('/')
  38. u= '{0}/{1}'.format( use.srv, f )
  39. f = 'cache/%s' % sha256(u)
  40. try:
  41. try:
  42. data = urequests.get(u)
  43. except:
  44. print(u)
  45. raise
  46. data = data.content
  47. if no_html and data[0]!=60:
  48. try:
  49. sum = open(f+".sum",'r').read()
  50. except:
  51. sum = ""
  52. if sha256( data ) != sum:
  53. with open(f,'wb') as u:
  54. u.write(data)
  55. with open(f+".sum",'wb') as u:
  56. u.write(sum)
  57. else:
  58. print("fopen",f,"sum","ok")
  59. else:
  60. print("fopen",f,"for", u)
  61. except Exception as e:
  62. sys.print_exception(e,sys.stderr)
  63. print('uses',f)
  64. return open(f,'rb')
  65. builtins.fopen = fopen
  66. if hasattr(use,'host'):
  67. try:
  68. code = fopen(':%s.py' % use.host, no_html=True).read().decode()
  69. except Exception as e:
  70. code = '#' + repr(e)
  71. print(code)
  72. try:
  73. exec( compile( code , '<%s>' % use.host, 'exec') , globals(), globals() )
  74. code=""
  75. if aio.paused is unset:
  76. print("start","aio","run_forever")
  77. aio.loop.run_forever()
  78. aio.loop.close()
  79. except Exception as e:
  80. print("="*79)
  81. print(code)
  82. print("="*79)
  83. sys.print_exception(e,sys.stderr)
  84. _ESC = '\x1b'
  85. _CSI = '\x1b['
  86. def CSI(n,X='',file=sys.stdout):
  87. global _ESC, _CSI
  88. file.write( ''.join( [_CSI, n, X] ) )
  89. #import xpy.readline as readline
  90. import xpy.readline as readline
  91. try:
  92. from xpy.readtouch import ReadTouch
  93. except Exception as e:
  94. pdb("19: no touch support from xpy.readtouch : {}".format((e)))
  95. class readtouch:
  96. def process_touch(self,*a,**k):
  97. pass
  98. class readline(readline.readline, readtouch):
  99. old_settings = None
  100. fd = 0
  101. MB = {
  102. 'B' : '',
  103. 'C': 0,
  104. 0 : 'L',
  105. 1 : 'M',
  106. 2 : 'R',
  107. 3 : '',
  108. 'D' : { '':'' },
  109. }
  110. @classmethod
  111. def Begin(cls,channel=sys.stdin):
  112. try:
  113. import tty
  114. cls.fd = channel.fileno()
  115. tty.setraw(cls.fd)
  116. import termios
  117. cls.old_settings = termios.tcgetattr(cls.fd)
  118. except:
  119. err('no tty or no termios')
  120. pdb("217: mouse mode 1003")
  121. CSI('?1003h')
  122. @classmethod
  123. def End(cls):
  124. if cls.old_settings:
  125. import termios
  126. if __UPY__:
  127. termios.tcsetattr(cls.fd, 1, cls.old_settings)
  128. else:
  129. termios.tcsetattr(cls.fd, termios.TCSADRAIN, cls.old_settings)
  130. CSI('?1003l')
  131. readline = readline()
  132. def repl(prompt=None, password=False):
  133. __print__ = print
  134. input = __import__('uio').StringIO()
  135. input.seek(0)
  136. tell = 0
  137. current = prompt
  138. frames = 0
  139. if sys.platform in ('esp32',):
  140. UART=__import__('machine').UART(1, tx=1, rx=3, baudrate=115200)
  141. __write__ = UART.write
  142. def switch(input=None):
  143. if input is not None:
  144. out(input)
  145. else:
  146. UART = __import__('machine').UART(0)
  147. __write__ = sys.stdout.write
  148. def switch(input=None):
  149. if input is not None:
  150. out(input)
  151. os.dupterm( None , 1)
  152. def out(*argv,**kw):
  153. output = __import__('uio').StringIO()
  154. if kw.get('file',sys.stdout) in (sys.stdout, sys.stderr,):
  155. kw['file']=output
  156. __print__(*argv, **kw)
  157. output.seek(0)
  158. data = output.read().encode().replace(b'\n',b'\r\n')
  159. __write__(data)
  160. output.close()
  161. else:
  162. __print__(*argv, **kw)
  163. builtins.print = out
  164. switch(current or '%c[2J%c[0;0H\n=~> ' % (27,27))
  165. while use.lives:
  166. try:
  167. if UART.any():
  168. current = UART.read(1)
  169. chr = ord(current)
  170. if chr<127:
  171. if chr>31:
  172. input.seek(tell)
  173. input.write(current)
  174. tell += 1
  175. if current == b'X' :
  176. try:
  177. UART.deinit()
  178. except:
  179. pass
  180. finally:
  181. machine.reset()
  182. elif chr in [13,10]: #???? flush 10 ?
  183. out('\n')
  184. input.seek(0)
  185. out( input.read() )
  186. out('\n')
  187. input = __import__('uio').StringIO()
  188. input.seek(0)
  189. tell = 0
  190. current = prompt
  191. else:
  192. out('\nCTRL : %s\n' % chr )
  193. elif chr==127:
  194. tell -= 1 if tell>0 else 0
  195. switch(current)
  196. current = None
  197. else:
  198. Time.sleep(.016)
  199. frames +=1
  200. if frames > 25*10:
  201. out(b'tic-tac\n')
  202. frames=0
  203. except Exception as e:
  204. __write__( repr(e).encode() )
  205. #print("\x1b[1,12r",end="")
  206. #print("Scroll reg 1-12");
  207. #