private void convertSrcToHtml(String bundle) {
   PythonInterpreter py = new PythonInterpreter();
   log(lvl, "Convert Sikuli source code " + bundle + " to HTML");
   py.set("local_convert", true);
   py.set("sikuli_src", bundle);
   py.exec(pyConverter);
 }
Example #2
0
  @Test
  public void raw_customFormatter() {
    PythonInterpreter interpreter = new PyGateway().getInterpreter();

    // Set a variable with the content you want to work with
    interpreter.set(
        "code",
        ""
            + "(defn year-end-evaluation\n"
            + "  []\n"
            + "  (if (> (rand) 0.5)\n"
            + "    \"You get a raise!\"\n"
            + "    \"Better luck next year!\"))");
    RFormatter rFormatter = new RFormatter();
    interpreter.set("out", rFormatter);

    // Simple use Pygments as you would in Python
    interpreter.exec(
        ""
            + "from pygments import highlight\n"
            + "from pygments.lexers.jvm import ClojureLexer\n"
            + "from pygments.formatter import Formatter\n"
            + "\n"
            + "class ForwardFormatter(Formatter):\n"
            + "    def format(self, tokensource, outfile):\n"
            + "        for ttype, value in tokensource:\n"
            + "            out.write(ttype, value)\n"
            + "\n"
            + "highlight(code, ClojureLexer(), ForwardFormatter())");

    assertThat(rFormatter.out.toString())
        .isEqualTo(
            ""
                + "Token.Punctuation:[(']\n"
                + "Token.Keyword.Declaration:[defn ']\n"
                + "Token.Name.Variable:[year-end-evaluation']\n"
                + "Token.Text:[\\n  ']\n"
                + "Token.Punctuation:[[']\n"
                + "Token.Punctuation:[]']\n"
                + "Token.Text:[\\n  ']\n"
                + "Token.Punctuation:[(']\n"
                + "Token.Keyword:[if ']\n"
                + "Token.Punctuation:[(']\n"
                + "Token.Name.Builtin:[> ']\n"
                + "Token.Punctuation:[(']\n"
                + "Token.Name.Function:[rand']\n"
                + "Token.Punctuation:[)']\n"
                + "Token.Text:[ ']\n"
                + "Token.Literal.Number.Float:[0.5']\n"
                + "Token.Punctuation:[)']\n"
                + "Token.Text:[\\n    ']\n"
                + "Token.Literal.String:[\"You get a raise!\"']\n"
                + "Token.Text:[\\n    ']\n"
                + "Token.Literal.String:[\"Better luck next year!\"']\n"
                + "Token.Punctuation:[)']\n"
                + "Token.Punctuation:[)']\n"
                + "Token.Text:[\\n']\n");
  }
 public String markdownToHtml(String chaine) {
   PythonInterpreter console = getMdBox().getPyconsole();
   console.set("text", chaine);
   console.exec(
       "render = Markdown(extensions=(ZdsExtension({'inline': False, 'emoticons': smileys}),),safe_mode = 'escape', enable_attributes = False, tab_length = 4, output_format = 'html5', smart_emphasis = True, lazy_ol = True).convert(text)");
   PyString render = console.get("render", PyString.class);
   return render.toString();
 }
  @Override
  public void clear() {
    HashMap<String, Object> bindings = getBindings();
    for (String s : bindings.keySet()) {
      bindings.put(s, null);
      py.set(s, null);
    }

    // let Jython do its housekeeping
    py.cleanup();
  }
  protected PicoContainer createContainerFromScript(
      PicoContainer parentContainer, Object assemblyScope) {
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader pyClassLoader = Py.getSystemState().getClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(getClassLoader());
      Py.getSystemState().setClassLoader(getClassLoader());

      PythonInterpreter interpreter = new PythonInterpreter();

      interpreter.set("parent", parentContainer);
      interpreter.set("assemblyScope", assemblyScope);
      interpreter.execfile(getScriptInputStream(), "picocontainer.py");
      return (PicoContainer) interpreter.get("pico", PicoContainer.class);
    } catch (IOException e) {
      throw new ScriptedPicoContainerMarkupException(e);
    } finally {
      Thread.currentThread().setContextClassLoader(oldClassLoader);
      Py.getSystemState().setClassLoader(pyClassLoader);
    }
  }
Example #6
0
  @Test
  public void raw_usecase() {
    PythonInterpreter interpreter = new PyGateway().getInterpreter();

    // Set a variable with the content you want to work with
    interpreter.set(
        "code",
        ""
            + "(defn year-end-evaluation\n"
            + "  []\n"
            + "  (if (> (rand) 0.5)\n"
            + "    \"You get a raise!\"\n"
            + "    \"Better luck next year!\"))");

    // Simple use Pygments as you would in Python
    interpreter.exec(
        "from pygments import highlight\n"
            + "from pygments.lexers.jvm import ClojureLexer\n"
            + "from pygments.formatters import RawTokenFormatter\n"
            + "\n"
            + "result = highlight(code, ClojureLexer(), RawTokenFormatter())");

    // Get the result that has been set in a variable
    PyObject result = interpreter.get("result");
    PyString string = (PyString) result;
    assertThat(string.getString())
        .isEqualTo(
            ""
                + "Token.Punctuation\tu'('\n"
                + "Token.Keyword.Declaration\tu'defn '\n"
                + "Token.Name.Variable\tu'year-end-evaluation'\n"
                + "Token.Text\tu'\\n  '\n"
                + "Token.Punctuation\tu'['\n"
                + "Token.Punctuation\tu']'\n"
                + "Token.Text\tu'\\n  '\n"
                + "Token.Punctuation\tu'('\n"
                + "Token.Keyword\tu'if '\n"
                + "Token.Punctuation\tu'('\n"
                + "Token.Name.Builtin\tu'> '\n"
                + "Token.Punctuation\tu'('\n"
                + "Token.Name.Function\tu'rand'\n"
                + "Token.Punctuation\tu')'\n"
                + "Token.Text\tu' '\n"
                + "Token.Literal.Number.Float\tu'0.5'\n"
                + "Token.Punctuation\tu')'\n"
                + "Token.Text\tu'\\n    '\n"
                + "Token.Literal.String\tu'\"You get a raise!\"'\n"
                + "Token.Text\tu'\\n    '\n"
                + "Token.Literal.String\tu'\"Better luck next year!\"'\n"
                + "Token.Punctuation\tu')'\n"
                + "Token.Punctuation\tu')'\n"
                + "Token.Text\tu'\\n'\n");
  }
 public void evalFile(String s) {
   for (String s2 : bindings.keySet()) {
     py.set(s2, bindings.get(s2));
   }
   py.setOut(getWriter());
   py.setErr(getErrorWriter());
   try {
     py.execfile(s);
     PyStringMap locals = (PyStringMap) py.getLocals();
     Object[] values = locals.values().toArray();
     Object[] keys = locals.keys().toArray();
     bindings.clear();
     for (int i = 0; i < keys.length; ++i) {
       bindings.put((String) keys[i], values[i]);
     }
   } catch (PyException pe) {
     getErrorWriter().write(pe.toString());
     getErrorWriter().flush();
   }
 }
 /**
  * Creates a jython object instance for the script cache.
  *
  * @param key the path to the jython script
  * @return an instantiated jython object
  * @throws Exception if the jython object failed to be instantiated
  */
 @Override
 public Object createEntry(Object key) throws Exception {
   // log.debug("createEntry({})", key);
   String path = key.toString();
   int qmarkPos = path.lastIndexOf("?");
   if (qmarkPos != -1) {
     path = path.substring(0, qmarkPos);
   }
   int slashPos = path.indexOf("/");
   String portalId = path.substring(0, slashPos);
   PyObject scriptObject = null;
   InputStream in = velocityService.getResource(path);
   if (in == null) {
     log.debug("Failed to load script: '{}'", path);
   } else {
     // add current and default portal directories to python sys.path
     addSysPaths(portalId, Py.getSystemState());
     // setup the python interpreter
     PythonInterpreter python = PythonInterpreter.threadLocalStateInterpreter(null);
     // expose services for backward compatibility - deprecated
     python.set("Services", scriptingServices);
     // python.setLocals(scriptObject);
     JythonLogger jythonLogger = new JythonLogger(path);
     python.setOut(jythonLogger);
     python.setErr(jythonLogger);
     python.execfile(in, path);
     String scriptClassName = StringUtils.capitalize(FilenameUtils.getBaseName(path)) + "Data";
     PyObject scriptClass = python.get(scriptClassName);
     if (scriptClass != null) {
       scriptObject = scriptClass.__call__();
     } else {
       log.debug("Failed to find class '{}'", scriptClassName);
     }
     python.cleanup();
   }
   return scriptObject;
 }
 @Override
 protected void removeFromRealEngine(String name) {
   py.set(name, null);
 }
 @Override
 protected void putInRealEngine(String name, Object value) {
   py.set(name, value);
 }
Example #11
0
  @SuppressWarnings("nls")
  public static void main(String[] args) {
    try {
      // Launch Thread Watchdog
      // Checks for deadlocks
      new Thread(
              new Runnable() {

                @Override
                public void run() {
                  ThreadMXBean bean = ManagementFactory.getThreadMXBean();
                  while (true) {
                    long[] threadIds =
                        bean.findDeadlockedThreads(); // Returns null if no threads are deadlocked.
                    if (threadIds != null) {
                      System.err.println("DEADLOCK");
                      System.err.println("========");
                      ThreadInfo[] infos = bean.getThreadInfo(threadIds);
                      for (ThreadInfo info : infos) {
                        System.err.println("STACK:");
                        StackTraceElement[] stack = info.getStackTrace();
                        for (StackTraceElement x : stack) {
                          System.err.println("    " + x);
                        }
                      }
                      System.exit(1);
                    }
                    try {
                      Thread.sleep(1000);
                    } catch (InterruptedException e) {
                      // nop
                    }
                  }
                }
              })
          .start();

      // Create Java/Jython interface
      // ============================

      try (InputStream sis =
              Sython.class
                  .getClassLoader()
                  .getResourceAsStream("com/nerdscentral/sython/sython.py");
          InputStreamReader sir = new InputStreamReader(sis);
          BufferedReader bsir = new BufferedReader(sir); ) {
        {
          String lin = null;
          while ((lin = bsir.readLine()) != null) {
            if (lin.trim().length() > 0) {
              init(lin);
            }
          }
        }
      }

      // Load operators
      // ==============
      try (PythonInterpreter interp = new PythonInterpreter()) {
        try (InputStream pis =
                Sython.class
                    .getClassLoader()
                    .getResourceAsStream("com/nerdscentral/sython/processors.txt");
            InputStreamReader pir = new InputStreamReader(pis);
            BufferedReader bpir = new BufferedReader(pir); ) {
          String lin = null;
          HashMap<String, SFPL_Operator> processors = new HashMap<>();
          interp.exec("import __builtin__");
          while ((lin = bpir.readLine()) != null) {
            if (lin.trim().length() > 0) {
              SFPL_Operator op = (SFPL_Operator) Class.forName(lin).newInstance();
              String word = op.Word();
              processors.put(word, op);
              init("    def " + word + "(self, input, *args):");
              init("        return self.run(\"" + word + "\",input,args)");
              init("    __builtin__." + word + "=" + word);
            }
          }
          List<SFPL_Operator> vols = new ArrayList<>(404);
          vols.addAll(SFP_DBs.getAll());
          vols.addAll(SFP_Pcnt.getAll());
          for (SFPL_Operator op : vols) {
            String word = op.Word();
            processors.put(word, op);
            init("    def " + word + "(self, input):");
            init("        return self.run(\"" + word + "\",input,[])");
            init("    __builtin__." + word + "=" + word);
          }
          init("");
          System.out.println("Python about to interpret:");
          // System.out.println(initCode);
          interp.exec(initCode.toString());
          PyObject pyo = interp.get("SonicField");
          PyDictionary pid = new PyDictionary();
          pid.putAll(processors);
          PyObject sf = pyo.__call__(pid);
          interp.exec("print \"Installing sf object\"");
          interp.set("sf", sf);
          interp.exec("__builtin__.sf=sf");
        }

        interp.exec("import __builtin__");
        interp.exec("__builtin__.sf=sf");
        interp.exec("import sython.concurrent");
        interp.exec("print \"Switching To Python Mode\"");
        interp.exec("print \"========================\"");
        long t0 = System.currentTimeMillis();
        for (String f : args) {
          interp.exec(f);
        }
        interp.exec("sython.concurrent.sf_shutdown()");
        interp.exec("print \"========================\"");
        interp.exec("print \"------- All DONE -------\"");
        long t1 = System.currentTimeMillis();
        System.out.println("Total Processing Took: " + ((t1 - t0) / 1000) + " seconds");
      }

    } catch (Throwable t) {
      while (t != null) {
        t.printStackTrace();
        t = t.getCause();
      }
      System.exit(1);
    }
    System.exit(0);
  }