Add psm writer

This commit is contained in:
Esa Kataja
2025-03-13 15:54:09 +02:00
parent f01f45ada2
commit e722565199
3 changed files with 185 additions and 5 deletions
+178
View File
@@ -0,0 +1,178 @@
from pathlib import Path
from datetime import datetime
from xml_parser import sanitize_name
get_all_code = """
function Get-AllCodeServerPages!NAME! {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Resource,
[Parameter(Mandatory=$true)]
[string]$RootProperty,
[Parameter(Mandatory=$false)]
[string]$CacheKeyProperty = "conceptCodeId",
[Parameter(Mandatory=$false)]
[ScriptBlock]$Converter = ${function:ConvertFrom-CodeServerObject!NAME!},
[Parameter(Mandatory=$false)]
[int]$PageSize = 500
)
begin {
if ($null -eq $Global:cache) {
$Global:cache = @{}
}
$cacheKey = $Resource.GetHashCode()
$cache[$cacheKey] = @{}
}
process {
$currentPage = 0;
$Global:debugOut = @()
while ($null -eq $data.totalPages -or $currentPage -lt $data.totalPages) {
$uri = "{0}?pageSize={1}&page={2}" -f $Resource, $PageSize, ++$currentPage
$data = Invoke-RestMethod -Uri $uri
write-debug $uri
if ($null -eq $data.totalPages) {
throw "Did not get totalPages, abort!"
} else {
$Global:debugOut += $data.conceptCodes
if ($null -ne $Converter) {
foreach ($codeObject in $data.$RootProperty) {
$convertedObject = Invoke-Command -ScriptBlock $Converter -ArgumentList (,$codeObject)
$Global:cache[$cacheKey].Add($convertedObject.$CacheKeyProperty, $convertedObject)
}
} else {
foreach ($codeObject in $data.$RootProperty) {
$key = ($objAttributes | Where-Object {$_.attributeName -eq $CacheKeyProperty}).attributeValue
$Global:cache[$cacheKey].Add($key, $codeObject.attributes)
}
}
}
}
write-output $cache[$cacheKey].Values
}
end {
}
}
"""
get_code = """
function Get-CodeServerObject!NAME! {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$conceptCodeId,
[Parameter(Mandatory=$true)]
[string]$Resource,
[Parameter(Mandatory=$true)]
[string]$RootProperty,
[Parameter(Mandatory=$false)]
[string]$CacheKeyProperty = "conceptCodeId"
)
begin {
if ($null -eq $Global:cache) {
$params = @{
Resource = $Resource;
RootProperty = $RootProperty;
}
if ($PSBoundParameters.ContainsKey("CacheKeyProperty")) {
$params.Add("CacheKeyProperty", $CacheKeyProperty)
}
[void](Get-AllCodeServerPagesRoles @params)
}
$cacheKey = $Resource.GetHashCode()
}
process {
Write-Debug $cacheKey
Write-Output $cache[$cacheKey][$conceptCodeId]
}
end {
}
}
"""
convert_from = """
Function ConvertFrom-CodeServerObject!NAME!([PSCustomObject[]]$codeServerObject!NAME!) {
$map = @{
!MAPS!
}
if ($null -eq $codeServerObject!NAME!) {
return
}
$obj = New-Object CodeServer!NAME! -ArgumentList $codeServerObject!NAME!
$Global:meDebug = $codeServerObject!NAME!
write-output $obj
}
"""
class_definition = """
class CodeServer!NAME! {
CodeServer!NAME!() {}
CodeServer!NAME!([psobject]$source) {
$dic = @{}
$source.attributes | foreach-object {$dic.Add($_.attributeName, $_.attributevalue)}
$this.{conceptCodeId} = $source.conceptCodeId
!VARIABLES!
}
!STRINGS!
}
Export-ModuleMember ConvertFrom-CodeServerObject!NAME!
Export-AllCodeServerPages!NAME!
Export-CodeServerObject!NAME!
"""
def define_maps(items: list[str]) -> str:
return "\n".join([f"\t'{sanitize_name(item)}' = '{item}'" for item in items])
def define_variables(items: list[str]) -> str:
return "\n".join([f"\t$this.{{{sanitize_name(item)}}} = $dic['{item}']" for item in items])
def define_strings(items: list[str]) -> str:
return "\n".join(["\t" + "[string]$" + "{" + sanitize_name(item) + "}" for item in items])
def write_psm(name, items: list[str], filename: Path = Path("Connector.psm1")) -> str:
from rich import print
if filename.exists():
filename.rename(str(filename) + f".{datetime.now().strftime('%Y-%m-%d.%H%M%S')}.bak")
output = get_all_code.replace("!NAME!", name)
output += get_code.replace("!NAME!", name)
output += convert_from.replace("!NAME!", name).replace("!MAPS!", define_maps(items))
output += class_definition.replace("!NAME!", name).replace("!VARIABLES!", define_variables(items)).replace("!STRINGS!", define_strings(items))
with open(filename, "w") as f:
f.write(output)
print(output)
pass