PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/PythonDocumentationProvider.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 224 lines | 183 code | 27 blank | 14 comment | 71 complexity | 36eb176a6f1d86648570ea9f036c71d8 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;
  16. using System.Collections.Generic;
  17. using System.Reflection;
  18. using Microsoft.Scripting;
  19. using Microsoft.Scripting.Actions;
  20. using Microsoft.Scripting.Hosting;
  21. using Microsoft.Scripting.Runtime;
  22. using IronPython.Runtime.Types;
  23. namespace IronPython.Runtime {
  24. class PythonDocumentationProvider : DocumentationProvider {
  25. private readonly PythonContext _context;
  26. public PythonDocumentationProvider(PythonContext context) {
  27. _context = context;
  28. }
  29. public override ICollection<MemberDoc> GetMembers(object value) {
  30. List<MemberDoc> res = new List<MemberDoc>();
  31. PythonModule mod = value as PythonModule;
  32. if (mod != null) {
  33. foreach (var kvp in mod.__dict__) {
  34. AddMember(res, kvp, false);
  35. }
  36. return res;
  37. }
  38. NamespaceTracker ns = value as NamespaceTracker;
  39. if (ns != null) {
  40. foreach (var v in ns.SymbolAttributes) {
  41. AddMember(
  42. res,
  43. new KeyValuePair<object, object>(
  44. SymbolTable.IdToString(v.Key),
  45. Importer.MemberTrackerToPython(_context.SharedClsContext, v.Value)
  46. ),
  47. false
  48. );
  49. }
  50. } else {
  51. OldInstance oi = value as OldInstance;
  52. if (oi != null) {
  53. foreach (var member in oi.Dictionary) {
  54. AddMember(res, member, false);
  55. }
  56. AddOldClassMembers(res, oi._class);
  57. } else {
  58. PythonType pt = value as PythonType;
  59. if (pt != null) {
  60. foreach (PythonType type in pt.ResolutionOrder) {
  61. foreach (var member in type.GetMemberDictionary(_context.SharedContext)) {
  62. AddMember(res, member, true);
  63. }
  64. }
  65. } else {
  66. OldClass oc = value as OldClass;
  67. if (oc != null) {
  68. AddOldClassMembers(res, oc);
  69. } else {
  70. pt = DynamicHelpers.GetPythonType(value);
  71. foreach (var member in pt.GetMemberDictionary(_context.SharedContext)) {
  72. AddMember(res, member, true);
  73. }
  74. }
  75. }
  76. IPythonObject ipo = value as IPythonObject;
  77. if (ipo != null && ipo.Dict != null) {
  78. foreach (var member in ipo.Dict) {
  79. AddMember(res, member, false);
  80. }
  81. }
  82. }
  83. }
  84. return res.ToArray();
  85. }
  86. private void AddOldClassMembers(List<MemberDoc> res, OldClass oc) {
  87. foreach (var member in oc._dict) {
  88. AddMember(res, member, true);
  89. }
  90. foreach (var baseCls in oc.BaseClasses) {
  91. AddOldClassMembers(res, baseCls);
  92. }
  93. }
  94. private static void AddMember(List<MemberDoc> res, KeyValuePair<object, object> member, bool fromClass) {
  95. string name = member.Key as string;
  96. if (name != null) {
  97. res.Add(MakeMemberDoc(name, member.Value, fromClass));
  98. }
  99. }
  100. private static MemberDoc MakeMemberDoc(string name, object value, bool fromClass) {
  101. MemberKind kind = MemberKind.None;
  102. if (value is BuiltinFunction) {
  103. kind = MemberKind.Function;
  104. } else if (value is NamespaceTracker) {
  105. kind = MemberKind.Namespace;
  106. } else if (value is PythonFunction) {
  107. if (fromClass) {
  108. kind = MemberKind.Method;
  109. } else {
  110. kind = MemberKind.Function;
  111. }
  112. } else if (value is BuiltinMethodDescriptor || value is Method) {
  113. kind = MemberKind.Method;
  114. } else if (value is PythonType) {
  115. var pt = value as PythonType;
  116. if (pt.IsSystemType && pt.UnderlyingSystemType.IsEnum) {
  117. kind = MemberKind.Enum;
  118. } else {
  119. kind = MemberKind.Class;
  120. }
  121. } else if (value is Delegate) {
  122. kind = MemberKind.Delegate;
  123. } else if (value is ReflectedProperty || value is ReflectedExtensionProperty) {
  124. kind = MemberKind.Property;
  125. } else if (value is ReflectedEvent) {
  126. kind = MemberKind.Event;
  127. } else if (value is ReflectedField) {
  128. kind = MemberKind.Field;
  129. } else if (value != null && value.GetType().IsEnum) {
  130. kind = MemberKind.EnumMember;
  131. } else if (value is PythonType || value is OldClass) {
  132. kind = MemberKind.Class;
  133. } else if (value is IPythonObject || value is OldInstance) {
  134. kind = MemberKind.Instance;
  135. }
  136. return new MemberDoc(
  137. name,
  138. kind
  139. );
  140. }
  141. public override ICollection<OverloadDoc> GetOverloads(object value) {
  142. BuiltinFunction bf = value as BuiltinFunction;
  143. if (bf != null) {
  144. return GetBuiltinFunctionOverloads(bf);
  145. }
  146. BuiltinMethodDescriptor bmd = value as BuiltinMethodDescriptor;
  147. if (bmd != null) {
  148. return GetBuiltinFunctionOverloads(bmd.Template);
  149. }
  150. PythonFunction pf = value as PythonFunction;
  151. if (pf != null) {
  152. return new[] {
  153. new OverloadDoc(
  154. pf.__name__,
  155. pf.__doc__ as string,
  156. GetParameterDocs(pf)
  157. )
  158. };
  159. }
  160. Method method = value as Method;
  161. if (method != null) {
  162. return GetOverloads(method.__func__);
  163. }
  164. Delegate dlg = value as Delegate;
  165. if (dlg != null) {
  166. return new[] { DocBuilder.GetOverloadDoc(dlg.GetType().GetMethod("Invoke"), dlg.GetType().Name, 0, false) };
  167. }
  168. return new OverloadDoc[0];
  169. }
  170. private static ICollection<ParameterDoc> GetParameterDocs(PythonFunction pf) {
  171. ParameterDoc[] res = new ParameterDoc[pf.ArgNames.Length];
  172. for (int i = 0; i < res.Length; i++) {
  173. ParameterFlags flags = ParameterFlags.None;
  174. if (i == pf.ExpandDictPosition) {
  175. flags |= ParameterFlags.ParamsDict;
  176. } else if (i == pf.ExpandListPosition) {
  177. flags |= ParameterFlags.ParamsArray;
  178. }
  179. res[i] = new ParameterDoc(
  180. pf.ArgNames[i],
  181. flags
  182. );
  183. }
  184. return res;
  185. }
  186. private static ICollection<OverloadDoc> GetBuiltinFunctionOverloads(BuiltinFunction bf) {
  187. OverloadDoc[] res = new OverloadDoc[bf.Targets.Count];
  188. for (int i = 0; i < bf.Targets.Count; i++) {
  189. res[i] = GetOverloadDoc(bf.__name__, bf.Targets[i]);
  190. }
  191. return res;
  192. }
  193. private static OverloadDoc GetOverloadDoc(string name, MethodBase method) {
  194. return DocBuilder.GetOverloadDoc(method, name, 0);
  195. }
  196. }
  197. }