#NEWLINE# def upload_file( _file , directory ) : #NEWLINE# #INDENT# ' Upload yang model into session storage ' #NEWLINE# f = None #NEWLINE# filename = None #NEWLINE# try : #NEWLINE# #INDENT# if ( not os . path . exists ( directory ) ) : #NEWLINE# #INDENT# logging . debug ( 'Creating session storage ..' ) #NEWLINE# os . makedirs ( directory ) #NEWLINE# #UNINDENT# if ( not os . path . exists ( directory ) ) : #NEWLINE# #INDENT# logging . error ( 'Failed to create session storage ..' ) #NEWLINE# return None #NEWLINE# #UNINDENT# logging . debug ( 'Copying file content ..' ) #NEWLINE# f = tempfile . NamedTemporaryFile ( 'w+' , suffix = '.py' , dir = directory , delete = False ) #NEWLINE# fname = f . name #NEWLINE# for chunk in _file . chunks ( ) : #NEWLINE# #INDENT# f . write ( chunk ) #NEWLINE# #UNINDENT# f . close ( ) #NEWLINE# parser = Parser ( fname ) #NEWLINE# target_file = os . path . join ( directory , parser . get_filename ( ) ) #NEWLINE# os . rename ( fname , target_file ) #NEWLINE# filename = parser . get_filename ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# logging . exception ( 'Failed to upload file: ' ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# logging . debug ( 'Cleaning up ..' ) #NEWLINE# if ( ( f is not None ) and os . path . exists ( f . name ) ) : #NEWLINE# #INDENT# logging . debug ( ( 'Deleting ' + f . name ) ) #NEWLINE# os . remove ( f . name ) #NEWLINE# #UNINDENT# #UNINDENT# return filename