Author Topic: Automation using Python?  (Read 11652 times)

mrelbe

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Automation using Python?
« on: February 08, 2011, 06:33:06 pm »
I would like to generate highly customizable reports; scripting will be needed. I do not want to access the SQL directly, nor do I want to use the inbuilt VB script engine in EA, since reports shall be generated as a background process (triggered by SVN commits) and published on wiki pages (Trac).

I cannot find any information about accessing EA programmatically via Python.

Does anyone know if it is possible to use the automation interface using Python?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Automation using Python?
« Reply #1 on: February 08, 2011, 06:47:03 pm »
If you search the Automation forum for "Python" (using the top left button, NOT the top right search box) that should already give you some pointer.
Moreover, there's an article on the community site about using LaTeX to create documentations. The examples use Perl, but it mentiones Python as well.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Automation using Python?
« Reply #2 on: February 09, 2011, 04:02:11 am »
The Perl adapter was created with ActiveState's Perl Development Kit (which is not as cheap as EA). However, if you don't want to buy that license you still can go with WSH to adapt Python (not sure whether I mentioned that on my community post).

I'd rather like to program with Python but already was too deep in Perl. Now I'll probably skip to Ruby...

q.

fginfrance

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: Automation using Python?
« Reply #3 on: March 14, 2011, 08:00:59 am »
It is actually quite straight forward.

You have to get and install the pywin package for the python version you use (me its py2.6)
With that , the EA API is wrapped and becomes available "by magic".

The following is a starter snippet for a python app that connects to an open EA model. To use it, you have to manually open a model and than execute your script. All EA API is available!

Writing EA plug-ins in python also works, but is a different story...

Have fun...

Code: [Select]
import win32com.client


      try:
        eaApp = win32com.client.Dispatch("EA.App")
      except:
        sys.stderr.write( "Unable to connect to EA\n" )
        exit()

      # from here onwards, the full EA API is available as python objects
      # so we can

      mEaRep = eaApp.Repository

      if mEaRep.ConnectionString == '':
        sys.stderr.write( "EA has no Model loaded\n" )
        exit()

      else:
        print "connecting...", self.mEaRep.ConnectionString

fginfrance

arti

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Automation using Python?
« Reply #4 on: November 30, 2011, 04:05:29 am »
Quote
Writing EA plug-ins in python also works, but is a different story...

But could you elaborate it for us a little bit? I'd like to write even simple one:)
First of all, according to the EA documentation you have to register the dll with your Add-In using regsrv32 (if compiled as native library) or regasm.exe (if compiled as a .NET DLL). However, when it comes to python we don't have any dll as a result. So do I have to some how make one, for instance using py2exe ? Or maybe there is another way?

Probably (I don't know it for sure) I may have to prepare some Python COM object (example below). Do I have to load Interop.EA.dll library to implement callback functions?  Is it a good way of thinking?

Code: [Select]
# We expose a single method in a Python COM object.
class PythonUtilities:
    _public_methods_ = [ 'SplitString' ]
    _reg_progid_ = "PythonDemos.Utilities"
    # NEVER copy the following ID

    # Use"print pythoncom.CreateGuid()" to make a new one.
    _reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"

    def SplitString(self, val, item=None):
        import string
        if item != None: item = str(item)
        return string.split(str(val), item)

# Add code so that when this script is run by
# Python.exe,.it self-registers.
if__name__=='__main__':
    print "Registering COM server…"
    import win32com.server.register
    win32com.server.register.UseCommandLine(PythonUtilities)

Artur
« Last Edit: November 30, 2011, 04:20:54 am by arti »

arti

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Automation using Python?
« Reply #5 on: December 20, 2011, 08:57:25 am »
I've finally got it.:) I know how to write EA plug-ins in python. As a matter of fact this is pretty close to that what was written in last post. As a prerequisite you should have Python and pywin32 (Python Extensions for Windows) installed for the python version you use. Personally  I used version 3.1
Below there is a sample snippet  ... but to use it you have to register the COM Server It can be done by executing command python ShowMeDoDemo.py. The vital issue here is that you can choose to respond to any Add-In event. Using the attribute named _public_methods_ you can set a list of strings that contains the names of the public methods for the object. In our case we have 'EA_FileOpen', 'EA_GetMenuItems' and 'EA_MenuClick'. The attribute _reg_progid_ is the progID for the object. This is the name of the COM object clients use to create the object and in order to EA see it you have to add it as a new key value under the location: HKEY_CURRENT_USER\Software\Sparx Systems\EAAddIns

example:
Code: [Select]
[HKEY_CURRENT_USER\Software\Sparx Systems\EAAddIns\ShowMeDo]
@="ShowMeDo.Demo"


and the snippet
Code: [Select]
import win32com.client

class ShowMeDoDemo:
    _public_methods_ = [ 'EA_FileOpen', 'EA_GetMenuItems', 'EA_MenuClick' ]
    _reg_progid_ = "ShowMeDo.Demo"
    # use
    # import pythoncom
    # print pythoncom.CreateGuid()
    # to make a GUID as they are machine-specific
    _reg_clsid_ = "{45B99DFF-462D-4995-83B3-8532D7739BE0}"
    
        
    def EA_FileOpen(self, repo_obj):
        "Callback EA_FileOpen"
        
        repo = win32com.client.Dispatch(repo_obj)
          
        print ("ConnectionString = ", repo.ConnectionString)
        
    def EA_GetMenuItems(self, repo_obj, menuLocation, menuName):
        "Callback EA_GetMenuItems"
        
        
        if menuName == "":
            return "-DoDemo"
        else:
            return "Show Dialog"
        
    def EA_MenuClick(self, repo_obj, menuLocation, menuName, itemName):
        "Callback EA_MenuClick"
        
        repo = win32com.client.Dispatch(repo_obj)
        
        ...
            
  
        

if __name__ == "__main__":
    # use 'python ShowMeDoDemo.py' to register the COM server
    # use 'python ShowMeDoDemo.py --unregister' to unregister it
    print ("Registering COM server...")
    import win32com.server.register
    win32com.server.register.UseCommandLine(ShowMeDoDemo)