/IronPython_2_0/Src/IronPython/Hosting/Python.cs

# · C# · 262 lines · 124 code · 48 blank · 90 comment · 6 complexity · 0065b1e2b7de3ce731bc03116213d1c6 MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using System.Collections.Generic;
  17. using Microsoft.Scripting.Utils;
  18. using Microsoft.Scripting.Hosting;
  19. using Microsoft.Scripting.Hosting.Providers;
  20. using Microsoft.Scripting.Runtime;
  21. using IronPython.Runtime;
  22. #if SILVERLIGHT
  23. [assembly: DynamicLanguageProvider(typeof(PythonContext), PythonContext.IronPythonDisplayName, PythonContext.IronPythonNames, PythonContext.IronPythonFileExtensions)]
  24. #endif
  25. namespace IronPython.Hosting {
  26. /// <summary>
  27. /// Provides helpers for interacting with IronPython.
  28. /// </summary>
  29. public static class Python {
  30. #region Public APIs
  31. /// <summary>
  32. /// Creates a new ScriptRuntime with the IronPython scipting engine pre-configured.
  33. /// </summary>
  34. /// <returns></returns>
  35. public static ScriptRuntime/*!*/ CreateRuntime() {
  36. return new ScriptRuntime(CreateRuntimeSetup(null));
  37. }
  38. /// <summary>
  39. /// Creates a new ScriptRuntime with the IronPython scipting engine pre-configured and
  40. /// additional options.
  41. /// </summary>
  42. public static ScriptRuntime/*!*/ CreateRuntime(IDictionary<string, object> options) {
  43. return new ScriptRuntime(CreateRuntimeSetup(options));
  44. }
  45. #if !SILVERLIGHT
  46. /// <summary>
  47. /// Creates a new ScriptRuntime with the IronPython scripting engine pre-configured
  48. /// in the specified AppDomain. The remote ScriptRuntime may be manipulated from
  49. /// the local domain but all code will run in the remote domain.
  50. /// </summary>
  51. public static ScriptRuntime/*!*/ CreateRuntime(AppDomain/*!*/ domain) {
  52. ContractUtils.RequiresNotNull(domain, "domain");
  53. return ScriptRuntime.CreateRemote(domain, CreateRuntimeSetup(null));
  54. }
  55. /// <summary>
  56. /// Creates a new ScriptRuntime with the IronPython scripting engine pre-configured
  57. /// in the specified AppDomain with additional options. The remote ScriptRuntime may
  58. /// be manipulated from the local domain but all code will run in the remote domain.
  59. /// </summary>
  60. public static ScriptRuntime/*!*/ CreateRuntime(AppDomain/*!*/ domain, IDictionary<string, object> options) {
  61. ContractUtils.RequiresNotNull(domain, "domain");
  62. return ScriptRuntime.CreateRemote(domain, CreateRuntimeSetup(options));
  63. }
  64. #endif
  65. /// <summary>
  66. /// Creates a new ScriptRuntime and returns the ScriptEngine for IronPython. If
  67. /// the ScriptRuntime is requierd it can be acquired from the Runtime property
  68. /// on the engine.
  69. /// </summary>
  70. public static ScriptEngine/*!*/ CreateEngine() {
  71. return GetEngine(CreateRuntime());
  72. }
  73. /// <summary>
  74. /// Creates a new ScriptRuntime with the specified options and returns the
  75. /// ScriptEngine for IronPython. If the ScriptRuntime is requierd it can be
  76. /// acquired from the Runtime property on the engine.
  77. /// </summary>
  78. public static ScriptEngine/*!*/ CreateEngine(IDictionary<string, object> options) {
  79. return GetEngine(CreateRuntime(options));
  80. }
  81. #if !SILVERLIGHT
  82. /// <summary>
  83. /// Creates a new ScriptRuntime and returns the ScriptEngine for IronPython. If
  84. /// the ScriptRuntime is requierd it can be acquired from the Runtime property
  85. /// on the engine.
  86. ///
  87. /// The remote ScriptRuntime may be manipulated from the local domain but
  88. /// all code will run in the remote domain.
  89. /// </summary>
  90. public static ScriptEngine/*!*/ CreateEngine(AppDomain/*!*/ domain) {
  91. return GetEngine(CreateRuntime(domain));
  92. }
  93. /// <summary>
  94. /// Creates a new ScriptRuntime with the specified options and returns the
  95. /// ScriptEngine for IronPython. If the ScriptRuntime is requierd it can be
  96. /// acquired from the Runtime property on the engine.
  97. ///
  98. /// The remote ScriptRuntime may be manipulated from the local domain but
  99. /// all code will run in the remote domain.
  100. /// </summary>
  101. public static ScriptEngine/*!*/ CreateEngine(AppDomain/*!*/ domain, IDictionary<string, object> options) {
  102. return GetEngine(CreateRuntime(domain, options));
  103. }
  104. #endif
  105. /// <summary>
  106. /// Given a ScriptRuntime gets the ScriptEngine for IronPython.
  107. /// </summary>
  108. public static ScriptEngine/*!*/ GetEngine(ScriptRuntime/*!*/ runtime) {
  109. return runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
  110. }
  111. /// <summary>
  112. /// Gets a ScriptScope which is the Python sys module for the provided ScriptRuntime.
  113. /// </summary>
  114. public static ScriptScope/*!*/ GetSysModule(this ScriptRuntime/*!*/ runtime) {
  115. ContractUtils.RequiresNotNull(runtime, "runtime");
  116. return GetSysModule(GetEngine(runtime));
  117. }
  118. /// <summary>
  119. /// Gets a ScriptScope which is the Python sys module for the provided ScriptEngine.
  120. /// </summary>
  121. public static ScriptScope/*!*/ GetSysModule(this ScriptEngine/*!*/ engine) {
  122. ContractUtils.RequiresNotNull(engine, "engine");
  123. return GetPythonService(engine).GetSystemState();
  124. }
  125. /// <summary>
  126. /// Gets a ScriptScope which is the Python __builtin__ module for the provided ScriptRuntime.
  127. /// </summary>
  128. public static ScriptScope/*!*/ GetBuiltinModule(this ScriptRuntime/*!*/ runtime) {
  129. ContractUtils.RequiresNotNull(runtime, "runtime");
  130. return GetBuiltinModule(GetEngine(runtime));
  131. }
  132. /// <summary>
  133. /// Gets a ScriptScope which is the Python __builtin__ module for the provided ScriptEngine.
  134. /// </summary>
  135. public static ScriptScope/*!*/ GetBuiltinModule(this ScriptEngine/*!*/ engine) {
  136. ContractUtils.RequiresNotNull(engine, "engine");
  137. return GetPythonService(engine).GetBuiltins();
  138. }
  139. /// <summary>
  140. /// Gets a ScriptScope which is the Python clr module for the provided ScriptRuntime.
  141. /// </summary>
  142. public static ScriptScope/*!*/ GetClrModule(this ScriptRuntime/*!*/ runtime) {
  143. ContractUtils.RequiresNotNull(runtime, "runtime");
  144. return GetClrModule(GetEngine(runtime));
  145. }
  146. /// <summary>
  147. /// Gets a ScriptScope which is the Python clr module for the provided ScriptEngine.
  148. /// </summary>
  149. public static ScriptScope/*!*/ GetClrModule(this ScriptEngine/*!*/ engine) {
  150. ContractUtils.RequiresNotNull(engine, "engine");
  151. return GetPythonService(engine).GetClr();
  152. }
  153. /// <summary>
  154. /// Imports the Python module by the given name and returns its ScriptSCope. If the
  155. /// module does not exist an exception is raised.
  156. /// </summary>
  157. public static ScriptScope/*!*/ ImportModule(this ScriptRuntime/*!*/ runtime, string/*!*/ moduleName) {
  158. ContractUtils.RequiresNotNull(runtime, "runtime");
  159. ContractUtils.RequiresNotNull(moduleName, "moduleName");
  160. return ImportModule(GetEngine(runtime), moduleName);
  161. }
  162. /// <summary>
  163. /// Imports the Python module by the given name and returns its ScriptSCope. If the
  164. /// module does not exist an exception is raised.
  165. /// </summary>
  166. public static ScriptScope/*!*/ ImportModule(this ScriptEngine/*!*/ engine, string/*!*/ moduleName) {
  167. ContractUtils.RequiresNotNull(engine, "engine");
  168. ContractUtils.RequiresNotNull(moduleName, "moduleName");
  169. return GetPythonService(engine).ImportModule(engine, moduleName);
  170. }
  171. #endregion
  172. #region Private helpers
  173. public static ScriptRuntimeSetup/*!*/ CreateRuntimeSetup(IDictionary<string, object> options) {
  174. ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
  175. setup.LanguageSetups.Add(CreateLanguageSetup(options));
  176. if (options != null) {
  177. object value;
  178. if (options.TryGetValue("Debug", out value) &&
  179. value is bool &&
  180. (bool)value) {
  181. setup.DebugMode = true;
  182. }
  183. if (options.TryGetValue("PrivateBinding", out value) &&
  184. value is bool &&
  185. (bool)value) {
  186. setup.PrivateBinding = true;
  187. }
  188. }
  189. return setup;
  190. }
  191. public static LanguageSetup/*!*/ CreateLanguageSetup(IDictionary<string, object> options) {
  192. var setup = new LanguageSetup(
  193. typeof(PythonContext).AssemblyQualifiedName,
  194. PythonContext.IronPythonDisplayName,
  195. PythonContext.IronPythonNames.Split(';'),
  196. PythonContext.IronPythonFileExtensions.Split(';')
  197. );
  198. if (options != null) {
  199. foreach (var entry in options) {
  200. setup.Options.Add(entry.Key, entry.Value);
  201. }
  202. }
  203. return setup;
  204. }
  205. private static PythonService/*!*/ GetPythonService(ScriptEngine/*!*/ engine) {
  206. return (PythonService)HostingHelpers.CallEngine<ScriptEngine, object>(
  207. engine,
  208. (context, arg) => ((PythonContext)context).GetPythonService(arg),
  209. engine
  210. );
  211. }
  212. #endregion
  213. }
  214. }