forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocator.ts
311 lines (284 loc) · 10.4 KB
/
locator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable max-classes-per-file */
import { Event, Uri } from 'vscode';
import { IAsyncIterableIterator, iterEmpty } from '../../common/utils/async';
import { PythonEnvInfo, PythonEnvKind, PythonEnvSource, PythonVersion } from './info';
import {
IPythonEnvsWatcher,
PythonEnvCollectionChangedEvent,
PythonEnvsChangedEvent,
PythonEnvsWatcher,
} from './watcher';
import type { Architecture } from '../../common/utils/platform';
/**
* A single update to a previously provided Python env object.
*/
export type PythonEnvUpdatedEvent<I = PythonEnvInfo> = {
/**
* The iteration index of The env info that was previously provided.
*/
index: number;
/**
* The env info that was previously provided.
*/
old?: I;
/**
* The env info that replaces the old info.
* Update is sent as `undefined` if we find out that the environment is no longer valid.
*/
update: I | undefined;
};
/**
* A fast async iterator of Python envs, which may have incomplete info.
*
* Each object yielded by the iterator represents a unique Python
* environment.
*
* The iterator is not required to have provide all info about
* an environment. However, each yielded item will at least
* include all the `PythonEnvBaseInfo` data.
*
* During iteration the information for an already
* yielded object may be updated. Rather than updating the yielded
* object or yielding it again with updated info, the update is
* emitted by the iterator's `onUpdated` (event) property. Once there are no more updates, the event emits
* `null`.
*
* If the iterator does not have `onUpdated` then it means the
* provider does not support updates.
*
* Callers can usually ignore the update event entirely and rely on
* the locator to provide sufficiently complete information.
*/
export interface IPythonEnvsIterator<I = PythonEnvInfo> extends IAsyncIterableIterator<I> {
/**
* Provides possible updates for already-iterated envs.
*
* Once there are no more updates, `null` is emitted.
*
* If this property is not provided then it means the iterator does
* not support updates.
*/
onUpdated?: Event<PythonEnvUpdatedEvent<I> | ProgressNotificationEvent>;
}
export enum ProgressReportStage {
discoveryStarted = 'discoveryStarted',
allPathsDiscovered = 'allPathsDiscovered',
discoveryFinished = 'discoveryFinished',
}
export type ProgressNotificationEvent = {
stage: ProgressReportStage;
};
export function isProgressEvent<I = PythonEnvInfo>(
event: PythonEnvUpdatedEvent<I> | ProgressNotificationEvent,
): event is ProgressNotificationEvent {
return 'stage' in event;
}
/**
* An empty Python envs iterator.
*/
export const NOOP_ITERATOR: IPythonEnvsIterator = iterEmpty<PythonEnvInfo>();
/**
* The most basic info to send to a locator when requesting environments.
*
* This is directly correlated with the `BasicPythonEnvsChangedEvent`
* emitted by watchers.
*/
type BasicPythonLocatorQuery = {
/**
* If provided, results should be limited to these env
* kinds; if not provided, the kind of each environment
* is not considered when filtering
*/
kinds?: PythonEnvKind[];
};
/**
* The portion of a query related to env search locations.
*/
type SearchLocations = {
/**
* The locations under which to look for environments.
*/
roots: Uri[];
/**
* If true, only query for workspace related envs, i.e do not look for environments that do not have a search location.
*/
doNotIncludeNonRooted?: boolean;
};
/**
* The full set of possible info to send to a locator when requesting environments.
*
* This is directly correlated with the `PythonEnvsChangedEvent`
* emitted by watchers.
*/
export type PythonLocatorQuery = BasicPythonLocatorQuery & {
/**
* If provided, results should be limited to within these locations.
*/
searchLocations?: SearchLocations;
/**
* If provided, results should be limited envs provided by these locators.
*/
providerId?: string;
/**
* If provided, results area limited to this env.
*/
envPath?: string;
};
type QueryForEvent<E> = E extends PythonEnvsChangedEvent ? PythonLocatorQuery : BasicPythonLocatorQuery;
export type BasicEnvInfo = {
kind: PythonEnvKind;
executablePath: string;
source?: PythonEnvSource[];
envPath?: string;
/**
* The project to which this env is related to, if any
* E.g. the project directory when dealing with pipenv virtual environments.
*/
searchLocation?: Uri;
version?: PythonVersion;
name?: string;
/**
* Display name provided by locators, not generated by us.
* E.g. display name as provided by Windows Registry or Windows Store, etc
*/
displayName?: string;
identifiedUsingNativeLocator?: boolean;
arch?: Architecture;
ctime?: number;
mtime?: number;
};
/**
* A single Python environment locator.
*
* Each locator object is responsible for identifying the Python
* environments in a single location, whether a directory, a directory
* tree, or otherwise. That location is identified when the locator
* is instantiated.
*
* Based on the narrow focus of each locator, the assumption is that
* calling iterEnvs() to pick up a changed env is effectively no more
* expensive than tracking down that env specifically. Consequently,
* events emitted via `onChanged` do not need to provide information
* for the specific environments that changed.
*/
export interface ILocator<I = PythonEnvInfo, E = PythonEnvsChangedEvent> extends IPythonEnvsWatcher<E> {
readonly providerId: string;
/**
* Iterate over the enviroments known tos this locator.
*
* Locators are not required to have provide all info about
* an environment. However, each yielded item will at least
* include all the `PythonEnvBaseInfo` data. To ensure all
* possible information is filled in, call `ILocator.resolveEnv()`.
*
* Updates to yielded objects may be provided via the optional
* `onUpdated` property of the iterator. However, callers can
* usually ignore the update event entirely and rely on the
* locator to provide sufficiently complete information.
*
* @param query - if provided, the locator will limit results to match
* @returns - the fast async iterator of Python envs, which may have incomplete info
*/
iterEnvs(query?: QueryForEvent<E>): IPythonEnvsIterator<I>;
}
export type ICompositeLocator<I = PythonEnvInfo, E = PythonEnvsChangedEvent> = Omit<ILocator<I, E>, 'providerId'>;
interface IResolver {
/**
* Find as much info about the given Python environment as possible.
* If path passed is invalid, then `undefined` is returned.
*
* @param path - Python executable path or environment path to resolve more information about
*/
resolveEnv(path: string): Promise<PythonEnvInfo | undefined>;
}
export interface IResolvingLocator<I = PythonEnvInfo> extends IResolver, ICompositeLocator<I> {}
export interface GetRefreshEnvironmentsOptions {
/**
* Get refresh promise which resolves once the following stage has been reached for the list of known environments.
*/
stage?: ProgressReportStage;
}
export type TriggerRefreshOptions = {
/**
* Only trigger a refresh if it hasn't already been triggered for this session.
*/
ifNotTriggerredAlready?: boolean;
};
export interface IDiscoveryAPI {
readonly refreshState: ProgressReportStage;
/**
* Tracks discovery progress for current list of known environments, i.e when it starts, finishes or any other relevant
* stage. Note the progress for a particular query is currently not tracked or reported, this only indicates progress of
* the entire collection.
*/
readonly onProgress: Event<ProgressNotificationEvent>;
/**
* Fires with details if the known list changes.
*/
readonly onChanged: Event<PythonEnvCollectionChangedEvent>;
/**
* Resolves once environment list has finished refreshing, i.e all environments are
* discovered. Carries `undefined` if there is no refresh currently going on.
*/
getRefreshPromise(options?: GetRefreshEnvironmentsOptions): Promise<void> | undefined;
/**
* Triggers a new refresh for query if there isn't any already running.
*/
triggerRefresh(query?: PythonLocatorQuery, options?: TriggerRefreshOptions): Promise<void>;
/**
* Get current list of known environments.
*/
getEnvs(query?: PythonLocatorQuery): PythonEnvInfo[];
/**
* Find as much info about the given Python environment as possible.
* If path passed is invalid, then `undefined` is returned.
*
* @param path - Full path of Python executable or environment folder to resolve more information about
*/
resolveEnv(path: string): Promise<PythonEnvInfo | undefined>;
}
export interface IEmitter<E> {
fire(e: E): void;
}
/**
* The generic base for Python envs locators.
*
* By default `resolveEnv()` returns undefined. Subclasses may override
* the method to provide an implementation.
*
* Subclasses will call `this.emitter.fire()` to emit events.
*
* Also, in most cases the default event type (`PythonEnvsChangedEvent`)
* should be used. Only in low-level cases should you consider using
* `BasicPythonEnvsChangedEvent`.
*/
abstract class LocatorBase<I = PythonEnvInfo, E = PythonEnvsChangedEvent> implements ILocator<I, E> {
public readonly onChanged: Event<E>;
public abstract readonly providerId: string;
protected readonly emitter: IEmitter<E>;
constructor(watcher: IPythonEnvsWatcher<E> & IEmitter<E>) {
this.emitter = watcher;
this.onChanged = watcher.onChanged;
}
// eslint-disable-next-line class-methods-use-this
public abstract iterEnvs(query?: QueryForEvent<E>): IPythonEnvsIterator<I>;
}
/**
* The base for most Python envs locators.
*
* By default `resolveEnv()` returns undefined. Subclasses may override
* the method to provide an implementation.
*
* Subclasses will call `this.emitter.fire()` * to emit events.
*
* In most cases this is the class you will want to subclass.
* Only in low-level cases should you consider subclassing `LocatorBase`
* using `BasicPythonEnvsChangedEvent.
*/
export abstract class Locator<I = PythonEnvInfo> extends LocatorBase<I> {
constructor() {
super(new PythonEnvsWatcher());
}
}