Ejemplo n.º 1
0
        private static void ExecuteIronPython(string args)
        {
            //TODO: 1)Create ngine
            ScriptEngine engine = Python.CreateEngine();

            //TODO: 2)Provide script and arguments
            string       script = "";
            ScriptSource source = engine.CreateScriptSourceFromFile(script);

            engine.GetSysModule().SetVariable("", "");

            //TODO: 3)Output redirect
            ScriptIO eIO = engine.Runtime.IO;

            MemoryStream errors = new MemoryStream();

            eIO.SetErrorOutput(errors, Encoding.Default);

            var results = new MemoryStream();

            eIO.SetOutput(results, Encoding.Default);
            //TODO: 4)Execute script
            var scope = engine.CreateScope();

            source.Execute(scope);

            //TODO: 5)Display output
            string str(byte[] x) => Encoding.Default.GetString(x);

            Console.WriteLine("ERRORS:");
            Console.WriteLine(str(errors.ToArray()));
            Console.WriteLine();
            Console.WriteLine("Results:");
            Console.WriteLine(str(results.ToArray()));
        }
Ejemplo n.º 2
0
        //private void _A(object obj)
        //{
        //    if (obj == null)
        //        return;
        //    this._destination.Write(obj);
        //}

        //private void _APF(object prefix, MarcField field, string code)
        //{
        //    if (field == null)
        //        return;
        //    this._APS(prefix, (object)field.FM(code[0]), (object)null);
        //}

        //private void _APS(object prefix, object obj, object suffix)
        //{
        //    if (obj == null)
        //        return;
        //    this._A(prefix);
        //    this._A(obj);
        //    this._A(suffix);
        //}

        //private void _APSF(object prefix, MarcField field, string code, object suffix)
        //{
        //    if (field == null)
        //        return;
        //    this._APS(prefix, (object)field.FM(code[0]), suffix);
        //}

        //private void _ASF(MarcField field, string code)
        //{
        //    this._A((object)this._SF(field, code));
        //}

        //private void _CA(bool condition, object onTrue, object onFalse)
        //{
        //    this._A(condition ? onTrue : onFalse);
        //}

        //private object _IIF(object first, object second)
        //{
        //    return first ?? second;
        //}

        //private string _FM(string label)
        //{
        //    return this.Record.FM(label);
        //}

        //private string[] _FMA(string label)
        //{
        //    return this.Record.FMA(label);
        //}

        //private MarcField[] _GFA(string tag)
        //{
        //    return this.Record.Fields.GetAll(tag);
        //}

        //private MarcField _GF(string tag)
        //{
        //    return this.Record.Fields.GetFirst(tag);
        //}

        //private void _NL()
        //{
        //    this._destination.WriteLine();
        //}

        //private bool _P(string label)
        //{
        //    MarcLabel marcLabel = MarcLabel.Parse(label);
        //    MarcField first = this.Record.Fields.GetFirst(marcLabel.Tag);
        //    if (first == null)
        //        return false;
        //    return marcLabel.IsField || first.SubFields.GetFirst(marcLabel.Code) != null;
        //}

        //private string _PF(string prefix, MarcField field, string code)
        //{
        //    if (field != null)
        //    {
        //        string str = field.FM(code[0]);
        //        if (!string.IsNullOrEmpty(str))
        //            return prefix + str;
        //    }
        //    return (string)null;
        //}

        //private bool _PS(MarcField field, string code)
        //{
        //    return field.SubFields.GetFirst(code[0]) != null;
        //}

        //private string _SF(MarcField field, string code)
        //{
        //    if (field == null)
        //        return (string)null;
        //    return field.FM(code[0]);
        //}

        //private void _V(string label)
        //{
        //    string str = this.Record.FM(label);
        //    if (string.IsNullOrEmpty(str))
        //        return;
        //    this._destination.Write(str);
        //}

        //private void _VA(string label, string separator)
        //{
        //    string[] strArray = this.Record.FMA(label);
        //    if (ArrayUtility.IsNullOrEmpty((Array)strArray))
        //        return;
        //    this._destination.Write(string.Join(separator, strArray));
        //}

        //private void _VL(string label)
        //{
        //    this._V(label);
        //    this._NL();
        //}

        //private void _VLA(string label, string separator)
        //{
        //    this._VA(label, separator);
        //    this._NL();
        //}

        ///// <summary>Clears the cache.</summary>
        //public virtual void ClearCache()
        //{
        //    this._codeCache.Clear();
        //}

        #endregion

        #region Public methods

        public void FormatRecord
        (
            [NotNull] TextWriter output,
            [NotNull] MarcRecord record,
            [NotNull] string format
        )
        {
            Code.NotNull(output, "output");
            Code.NotNull(record, "record");
            Code.NotNullNorEmpty(format, "format");

            Output           = output;
            Record           = record;
            Locals["Record"] = record;
            ScriptIO          io          = Interpreter.Runtime.IO;
            InterceptorStream interceptor = new InterceptorStream
                                            (
                Output,
                io.OutputEncoding
                                            );

            io.SetOutput(interceptor, io.OutputEncoding);
        }