Jump to content
GioPet

Encrypting Python Scripts with external libraries

Recommended Posts

Hello, 

 

I have been working on a few Python Scripts that use third party libraries - for example BeautifulSoup4.

I intend to Encryption this script into a vsm plugin so that the external libraries are bound to it and can be used on other installations of Vectorworks.

 

I realised that the Encryption/Obfuscation method through Vectorworks is not able to bind the external packages into the vsm file:

I've included the path to these libraries in the Script Options and I am using the steps indicated by Vlado in this post for the related xml file.

 

ScriptOptions.thumb.png.3ff7fcd50d33d9815d7a8120164e5b04.png

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- This file defines how the corresponding script plug-in should be packaged--><Plugin>
    <Package>
        <File>code/__init__.py</File>
        <File>bS4/</File>
        <File>bS4/__init__.py</File>
        <File>bS4/.py</File>
        <File>bS4/diagnose.py</File>
		<File>bS4/element.py</File>
		<File>bS4/testing.py</File>
		<File>bS4/builder/__init__.py</File>
		<File>bS4/builder/_html5lib.py</File>
		<File>bS4/builder/_htmlparser.py</File>
		<File>bS4/builder/_lxml.py</File>        
    </Package>
</Plugin>

Can any one advise on how to resolve this?

Thank you in advance.

 

Giovanni

Share this post


Link to post

Is /7_Vectorworks 2018 your VW user folder?  It’s an unusual name, so if not, you need to add a path attribute to the xml. 

 

For this xml, the .vsm file would be in the /7_Vectorworks 2018/plug-ins folder, along with the xml file. 

 

You’re not including the main.py file if you have one — any .py files in /code that you use also need to be in the xml file. The only reason not to include them is if your main code is all directly in the vsm. I.e. if you open the vsm in the plug-in manager’s script editor and call import bs4 from there

Share this post


Link to post

Hi Joshua, 

 

thank you for your response.

Yes my user folder was set to /7_Vectorworks 2017, and I keeping the xml in the Plug-in folder within that.

For clarity - I have know reverted back to the standard path and still keeping the xml next to the vsm.

 

Indeed, I don't have a 'main.py' because all my code is in the vsm.

 

The Encryption works with no errors, but when I run the Encrypted vsm on a different installation of Vectorworks I get the error:
ImportError: No module named 'bs4'

(bs4 module is the BeautifulSoup module) 

My python call in the script is: 

from bs4 import BeautifulSoup

So something is not working in actually binding the package with the vsm file..

any idea?

here is a screenshot of my Plug-in Folder  with the Development/code and Development/bs4  folders.

 

PluginFolder.thumb.png.632af6b8d76ef1bd72283f8027651151.png

 

 

thank you

Giovanni

 

Share this post


Link to post

That's the case:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- This file defines how the corresponding script plug-in should be packaged--><Plugin>
    <Package>
        <File>Development/code/__init__.py</File>
        <File>Development/bS4/</File>
        <File>Development/bS4/__init__.py</File>
        <File>Development/bS4/.py</File>
        <File>Development/bS4/diagnose.py</File>
		<File>Development/bS4/element.py</File>
		<File>Development/bS4/testing.py</File>
		<File>Development/bS4/builder/__init__.py</File>
		<File>Development/bS4/builder/_html5lib.py</File>
		<File>Development/bS4/builder/_htmlparser.py</File>
		<File>Development/bS4/builder/_lxml.py</File>        
    </Package>
</Plugin>

Share this post


Link to post
Posted (edited)

Does this run the external scripts?  What would be the format If i wanted to place a python script next the the .vsm and run that script from within the vectorworks python script?

Edited by The Hamma

Share this post


Link to post

You need to use the import command. 

 

So your .vsm file literally looks like:

 

import external_python_script

And then you make sure that the external python script's code does some thing on initialization. 

 

To encrypt the plugin that's in the same directory, I just encrypt with a "blank" .xml file:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- This file defines how the corresponding script plug-in should be packaged-->
<Plugin>
    <Package>
    </Package>
</Plugin>

  As long as there both in the same directory should work fine. Note if you're .vsm + Script are in a subfolder within plugin folder you may need to modify the import command accordingly.

Share this post


Link to post

OK I have another question.  If I run an external python script from an internal script can I pass the variables from the external script back to the internal script when the external script completes?

Share this post


Link to post

 

19 hours ago, The Hamma said:

OK I have another question.  If I run an external python script from an internal script can I pass the variables from the external script back to the internal script when the external script completes?

 

@The Hamma That's not exactly how includes in python work. Everything in the include is a resource, treating the included file like a class. Included variables become properties of the class and functions become methods of the class. Nothing in the external runs until you call it — so you would likely define a function in the "external" script that returns a value to the "internal" script.

 

https://realpython.com/python-import/ 

Share this post


Link to post

Ok, I can use

from VPRename_A import hVP, numberStr

to retrieve hVP and numberStr from VPRenumb_A

 

Now that I have that solved It does not seem to fix my issue with the script.  

 

If I run script "VP Label (Consecutive)" from the workspace and I place "VPRenumb_A.vsm"  in the workspace and call it via "vs.DoMenuTextByName('VPRenumb_A', 0)" it actually leaves my script to run the second script and this completes without giving me any messages about renumbering the drawing labels.  

 

If I run script "VP Label (Consecutive)" from the workspace and I place "VPRenumb_A.py"  in the plugins folder and call it via "from VPRename_A import hVP, numberStr" it actually imports the "VPRenumb_A.py" into the first script to run and this completes giving me a warning messages about renumbering the drawing labels.  

 

Is there a way to call an external python script without running it within the first script like "vs.DoMenuTextbyName" does? Note I still need to receive or send variables to or from the second script and not all of the labels redraw correctly.

 606774933_Annotation2020-08-05101109.thumb.png.bdaa6d650ea9f4be025a1e86ceadc9cf.png1352412360_Annotation2020-08-05093443.thumb.png.c5177bd8cf75339e4ec592c7a1bcd23e.png

 

I have attached the two versions. 

 

 

VP Label (Consecutive) (DoMenubytextName).zip VP Label (Consecutive) (import).zip

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×
×
  • Create New...