PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/Product/Uwp.Interpreter/PythonUwpInterpreter.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 222 lines | 172 code | 32 blank | 18 comment | 44 complexity | ef91893858c6a383b9502b4958fdbdbe MD5 | raw file
  1. // Python Tools for Visual Studio
  2. // Copyright(c) Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the License); you may not use
  6. // this file except in compliance with the License. You may obtain a copy of the
  7. // License at http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  10. // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
  11. // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. //
  14. // See the Apache Version 2.0 License for specific language governing
  15. // permissions and limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using Microsoft.PythonTools.Analysis;
  23. using Microsoft.PythonTools.Interpreter;
  24. namespace Microsoft.PythonTools.Uwp.Interpreter {
  25. class PythonUwpInterpreter : IPythonInterpreter, IPythonInterpreterWithProjectReferences2, IDisposable {
  26. readonly Version _langVersion;
  27. private PythonInterpreterFactoryWithDatabase _factory;
  28. private PythonTypeDatabase _typeDb;
  29. private HashSet<ProjectReference> _references;
  30. public PythonUwpInterpreter(PythonInterpreterFactoryWithDatabase factory) {
  31. _langVersion = factory.Configuration.Version;
  32. _factory = factory;
  33. _typeDb = _factory.GetCurrentDatabase();
  34. _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;
  35. }
  36. private void OnNewDatabaseAvailable(object sender, EventArgs e) {
  37. var factory = _factory;
  38. if (factory == null) {
  39. // We have been disposed already, so ignore this event
  40. return;
  41. }
  42. var evt = ModuleNamesChanged;
  43. if (evt != null) {
  44. evt(this, EventArgs.Empty);
  45. }
  46. }
  47. #region IPythonInterpreter Members
  48. public IPythonType GetBuiltinType(BuiltinTypeId id) {
  49. if (id == BuiltinTypeId.Unknown) {
  50. return null;
  51. }
  52. if (_typeDb == null) {
  53. throw new KeyNotFoundException(string.Format("{0} ({1})", id, (int)id));
  54. }
  55. var name = GetBuiltinTypeName(id, _typeDb.LanguageVersion);
  56. var res = _typeDb.BuiltinModule.GetAnyMember(name) as IPythonType;
  57. if (res == null) {
  58. throw new KeyNotFoundException(string.Format("{0} ({1})", id, (int)id));
  59. }
  60. return res;
  61. }
  62. public IList<string> GetModuleNames() {
  63. if (_typeDb == null) {
  64. return new string[0];
  65. }
  66. return new List<string>(_typeDb.GetModuleNames());
  67. }
  68. public IPythonModule ImportModule(string name) {
  69. if (_typeDb == null) {
  70. return null;
  71. }
  72. return _typeDb.GetModule(name);
  73. }
  74. public IModuleContext CreateModuleContext() {
  75. return null;
  76. }
  77. public void Initialize(PythonAnalyzer state) {
  78. }
  79. public event EventHandler ModuleNamesChanged;
  80. public Task AddReferenceAsync(ProjectReference reference, CancellationToken cancellationToken = default(CancellationToken)) {
  81. if (reference == null) {
  82. return MakeExceptionTask(new ArgumentNullException("reference"));
  83. }
  84. if (_references == null) {
  85. _references = new HashSet<ProjectReference>();
  86. // If we needed to set _references, then we also need to clone
  87. // _typeDb to avoid adding modules to the shared database.
  88. if (_typeDb != null) {
  89. _typeDb = _typeDb.Clone();
  90. }
  91. }
  92. switch (reference.Kind) {
  93. case ProjectReferenceKind.ExtensionModule:
  94. _references.Add(reference);
  95. string filename;
  96. try {
  97. filename = Path.GetFileNameWithoutExtension(reference.Name);
  98. } catch (Exception e) {
  99. return MakeExceptionTask(e);
  100. }
  101. if (_typeDb != null) {
  102. return Task.Factory.StartNew(EmptyTask);
  103. }
  104. break;
  105. }
  106. return Task.Factory.StartNew(EmptyTask);
  107. }
  108. public void RemoveReference(ProjectReference reference) {
  109. switch (reference.Kind) {
  110. case ProjectReferenceKind.ExtensionModule:
  111. if (_references != null && _references.Remove(reference) && _typeDb != null) {
  112. RaiseModulesChanged(null);
  113. }
  114. break;
  115. }
  116. }
  117. public IEnumerable<ProjectReference> GetReferences() {
  118. return _references != null ? _references : Enumerable.Empty<ProjectReference>();
  119. }
  120. private static Task MakeExceptionTask(Exception e) {
  121. var res = new TaskCompletionSource<Task>();
  122. res.SetException(e);
  123. return res.Task;
  124. }
  125. private static void EmptyTask() {
  126. }
  127. private void RaiseModulesChanged(Task task) {
  128. if (task != null && task.Exception != null) {
  129. throw task.Exception;
  130. }
  131. var modNamesChanged = ModuleNamesChanged;
  132. if (modNamesChanged != null) {
  133. modNamesChanged(this, EventArgs.Empty);
  134. }
  135. }
  136. public static string GetBuiltinTypeName(BuiltinTypeId id, Version languageVersion) {
  137. string name;
  138. switch (id) {
  139. case BuiltinTypeId.Bool: name = "bool"; break;
  140. case BuiltinTypeId.Complex: name = "complex"; break;
  141. case BuiltinTypeId.Dict: name = "dict"; break;
  142. case BuiltinTypeId.Float: name = "float"; break;
  143. case BuiltinTypeId.Int: name = "int"; break;
  144. case BuiltinTypeId.List: name = "list"; break;
  145. case BuiltinTypeId.Long: name = languageVersion.Major == 3 ? "int" : "long"; break;
  146. case BuiltinTypeId.Object: name = "object"; break;
  147. case BuiltinTypeId.Set: name = "set"; break;
  148. case BuiltinTypeId.Str: name = "str"; break;
  149. case BuiltinTypeId.Unicode: name = languageVersion.Major == 3 ? "str" : "unicode"; break;
  150. case BuiltinTypeId.Bytes: name = languageVersion.Major == 3 ? "bytes" : "str"; break;
  151. case BuiltinTypeId.Tuple: name = "tuple"; break;
  152. case BuiltinTypeId.Type: name = "type"; break;
  153. case BuiltinTypeId.BuiltinFunction: name = "builtin_function"; break;
  154. case BuiltinTypeId.BuiltinMethodDescriptor: name = "builtin_method_descriptor"; break;
  155. case BuiltinTypeId.DictKeys: name = "dict_keys"; break;
  156. case BuiltinTypeId.DictValues: name = "dict_values"; break;
  157. case BuiltinTypeId.DictItems: name = "dict_items"; break;
  158. case BuiltinTypeId.Function: name = "function"; break;
  159. case BuiltinTypeId.Generator: name = "generator"; break;
  160. case BuiltinTypeId.NoneType: name = "NoneType"; break;
  161. case BuiltinTypeId.Ellipsis: name = "ellipsis"; break;
  162. case BuiltinTypeId.Module: name = "module_type"; break;
  163. case BuiltinTypeId.ListIterator: name = "list_iterator"; break;
  164. case BuiltinTypeId.TupleIterator: name = "tuple_iterator"; break;
  165. case BuiltinTypeId.SetIterator: name = "set_iterator"; break;
  166. case BuiltinTypeId.StrIterator: name = "str_iterator"; break;
  167. case BuiltinTypeId.UnicodeIterator: name = languageVersion.Major == 3 ? "str_iterator" : "unicode_iterator"; break;
  168. case BuiltinTypeId.BytesIterator: name = languageVersion.Major == 3 ? "bytes_iterator" : "str_iterator"; break;
  169. case BuiltinTypeId.CallableIterator: name = "callable_iterator"; break;
  170. case BuiltinTypeId.Property: name = "property"; break;
  171. case BuiltinTypeId.ClassMethod: name = "classmethod"; break;
  172. case BuiltinTypeId.StaticMethod: name = "staticmethod"; break;
  173. case BuiltinTypeId.FrozenSet: name = "frozenset"; break;
  174. case BuiltinTypeId.Unknown:
  175. default:
  176. return null;
  177. }
  178. return name;
  179. }
  180. #endregion
  181. public void Dispose() {
  182. _typeDb = null;
  183. var factory = _factory;
  184. _factory = null;
  185. if (factory != null) {
  186. factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
  187. }
  188. }
  189. }
  190. }