VS:A more Complicated Installation Script

From Vectorworks Developer
Revision as of 19:37, 5 June 2014 by Root (talk | contribs)
Jump to navigation Jump to search

What's that

Here we want to write a Python script that first download plant definitions and symbols as a zip file from web and then would proceed with unzipping and copying the content into the "Objects-Landscape & Site" folder:

import os
import sys
import vs
import zipfile 
import shutil
import urllib.request



Package_URL = "https://dl.dropboxusercontent.com/u/40741343/SamplePlantLibrary.zip"
InputFILE_Name = Package_URL.split('/')[-1]



# Use negative number to get user path and positive number to get application path 
VW_USER_LIBARY_PATH = vs.GetFolderPath(-13)


VW_USER_PLANTOBJ_PATH = os.path.join(VW_USER_LIBARY_PATH , "Objects-Landscape & Site" ) 
if not os.path.exists(VW_USER_PLANTOBJ_PATH):
    os.makedirs(VW_USER_PLANTOBJ_PATH) 

#if not os.path.exists(os.path.join( VW_USER_WORKSPACE_PATH )):
#    os.makedirs(os.path.join( VW_USER_WORKSPACE_PATH )) 

# download to VW_USER_PLANTOBJ_PATH
try:
    u = urllib.request.urlopen(Package_URL)    
except:
    vs.AlrtDialog("Error in getting file information from given URL!")
    raise
    
input_package_info = u.info() 

file_type = str(input_package_info['Content-type'].split('/')[-1])
if(file_type != "zip"):
    vs.AlrtDialog("Wrong format is provided!")
    raise

urllib.request.urlretrieve(Package_URL, os.path.join(VW_USER_PLANTOBJ_PATH , InputFILE_Name ) )

This above part of the installer would create "Objects-Landscape & Site" folder in User library path and would dowonload and copy provieded zip file into the folder.

Now we just need to unzip the file:

# Unzip to VW_USER_PLANTOBJ_PATH

if not os.path.exists(VW_USER_PLANTOBJ_PATH):
    os.makedirs(VW_USER_PLANTOBJ_PATH) 

try:                                                                        
    fh = open( os.path.join(VW_USER_PLANTOBJ_PATH , InputFILE_Name ) , 'rb' )                                            
    try:                                                                     
        z = zipfile.ZipFile( fh )                                            
        for zipedName in z.namelist():                                    
            folder = zipedName.split( '/' )            
            if ( len(folder) > 0 ):                                        
                fileName = folder[ -1 ]                                                       
                if ( len(fileName) > 0 and fileName[0] != '.' ):            
                    outDir = VW_USER_PLANTOBJ_PATH                                    
                    if ( len(folder) > 1 ):                                
                        if ( len(folder) > 1 ):                            
                            outDir  += '/'                                
                            outDir  += '/'.join( folder[:-1] )                              
                            if os.path.exists( outDir ):                    
                                shutil.rmtree( outDir )                    
                                                                             
        for zipedName in z.namelist():                                    
            folder = zipedName.split( '/' )            
            if ( len(folder) > 0 ):                                        
                fileName = folder[ -1 ]                                    
                if ( len(fileName) > 0 and fileName[0] != '.' ):            
                    outDir = VW_USER_PLANTOBJ_PATH                                    
                    if ( len(folder) > 1 ):                                
                            outDir  += '/'                                
                            outDir  += '/'.join( folder[:-1] )            
                            if not os.path.exists( outDir ):                
                                os.makedirs( outDir )                                                        
                                                                             
                    tmpoutfile = None                                        
                    outFilePath = outDir + '/' + fileName     
                    try:                                                    
                        tmpoutfile = open( outFilePath, 'wb' )                
                        tmpoutfile.write( z.read( zipedName ) )                
                    except Exception as ex:                              
                        pass                                                
                                                                           
                    if (tmpoutfile != None):                                    
                        tmpoutfile.close()                                    
                                                                           
    except Exception as ex:                                         
        pass                                                                
                                                                             
    fh.close()                    
    os.remove(os.path.join(VW_USER_PLANTOBJ_PATH , InputFILE_Name ))                                          
                                                                             
except Exception as ex:                                                                                                            
    pass                                                                    

Now we just need to run the script from the VW from Tools->Plug-ins->Run-script If it has been orginized as expained above(plant definitions in "Plant Definition Library" and symbols in "Plant Symbol Library") we can access the library from VW:

File:PlantLibrary.jpg

See also

User Comments