Skip to content

Commit c5f7c73

Browse files
Formatted script, fix analyser issuers
1 parent 15dff7a commit c5f7c73

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

Source/EnablePython.psm1

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Set-StrictMode -Version 2.0
33
$OLD_ENV_PATH = $null
44

55
function Disable-Python {
6-
<#
6+
<#
77
.SYNOPSIS
88
Disables any Python version enabled by the Enable-Python function.
99
@@ -35,12 +35,12 @@ It will also check for the existance of a 'deactivate' function, and it will cal
3535
}
3636

3737
function Enable-Python {
38-
<#
38+
<#
3939
.SYNOPSIS
4040
Adds an installed version of Python to the current shell environment.
4141
4242
.DESCRIPTION
43-
The Enable-Python function adds the install path and scripts path for the specified Python version to the PATH
43+
The Enable-Python function adds the install path and scripts path for the specified Python version to the PATH
4444
environment variable for this shell session. If multiple Python distributions are found, they are sorted in the
4545
following order:
4646
- Scope (current user installs sorted first)
@@ -68,25 +68,25 @@ The operating system install scope (either "CurrentUser" or "AllUsers") to filte
6868
6969
.EXAMPLE
7070
Enable-Python
71-
Gets all Python installs available, sorts them, and enables the top-most install (CurrentUser scope installs before
71+
Gets all Python installs available, sorts them, and enables the top-most install (CurrentUser scope installs before
7272
AllUser, PythonCore above others then alphabetical company name, highest version number, 64-bit before 32-bit).
7373
7474
.EXAMPLE
7575
Enable-Python -Company ContinuumAnalytics
76-
Gets all Python installs from Continuum Analytics, Inc., sorts them, and enables the top-most install (CurrentUser
76+
Gets all Python installs from Continuum Analytics, Inc., sorts them, and enables the top-most install (CurrentUser
7777
scope installs before AllUser, highest version number, 64-bit before 32-bit).
7878
7979
.EXAMPLE
8080
Enable-Python -Version 2.7 -Platform 32
81-
Filters for 32-bit Python distributions with a version of 2.7.*, sorts them, and enables the top-most install
82-
(CurrentUser scope installs before AllUser, PythonCore above others then alphabetical company name, highest version
81+
Filters for 32-bit Python distributions with a version of 2.7.*, sorts them, and enables the top-most install
82+
(CurrentUser scope installs before AllUser, PythonCore above others then alphabetical company name, highest version
8383
number, 64-bit before 32-bit).
8484
8585
.EXAMPLE
8686
Enable-Python 3.4 64
87-
The Version and Platform parameters are positional, and can be specified without their respective names. Filters for
88-
64-bit Python distributions with a version of 3.4.*, sorts them, and enables the top-most install (CurrentUser scope
89-
installs before AllUser, PythonCore above others then alphabetical company name, highest version number, 64-bit before
87+
The Version and Platform parameters are positional, and can be specified without their respective names. Filters for
88+
64-bit Python distributions with a version of 3.4.*, sorts them, and enables the top-most install (CurrentUser scope
89+
installs before AllUser, PythonCore above others then alphabetical company name, highest version number, 64-bit before
9090
32-bit).
9191
9292
.LINK
@@ -118,13 +118,13 @@ https://github.com/DavidWhittingham/ps-EnablePython
118118
process {
119119
$getPythonArgs = @{
120120
"Company" = $Company;
121-
"Tag" = $Tag;
121+
"Tag" = $Tag;
122122
"Version" = $Version;
123123
}
124124
if ($null -ne $Platform) {
125125
$getPythonArgs.Platform = $Platform
126126
}
127-
if (![string]::IsNullOrWhiteSpace($Scope)) {
127+
if (![string]::IsNullOrWhiteSpace($Scope)) {
128128
$getPythonArgs.Scope = $Scope
129129
}
130130

@@ -135,7 +135,7 @@ https://github.com/DavidWhittingham/ps-EnablePython
135135
}
136136

137137
if ($pythons.Length -gt 1) {
138-
Write-Host "Multiple Python distributions found matching that criteria, enabling the top choice..."
138+
Write-Information "Multiple Python distributions found matching that criteria, enabling the top choice..."
139139
}
140140

141141
# disable any existing Python version before enabling a new one
@@ -149,17 +149,17 @@ https://github.com/DavidWhittingham/ps-EnablePython
149149
$script:OLD_ENV_PATH = $Env:PATH
150150
$Env:PATH = "$($foundVersion.InstallPath);$($foundVersion.ScriptsPath);$script:OLD_ENV_PATH"
151151

152-
Write-Host """$($foundVersion.Name)"" has been enabled."
152+
Write-Information """$($foundVersion.Name)"" has been enabled."
153153
}
154154
}
155155

156156
function Get-Python {
157-
<#
157+
<#
158158
.SYNOPSIS
159159
Gets the installed versions of Python on the current machine.
160160
161161
.DESCRIPTION
162-
The Get-Python function finds Python installs on the current machine, collects their details and returns them as a
162+
The Get-Python function finds Python installs on the current machine, collects their details and returns them as a
163163
sorted list. Installs are sorted in the following order:
164164
- Scope (current user installs sorted first)
165165
- Company (alphabetical)
@@ -197,7 +197,7 @@ Gets all 32-bit Python installs with a version of 2.7.* and sorts them.
197197
198198
.EXAMPLE
199199
Get-Python 3.4 64
200-
Gets all 364-bit Python installs with a version of 3.4.* and sorts them. The Version and Platform parameters are
200+
Gets all 364-bit Python installs with a version of 3.4.* and sorts them. The Version and Platform parameters are
201201
positional, and can be specified without their respective names.
202202
203203
.LINK
@@ -206,7 +206,7 @@ https://github.com/DavidWhittingham/ps-EnablePython
206206
#>
207207

208208
[CmdletBinding()]
209-
[OutputType([PSObject])]
209+
[OutputType([System.Array])]
210210

211211
param (
212212
[Parameter()]
@@ -238,18 +238,18 @@ https://github.com/DavidWhittingham/ps-EnablePython
238238

239239
$regKeyLocations = (
240240
@{
241-
path = "HKCU\Software\Python\"
241+
path = "HKCU\Software\Python\"
242242
scope = "CurrentUser"
243243
},
244244
@{
245-
path = "HKLM\Software\Python\"
245+
path = "HKLM\Software\Python\"
246246
scope = "AllUsers"
247247
}
248248
)
249249

250250
if (is64Bit) {
251251
$regKeyLocations += @{
252-
path = "HKLM\SOFTWARE\Wow6432Node\Python\"
252+
path = "HKLM\SOFTWARE\Wow6432Node\Python\"
253253
scope = "AllUsers"
254254
}
255255
}
@@ -266,7 +266,7 @@ https://github.com/DavidWhittingham/ps-EnablePython
266266

267267
$tagKeys = Get-ChildItem -Path $companyKey.PSPath -ErrorAction "SilentlyContinue"
268268

269-
foreach($tagKey in $tagKeys) {
269+
foreach ($tagKey in $tagKeys) {
270270
if (Test-Path (Join-Path $tagKey.PSPath "\InstallPath")) {
271271
# tests if a valid install actually exists, uninstalled version can leave the Company/Tag structure
272272
$pythons.Add((createPythonVersion $tagKey $location.scope))
@@ -278,7 +278,7 @@ https://github.com/DavidWhittingham/ps-EnablePython
278278
# sort python core first
279279
$sortProperties = (
280280
@{
281-
Expression =" Scope";
281+
Expression = " Scope";
282282
Descending = $true
283283
},
284284
@{
@@ -287,7 +287,8 @@ https://github.com/DavidWhittingham/ps-EnablePython
287287
},
288288
@{
289289
Expression = "Version";
290-
Descending = $true},
290+
Descending = $true
291+
},
291292
@{
292293
Expression = "Platform";
293294
Descending = $true
@@ -299,28 +300,28 @@ https://github.com/DavidWhittingham/ps-EnablePython
299300
)
300301

301302
if (![string]::IsNullOrWhiteSpace($Scope)) {
302-
$pythons = ($pythons | Where-Object {"$($_.Scope)" -like "$Scope" })
303+
$pythons = ($pythons | Where-Object { "$($_.Scope)" -like "$Scope" })
303304
}
304305

305306
if (![string]::IsNullOrWhiteSpace($Company)) {
306-
$pythons = ($pythons | Where-Object {"$($_.Company)" -like "$Company" + "*" })
307+
$pythons = ($pythons | Where-Object { "$($_.Company)" -like "$Company" + "*" })
307308
}
308309

309310
if (![string]::IsNullOrWhiteSpace($Tag)) {
310-
$pythons = ($pythons | Where-Object {"$($_.Tag)" -like "$Tag" + "*" })
311+
$pythons = ($pythons | Where-Object { "$($_.Tag)" -like "$Tag" + "*" })
311312
}
312313

313314
if (![string]::IsNullOrWhiteSpace($Version)) {
314-
$pythons = ($pythons | Where-Object {"$($_.Version)" -like "$Version*" })
315+
$pythons = ($pythons | Where-Object { "$($_.Version)" -like "$Version*" })
315316
}
316317

317318
if ($null -ne $Platform) {
318-
$pythons = ($pythons | Where-Object {"$($_.Platform)" -like "$Platform" })
319+
$pythons = ($pythons | Where-Object { "$($_.Platform)" -like "$Platform" })
319320
}
320321

321322
@(
322-
@($pythons | Where-Object {$_.Company -eq "PythonCore"} | Sort-Object -Property $sortProperties) +
323-
@($pythons | Where-Object {$_.Company -ne "PythonCore"} | Sort-Object -Property $sortProperties)
323+
@($pythons | Where-Object { $_.Company -eq "PythonCore" } | Sort-Object -Property $sortProperties) +
324+
@($pythons | Where-Object { $_.Company -ne "PythonCore" } | Sort-Object -Property $sortProperties)
324325
)
325326
}
326327
}
@@ -332,28 +333,30 @@ function createPythonVersion([Microsoft.Win32.RegistryKey]$tagKey, [string]$scop
332333
$version = getPythonVersion($pythonExecutable)
333334
$company = $parentKey.PSChildName
334335
$companyDisplayName = if ((Get-ItemProperty -Path $parentKey.PSPath) -match "DisplayName") {
335-
(Get-ItemProperty -Path $parentKey.PSPath)."DisplayName"
336-
} else {
337-
$null
338-
}
336+
(Get-ItemProperty -Path $parentKey.PSPath)."DisplayName"
337+
}
338+
else {
339+
$null
340+
}
339341
$tagDisplayName = if ((Get-ItemProperty -Path $tagKey.PSPath) -match "DisplayName") {
340342
(Get-ItemProperty -Path $tagKey.PSPath)."DisplayName"
341-
} else {
343+
}
344+
else {
342345
$null
343346
}
344347

345348
# Create a new PSObject for storing Python version information
346349
$obj = New-Object -TypeName PSObject -Prop (@{
347-
"Company"=$company;
348-
"CompanyDisplayName"=$companyDisplayName;
349-
"Tag"=$tagKey.PSChildName;
350-
"TagDisplayName"=$tagDisplayName;
351-
"InstallPath"=(Get-ItemProperty -Path $installPathKey.PSPath)."(Default)";
352-
"Platform"=if ((is64Bit) -and !($tagKey -match "Wow6432Node")) {"64"} else {"32"};
353-
"Executable"=$pythonExecutable;
354-
"Version"=$version;
355-
"Scope"=$scope;
356-
})
350+
"Company" = $company;
351+
"CompanyDisplayName" = $companyDisplayName;
352+
"Tag" = $tagKey.PSChildName;
353+
"TagDisplayName" = $tagDisplayName;
354+
"InstallPath" = (Get-ItemProperty -Path $installPathKey.PSPath)."(Default)";
355+
"Platform" = if ((is64Bit) -and !($tagKey -match "Wow6432Node")) { "64" } else { "32" };
356+
"Executable" = $pythonExecutable;
357+
"Version" = $version;
358+
"Scope" = $scope;
359+
})
357360

358361
Add-Member -InputObject $obj -MemberType ScriptProperty -Name "Name" -Value {
359362
$company = if ($null -eq $this.CompanyDisplayName) { $this.Company } else { $this.CompanyDisplayName }
@@ -376,11 +379,13 @@ function getPythonCommand([Microsoft.Win32.RegistryKey]$installPathKey) {
376379
# test for "ExecutablePath" folder
377380
if ((Get-ItemProperty -Path $installPathKey.PSPath) -match "ExecutablePath") {
378381
Get-Command (Get-ItemProperty -Path $installPathKey.PSPath)."ExecutablePath"
379-
} else {
382+
}
383+
else {
380384
$pythonPath = Join-Path (Get-ItemProperty -Path $installPathKey.PSPath)."(Default)" "python.exe"
381385
if (Test-Path $pythonPath) {
382386
Get-Command $pythonPath
383-
} else {
387+
}
388+
else {
384389
$null
385390
}
386391
}
@@ -389,7 +394,8 @@ function getPythonCommand([Microsoft.Win32.RegistryKey]$installPathKey) {
389394
function getPythonVersion([System.Management.Automation.ApplicationInfo]$executablePath) {
390395
if ($executablePath -eq $null) {
391396
$null
392-
} else {
397+
}
398+
else {
393399
& $executablePath -c 'from sys import version_info; print(""{0}.{1}.{2}"".format(version_info[0], version_info[1], version_info[2]))'
394400
}
395401
}

0 commit comments

Comments
 (0)