Browse Source

added 'file print' command

kmpm 8 years ago
parent
commit
a36d99f836
3 changed files with 21 additions and 10 deletions
  1. 2 0
      lib/luacode.py
  2. 5 2
      lib/main.py
  3. 14 8
      lib/uploader.py

+ 2 - 0
lib/luacode.py

@@ -5,6 +5,8 @@
 
 DOWNLOAD_FILE = "file.open('{filename}') print(file.seek('end', 0)) file.seek('set', {bytes_read}) uart.write(0, file.read({chunk_size}))file.close()"
 
+PRINT_FILE = "file.open('{filename}') print('---{filename}---') print(file.read()) file.close() print('---')"
+
 LIST_FILES = 'for key,value in pairs(file.list()) do print(key,value) end'
 
 SAVE_LUA = \

+ 5 - 2
lib/main.py

@@ -47,7 +47,7 @@ def operation_upload(uploader, sources, verify, do_compile, do_file, do_restart)
                 elif do_file:
                     uploader.file_do(d)
         else:
-            raise Exception('Error preparing nodemcu for reception') 
+            raise Exception('Error preparing nodemcu for reception')
     else:
         raise Exception('You must specify a destination filename for each file you want to upload.')
 
@@ -79,6 +79,9 @@ def operation_file(uploader, cmd, filename=''):
     elif cmd == 'remove':
         for f in filename:
             uploader.file_remove(f)
+    elif cmd == 'print':
+        for f in filename:
+            uploader.file_print(f)
 
 
 
@@ -180,7 +183,7 @@ def main_func():
 
     file_parser.add_argument(
         'cmd',
-        choices=('list', 'do', 'format', 'remove'),
+        choices=('list', 'do', 'format', 'remove', 'print'),
         help="list=list files, do=dofile given path, format=formate file area, remove=remove given path")
 
     file_parser.add_argument('filename', nargs='*', help='path for cmd')

+ 14 - 8
lib/uploader.py

@@ -9,7 +9,7 @@ import os
 import serial
 
 from .utils import default_port
-from .luacode import DOWNLOAD_FILE, SAVE_LUA, LUA_FUNCTIONS, LIST_FILES, UART_SETUP
+from .luacode import DOWNLOAD_FILE, SAVE_LUA, LUA_FUNCTIONS, LIST_FILES, UART_SETUP, PRINT_FILE
 
 log = logging.getLogger(__name__)
 
@@ -64,8 +64,8 @@ class Uploader(object):
         except AttributeError:
             #pySerial 2.7
             self._port.baudrate = baud
-    
-    
+
+
     def clear_buffers(self):
         try:
             self._port.reset_input_buffer()
@@ -74,7 +74,7 @@ class Uploader(object):
             #pySerial 2.7
             self._port.flushInput()
             self._port.flushOutput()
-        
+
 
     def expect(self, exp='> ', timeout=TIMEOUT):
         """will wait for exp to be returned from nodemcu or timeout"""
@@ -91,10 +91,10 @@ class Uploader(object):
         data = ''
         while not data.endswith(exp) and time.time() <= end:
             data += self._port.read()
-        
+
         if time.time() > end and not data.endswith(exp) and len(exp) > 0:
             raise Exception('Timeout expecting ' + exp)
-        
+
         self._port.timeout = timer
         log.debug('expect returned: `{0}`'.format(data))
         return data
@@ -143,7 +143,7 @@ class Uploader(object):
         else:
             log.debug('Found all required lua functions, no need to upload them')
             return True
-            
+
         data = SAVE_LUA.format(baud=self._port.baudrate)
         ##change any \r\n to just \n and split on that
         lines = data.replace('\r', '').split('\n')
@@ -225,7 +225,7 @@ class Uploader(object):
         #zero size block
         self.write_chunk('')
 
-        if verify == 'standard':
+        if verify == 'text':
             log.info('Verifying...')
             data = self.download_file(destination)
             if content != data:
@@ -309,6 +309,12 @@ class Uploader(object):
             log.info(res)
         return res
 
+    def file_print(self, f):
+        log.info('Printing ' + f)
+        res = self.exchange(PRINT_FILE.format(filename=f))
+        log.info(res)
+        return res
+
     def node_heap(self):
         log.info('Heap')
         res = self.exchange('print(node.heap())')