/Python/Product/Uwp.Interpreter/PythonUwpInterpreter.cs
C# | 222 lines | 172 code | 32 blank | 18 comment | 44 complexity | ef91893858c6a383b9502b4958fdbdbe MD5 | raw file
- // Python Tools for Visual Studio
- // Copyright(c) Microsoft Corporation
- // All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the License); you may not use
- // this file except in compliance with the License. You may obtain a copy of the
- // License at http://www.apache.org/licenses/LICENSE-2.0
- //
- // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
- // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
- // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
- // MERCHANTABLITY OR NON-INFRINGEMENT.
- //
- // See the Apache Version 2.0 License for specific language governing
- // permissions and limitations under the License.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.PythonTools.Analysis;
- using Microsoft.PythonTools.Interpreter;
- namespace Microsoft.PythonTools.Uwp.Interpreter {
- class PythonUwpInterpreter : IPythonInterpreter, IPythonInterpreterWithProjectReferences2, IDisposable {
- readonly Version _langVersion;
- private PythonInterpreterFactoryWithDatabase _factory;
- private PythonTypeDatabase _typeDb;
- private HashSet<ProjectReference> _references;
- public PythonUwpInterpreter(PythonInterpreterFactoryWithDatabase factory) {
- _langVersion = factory.Configuration.Version;
- _factory = factory;
- _typeDb = _factory.GetCurrentDatabase();
- _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;
- }
- private void OnNewDatabaseAvailable(object sender, EventArgs e) {
- var factory = _factory;
- if (factory == null) {
- // We have been disposed already, so ignore this event
- return;
- }
- var evt = ModuleNamesChanged;
- if (evt != null) {
- evt(this, EventArgs.Empty);
- }
- }
- #region IPythonInterpreter Members
- public IPythonType GetBuiltinType(BuiltinTypeId id) {
- if (id == BuiltinTypeId.Unknown) {
- return null;
- }
- if (_typeDb == null) {
- throw new KeyNotFoundException(string.Format("{0} ({1})", id, (int)id));
- }
- var name = GetBuiltinTypeName(id, _typeDb.LanguageVersion);
- var res = _typeDb.BuiltinModule.GetAnyMember(name) as IPythonType;
- if (res == null) {
- throw new KeyNotFoundException(string.Format("{0} ({1})", id, (int)id));
- }
- return res;
- }
- public IList<string> GetModuleNames() {
- if (_typeDb == null) {
- return new string[0];
- }
- return new List<string>(_typeDb.GetModuleNames());
- }
- public IPythonModule ImportModule(string name) {
- if (_typeDb == null) {
- return null;
- }
- return _typeDb.GetModule(name);
- }
- public IModuleContext CreateModuleContext() {
- return null;
- }
- public void Initialize(PythonAnalyzer state) {
- }
- public event EventHandler ModuleNamesChanged;
- public Task AddReferenceAsync(ProjectReference reference, CancellationToken cancellationToken = default(CancellationToken)) {
- if (reference == null) {
- return MakeExceptionTask(new ArgumentNullException("reference"));
- }
- if (_references == null) {
- _references = new HashSet<ProjectReference>();
- // If we needed to set _references, then we also need to clone
- // _typeDb to avoid adding modules to the shared database.
- if (_typeDb != null) {
- _typeDb = _typeDb.Clone();
- }
- }
- switch (reference.Kind) {
- case ProjectReferenceKind.ExtensionModule:
- _references.Add(reference);
- string filename;
- try {
- filename = Path.GetFileNameWithoutExtension(reference.Name);
- } catch (Exception e) {
- return MakeExceptionTask(e);
- }
- if (_typeDb != null) {
- return Task.Factory.StartNew(EmptyTask);
- }
- break;
- }
- return Task.Factory.StartNew(EmptyTask);
- }
- public void RemoveReference(ProjectReference reference) {
- switch (reference.Kind) {
- case ProjectReferenceKind.ExtensionModule:
- if (_references != null && _references.Remove(reference) && _typeDb != null) {
- RaiseModulesChanged(null);
- }
- break;
- }
- }
- public IEnumerable<ProjectReference> GetReferences() {
- return _references != null ? _references : Enumerable.Empty<ProjectReference>();
- }
- private static Task MakeExceptionTask(Exception e) {
- var res = new TaskCompletionSource<Task>();
- res.SetException(e);
- return res.Task;
- }
- private static void EmptyTask() {
- }
- private void RaiseModulesChanged(Task task) {
- if (task != null && task.Exception != null) {
- throw task.Exception;
- }
- var modNamesChanged = ModuleNamesChanged;
- if (modNamesChanged != null) {
- modNamesChanged(this, EventArgs.Empty);
- }
- }
- public static string GetBuiltinTypeName(BuiltinTypeId id, Version languageVersion) {
- string name;
- switch (id) {
- case BuiltinTypeId.Bool: name = "bool"; break;
- case BuiltinTypeId.Complex: name = "complex"; break;
- case BuiltinTypeId.Dict: name = "dict"; break;
- case BuiltinTypeId.Float: name = "float"; break;
- case BuiltinTypeId.Int: name = "int"; break;
- case BuiltinTypeId.List: name = "list"; break;
- case BuiltinTypeId.Long: name = languageVersion.Major == 3 ? "int" : "long"; break;
- case BuiltinTypeId.Object: name = "object"; break;
- case BuiltinTypeId.Set: name = "set"; break;
- case BuiltinTypeId.Str: name = "str"; break;
- case BuiltinTypeId.Unicode: name = languageVersion.Major == 3 ? "str" : "unicode"; break;
- case BuiltinTypeId.Bytes: name = languageVersion.Major == 3 ? "bytes" : "str"; break;
- case BuiltinTypeId.Tuple: name = "tuple"; break;
- case BuiltinTypeId.Type: name = "type"; break;
- case BuiltinTypeId.BuiltinFunction: name = "builtin_function"; break;
- case BuiltinTypeId.BuiltinMethodDescriptor: name = "builtin_method_descriptor"; break;
- case BuiltinTypeId.DictKeys: name = "dict_keys"; break;
- case BuiltinTypeId.DictValues: name = "dict_values"; break;
- case BuiltinTypeId.DictItems: name = "dict_items"; break;
- case BuiltinTypeId.Function: name = "function"; break;
- case BuiltinTypeId.Generator: name = "generator"; break;
- case BuiltinTypeId.NoneType: name = "NoneType"; break;
- case BuiltinTypeId.Ellipsis: name = "ellipsis"; break;
- case BuiltinTypeId.Module: name = "module_type"; break;
- case BuiltinTypeId.ListIterator: name = "list_iterator"; break;
- case BuiltinTypeId.TupleIterator: name = "tuple_iterator"; break;
- case BuiltinTypeId.SetIterator: name = "set_iterator"; break;
- case BuiltinTypeId.StrIterator: name = "str_iterator"; break;
- case BuiltinTypeId.UnicodeIterator: name = languageVersion.Major == 3 ? "str_iterator" : "unicode_iterator"; break;
- case BuiltinTypeId.BytesIterator: name = languageVersion.Major == 3 ? "bytes_iterator" : "str_iterator"; break;
- case BuiltinTypeId.CallableIterator: name = "callable_iterator"; break;
- case BuiltinTypeId.Property: name = "property"; break;
- case BuiltinTypeId.ClassMethod: name = "classmethod"; break;
- case BuiltinTypeId.StaticMethod: name = "staticmethod"; break;
- case BuiltinTypeId.FrozenSet: name = "frozenset"; break;
- case BuiltinTypeId.Unknown:
- default:
- return null;
- }
- return name;
- }
- #endregion
- public void Dispose() {
- _typeDb = null;
- var factory = _factory;
- _factory = null;
- if (factory != null) {
- factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
- }
- }
- }
- }