Menu ▾ ▴

launch python file programmatical

Bewa
2009-04-24
2013-03-15
  • Bewa

    Bewa - 2009-04-24

    Hello everyone,

    I am currently building an eclipse plugin where I wish to keep multiple python files in a hierarchical structure, i.e. grouped in folders.
    Now my concern is how to launch them. I could launch each file manually and give the file the arguments by hand, but that will be too much work.
    I would like to have it in a way that I write a function to read the arguments from a text-file and then launch each .py-file in a specified folder programmatical. Like this:

    launch(IFile pyFile, String args)

    I have found an article on how to launch java-programs (http://www.eclipse.org/articles/Article-Java-launch/launching-java.html), where I found this snippet:

    ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();  
    ILaunchConfigurationType type = manager.getLaunchConfigurationType(ID_JAVA_APPLICATION);

    Now my first question is, which String Constant would I have to use to get the LaunchConfigurationType applicable for python? If you know a better way like reading a *.launch-file from the project folder after manipulating the arguments stored therein, let me know.

    Further snippets deal with setting details of the LaunchConfiguration like classpath, I do not think this will be a big problem.

    The last thing is actually runnig the python file, which sould go well with

    ILaunchConfiguration configuration = workingCopy.doSave();
    DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);

    given that the file itself is stored in the configuration.
    And here is my second question: Does this approach make sense or do you know of any detail I missed?

    Any help is greatly appreciated!

    Bewa

     
    • Fabio Zadrozny

      Fabio Zadrozny - 2009-04-24

      The launch configuration type you want is: org.python.pydev.debug.core.Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE (there are constants in that same class for other launches)

      Also, you can take a look at org.python.pydev.debug.ui.launching.AbstractLaunchShortcut for code on how to make a launch and find an existing launch.

      Cheers,

      Fabio

       
    • Bewa

      Bewa - 2009-04-27

      Thank you, Fabio, for your help!

      I have managed to launch it and  was able to get the output from the script to the console.

      For all who want to know, here is my code:

      ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();

      ILaunchConfigurationType type = manager.getLaunchConfigurationType(org.python.pydev.debug.core.Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE);

      ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null,"desc");

      workingCopy.setAttribute(org.python.pydev.debug.core.Constants.ATTR_PROGRAM_ARGUMENTS,
      "argument");
      workingCopy.setAttribute(org.python.pydev.debug.core.Constants.ATTR_PROJECT, "project");
      workingCopy.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION",
      "${workspace_loc:project/folder/something.py}");

      ILaunchConfiguration configuration = workingCopy.doSave();
      DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);

      Not a best-practise regarding the code style, but...