Browse Source

Implement downloading of files.

Lourens Rozema 9 years ago
parent
commit
ffdee958af
2 changed files with 48 additions and 0 deletions
  1. 13 0
      README.md
  2. 35 0
      nodemcu-uploader.py

+ 13 - 0
README.md

@@ -26,6 +26,19 @@ Uploading a number of files, but saving with a different file name.
 ./nodemcu-uploader.py upload -f init.lua -d new_init.lua -f README.md -d new_README.md
 ```
 
+###Download
+Downloading a number of files.
+
+```
+./nodemcu-uploader.py download -f init.lua -f README.md -f nodemcu-uploader.py
+```
+
+Downloading a number of files, but saving with a different file name.
+
+```
+./nodemcu-uploader.py download -f init.lua -d new_init.lua -f README.md -d new_README.md
+```
+
 ###List files
 ```
 ./nodemcu-uploader.py --port com1 file list

+ 35 - 0
nodemcu-uploader.py

@@ -108,6 +108,16 @@ class Uploader:
             return
 
 
+    def read_file(self, filename, destination = ''):
+        if not destination:
+            destination = filename
+        self.dump()
+        self._port.write(r"file.open('" + filename + r"') print(file.seek('end', 0)) file.seek('set', 0) uart.write(0, file.read()) file.close()" + '\n')
+        cmd, size, data = self.dump().split('\n', 2)
+        data = data[0:int(size)]
+        with open(destination, 'w') as f:
+          f.write(data)
+
     def write_file(self, path, destination = ''):
         filename = os.path.basename(path)
         if not destination:
@@ -283,6 +293,20 @@ if __name__ == '__main__':
             default=False
     )
 
+    download_parser = subparsers.add_parser(
+            'download',
+            help = 'Path to one or more files to be downloaded. Destination name will be the same as the file name.')
+
+    download_parser.add_argument(
+            '--filename', '-f',
+            help = 'File to download. You can specify this option multiple times.',
+            action='append')
+
+    download_parser.add_argument(
+            '--destination', '-d',
+            help = 'Name to be used when saving in NodeMCU. You should specify one per file.',
+            action='append')
+
     file_parser = subparsers.add_parser(
         'file',
         help = 'File functions')
@@ -316,6 +340,17 @@ if __name__ == '__main__':
             raise Exception('You must specify a destination filename for each file you want to upload.')
         print 'All done!'
 
+    if args.operation == 'download':
+        if not args.destination:
+            for f in args.filename:
+                uploader.read_file(f)
+        elif len(args.destination) == len(args.filename):
+            for f, d in zip(args.filename, args.destination):
+                uploader.read_file(f, d)
+        else:
+            raise Exception('You must specify a destination filename for each file you want to download.')
+        print 'All done!'
+
     elif args.operation == 'file':
         if args.cmd == 'list':
             uploader.file_list()