Skip to content

Init with get method Python TSToy resource #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions samples/python/resources/first/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be added to the global gitignore or merged into this project gitignore. For PyCharm
# Community Edition, use 'PyCharm CE' in the configurations.
.idea/

# Build output
python.zip

# uv
.uv/
uv.lock
3 changes: 3 additions & 0 deletions samples/python/resources/first/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python TSToy resource

To be filled in
95 changes: 95 additions & 0 deletions samples/python/resources/first/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[CmdletBinding()]
param (
[ValidateSet('build', 'test')]
[string]$mode = 'build',
[string]$name = 'pythontstoy'
)

function Build-PythonProject {
[CmdletBinding()]
param (
[Parameter()]
[string]$Name
)
begin {
Write-Verbose -Message "Starting Python project build process"

$sourceDir = Join-Path -Path $PSScriptRoot -ChildPath 'src'
$outputDir = Join-Path -Path $PSScriptRoot -ChildPath 'dist'
}

process {
Install-Uv

Push-Location -Path $sourceDir -ErrorAction Stop

try {
# Create virtual environment
& uv venv

# Activate it
& .\.venv\Scripts\activate.ps1

# Sync all the dependencies
& uv sync

# Create executable
$pyInstallerArgs = @(
'main.py',
'-F',
'--clean',
'--distpath', $outputDir,
'--name', $Name
)
& pyinstaller.exe @pyInstallerArgs
}
finally {
deactivate
Pop-Location -ErrorAction Ignore
}
}

end {
Write-Verbose -Message "Python project build process completed"
}
}

function Install-Uv() {
begin {
Write-Verbose -Message "Installing Uv dependencies"
}

process {
if ($IsWindows) {
if (-not (Get-Command uv -ErrorAction SilentlyContinue)) {
Write-Verbose -Message "Installing uv package manager on Windows"
Invoke-RestMethod https://astral.sh/uv/install.ps1 | Invoke-Expression

}
$env:Path = "$env:USERPROFILE\.local\bin;$env:Path"
}
elseif ($IsLinux) {
curl -LsSf https://astral.sh/uv/install.sh | sh
}
}

end {
Write-Verbose -Message "Uv installation process completed"
}
}

switch ($mode) {
'build' {
Build-PythonProject -Name $name
}
'test' {
Build-PythonProject -Name $name

$testContainer = New-PesterContainer -Path (Join-Path 'tests' 'acceptance.tests.ps1') -Data @{
Name = $name
}

Invoke-Pester -Container $testContainer -Output Detailed
}

}
44 changes: 44 additions & 0 deletions samples/python/resources/first/src/commands/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import click

def common(function):
"""Add common options to click commands"""
function = click.option(
"--input",
"input_json",
help="JSON input data with settings",
required=False,
type=str,
)(function)

function = click.option(
"--scope",
help="Target configuration scope (user or machine)",
type=click.Choice(["user", "machine"]),
required=False,
)(function)

function = click.option(
"--exist",
"exist",
help="Check if configuration exists",
is_flag=True,
default=None,
)(function)

function = click.option(
"--updateAutomatically",
"updateAutomatically",
help="Whether updates should be automatic",
type=bool,
required=False,
)(function)

function = click.option(
"--updateFrequency",
"updateFrequency",
help="Update frequency in days (1-180)",
type=int,
required=False,
)(function)

return function
34 changes: 34 additions & 0 deletions samples/python/resources/first/src/commands/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import click
import json
import sys
from utils.logger import Logger
from config.config import Settings

logger = Logger()

@click.command("export")
def export_command():
"""Export all configuration settings (user and machine) as JSON.

This command retrieves both user and machine configurations and
outputs them as a JSON object. If a configuration doesn't
exist, it will show the default values.
"""
try:
user_settings = Settings(scope="user")
machine_settings = Settings(scope="machine")

user_result, user_err = Settings.get_current_state(user_settings, logger)
machine_result, machine_err = Settings.get_current_state(machine_settings, logger)

if user_err:
logger.warning(f"Error retrieving user configuration: {user_err}", "export_command")
if machine_err:
logger.warning(f"Error retrieving machine configuration: {machine_err}", "export_command")

print(user_result.to_json())
print(machine_result.to_json())

except Exception as e:
logger.critical(f"Unexpected error in export command: {str(e)}", "export_command")
sys.exit(1)
29 changes: 29 additions & 0 deletions samples/python/resources/first/src/commands/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import click
import sys
from utils.logger import Logger
from config.config import Settings
from commands.common import common

logger = Logger()

@click.command("get")
@common
def get_command(input_json, scope, exist, updateAutomatically, updateFrequency):
"""Gets the current state of a tstoy configuration file."""
try:
data = Settings.validate(
input_json, scope, exist, updateAutomatically, updateFrequency, 'get', logger
)

settings = Settings.from_dict(data)

result_settings, err = Settings.get_current_state(settings, logger)
if err:
logger.error(f"Failed to get settings: {err}", "get_command")
sys.exit(1)

result_settings.print_config()

except Exception as e:
logger.critical(f"Unexpected error: {str(e)}", "get_command")
sys.exit(1)
Loading