Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
NAS Access
-
I'm very happy that the code was useable.
-
@brumm Without your code, I never could get this smb working. Sincerely, thanks a lot
-
@brumm If you want to add some callback to upload and download...
def download(self, path, filename,buffersize=None,callback=None): try: #print('Download = ' + path + filename) attr = self.conn.getAttributes(self.service_name, path+filename) #print('Size = %.1f kB' % (attr.file_size / 1024.0)) #print('start download') file_obj = BytesIO() fw = open(filename, 'wb') offset = 0 transmit =0 while True: if not buffersize: file_attributes, filesize = self.conn.retrieveFile(self.service_name, path+filename, file_obj) else: file_attributes, filesize = self.conn.retrieveFileFromOffset(self.service_name, path+filename, file_obj,offset=offset,max_length=buffersize) if callback: transmit = transmit + filesize callback(transmit) file_obj.seek(offset) for line in file_obj: fw.write(line) offset = offset + filesize if (not buffersize) or (filesize == 0): break fw.close() #print('download finished') except Exception as e: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) def upload(self, path, filename,buffersize=None,callback=None): try: #print('Upload = ' + path + filename) #print('Size = %.1f kB' % (os.path.getsize(filename) / 1024.0)) #print('start upload') file_obj = open(filename, 'rb') offset = 0 while True: if not buffersize: filesize = self.conn.storeFile(self.service_name, path+filename, file_obj) break else: buffer_obj = file_obj.read(buffersize) if buffer_obj: buffer_fileobj = BytesIO() buffer_fileobj.write(buffer_obj) buffer_fileobj.seek(0) offset_new = self.conn.storeFileFromOffset(self.service_name, path+filename, buffer_fileobj, offset=offset, truncate=False) #return the file position where the next byte will be written. offset = offset_new if callback: callback(offset) else: break file_obj.close() #print('upload finished') except Exception as e: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)```
-
@cvp Please feel free to upload it to your own github site, if you like. It's okay for me.
-
@brumm ok, Thanks, I'll do it
Edit: done at https://github.com/cvpe/Pythonista-scripts
-
@brumm As you have deeply studied SMB, I have a little question .
I use it to access (read and write) an USB key connected to an USB slot of my internet router.
Do you think I can remove safely the USB key as soon I have closed the smb connection? -
@cvp I guess that your internet router has no (or a small) read/write buffer. Before you lose data I would recommend to use a USB key with a LED, to get sure that your router has enough time to write all data.
-
@brumm Thanks for the advice. But do you think that all buffers are flushed when the smb connection is closed?
-
@cvp When you use smb.SMBConnection it is a synchronous connection and you can send only one command after another. So it shouldn't be necessary to flush the buffer, but there could be a small gap until the cache of the router is empty. However I'm not an expert, so I would still recommend to use an USB key with a LED.
-
@brumm ok Thanks
-
@cvp Sorry for my newbie question : Besides your SMB_Client.py you use more Python libs. How do I get these into Pythonista? Merci!
-
@elkrause if you check some posts above in this topic, I explain which libs I had installed, like nmb...
-
@elkrause, or maybe you want to know how to load packages from PyPi?
First install stash (google ”pythonista stash”), then it’s just ”pip install whatever” (as long as it’s pure python).
-
@mikael You're obviously right. I didn't have understood the request like that...
-
-
@elkrause said:
how to download specific GitHib repos
There are several ways. Easiest, if available, is to download a folder as .zip via
File will arrive in Files from where you can copy/import to Pythonista and then unzip the file to your sites-package folder.
-
@elkrause better is to unzip in Files app, then in split view, drag and drop needed folder to Pythonista site-packages
-
Thanks @cvp for explaining the basics! I managed to install the libs. I needed to be logged in GitHub to see the download link.