Beispiel #1
0
 def test_08_04_addAll(self):
     c1 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c1.add("Foo")
     c1.add("Bar")
     c2 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c2.add("Baz")
     c2.addAll(c1.o)
     self.assertIn("Foo", c2)
Beispiel #2
0
 def test_08_09_removeAll(self):
     c1 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c1.add("Foo")
     c1.add("Bar")
     c2 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c2.add("Foo")
     c1.removeAll(c2)
     self.assertNotIn("Foo", c1)
Beispiel #3
0
 def test_08_07_contains_all(self):
     c1 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c1.add("Foo")
     c1.add("Bar")
     c2 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c2.add("Baz")
     self.assertFalse(c2.containsAll(c1.o))
     c2 += c1
     self.assertTrue(c2.containsAll(c1.o))
Beispiel #4
0
 def getIndex(self):
     index = J.call(self.o, "getIndex", "()Limagej/module/ModuleIndex;")
     index = J.get_collection_wrapper(index, wrap_module_info)
     index.getC = lambda c: J.get_collection_wrapper(
         J.call(index.o, "get", "(Ljava/lang/Class;)Ljava/util/List;",c),
         wrap_module_info)
     index.getS = lambda class_name: \
         index.getC(J.class_for_name(class_name))
     return index
Beispiel #5
0
 def test_08_06__iadd__(self):
     c1 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c1.add("Foo")
     c1.add("Bar")
     c2 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c2.add("Baz")
     c2 += c1
     for k in ("Foo", "Bar", "Baz"):
         self.assertIn(k, c2)
     c2 += ["Hello", "World"]
     self.assertIn("Hello", c2)
     self.assertIn("World", c2)
Beispiel #6
0
 def test_08_05__add__(self):
     c1 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c1.add("Foo")
     c1.add("Bar")
     c2 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c2.add("Baz")
     c3 = c1 + c2
     for k in ("Foo", "Bar", "Baz"):
         self.assertIn(k, c3)
     
     c4 = c3 + ["Hello", "World"]
     self.assertIn("Hello", c4)
     self.assertIn("World", c4)
Beispiel #7
0
 def test_08_03_contains(self):
     c = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c.add("Foo")
     self.assertTrue(c.contains("Foo"))
     self.assertFalse(c.contains("Bar"))
     self.assertIn("Foo", c)
     self.assertNotIn("Bar", c)
Beispiel #8
0
 def test_08_11_toArray(self):
     c1 = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     c1.add("Foo")
     c1.add("Bar")
     result = [javabridge.to_string(x) for x in c1.toArray()]
     self.assertIn("Foo", result)
     self.assertIn("Bar", result)
 def test_08_01_wrap_collection(self):
     c = javabridge.make_instance("java/util/HashSet", "()V")
     w = javabridge.get_collection_wrapper(c)
     self.assertFalse(hasattr(w, "addI"))
     self.assertEqual(w.size(), 0)
     self.assertEqual(len(w), 0)
     self.assertTrue(w.isEmpty())
 def test_08_01_wrap_collection(self):
     c = javabridge.make_instance("java/util/HashSet", "()V")
     w = javabridge.get_collection_wrapper(c)
     self.assertFalse(hasattr(w, "addI"))
     self.assertEqual(w.size(), 0)
     self.assertEqual(len(w), 0)
     self.assertTrue(w.isEmpty())
Beispiel #11
0
def is_image_extension(suffix):
    """Return True if the extension is one of those recongized by bioformats"""
    extensions = javabridge.get_collection_wrapper(
        javabridge.static_call(
            "org/cellprofiler/imageset/filter/IsImagePredicate",
            "getImageSuffixes", "()Ljava/util/Set;"))
    return extensions.contains(suffix.lower())
    def __getitem__(self, item):
        """
        Returns the specified item, using 0-based indices.

        :param item: the 0-based index
        :type item: int
        :return: the AssociationRule
        :rtype: AssociationRule
        """
        rules = javabridge.get_collection_wrapper(
            javabridge.call(self.jobject, "getRules", "()Ljava/util/List;"))
        return AssociationRule(rules[item])
 def dependencies(self):
     """
     Returns the dependencies of the package.
     :return: the list of Dependency objects
     :rtype: list of Dependency
     """
     result = []
     dependencies = javabridge.get_collection_wrapper(
         javabridge.call(self.jobject, "getDependencies", "()Ljava/util/List;"))
     for dependency in dependencies:
         result.append(Dependency(dependency))
     return result
Beispiel #14
0
    def __getitem__(self, item):
        """
        Returns the specified item, using 0-based indices.

        :param item: the 0-based index
        :type item: int
        :return: the AssociationRule
        :rtype: AssociationRule
        """
        rules = javabridge.get_collection_wrapper(
            javabridge.call(self.jobject, "getRules", "()Ljava/util/List;"))
        return AssociationRule(rules[item])
    def premise(self):
        """
        Get the the premise.

        :return: the premise, list of Item objects
        :rtype: list
        """
        items = javabridge.get_collection_wrapper(
            javabridge.call(self.jobject, "getPremise", "()Ljava/util/Collection;"))
        result = []
        for item in items:
            result.append(Item(item))
        return result
 def get_dependencies(self):
     """
     Returns the dependencies of the package.
     :return: the list of Dependency objects
     :rtype: list of Dependency
     """
     result = []
     dependencies = javabridge.get_collection_wrapper(
         javabridge.call(self.jobject, "getDependencies",
                         "()Ljava/util/List;"))
     for dependency in dependencies:
         result.append(Dependency(dependency))
     return result
Beispiel #17
0
    def premise(self):
        """
        Get the the premise.

        :return: the premise, list of Item objects
        :rtype: list
        """
        items = javabridge.get_collection_wrapper(
            javabridge.call(self.jobject, "getPremise", "()Ljava/util/Collection;"))
        result = []
        for item in items:
            result.append(Item(item))
        return result
Beispiel #18
0
def select_overlay(display, overlay, select=True):
    '''Select or deselect an overlay
    
    display - the overlay's display
    
    overlay - the overlay to select
    '''
    for view in J.get_collection_wrapper(display, fn_wrapper = wrap_data_view):
        if J.call(overlay, "equals", "(Ljava/lang/Object;)Z", view.getData()):
            view.setSelected(select)
            break
    else:
        logger.info("Failed to select overlay")
def available_packages():
    """
    Returns a list of all packages that aren't installed yet.
    :return: the list of packages
    :rtype: list
    """
    establish_cache()
    result = []
    pkgs = javabridge.get_collection_wrapper(
        javabridge.static_call("weka/core/WekaPackageManager",
                               "getAvailablePackages", "()Ljava/util/List;"))
    for pkge in pkgs:
        result.append(Package(pkge))
    return result
def installed_packages():
    """
    Returns a list of the installed packages.
    :return: the list of packages
    :rtype: list
    """
    establish_cache()
    result = []
    pkgs = javabridge.get_collection_wrapper(
        javabridge.static_call(
            "weka/core/WekaPackageManager", "getInstalledPackages", "()Ljava/util/List;"))
    for pkge in pkgs:
        result.append(Package(pkge))
    return result
def get_installed_packages():
    """
    Returns a list of the installed packages.
    :return: the list of packages
    :rtype: list
    """
    establish_cache()
    result = []
    pkgs = javabridge.get_collection_wrapper(
        javabridge.static_call("weka/core/WekaPackageManager",
                               "getInstalledPackages", "()Ljava/util/List;"))
    for pkge in pkgs:
        result.append(Package(pkge))
    return result
def engine(q_request, q_response):
    logger.info("Starting script engine thread")
    J.attach()
    while True:
        try:
            msg = J.run_script(
                """importPackage(Packages.org.scijava.plugins.scripting.cpython);
               q_request.take();""", dict(q_request=q_request))
            if logger.level <= logging.INFO:
                logger.info("Received engine request: %s", J.to_string(msg))
            payload = J.get_collection_wrapper(
                J.run_script("msg.payload", dict(msg=msg)))
            if J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                msg.command==CPythonScriptEngine.EngineCommands.EXECUTE;
                """, dict(msg=msg)):
                response = do_execute(payload)
            elif J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                msg.command==CPythonScriptEngine.EngineCommands.EVALUATE;
                """, dict(msg=msg)):
                response = do_evaluate(payload)
            elif J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                msg.command==CPythonScriptEngine.EngineCommands.CLOSE_ENGINE;
                """, dict(msg=msg)):
                logger.info("Exiting script engine thread after close request")
                break
            else:
                logger.warn(
                    "Received unknown command: %s" %
                    J.run_script("msg.command.toString()", dict(msg=msg)))
                response = J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                var exception = new java.lang.RuntimeException(
                    java.lang.String.format('Unknown command: %s', msg.command.toString()));
                var payload = new java.util.ArrayList();
                payload.add(exception);
                new CPythonScriptEngine.Message(
                    CPythonScriptEngine.EngineCommands.EXCEPTION, payload);
                """, dict(msg=msg))
            J.run_script("q_response.put(response);",
                         dict(q_response=q_response, response=response))
        except:
            # To do: how to handle failure, probably from .take()
            # Guessing that someone has managed to interrupt our thread
            logger.warn("Exiting script engine thread", exc_info=True)
    J.detach()
Beispiel #23
0
def select_overlay(display, overlay, select=True):
    '''Select or deselect an overlay
    
    display - the overlay's display
    
    overlay - the overlay to select
    '''
    for view in J.get_collection_wrapper(display, fn_wrapper = wrap_data_view):
        if J.call(overlay, "equals", "(Ljava/lang/Object;)Z", view.getData()):
            J.execute_runnable_in_main_thread(J.run_script(
                """new java.lang.Runnable() {
                    run: function() { view.setSelected(select);}
                   }""", dict(view = view.o, select=select)), synchronous=True)
            break
    else:
        logger.info("Failed to select overlay")
Beispiel #24
0
def select_overlay(display, overlay, select=True):
    '''Select or deselect an overlay
    
    display - the overlay's display
    
    overlay - the overlay to select
    '''
    for view in J.get_collection_wrapper(display, fn_wrapper = wrap_data_view):
        if J.call(overlay, "equals", "(Ljava/lang/Object;)Z", view.getData()):
            J.execute_runnable_in_main_thread(J.run_script(
                """new java.lang.Runnable() {
                    run: function() { view.setSelected(select);}
                   }""", dict(view = view.o, select=select)), synchronous=True)
            break
    else:
        logger.info("Failed to select overlay")
def jstring_list_to_string_list(l, return_empty_if_none=True):
    """
    Converts a Java java.util.List containing strings into a Python list.

    :param l: the list to convert
    :type l: JB_Object
    :param return_empty_if_none: whether to return an empty list or None when list object is None
    :type return_empty_if_none: bool
    :return: the list with UTF strings
    :rtype: list
    """
    result = []
    if l is None:
        if return_empty_if_none:
            return []
        else:
            return None
    objs = javabridge.get_collection_wrapper(l)
    for obj in objs:
        result.append(javabridge.get_env().get_string_utf(obj))
    return result
def engine_requester():
    J.attach()
    while True:
        try:
            msg = J.run_script(
                """importPackage(Packages.org.scijava.plugins.scripting.cpython);
               CPythonScriptEngine.engineRequestQueue.take();""")
            if logger.level <= logging.INFO:
                logger.info("Received engine request: %s", J.to_string(msg))
            payload = J.get_collection_wrapper(
                J.run_script("msg.payload", dict(msg=msg)))
            if J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                msg.command==CPythonScriptEngine.EngineCommands.NEW_ENGINE;
                """, dict(msg=msg)):
                do_new_engine(payload)
            elif J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                msg.command==CPythonScriptEngine.EngineCommands.CLOSE_SERVICE;
                """, dict(msg=msg)):
                logger.info("Exiting script service thread in response to "
                            "termination request")
                break
            else:
                J.run_script(
                    """importPackage(Packages.org.scijava.plugins.scripting.cpython);
                var exception = new java.lang.RuntimeException(
                    java.lang.String.format('Unknown command: %s', msg.command.toString()));
                var payload = new java.util.ArrayList();
                payload.add(exception);
                var response = new CPythonScriptEngine.Message(
                    CPythonScriptEngine.EngineCommands.EXCEPTION, payload);
                CPythonScriptEngine.engineResponseQueue.put(response);
                """)
        except:
            # To do: how to handle failure, probably from .take()
            # Guessing that someone has managed to interrupt our thread
            logger.warn("Exiting script service thread", exc_info=True)
    J.detach()
 def test_08_02_add(self):
     c = javabridge.get_collection_wrapper(
         javabridge.make_instance("java/util/HashSet", "()V"))
     self.assertTrue(c.add("Foo"))
     self.assertEqual(len(c), 1)
     self.assertFalse(c.isEmpty())
Beispiel #28
0
def is_image_extension(suffix):
    '''Return True if the extension is one of those recongized by bioformats'''
    extensions = J.get_collection_wrapper(
        J.static_call("org/cellprofiler/imageset/filter/IsImagePredicate",
                      "getImageSuffixes", "()Ljava/util/Set;"))
    return extensions.contains(suffix.lower())
 def test_08_02_add(self):
     c = javabridge.get_collection_wrapper(javabridge.make_instance("java/util/HashSet", "()V"))
     self.assertTrue(c.add("Foo"))
     self.assertEqual(len(c), 1)
     self.assertFalse(c.isEmpty())