Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-11-2017, 01:59 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 938
Post Example: Python - Importing custom modules referencing the 'crt' object

These examples provide techniques for importing your custom-written python modules that need to reference the crt scripting object.

The main script (which imports the module code) performs this sequence of actions:
  1. sys.dont_write_bytecode = True
    This statement is used to PREVENT python from automatically creating .PYC files. Otherwise, coding, testing, and revising your module's functionality would prove cumbersome and confusing since python imports the .PYC file (which doesn't reflect your current code) if it exists.

  2. import crt_module
    This is the line of code that tells the python interpreter to import your module code, if it's not already in the sys.modules table.

  3. reload(crt_module)
    The python environment is initialized only once in SecureCRT at startup, and remains as-is until the SecureCRT process terminates. This means that once you run a script which imports a module, that module is in the sys.modules table for the lifetime of the SecureCRT process. If you are in the process of developing and testing a module, code changes you make will not be reflected unless you either:
    1. Restart SecureCRT
      or
    2. Use the reload(crt_module) statement to reload the module code from your module's .py file.

  4. "Injects" the crt object into the module (so that statements within the module that reference the crt object won't result in an error). In this pair of example scripts,
    • the crt_Module.py module code defines a method as Inject_crt_Object(obj_crt_API), which allows the crt application programming interface known to the main script to be used within the module code itself.
    • the main script's .py code, following the import(crt_module) and reload(crt_module) statements, calls the module's Inject_crt_Object method, passing in its copy of the crt object, as in: Inject_crt_Object(crt)

Files attached here include:
Attached Files
File Type: txt ImportModuleWith_crt_Reference.py.txt (4.5 KB, 1204 views)
File Type: txt crt_Module.py.txt (953 Bytes, 1076 views)
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
 

Tags
example script , scripting , securecrt


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:01 PM.