<commit_before>#!/usr/bin/env python
"""Run a script as a droplet (an application onto which you drop file) with a log window

To build a Mac droplet using py2app, assuming the code that does the work is in mainScript.py:

- Write a trivial script that constructs/runs DropletRunner; this is the script
  that you tell setup.py about; it might be called runMainScript.py:
    import droplet
    import mainScript # so py2app finds it and its dependencies
    droplet.DropletRunner("mainScript.py")

- In the application PList specify the sorts of files that can be dropped, e.g.:

    plist = dict(
    ...
        CFBundleDocumentTypes       = [
            dict(
                CFBundleTypeName = "TEXT",
                LSItemContentTypes = [
                    "public.plain-text",
                    "public.text",
                    "public.data",
                ],
                CFBundleTypeRole = "Viewer",
            ),
        ],
    )

    Notes:
    - There are keywords that allow you to specify allowed file suffixes
      but they are deprecated in Mac OS X 10.5 so I don't show them:
    - CFBundleTypeRole is required; another common value is "Editor".

- Copy the main droplet script into the the application bundle Contents/Resources
  so DropletRunner can find it, e.g.:
    import mainScriptModule
    shutil.copy(
        "mainScript.py",
        os.path.join("dist", appName, "Contents", "Resources"),
    )

History:
2010-06-16 ROwen
2010-06-17 ROwen    Added title and initialText arguments to constructor.
"""
import sys
import os.path
import subprocess
import Tkinter
import RO.OS
import RO.Constants
import LogWdg

__all__ = ["DropletRunner"]

class DropletRunner():
    """Run a script as a droplet (an application onto which you drop file) with a log window.
    
    Data the script writes to sys.stdout and sys.stderr is written to a log window;
    stderr output is shown in red.    

    On Mac OS X additional files may be dropped on the application icon once the first batch is processed.
    I don't know how to support this on other platforms.
    """
    def __init__(self, scriptPath, title=None, initialText=None, **keyArgs):
        """Construct and run a DropletRunner
        
        Inputs:
        - scriptPath: path to script to run when files are dropped on the application
        - title: title for log window; if None then generated from scriptPath
        - initialText: initial text to display in log window
        **keyArgs: all other keyword arguments are sent to the RO.Wdg.LogWdg constructor
        """
        self.isRunning = False
        self.scriptPath = os.path.abspath(scriptPath)
        if not os.path.isfile(scriptPath):
            raise RuntimeError("Cannot find script %r" % (self.scriptPath,))

        self.tkRoot = Tkinter.Tk()
        
        if title == None:
            title = os.path.splitext(os.path.basename(scriptPath))[0]
        self.root.title(title)

        if RO.OS.PlatformName == "mac":
            self.tkRoot.createcommand('::tk::mac::OpenDocument', self._macOpenDocument)
            # the second argument is a process ID (approximately) if run as an Applet;
            # the conditional handles operation from the command line
            if len(sys.argv) > 1 and sys.argv[1].startswith("-"):
                filePathList = sys.argv[2:]
            else:
                filePathList = sys.argv[1:]
        else:
            filePathList = sys.argv[1:]

        self.logWdg = LogWdg.LogWdg(self.tkRoot, **keyArgs)
        self.logWdg.grid(row=0, column=0, sticky="nsew")
        self.tkRoot.grid_rowconfigure(0, weight=1)
        self.tkRoot.grid_columnconfigure(0, weight=1)
        
        if initialText:
            self.logWdg.addOutput(initialText)

        if filePathList:
            self.runFiles(filePathList)
<commit_msg>ROPackage/python/RO/Wdg/DropletRunner.py - fixed a minor bug (root -> tkRoot)<commit_after>#!/usr/bin/env python
"""Run a script as a droplet (an application onto which you drop file) with a log window

To build a Mac droplet using py2app, assuming the code that does the work is in mainScript.py:

- Write a trivial script that constructs/runs DropletRunner; this is the script
  that you tell setup.py about; it might be called runMainScript.py:
    import droplet
    import mainScript # so py2app finds it and its dependencies
    droplet.DropletRunner("mainScript.py")

- In the application PList specify the sorts of files that can be dropped, e.g.:

    plist = dict(
    ...
        CFBundleDocumentTypes       = [
            dict(
                CFBundleTypeName = "TEXT",
                LSItemContentTypes = [
                    "public.plain-text",
                    "public.text",
                    "public.data",
                ],
                CFBundleTypeRole = "Viewer",
            ),
        ],
    )

    Notes:
    - There are keywords that allow you to specify allowed file suffixes
      but they are deprecated in Mac OS X 10.5 so I don't show them:
    - CFBundleTypeRole is required; another common value is "Editor".

- Copy the main droplet script into the the application bundle Contents/Resources
  so DropletRunner can find it, e.g.:
    import mainScriptModule
    shutil.copy(
        "mainScript.py",
        os.path.join("dist", appName, "Contents", "Resources"),
    )

History:
2010-06-16 ROwen
2010-06-17 ROwen    Added title and initialText arguments to constructor.
"""
import sys
import os.path
import subprocess
import Tkinter
import RO.OS
import RO.Constants
import LogWdg

__all__ = ["DropletRunner"]

class DropletRunner():
    """Run a script as a droplet (an application onto which you drop file) with a log window.
    
    Data the script writes to sys.stdout and sys.stderr is written to a log window;
    stderr output is shown in red.    

    On Mac OS X additional files may be dropped on the application icon once the first batch is processed.
    I don't know how to support this on other platforms.
    """
    def __init__(self, scriptPath, title=None, initialText=None, **keyArgs):
        """Construct and run a DropletRunner
        
        Inputs:
        - scriptPath: path to script to run when files are dropped on the application
        - title: title for log window; if None then generated from scriptPath
        - initialText: initial text to display in log window
        **keyArgs: all other keyword arguments are sent to the RO.Wdg.LogWdg constructor
        """
        self.isRunning = False
        self.scriptPath = os.path.abspath(scriptPath)
        if not os.path.isfile(scriptPath):
            raise RuntimeError("Cannot find script %r" % (self.scriptPath,))

        self.tkRoot = Tkinter.Tk()
        
        if title == None:
            title = os.path.splitext(os.path.basename(scriptPath))[0]
        self.tkRoot.title(title)

        if RO.OS.PlatformName == "mac":
            self.tkRoot.createcommand('::tk::mac::OpenDocument', self._macOpenDocument)
            # the second argument is a process ID (approximately) if run as an Applet;
            # the conditional handles operation from the command line
            if len(sys.argv) > 1 and sys.argv[1].startswith("-"):
                filePathList = sys.argv[2:]
            else:
                filePathList = sys.argv[1:]
        else:
            filePathList = sys.argv[1:]

        self.logWdg = LogWdg.LogWdg(self.tkRoot, **keyArgs)
        self.logWdg.grid(row=0, column=0, sticky="nsew")
        self.tkRoot.grid_rowconfigure(0, weight=1)
        self.tkRoot.grid_columnconfigure(0, weight=1)
        
        if initialText:
            self.logWdg.addOutput(initialText)

        if filePathList:
            self.runFiles(filePathList)

{'fraction_non_alphanumeric': 0.08389261744966443, 'fraction_numerical': 0.008518327310273618, 'mean_word_length': 2.3286082474226806, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1546395', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2122, 'n_words': 768}