Exemple #1
0
        public string GetCurrentString(string name)
        {
            PyObject res = _GetCurrent(new PyString(name));

            if (res.Type != PyObjectType.String)
            {
                return("");
            }

            return(res.As <PyString>().Value);
        }
Exemple #2
0
        public int GetCurrentInt(string name)
        {
            PyObject res = _GetCurrent(new PyString(name));

            if ((res.Type != PyObjectType.Long) || (res.Type != PyObjectType.IntegerVar))
            {
                return(0);
            }

            return(res.As <PyInt>().Value);
        }
Exemple #3
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("VipKeyCommand", "Wrong type");
                return(false);
            }

            PyTuple tmp = data.As <PyTuple>();

            if (tmp.Items.Count != 3)
            {
                Log.Error("VipKeyCommand", "Wrong size, expected 3 but got " + tmp.Items.Count);
                return(false);
            }

            if (tmp.Items[0].Type != PyObjectType.None)
            {
                Log.Error("VipKeyCommand", "Wrong type for item 1");
                return(false);
            }

            if (tmp.Items[1].Type != PyObjectType.String)
            {
                Log.Error("VipKeyCommand", "Wrong type for item 2");
                return(false);
            }

            if (tmp.Items[2].Type != PyObjectType.String)
            {
                Log.Error("VipKeyCommand", "Wrong type for item 3");
                return(false);
            }

            PyString command = tmp.Items[1].As <PyString>();

            if (command.Value != "VK")
            {
                Log.Error("VipKeyCommand", "Wrong command name, expected VK but got \"" + command.Value + "\"");
                return(false);
            }

            /* We cant check the vipKey, because the client sends different vipKeys, who know why ?
             * PyString vipKey = tmp.Items[2].As<PyString>();
             *
             * if (vipKey.Value != vipkey)
             * {
             *  Log.Error("VipKeyCommand", "Wrong vipKey value, expected \"" + vipkey + "\" but got \"" + vipKey.Value + "\"");
             *  return false;
             * }*/

            return(true);
        }
Exemple #4
0
        public bool Decode( PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("VipKeyCommand", "Wrong type");
                return false;
            }

            PyTuple tmp = data.As<PyTuple>();

            if (tmp.Items.Count != 3)
            {
                Log.Error("VipKeyCommand", "Wrong size, expected 3 but got " + tmp.Items.Count);
                return false;
            }

            if (tmp.Items[0].Type != PyObjectType.None)
            {
                Log.Error("VipKeyCommand", "Wrong type for item 1");
                return false;
            }

            if (tmp.Items[1].Type != PyObjectType.String)
            {
                Log.Error("VipKeyCommand", "Wrong type for item 2");
                return false;
            }

            if (tmp.Items[2].Type != PyObjectType.String)
            {
                Log.Error("VipKeyCommand", "Wrong type for item 3");
                return false;
            }

            PyString command = tmp.Items[1].As<PyString>();

            if (command.Value != "VK")
            {
                Log.Error("VipKeyCommand", "Wrong command name, expected VK but got \"" + command.Value + "\"");
                return false;
            }

            /* We cant check the vipKey, because the client sends different vipKeys, who know why ?
            PyString vipKey = tmp.Items[2].As<PyString>();

            if (vipKey.Value != vipkey)
            {
                Log.Error("VipKeyCommand", "Wrong vipKey value, expected \"" + vipkey + "\" but got \"" + vipKey.Value + "\"");
                return false;
            }*/

            return true;
        }
Exemple #5
0
        public static object ToPrimitiveType(string typePy, PyObject obj)
        {
            Type type = np_dtypesToTypes.GetValueOrDefault(typePy);

            if (type == typeof(bool))
            {
                return(obj.As <bool>());
            }
            else if (type == typeof(short))
            {
                return(obj.As <short>());
            }
            else if (type == typeof(int))
            {
                return(obj.As <int>());
            }
            else if (type == typeof(long))
            {
                return(obj.As <long>());
            }
            else if (type == typeof(ushort))
            {
                return(obj.As <ushort>());
            }
            else if (type == typeof(uint))
            {
                return(obj.As <uint>());
            }
            else if (type == typeof(ulong))
            {
                return(obj.As <ulong>());
            }
            else if (type == typeof(float))
            {
                return(obj.As <float>());
            }
            else if (type == typeof(double))
            {
                return(obj.As <double>());
            }
            else
            {
                throw new NumpyException($"type '{typePy}' not supported");
            }
        }
        /// <summary>
        /// Gets the period specification from the PyObject that can either represent a function object that defines the start time of a consolidated data or a timespan.
        /// </summary>
        /// <param name="pyObject">Python object that defines either a function object that defines the start time of a consolidated data or a timespan</param>
        /// <returns>IPeriodSpecification that represents the PyObject</returns>
        private static IPeriodSpecification GetPeriodSpecificationFromPyObject(PyObject pyObject)
        {
            Func <DateTime, CalendarInfo> expiryFunc;

            if (pyObject.TryConvertToDelegate(out expiryFunc))
            {
                return(new FuncPeriodSpecification(expiryFunc));
            }

            using (Py.GIL())
            {
                return(new TimeSpanPeriodSpecification(pyObject.As <TimeSpan>()));
            }
        }
Exemple #7
0
 public T this[int index]
 {
     get
     {
         var item = Runtime.PyList_GetItem(pyObject, index);
         var pyItem = new PyObject(item);
         return pyItem.As<T>()!;
     }
     set
     {
         var pyItem = value.ToPython();
         var result = Runtime.PyList_SetItem(pyObject, index, new NewReference(pyItem).Steal());
         if (result == -1)
             Runtime.CheckExceptionOccurred();
     }
 }
Exemple #8
0
        // We dont need to encode it, just decode as this is only sent by the client
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("PlaceboRequest", "Wrong type");
                return(false);
            }

            PyTuple tmp = data.As <PyTuple>();

            if (tmp.Items.Count != 2)
            {
                Log.Error("PlaceboRequest", "Wrong item count, expected 2 but got " + tmp.Items.Count);
                return(false);
            }

            if (tmp.Items[0].Type != PyObjectType.String)
            {
                Log.Error("PlaceboRequest", "Wrong item 1 type");
                return(false);
            }

            if (tmp.Items[1].Type != PyObjectType.Dict)
            {
                Log.Error("PlaceboRequest", "Wrong item 2 type");
                return(false);
            }

            PyString command = tmp.Items[0].As <PyString>();

            if (command.Value != "placebo")
            {
                Log.Error("PlaceboRequest", "Wrong value for command, expected \"" + request + "\", but got \"" + command.Value + "\"");
                return(false);
            }

            PyDict args = tmp.Items[1].As <PyDict>();

            if (args.Dictionary.Count != 0)
            {
                Log.Warning("PlaceboRequest", "PlaceboRequest arguments are not supported yet");
                Log.Warning("PlaceboRequest", PrettyPrinter.Print(args));
            }

            return(true);
        }
Exemple #9
0
 public T this[int index]
 {
     get
     {
         var item   = Runtime.PyList_GetItem(pyObject.Reference, index);
         var pyItem = new PyObject(item);
         return(pyItem.As <T>());
     }
     set
     {
         var pyItem = value.ToPython();
         var result = Runtime.PyList_SetItem(pyObject.Handle, index, pyItem.Handle);
         if (result == -1)
         {
             Runtime.CheckExceptionOccurred();
         }
     }
 }
Exemple #10
0
        public bool Decode(PyObject info)
        {
            if (info.Type != PyObjectType.ObjectData)
            {
                Log.Error("NodeInfo", "Wrong type for ObjectData");
                return(false);
            }

            PyObjectData data = info.As <PyObjectData>();

            if (data.Name != "machoNet.nodeInfo")
            {
                Log.Error("NodeInfo", "Wrong object name, expected machoNet.nodeInfo but got " + data.Name);
                return(false);
            }

            if (data.Arguments.Type != PyObjectType.Tuple)
            {
                Log.Error("NodeInfo", "Wrong type for ObjectData arguments, expected Tuple");
                return(false);
            }

            PyTuple args = data.Arguments.As <PyTuple>();

            if (args.Items[0].Type != PyObjectType.Long)
            {
                Log.Error("NodeInfo", "Wrong type for tuple0 item0, expected int");
                return(false);
            }

            nodeID = args.Items[0].As <PyInt>().Value;

            if (args.Items[1].Type != PyObjectType.List)
            {
                Log.Error("NodeInfo", "Wrong type for tuple0 item1, expected list");
                return(false);
            }

            solarSystems = args.Items[1].As <PyList>();

            return(true);
        }
            public override ValueType Execute(ValueType arg)
            {
                // handle refering a clr object created in previous domain,
                // it should had been deserialized and became callable agian.
                IntPtr handle = (IntPtr)arg;

                try
                {
                    using (Py.GIL())
                    {
                        IntPtr tp       = Runtime.Runtime.PyObject_TYPE(handle);
                        IntPtr tp_clear = Marshal.ReadIntPtr(tp, TypeOffset.tp_clear);
                        Assert.That(tp_clear, Is.Not.Null);

                        using (PyObject obj = new PyObject(handle))
                        {
                            obj.InvokeMethod("Method");
                            obj.InvokeMethod("StaticMethod");

                            using (var scope = Py.CreateScope())
                            {
                                scope.Set("obj", obj);
                                scope.Exec(@"
obj.Method()
obj.StaticMethod()
obj.Property += 1
obj.Field += 10
");
                            }
                            var clrObj = obj.As <Domain.MyClass>();
                            Assert.AreEqual(clrObj.Property, 2);
                            Assert.AreEqual(clrObj.Field, 20);
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    throw;
                }
                return(0);
            }
Exemple #12
0
        public bool Decode(PyObject info)
        {
            if (info.Type != PyObjectType.ObjectData)
            {
                Log.Error("NodeInfo", "Wrong type for ObjectData");
                return false;
            }

            PyObjectData data = info.As<PyObjectData>();

            if (data.Name != "machoNet.nodeInfo")
            {
                Log.Error("NodeInfo", "Wrong object name, expected machoNet.nodeInfo but got " + data.Name);
                return false;
            }

            if (data.Arguments.Type != PyObjectType.Tuple)
            {
                Log.Error("NodeInfo", "Wrong type for ObjectData arguments, expected Tuple");
                return false;
            }

            PyTuple args = data.Arguments.As<PyTuple>();

            if (args.Items[0].Type != PyObjectType.Long)
            {
                Log.Error("NodeInfo", "Wrong type for tuple0 item0, expected int");
                return false;
            }

            nodeID = args.Items[0].As<PyInt>().Value;

            if (args.Items[1].Type != PyObjectType.List)
            {
                Log.Error("NodeInfo", "Wrong type for tuple0 item1, expected list");
                return false;
            }

            solarSystems = args.Items[1].As<PyList>();

            return true;
        }
Exemple #13
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("QueueCheckCommand", "Wrong type");
                return(false);
            }

            PyTuple tmp = data.As <PyTuple>();

            if (tmp.Items.Count != 2)
            {
                Log.Error("QueueCheckCommand", "Wrong size, expected 2 but got " + tmp.Items.Count);
                return(false);
            }

            if (tmp.Items[0].Type != PyObjectType.None)
            {
                Log.Error("QueueCheckCommand", "Wrong type for item 1");
                return(false);
            }

            if (tmp.Items[1].Type != PyObjectType.String)
            {
                Log.Error("QueueCheckCommand", "Wrong type for item 2");
                return(false);
            }

            PyString command = tmp.Items[1].As <PyString>();

            if (command.Value != "QC")
            {
                Log.Error("QueueCheckCommand", "Wrong value for command, expected \"QC\" but got \"" + command.Value + "\"");
                return(false);
            }

            return(true);
        }
Exemple #14
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("QueueCheckCommand", "Wrong type");
                return false;
            }

            PyTuple tmp = data.As<PyTuple>();

            if (tmp.Items.Count != 2)
            {
                Log.Error("QueueCheckCommand", "Wrong size, expected 2 but got " + tmp.Items.Count);
                return false;
            }

            if (tmp.Items[0].Type != PyObjectType.None)
            {
                Log.Error("QueueCheckCommand", "Wrong type for item 1");
                return false;
            }

            if (tmp.Items[1].Type != PyObjectType.String)
            {
                Log.Error("QueueCheckCommand", "Wrong type for item 2");
                return false;
            }

            PyString command = tmp.Items[1].As<PyString>();

            if (command.Value != "QC")
            {
                Log.Error("QueueCheckCommand", "Wrong value for command, expected \"QC\" but got \"" + command.Value + "\"");
                return false;
            }

            return true;
        }
Exemple #15
0
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            PyScope scope = PyScriptManager.ScriptScopes["Temp"];

            scope.ImportAll("math");

            scope.Set("value", value);

            PyObject result = scope.Eval(Script);
            string   errorMsg;

            try
            {
                errorMsg = result.As <string>();                //Expect true(bool) for valid input, an error message string for invalid input
            }
            catch (InvalidCastException)
            {
                return(new ValidationResult(true, null));
            }


            return(new ValidationResult(false, errorMsg));
        }
Exemple #16
0
        public static object UnFlatten <T>(Space space, Array x)
        {
            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters["space"] = space;
            parameters["x"]     = x;
            PyObject py     = InvokeStaticMethod(caller, "flatten", parameters);
            object   result = null;

            if (typeof(T).Name == "NDarray")
            {
                result = new NDarray(py);
            }
            else if (typeof(T).Name == "Int32")
            {
                result = py.As <int>();
            }
            else
            {
                result = py;
            }

            return(result);
        }
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.ObjectEx)
            {
                Log.Error("PyException", "Wrong container type");
                return false;
            }

            PyObjectEx p = data.As<PyObjectEx>();

            if (p.IsType2 == true)
            {
                Log.Error("PyException", "Wrong PyObjectEx type, expected Normal, but got Type2");
                return false;
            }

            if (p.Header.Type != PyObjectType.Tuple)
            {
                Log.Error("PyException", "Wrong item 1 type");
                return false;
            }

            PyTuple args = p.Header.As<PyTuple>();
            if (args.Items.Count != 3)
            {
                Log.Error("PyException", "Wrong tuple 1 item count, expected 3 but got " + args.Items.Count);
                return false;
            }

            if (args.Items[0].Type != PyObjectType.Token)
            {
                Log.Error("PyException", "Wrong tuple item 1 type");
                return false;
            }

            PyToken type = args.Items[0].As<PyToken>();
            exception_type = type.Token;

            if (exception_type.StartsWith("exceptions.") == false)
            {
                Log.Warning("PyException", "Trying to decode a non-exception packet: " + exception_type);
                return false;
            }

            if (args.Items[1].Type != PyObjectType.Tuple)
            {
                Log.Error("PyException", "Wrong tuple item 2 type");
                return false;
            }

            PyTuple msg = args.Items[1].As<PyTuple>();

            if (msg.Items.Count != 1)
            {
                Log.Error("PyException", "Wrong item 2 tuple count, expected 1 but got " + msg.Items.Count);
                return false;
            }

            if (msg.Items[0].Type != PyObjectType.String)
            {
                Log.Error("PyException", "Wrong tuple 2 item 1 type");
                return false;
            }

            PyString msg_data = msg.Items[0].As<PyString>();

            message = msg_data.Value;

            if (args.Items[2].Type != PyObjectType.Dict)
            {
                Log.Error("PyException", "Wrong tuple 1 item 3 type");
                return false;
            }

            PyDict info = args.Items[2].As<PyDict>();

            if (info.Contains("origin") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key origin");
                return false;
            }

            origin = info.Get("origin").As<PyString>().Value;

            if (info.Contains("reasonArgs") == false)
            {
                Log.Error("PyException", "Dict item 1 doesn has key reasonArgs");
                return false;
            }

            reasonArgs = info.Get("reasonArgs").As<PyDict>();

            if (info.Contains("clock") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key clock");
                return false;
            }

            clock = info.Get("clock").IntValue;

            if (info.Contains("loggedOnUserCount") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key loggedOnUserCount");
                return false;
            }

            loggedOnUserCount = info.Get("loggedOnUserCount");

            if (info.Contains("region") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key region");
                return false;
            }

            region = info.Get("region").As<PyString>().Value;

            if (info.Contains("reason") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key reason");
                return false;
            }

            reason = info.Get("reason").As<PyString>().Value;

            if(info.Contains("version") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key version");
                return false;
            }

            version = info.Get("version").As<PyFloat>().Value;

            if (info.Contains("build") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key build");
                return false;
            }

            build = info.Get("build").As<PyInt>().Value;

            if (info.Contains("reasonCode") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key reasonCode");
                return false;
            }

            reasonCode = info.Get("reasonCode").StringValue;

            if (info.Contains("codename") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key codename");
                return false;
            }

            codename = info.Get("codename").As<PyString>().Value;

            if (info.Contains("machoVersion") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key machoVersion");
                return false;
            }

            machoVersion = info.Get("machoVersion").As<PyInt>().Value;

            return true;
        }
 public dynamic ToCSharp(PyObject variable)
 {
     Initpython();
     return(variable.As <dynamic>());
 }
        public bool Decode(PyObject from)
        {
            if (from.Type != PyObjectType.ObjectData)
            {
                return false;
            }

            PyObjectData obj = from.As<PyObjectData>();

            if (obj.Name != "macho.MachoAddress")
            {
                return false;
            }

            if (obj.Arguments.Type != PyObjectType.Tuple)
            {
                return false;
            }

            PyTuple args = obj.Arguments.As<PyTuple>();
            if (args.Items.Count < 3)
            {
                return false;
            }

            if (args.Items[0].Type != PyObjectType.String)
            {
                return false;
            }

            PyString typei = args.Items[0].As<PyString>();

            switch (typei.Value)
            {
                case "A":
                    if (args.Items.Count != 3)
                    {
                        return false;
                    }

                    if (!DecodeService(args.Items[1]) || !DecodeCallID(args.Items[2]))
                    {
                        return false;
                    }

                    type = AddrType.Any;
                    break;

                case "N":
                    if (args.Items.Count != 4)
                    {
                        return false;
                    }

                    if (!DecodeTypeID(args.Items[1]) || !DecodeService(args.Items[2]) || !DecodeCallID(args.Items[3]))
                    {
                        return false;
                    }

                    type = AddrType.Node;

                    break;

                case "C":
                    if (args.Items.Count != 4)
                    {
                        return false;
                    }

                    if (!DecodeTypeID(args.Items[1]) || !DecodeCallID(args.Items[2]) || !DecodeService(args.Items[3]))
                    {
                        return false;
                    }

                    type = AddrType.Client;

                    break;

                case "B":
                    if (args.Items.Count != 4)
                    {
                        return false;
                    }

                    type = AddrType.Broadcast;

                    if (args.Items[1].Type != PyObjectType.String)
                    {
                        return false;
                    }

                    if (args.Items[3].Type != PyObjectType.String)
                    {
                        return false;
                    }

                    PyString bid = args.Items[1].As<PyString>();
                    PyString idt = args.Items[3].As<PyString>();

                    service = bid.Value;
                    bcast_type = idt.Value;

                    break;

                default:
                    return false;
            }

            return true;
        }
        private bool DecodeTypeID(PyObject data)
        {
            if (( data.Type == PyObjectType.IntegerVar) || ( data.Type == PyObjectType.Long) )
            {
                typeID = (ulong)data.As<PyInt>().Value;
            }
            else if (data.Type == PyObjectType.None)
            {
                typeID = 0;
            }
            else
            {
                return false;
            }

            return true;
        }
        private bool DecodeService(PyObject data)
        {
            if (data.Type == PyObjectType.String)
            {
                service = data.As<PyString>().Value;
            }
            else if (data.Type == PyObjectType.None)
            {
                service = "";
            }
            else
            {
                return false;
            }

            return true;
        }
Exemple #22
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("AuthenticationReq", "Wrong type");
                return false;
            }

            PyTuple tmp = data.As<PyTuple>();

            if (tmp.Items.Count != 2)
            {
                Log.Error("AuthenticationReq", "Wrong size, expected 2 but got " + tmp.Items.Count);
                return false;
            }

            if (tmp.Items[0].Type != PyObjectType.String)
            {
                Log.Error("AuthenticationReq", "Wrong type for item 1");
                return false;
            }

            if (tmp.Items[1].Type != PyObjectType.Dict)
            {
                Log.Error("AuthenticationReq", "Wrong type for item 2");
                return false;
            }

            PyDict info = tmp.Items[1].As<PyDict>();

            if (info.Contains("boot_version") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_version");
                return false;
            }

            boot_version = info.Get("boot_version").As<PyFloat>().Value;

            if (info.Contains("boot_region") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_region");
                return false;
            }

            boot_region = info.Get("boot_region").As<PyString>().Value;

            if (info.Contains("user_password") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_password");
                return false;
            }

            if (info.Get("user_password").Type == PyObjectType.None)
            {
                user_password = null;
            }
            else
            {
                // user_password = info.Get("user_password").As<PyString>().Value;
                PyObjectEx obj = info.Get("user_password").As<PyObjectEx>();
                user_password = obj.Header.As<PyTuple>().Items[0].As<PyTuple>().Items[1].As<PyString>().Value;
            }

            if (info.Contains("user_affiliateid") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_affiliateid");
                return false;
            }

            user_affiliateid = info.Get("user_affiliateid").As<PyInt>().Value;

            if (info.Contains("user_password_hash") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_password_hash");
                return false;
            }

            if (info.Get("user_password_hash").Type == PyObjectType.None)
            {
                user_password_hash = null;
            }
            else
            {
                user_password_hash = info.Get("user_password_hash").As<PyString>().Value;
            }

            if (info.Contains("macho_version") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key macho_version");
                return false;
            }

            macho_version = info.Get("macho_version").As<PyInt>().Value;

            if (info.Contains("boot_codename") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_codename");
                return false;
            }

            boot_codename = info.Get("boot_codename").As<PyString>().Value;

            if (info.Contains("boot_build") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_build");
                return false;
            }

            boot_build = info.Get("boot_build").As<PyInt>().Value;

            if (info.Contains("user_name") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_name");
                return false;
            }

            user_name = info.Get("user_name").As<PyString>().Value;

            if (info.Contains("user_languageid") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_languageid");
                return false;
            }

            user_languageid = info.Get("user_languageid").As<PyString>().Value;

            return true;
        }
Exemple #23
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.ObjectEx)
            {
                Log.Error("PyException", "Wrong container type");
                return(false);
            }

            PyObjectEx p = data.As <PyObjectEx>();

            if (p.IsType2 == true)
            {
                Log.Error("PyException", "Wrong PyObjectEx type, expected Normal, but got Type2");
                return(false);
            }

            if (p.Header.Type != PyObjectType.Tuple)
            {
                Log.Error("PyException", "Wrong item 1 type");
                return(false);
            }

            PyTuple args = p.Header.As <PyTuple>();

            if (args.Items.Count != 3)
            {
                Log.Error("PyException", "Wrong tuple 1 item count, expected 3 but got " + args.Items.Count);
                return(false);
            }

            if (args.Items[0].Type != PyObjectType.Token)
            {
                Log.Error("PyException", "Wrong tuple item 1 type");
                return(false);
            }

            PyToken type = args.Items[0].As <PyToken>();

            exception_type = type.Token;

            if (exception_type.StartsWith("exceptions.") == false)
            {
                Log.Warning("PyException", "Trying to decode a non-exception packet: " + exception_type);
                return(false);
            }

            if (args.Items[1].Type != PyObjectType.Tuple)
            {
                Log.Error("PyException", "Wrong tuple item 2 type");
                return(false);
            }

            PyTuple msg = args.Items[1].As <PyTuple>();

            if (msg.Items.Count != 1)
            {
                Log.Error("PyException", "Wrong item 2 tuple count, expected 1 but got " + msg.Items.Count);
                return(false);
            }

            if (msg.Items[0].Type != PyObjectType.String)
            {
                Log.Error("PyException", "Wrong tuple 2 item 1 type");
                return(false);
            }

            PyString msg_data = msg.Items[0].As <PyString>();

            message = msg_data.Value;

            if (args.Items[2].Type != PyObjectType.Dict)
            {
                Log.Error("PyException", "Wrong tuple 1 item 3 type");
                return(false);
            }

            PyDict info = args.Items[2].As <PyDict>();

            if (info.Contains("origin") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key origin");
                return(false);
            }

            origin = info.Get("origin").As <PyString>().Value;

            if (info.Contains("reasonArgs") == false)
            {
                Log.Error("PyException", "Dict item 1 doesn has key reasonArgs");
                return(false);
            }

            reasonArgs = info.Get("reasonArgs").As <PyDict>();

            if (info.Contains("clock") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key clock");
                return(false);
            }

            clock = info.Get("clock").IntValue;

            if (info.Contains("loggedOnUserCount") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key loggedOnUserCount");
                return(false);
            }

            loggedOnUserCount = info.Get("loggedOnUserCount");

            if (info.Contains("region") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key region");
                return(false);
            }

            region = info.Get("region").As <PyString>().Value;

            if (info.Contains("reason") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key reason");
                return(false);
            }

            reason = info.Get("reason").As <PyString>().Value;

            if (info.Contains("version") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key version");
                return(false);
            }

            version = info.Get("version").As <PyFloat>().Value;

            if (info.Contains("build") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key build");
                return(false);
            }

            build = info.Get("build").As <PyInt>().Value;

            if (info.Contains("reasonCode") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key reasonCode");
                return(false);
            }

            reasonCode = info.Get("reasonCode").StringValue;

            if (info.Contains("codename") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key codename");
                return(false);
            }

            codename = info.Get("codename").As <PyString>().Value;

            if (info.Contains("machoVersion") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key machoVersion");
                return(false);
            }

            machoVersion = info.Get("machoVersion").As <PyInt>().Value;

            return(true);
        }
Exemple #24
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("AuthenticationReq", "Wrong type");
                return(false);
            }

            PyTuple tmp = data.As <PyTuple>();

            if (tmp.Items.Count != 2)
            {
                Log.Error("AuthenticationReq", "Wrong size, expected 2 but got " + tmp.Items.Count);
                return(false);
            }

            if (tmp.Items[0].Type != PyObjectType.String)
            {
                Log.Error("AuthenticationReq", "Wrong type for item 1");
                return(false);
            }

            if (tmp.Items[1].Type != PyObjectType.Dict)
            {
                Log.Error("AuthenticationReq", "Wrong type for item 2");
                return(false);
            }

            PyDict info = tmp.Items[1].As <PyDict>();

            if (info.Contains("boot_version") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_version");
                return(false);
            }

            boot_version = info.Get("boot_version").As <PyFloat>().Value;

            if (info.Contains("boot_region") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_region");
                return(false);
            }

            boot_region = info.Get("boot_region").As <PyString>().Value;

            if (info.Contains("user_password") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_password");
                return(false);
            }

            if (info.Get("user_password").Type == PyObjectType.None)
            {
                user_password = null;
            }
            else
            {
                // user_password = info.Get("user_password").As<PyString>().Value;
                PyObjectEx obj = info.Get("user_password").As <PyObjectEx>();
                user_password = obj.Header.As <PyTuple>().Items[0].As <PyTuple>().Items[1].As <PyString>().Value;
            }

            if (info.Contains("user_affiliateid") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_affiliateid");
                return(false);
            }

            user_affiliateid = info.Get("user_affiliateid").As <PyInt>().Value;

            if (info.Contains("user_password_hash") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_password_hash");
                return(false);
            }

            if (info.Get("user_password_hash").Type == PyObjectType.None)
            {
                user_password_hash = null;
            }
            else
            {
                user_password_hash = info.Get("user_password_hash").As <PyString>().Value;
            }

            if (info.Contains("macho_version") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key macho_version");
                return(false);
            }

            macho_version = info.Get("macho_version").As <PyInt>().Value;

            if (info.Contains("boot_codename") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_codename");
                return(false);
            }

            boot_codename = info.Get("boot_codename").As <PyString>().Value;

            if (info.Contains("boot_build") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_build");
                return(false);
            }

            boot_build = info.Get("boot_build").As <PyInt>().Value;

            if (info.Contains("user_name") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_name");
                return(false);
            }

            user_name = info.Get("user_name").As <PyString>().Value;

            if (info.Contains("user_languageid") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_languageid");
                return(false);
            }

            user_languageid = info.Get("user_languageid").As <PyString>().Value;

            return(true);
        }
Exemple #25
0
        public static object ToClrObject(PyObject obj, Type expectedType)
        {
            if (obj == null)
            {
                return(null);
            }

            var pyType   = obj.GetPythonType();
            var typeName = pyType.GetAttr("__name__").ToString();

            if (typeName == "NoneType")
            {
                return(null);
            }
            else if (typeName == "str")
            {
                return(obj.As <string>());
            }
            else if (typeName == "int")
            {
                return(obj.As <int>());
            }
            else if (typeName == "float")
            {
                return(obj.As <double>());
            }
            else if (typeName == "bool")
            {
                return(obj.As <bool>());
            }
            else if (NumpyHelper.IsNumpyPrimitive(typeName))
            {
                return(NumpyHelper.ToPrimitiveType(typeName, obj));
            }
            else if (typeName == "ndarray")
            {
                return(NumpyHelper.ToA(obj));
            }
            else if (typeName == "tuple")
            {
                int length = obj.Length();
                if (length == 0)
                {
                    return(null);
                }

                var pyItems = new List <PyObject>(length);
                for (int i = 0; i < length; i += 1)
                {
                    PyObject item = obj[i];
                    pyItems.Add(item);
                }

                object[] clrItems;
                object   tuple;
                if (typeof(ITuple).IsAssignableFrom(expectedType))
                {
                    var types = TypeHelpers.GetTupleTypes(expectedType);
                    clrItems = pyItems.Select((x, i) => ToClrObject(x, types[i])).ToArray();
                    tuple    = Activator.CreateInstance(expectedType, clrItems);
                }
                else if (expectedType == typeof(object))
                {
                    var types = new Type[length];
                    Array.Fill(types, typeof(object));
                    var tupleType = TypeHelpers.CreateTupleType(types);
                    clrItems = pyItems.Select(x => ToClrObject(x, typeof(object))).ToArray();
                    tuple    = Activator.CreateInstance(tupleType, clrItems);
                }
                else
                {
                    throw new Exception($"Target type {expectedType.Name} cannot be assigned from python tuple.");
                }

                return(tuple);
            }
            else if (typeName == "list" || typeName == "deque")
            {
                Type  elementType;
                Type  listType = TypeHelpers.GetGenericTypeBase(expectedType, typeof(IList <>));
                IList list;
                if (listType != null)
                {
                    elementType = listType.GetGenericArguments()[0];
                    listType    = typeof(List <>).MakeGenericType(elementType);
                    list        = (IList)Activator.CreateInstance(listType);
                }
                else if (expectedType == typeof(object))
                {
                    elementType = typeof(object);
                    list        = new List <object>();
                }
                else
                {
                    throw new Exception($"Target type {expectedType.Name} cannot be assigned from python list.");
                }

                int length = obj.Length();
                for (int i = 0; i < length; i += 1)
                {
                    PyObject item = obj[i];
                    list.Add(ToClrObject(item, elementType));
                }

                return(list);
            }
            else if (typeName == "dict")
            {
                Type keyType;
                Type valueType;

                Type        dictionaryType = TypeHelpers.GetGenericTypeBase(expectedType, typeof(IDictionary <,>));
                IDictionary dictionary;
                if (dictionaryType != null)
                {
                    var genericArgs = dictionaryType.GetGenericArguments();
                    keyType        = genericArgs[0];
                    valueType      = genericArgs[1];
                    dictionaryType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
                    dictionary     = (IDictionary)Activator.CreateInstance(dictionaryType);
                }
                else if (expectedType == typeof(object))
                {
                    keyType    = typeof(string);
                    valueType  = typeof(object);
                    dictionary = new Dictionary <string, object>();
                }
                else
                {
                    throw new Exception($"Target type {expectedType.Name} cannot be assigned from python dict.");
                }

                var dict = new PyDict(obj);
                foreach (PyObject key in dict.Keys())
                {
                    PyObject value = dict[key];
                    dictionary.Add(ToClrObject(key, keyType), ToClrObject(value, valueType));
                }

                return(dictionary);
            }
            else if (typeName == "JointSet")
            {
                return(RoboticsTypesExtensions.FromPyJointSet(obj));
            }
            else if (typeName == "JointValues")
            {
                return(RoboticsTypesExtensions.FromPyJointValues(obj));
            }
            else if (typeName == "Pose")
            {
                return(RoboticsTypesExtensions.FromPyPose(obj));
            }
            else if (typeName == "timedelta")
            {
                return(RoboticsTypesExtensions.FromPyTimeDelta(obj));
            }
            else if (typeName == "JointTrajectoryPoint")
            {
                return(RoboticsTypesExtensions.FromPyJoinTrajectoryPoint(obj));
            }
            else if (typeName == "JointTrajectory")
            {
                return(RoboticsTypesExtensions.FromPyJointTrajectory(obj));
            }
            else if (typeName == "JointStates")
            {
                return(RoboticsTypesExtensions.FromPyJointStates(obj));
            }
            else if (typeName == "JointPath")
            {
                return(RoboticsTypesExtensions.FromPyJointPath(obj));
            }
            else if (typeName == "CartesianPath")
            {
                return(RoboticsTypesExtensions.FromPyCartesianPath(obj));
            }
            else if (typeName == "PlanParameters")
            {
                return(RoboticsTypesExtensions.FromPyPlanParameters(obj));
            }
            else if (typeName == "CollisionPrimitive")
            {
                return(RoboticsTypesExtensions.FromPyCollisionPrimitive(obj));
            }
            else if (typeName == "CollisionObject")
            {
                return(RoboticsTypesExtensions.FromPyCollisionObject(obj));
            }
            else
            {
                throw new Exception($"Encountered unsupported type annotation '{pyType.ToString()}'.");
            }
        }
Exemple #26
0
        public bool Decode(PyObject data)
        {
            isNode = false;

            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("LowLevelVersionExchange", "Wrong type");
                return(false);
            }

            PyTuple tmp = data.As <PyTuple>();

            if (tmp.Items.Count != 6)
            {
                Log.Error("LowLevelVersionExchange", "Wrong item count");
                return(false);
            }

            PyObject birth = tmp.Items[0];

            if ((birth.Type != PyObjectType.IntegerVar) && (birth.Type != PyObjectType.LongLong) && (birth.Type != PyObjectType.Long))
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for birthday. Type: " + (uint)birth.Type);
                return(false);
            }

            birthday = birth.As <PyInt>().Value;

            PyObject macho = tmp.Items[1];

            if ((macho.Type != PyObjectType.IntegerVar) && (macho.Type != PyObjectType.LongLong) && (macho.Type != PyObjectType.Long))
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for machoVersion");
                return(false);
            }

            machoVersion = macho.As <PyInt>().Value;

            PyObject users = tmp.Items[2];

            if (users.Type == PyObjectType.None)
            {
                usercount = 0;
            }
            else if (users.Type == PyObjectType.Long)
            {
                usercount = users.As <PyInt>().Value;
            }
            else if (users.Type == PyObjectType.String)
            {
                isNode         = true;
                nodeIdentifier = users.As <PyString>().Value;
            }
            else
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for usercount/node identifier");
                return(false);
            }

            PyObject ver = tmp.Items[3];

            if (ver.Type != PyObjectType.Float)
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for version");
                return(false);
            }

            version = ver.As <PyFloat>().Value;

            PyObject b = tmp.Items[4];

            if ((b.Type != PyObjectType.IntegerVar) && (b.Type != PyObjectType.LongLong) && (b.Type != PyObjectType.Long))
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for build");
                return(false);
            }

            build = b.As <PyInt>().Value;

            PyObject code = tmp.Items[5];

            if (code.Type != PyObjectType.String)
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for codename");
                return(false);
            }

            codename = code.As <PyString>().Value;

            return(true);
        }
        public bool Decode(PyObject data)
        {
            isNode = false;

            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("LowLevelVersionExchange", "Wrong type");
                return false;
            }

            PyTuple tmp = data.As<PyTuple>();

            if (tmp.Items.Count != 6)
            {
                Log.Error("LowLevelVersionExchange", "Wrong item count");
                return false;
            }

            PyObject birth = tmp.Items[0];

            if ( (birth.Type != PyObjectType.IntegerVar) && (birth.Type != PyObjectType.LongLong) && (birth.Type != PyObjectType.Long))
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for birthday. Type: " + (uint)birth.Type);
                return false;
            }

            birthday = birth.As<PyInt>().Value;

            PyObject macho = tmp.Items[1];

            if ((macho.Type != PyObjectType.IntegerVar) && (macho.Type != PyObjectType.LongLong) && (macho.Type != PyObjectType.Long))
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for machoVersion");
                return false;
            }

            machoVersion = macho.As<PyInt>().Value;

            PyObject users = tmp.Items[2];

            if (users.Type == PyObjectType.None)
            {
                usercount = 0;
            }
            else if(users.Type == PyObjectType.Long)
            {
                usercount = users.As<PyInt>().Value;
            }
            else if (users.Type == PyObjectType.String)
            {
                isNode = true;
                nodeIdentifier = users.As<PyString>().Value;
            }
            else
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for usercount/node identifier");
                return false;
            }

            PyObject ver = tmp.Items[3];

            if (ver.Type != PyObjectType.Float)
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for version");
                return false;
            }

            version = ver.As<PyFloat>().Value;

            PyObject b = tmp.Items[4];

            if ((b.Type != PyObjectType.IntegerVar) && (b.Type != PyObjectType.LongLong) && (b.Type != PyObjectType.Long))
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for build");
                return false;
            }

            build = b.As<PyInt>().Value;

            PyObject code = tmp.Items[5];

            if (code.Type != PyObjectType.String)
            {
                Log.Error("LowLevelVersionExchange", "Wrong type for codename");
                return false;
            }

            codename = code.As<PyString>().Value;

            return true;
        }