forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.ts
More file actions
91 lines (81 loc) · 2.32 KB
/
Copy pathUtils.ts
File metadata and controls
91 lines (81 loc) · 2.32 KB
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
"use strict";
import {IPythonProcess, IPythonThread, IPythonModule, IPythonEvaluationResult} from "./Contracts";
import * as path from "path";
import * as fs from 'fs';
const PathValidity: Map<string, boolean> = new Map<string, boolean>();
export function validatePath(filePath: string): Promise<string> {
if (filePath.length === 0) {
return Promise.resolve('');
}
if (PathValidity.has(filePath)) {
return Promise.resolve(PathValidity.get(filePath) ? filePath : '');
}
return new Promise<string>(resolve => {
fs.exists(filePath, exists => {
PathValidity.set(filePath, exists);
return resolve(exists ? filePath : '');
});
});
}
export function validatePathSync(filePath: string): boolean {
if (filePath.length === 0) {
return false;
}
if (PathValidity.has(filePath)) {
return PathValidity.get(filePath);
}
const exists = fs.existsSync(filePath);
PathValidity.set(filePath, exists);
return exists;
}
export function CreatePythonThread(id: number, isWorker: boolean, process: IPythonProcess, name: string = ""): IPythonThread {
return {
IsWorkerThread: isWorker,
Process: process,
Name: name,
Id: id,
Frames: []
};
}
export function CreatePythonModule(id: number, fileName: string): IPythonModule {
let name = fileName;
if (typeof fileName === "string") {
try {
name = path.basename(fileName);
}
catch (ex) {
}
}
else {
name = "";
}
return {
ModuleId: id,
Name: name,
Filename: fileName
};
}
export function FixupEscapedUnicodeChars(value: string): string {
return value;
}
export class IdDispenser {
private _freedInts: number[] = [];
private _curValue: number = 0;
public Allocate(): number {
if (this._freedInts.length > 0) {
let res: number = this._freedInts[this._freedInts.length - 1];
this._freedInts.splice(this._freedInts.length - 1, 1);
return res;
} else {
let res: number = this._curValue++;
return res;
}
}
public Free(id: number) {
if (id + 1 === this._curValue) {
this._curValue--;
} else {
this._freedInts.push(id);
}
}
}