All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.razorvine.pyro.serializer.PyroExceptionSerpent Maven / Gradle / Ivy

Go to download

This library allows your Java program to interface very easily with the Python world. It uses the Pyro protocol to call methods on remote objects. (See https://pyro5.readthedocs.io/). Pyrolite only implements part of the client side Pyro library, hence its name 'lite'... So if you don't need Pyro's full feature set, Pyrolite may be a good choice to connect java or .NET and python. Version 5.0 changes: support Pyro5 wire protocol. Dropped support of Pyro4 (stick to version 4.xx for that).

There is a newer version: 5.1
Show newest version
package net.razorvine.pyro.serializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.razorvine.pyro.PyroException;
import net.razorvine.serpent.IClassSerializer;

/**
 * Serpent extension to be able to serialize PyroException objects with Serpent.
 *  
 * @author Irmen de Jong ([email protected])
 */
public class PyroExceptionSerpent implements IClassSerializer {

	@SuppressWarnings("unchecked")
	public static Object FromSerpentDict(Map dict) {
		Object[] args = (Object[]) dict.get("args");
		PyroException ex = new PyroException((String)args[0]);
		Map attrs = (Map)dict.get("attributes");
		// we can only deal with a possible _pyroTraceback attribute
		if(attrs.containsKey("_pyroTraceback"))
		{
			Object tb = attrs.get("_pyroTraceback");
			// if the traceback is a list of strings, create one string from it
			if(tb instanceof List) {
				StringBuilder sb=new StringBuilder();
				for(Object line: (List)tb) {
					sb.append(line);
				}	
				ex._pyroTraceback=sb.toString();
			} else {
				ex._pyroTraceback=(String)tb;
			}
		}
		return ex;
	}

	public Map convert(Object obj) {
		PyroException ex = (PyroException) obj;
		Map dict = new HashMap();
		// {'attributes':{},'__exception__':True,'args':('hello',),'__class__':'PyroError'}
		dict.put("__class__", "PyroError");
		dict.put("__exception__", true);
		dict.put("args", new Object[] {ex.getMessage()});
		Map attrMap = new HashMap();
		if(ex._pyroTraceback!=null)
			attrMap.put("_pyroTraceback", new String[]{ ex._pyroTraceback} );   // transform single string back into list
		dict.put("attributes", attrMap);
		return dict;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy