Print() public static method

public static Print ( PyObject obj ) : string
obj PyObject
return string
Example #1
0
        public override string dump(string prefix)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("[PyList " + Items.Count + " items]" + PrettyPrinter.PrintRawData(this));
            foreach (var item in Items)
            {
                PrettyPrinter.Print(builder, prefix + PrettyPrinter.Spacer, item);
            }
            return(builder.ToString());
        }
Example #2
0
        public override string dump(string prefix)
        {
            StringBuilder builder = new StringBuilder();

            if (RawData != null)
            {
                builder.AppendLine("[PySubStream " + RawData.Length + " bytes]");
            }
            else
            {
                builder.AppendLine("[PySubStream]");
            }
            PrettyPrinter.Print(builder, prefix + PrettyPrinter.Spacer, Data);
            return(builder.ToString());
        }
Example #3
0
        public override string dump(string prefix)
        {
            string        pfx1    = prefix + PrettyPrinter.Spacer;
            string        pfx2    = pfx1 + PrettyPrinter.Spacer;
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("[PyPacket typeID=" + packetType + "  userID=" + userID + "  name='" + typeString + "']");
            builder.AppendLine(pfx1 + "Source:");
            PrettyPrinter.Print(builder, pfx2, source);
            builder.AppendLine(pfx1 + "Destination:");
            PrettyPrinter.Print(builder, pfx2, dest);
            builder.AppendLine(pfx1 + "Payload:");
            PrettyPrinter.Print(builder, pfx2, payload);
            builder.AppendLine(pfx1 + "Named Payload:");
            PrettyPrinter.Print(builder, pfx2, namedPayload);
            return(builder.ToString());
        }
Example #4
0
        public override string dump(string prefix)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("[PyTuple " + Items.Count + " items]" + PrettyPrinter.PrintRawData(this));
            foreach (var item in Items)
            {
                if (item != null)
                {
                    PrettyPrinter.Print(builder, prefix + PrettyPrinter.Spacer, item);
                }
                else
                {
                    builder.AppendLine("<nullptr>");
                }
            }
            return(builder.ToString());
        }
Example #5
0
        public override string dump(string prefix)
        {
            StringBuilder builder = new StringBuilder();
            string        pfx1    = prefix + PrettyPrinter.Spacer;
            string        pfx2    = pfx1 + PrettyPrinter.Spacer + PrettyPrinter.Spacer;

            builder.AppendLine("[PyDict " + Dictionary.Count + " kvp]" + PrettyPrinter.PrintRawData(this));
            foreach (var kvp in Dictionary)
            {
                PrettyPrinter.Print(builder, pfx1 + "Key:", kvp.Key);
                if (kvp.Value == null)
                {
                    builder.AppendLine(pfx1 + "==Value: <nullptr>");
                }
                else
                {
                    builder.AppendLine(pfx1 + "==Value:" + kvp.Value.dump(pfx2).TrimEnd('\r', '\n'));
                }
            }
            return(builder.ToString());
        }
Example #6
0
        public override string dump(string prefix)
        {
            string        pfx1    = prefix + PrettyPrinter.Spacer;
            string        pfx2    = pfx1 + PrettyPrinter.Spacer;
            string        pfx3    = pfx2 + PrettyPrinter.Spacer + PrettyPrinter.Spacer;
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("[PyObjectEx " + (IsType2 ? "Type2" : "Normal") + "]" + PrettyPrinter.PrintRawData(this));
            builder.AppendLine(pfx1 + "Header:");
            PrettyPrinter.Print(builder, pfx2, Header);
            builder.AppendLine(pfx1 + "List:");
            foreach (var item in List)
            {
                PrettyPrinter.Print(builder, pfx2, item);
            }
            builder.AppendLine(pfx1 + "Dictionary:");
            foreach (var kvp in Dictionary)
            {
                PrettyPrinter.Print(builder, pfx2 + "Key:", kvp.Key);
                builder.AppendLine(pfx2 + "==Value:" + kvp.Value.dump(pfx3).TrimEnd('\r', '\n'));
                //PrettyPrinter.Print(builder, pfx2 + "==Value:", kvp.Value);
            }
            return(builder.ToString());
        }
Example #7
0
        private bool process(string filename, StreamWriter singleWriter)
        {
            // Does the file exist?
            if (!File.Exists(filename))
            {
                addText("File not found: " + filename);
                // No, fail!
                return(false);
            }
            byte[] data = null;
            using (var f = File.Open(filename, FileMode.Open))
            {
                if (f.Length == 0)
                {
                    if (singleWriter != null)
                    {
                        // Write the filename.
                        singleWriter.WriteLine(Path.GetFileName(filename));
                        // Write the decoded file.
                        singleWriter.WriteLine("Zero Length file.");
                    }
                    return(true);
                }
                data = new byte[f.Length];
                f.Read(data, 0, (int)f.Length);
                f.Close();
            }
            if (data == null)
            {
                // No data loaded.
                return(false);
            }
            // Is this a compressed file?
            if (data[0] == ZlibMarker)
            {
                // Yes, decompress it.
                data = Zlib.Decompress(data);
                if (data == null)
                {
                    // Decompress failed.
                    return(false);
                }
            }
            // Is this a proper python serial stream?
            if (data[0] != HeaderByte)
            {
                // No, is this a python file? If yes, ignore it but dont cause an error.
                if (data[0] == PythonMarker && decompilePython && !imbededPython)
                {
                    try
                    {
                        Bytecode code = new Bytecode();
                        code.load(data);
                        string outfile = null;
                        if (code.body != null)
                        {
                            Python.PyString fns = code.body.filename as Python.PyString;
                            if (fns != null)
                            {
                                outfile = fns.str;
                                outfile = outfile.Replace(':', '_');
                                outfile = outfile.Replace('\\', '-');
                                outfile = outfile.Replace('/', '-');
                            }
                        }
                        if (outfile != null)
                        {
                            string pyd = Path.GetDirectoryName(filename);
                            pyd += "\\py\\";
                            if (!Directory.Exists(pyd))
                            {
                                Directory.CreateDirectory(pyd);
                            }
                            outfile = pyd + "\\" + outfile + ".txt";
                            string dump = Python.PrettyPrinter.print(code, true);
                            File.WriteAllText(outfile, dump);
                        }
                    }
                    catch (Exception e)
                    {
                        string err = Path.GetFileName(filename) + Environment.NewLine + "Error: " + e.ToString();
                        // We, had an error but should still produce some kind of notice in the output.
                        addText(err + Environment.NewLine);
                        if (singleWriter != null)
                        {
                            //// Write the filename.
                            //singleWriter.WriteLine(Path.GetFileName(filename));
                            //// Write the decoded file.
                            //singleWriter.WriteLine("Python Decoder Error.");
                            //singleWriter.WriteLine(err);
                        }
                        else
                        {
                            //File.WriteAllText(filename + ".txt", err);
                        }
                        return(false);
                    }
                }
                return(data[0] == PythonMarker);
            }
            bool decodeDone = false;

            try
            {
                Unmarshal un = new Unmarshal();
                un.analizeInput = analizeInput;
                PyRep obj = un.Process(data);
                decodeDone = true;
                obj        = analyse(obj, filename);
                eveMarshal.PrettyPrinter printer = new eveMarshal.PrettyPrinter();
                printer.analizeInput    = analizeInput;
                printer.decompilePython = decompilePython;
                string decoded = printer.Print(obj);
                if (singleWriter != null)
                {
                    // Write the filename.
                    singleWriter.WriteLine(Path.GetFileName(filename));
                    // Write the decoded file.
                    singleWriter.Write(decoded);
                    singleWriter.Flush();
                }
                else
                {
                    File.WriteAllText(filename + ".txt", decoded);
                }
                if (un.unknown.Length > 0)
                {
                    addText(workingDirectory + Environment.NewLine);
                    addText(un.unknown.ToString() + Environment.NewLine);
                }
            }
            catch (Exception e)
            {
                string err = Path.GetFileName(filename) + Environment.NewLine + "Error: " + e.ToString();
                // We, had an error but should still produce some kind of notice in the output.
                addText(err + Environment.NewLine);
                if (singleWriter != null)
                {
                    // Write the filename.
                    singleWriter.WriteLine(Path.GetFileName(filename));
                    // Write the decoded file.
                    singleWriter.WriteLine(decodeDone ? "Printer Error. " : "Decoder Error.");
                    singleWriter.WriteLine(err);
                }
                else
                {
                    File.WriteAllText(filename + ".txt", err);
                }
                return(false);
            }
            return(true);
        }