public void CanComposeExportsFromPythonCodeWithDecorator()
        {
            var pythonCode =
                @"
@export(IItemSource)
class StringItemSource(BasePythonItemSource):
    def GetAllItems(self):
        return [""Item 1"", ""Item 2"", ""Item 3""]
";

            var _engine       = Python.CreateEngine();
            var script        = _engine.CreateScriptSourceFromString(pythonCode);
            var typeExtractor = new ExtractTypesFromScript(_engine);
            var exports       = typeExtractor.GetPartsFromScript(script, IronPythonCommandsMefExport.InitialScopeTypes).ToList();

            var container = new CompositionContainer();
            var batch     = new CompositionBatch(exports, new ComposablePart[] {});

            container.Compose(batch);

            var instance = new MockImporter();

            container.SatisfyImportsOnce(instance);

            Assert.Equal(1, instance.ItemSources.Count());
        }
        public void CanImportJustOneItemIntoPythonClassUsingDecorator()
        {
            var pythonCode =
                @"
class StringItemSource:
    @import_one(IActOnItem)
    def import_action(self, action):
        self.action = action
";

            var _engine = Python.CreateEngine();
            var script  = _engine.CreateScriptSourceFromString(pythonCode);

            var typeExtractor = new ExtractTypesFromScript(_engine);
            var exports       = typeExtractor.GetPartsFromScript(script, IronPythonCommandsMefExport.InitialScopeTypes).ToList();

            var container = new CompositionContainer(new TypeCatalog(typeof(MockExporter), typeof(MockImportActions)));

            var batch = new CompositionBatch(exports, new ComposablePart[] {});

            container.Compose(batch);

            object action = exports.First().Instance.action;

            Assert.NotNull(action);
            Assert.IsAssignableFrom <IActOnItem>(action);
        }
        public void CanImportIntoPythonClassUsingDecorator()
        {
            var pythonCode =
                @"
class StringItemSource:
    @import_many(IActOnItem)
    def import_actions(self, actions):
        self.actions = actions
";

            var _engine = Python.CreateEngine();
            var paths   = _engine.GetSearchPaths();

            paths.Add(@"D:\documents\dev\ILoveLucene\lib\ironpython\Lib");
            _engine.SetSearchPaths(paths);
            var script = _engine.CreateScriptSourceFromString(pythonCode);

            var typeExtractor = new ExtractTypesFromScript(_engine);
            var exports       = typeExtractor.GetPartsFromScript(script, IronPythonCommandsMefExport.InitialScopeTypes).ToList();

            var container = new CompositionContainer(new TypeCatalog(typeof(MockExporter), typeof(MockImportActions)));

            var batch = new CompositionBatch(exports, new ComposablePart[] {});

            container.Compose(batch);

            var value = container.GetExportedValue <MockImportActions>();

            Assert.Equal(1, value.ActOnItems.Count());
            IEnumerable actions = exports.First().Instance.actions;

            Assert.Equal(1, actions.OfType <IActOnItem>().Count());
            Assert.Equal(1, actions.OfType <MockExporter>().Count());
        }
        public void CanImportListIntoPythonClass()
        {
            var pythonCode =
                @"
class StringItemSource:
    def import_actions(self, actions):
        self.actions = actions
    def normal_method(self):
        pass

StringItemSource.import_actions.func_dict['imports'] = IronPythonImportDefinition('import_action', IActOnItem, 'ZeroOrOne', True, True)
";

            var _engine = Python.CreateEngine();
            var script  = _engine.CreateScriptSourceFromString(pythonCode);

            var typeExtractor = new ExtractTypesFromScript(_engine);
            var exports       = typeExtractor.GetPartsFromScript(script, IronPythonCommandsMefExport.InitialScopeTypes).ToList();

            var container = new CompositionContainer(new TypeCatalog(typeof(MockExporter), typeof(MockImportActions)));

            var batch = new CompositionBatch(exports, new ComposablePart[] {});

            container.Compose(batch);

            var value = container.GetExportedValue <MockImportActions>();

            Assert.Equal(1, value.ActOnItems.Count());
            IEnumerable actions = exports.First().Instance.actions;

            Assert.Equal(1, actions.OfType <IActOnItem>().Count());
            Assert.Equal(1, actions.OfType <MockExporter>().Count());
        }
        public void ComposeWithTypesExportedFromPythonAndCSharp(
            object compositionTarget,
            string scriptsToImport,
            params Type[] typesToImport)
        {
            ScriptSource script;
            var engine = Python.CreateEngine();
            using (var scriptStream = GetType().Assembly.
                GetManifestResourceStream(GetType(), scriptsToImport))
            using (var scriptText = new StreamReader(scriptStream))
            {
                script = engine.CreateScriptSourceFromString(scriptText.ReadToEnd());
            }

            var typeExtractor = new ExtractTypesFromScript(engine);
            var exports = typeExtractor.GetPartsFromScript(script, typesToImport).ToList();

            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            var container = new CompositionContainer(catalog);
            var batch = new CompositionBatch(exports, new ComposablePart[] { });
            container.Compose(batch);

            container.SatisfyImportsOnce(compositionTarget);
        }
        public void ClassWithoutExportsResultsInZeroParts()
        {
            var pythonCode =
                @"
class StringItemSource(BasePythonItemSource):
    def GetAllItems(self):
        return [""Item 1"", ""Item 2"", ""Item 3""]
";

            var _engine       = Python.CreateEngine();
            var script        = _engine.CreateScriptSourceFromString(pythonCode);
            var typeExtractor = new ExtractTypesFromScript(_engine);
            var exports       = typeExtractor.GetPartsFromScript(script, IronPythonCommandsMefExport.InitialScopeTypes).ToList();

            Assert.Equal(0, exports.Count());
        }
    public Demo()
    {
        // Create IronPython
        var engine = Python.CreateEngine();
        var script = engine.CreateScriptSourceFromString(PythonScript.Code);
        // Configure the engine with types
        var typesYouWantPythonToHaveAccessTo = new[] { typeof(IMessenger), typeof(IConfig) };
        var typeExtractor = new ExtractTypesFromScript(engine);
        var exports       = typeExtractor.GetPartsFromScript(script,
                                                             typesYouWantPythonToHaveAccessTo);
        // Compose with MEF
        var catalog   = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var container = new CompositionContainer(catalog);
        var batch     = new CompositionBatch(exports, new ComposablePart[] { });

        container.Compose(batch);
        container.SatisfyImportsOnce(this);
    }
        public void Compose <T>(string pluginPath, string servicePath, T exportedValue)
        {
            _imports = new Imports();
            var success   = false;
            var pluginDir = pluginPath;

            try
            {
                // load .py
                var pythonFiles = new List <FileInfo>();
                // service
                pythonFiles.AddRange(new DirectoryInfo(servicePath).GetFiles("*.py"));
                // plugins
                foreach (var dir in Directory.EnumerateDirectories(pluginDir))
                {
                    var dirInfo = new DirectoryInfo(dir);
                    var file    = dirInfo.GetFiles("*.py");
                    pythonFiles.AddRange(file);
                }

                // create python
                var engine = Python.CreateEngine();
                var path   = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                var dlla   = Path.Combine(path, "IronPython.Stdlib.dll");
                var dllb   = Path.Combine(path, "Bin", "IronPython.Stdlib.dll");
                if (File.Exists(dlla))
                {
                    engine.Runtime.LoadAssembly(Assembly.LoadFrom(dlla));
                }
                else if (File.Exists(dllb))
                {
                    engine.Runtime.LoadAssembly(Assembly.LoadFrom(dllb));
                }
                //var paths = engine.GetSearchPaths();
                //paths.Add(currentDir);
                //engine.SetSearchPaths(paths);

                // configure the engine with types
                var typesInjectTo = new[] { typeof(IPlugin), typeof(IHost), typeof(IService) };
                var typeExtractor = new ExtractTypesFromScript(engine);

                // add parts
                var parts = new List <ComposablePart>();
                foreach (var py in pythonFiles)
                {
                    var exports = typeExtractor.GetPartsFromScript(py.FullName, typesInjectTo);
                    parts.AddRange(exports);
                }
                var batch     = new CompositionBatch(parts, new ComposablePart[] {});
                var container = new CompositionContainer();

                container.ComposeExportedValue(exportedValue);
                container.Compose(batch);
                container.ComposeParts(_imports);
                success = true;
            }
            catch (CompositionException compositionException)
            {
                Debug.WriteLine(compositionException.Message);
            }
            catch (ReflectionTypeLoadException ex)
            {
                foreach (var item in ex.LoaderExceptions)
                {
                    Debug.WriteLine(item.Message);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }