11import os
22import logging
33import uuid
4+ import gzip
45import tarfile
5- from tempfile import TemporaryDirectory
6+ import shutil
67from zipfile import ZipFile
8+ from tempfile import TemporaryDirectory
79from jupyter_geppetto .webapi import get , post
810from notebook .base .handlers import IPythonHandler
911
10- ALLOWED_EXTENSIONS = ["py" , "zip" , "pdf" , "txt" , "xls" , "png" , "jpeg" ]
12+ ALLOWED_EXTENSIONS = ["py" , "zip" , "gz" , ".tar.gz" , " pdf" , "txt" , "xls" , "png" , "jpeg" ]
1113
1214UPLOAD_FOLDER_NAME = "uploads"
1315UPLOAD_FOLDER_PATH = os .path .join (os .getcwd (), UPLOAD_FOLDER_NAME )
@@ -75,6 +77,14 @@ def uploads(handler: IPythonHandler):
7577 if filename .endswith ('.zip' ):
7678 with ZipFile (file_path ) as zipObj :
7779 zipObj .extractall (UPLOAD_FOLDER_PATH )
80+
81+ elif filename .endswith ('.tar.gz' ):
82+ with tarfile .open (file_path , mode = 'r:gz' ) as tar :
83+ tar .extractall (UPLOAD_FOLDER_PATH )
84+
85+ elif filename .endswith ('.gz' ):
86+ with gzip .open (file_path , "rb" ) as gz , open (file_path .replace ('.gz' , '' ), 'wb' ) as ff :
87+ shutil .copyfileobj (gz , ff )
7888
7989 handler .set_status (200 , f"Number of files saved: { files_saved } . Number of files sent: { len (files ['file' ])} " )
8090
@@ -105,4 +115,20 @@ def downloads(handler: IPythonHandler):
105115
106116 send_files (handler , tar_gz_file_path , tar_gz_file_name )
107117
108- handler .finish ()
118+ handler .finish ()
119+
120+ @get ('/export' )
121+ def exportModel (self , args ):
122+ try :
123+ with redirect_stdout (sys .__stdout__ ):
124+ if not args ['netCells' ]:
125+ sim .initialize (netParams = self .netParams , simConfig = self .simConfig )
126+ sim .cfg .filename = args ['fileName' ]
127+ include = [el for el in specs .SimConfig ().saveDataInclude if el in args .keys () and args [el ]]
128+ if args ['netCells' ]: include += ['netPops' ]
129+ sim .cfg .saveJson = True
130+ sim .saveData (include )
131+ sim .cfg .saveJson = False
132+ return utils .getJSONReply ()
133+ except :
134+ return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
0 commit comments