mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
feat: impl docs (#4)
The entire public api is covered with documentation in two languages - Russian and English. the library now supports the latest three versions of python - 3.12, 3.13 and 3.14 minor design changes: now, when a Boolean flag is entered, its value is an empty string, not None. tests have been adapted to the supported versions of python, readmi has been redesigned in two languages, German is no longer available.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
from argenta import Router, Response, Command, DataBridge
|
||||
from argenta.command import Flag
|
||||
from argenta.di import FromDishka
|
||||
|
||||
router = Router(title="Authentication")
|
||||
|
||||
def authenticate_user(username: str) -> str:
|
||||
return f"token_for_{username}"
|
||||
|
||||
|
||||
@router.command(Command("login", flags=Flag("username")))
|
||||
def login_handler(response: Response, data_bridge: FromDishka[DataBridge]):
|
||||
username_flag = response.input_flags.get_flag_by_name("username")
|
||||
if not username_flag or not username_flag.input_value:
|
||||
print("Error: username must be specified using the --username flag.")
|
||||
return
|
||||
|
||||
username = username_flag.input_value
|
||||
token = authenticate_user(username)
|
||||
|
||||
data_bridge.update({"auth_token": token})
|
||||
print(f"Login successful! User '{username}' authenticated.")
|
||||
|
||||
|
||||
@router.command("get-profile")
|
||||
def get_profile_handler(response: Response, data_bridge: FromDishka[DataBridge]):
|
||||
token = data_bridge.get_by_key("auth_token")
|
||||
|
||||
if not token:
|
||||
print("Error: you are not authenticated. Please run the 'login' command first.")
|
||||
return
|
||||
|
||||
print(f"Loading profile using token: [yellow]{token}[/yellow]")
|
||||
|
||||
|
||||
@router.command("logout")
|
||||
def logout_handler(response: Response, data_bridge: FromDishka[DataBridge]):
|
||||
try:
|
||||
data_bridge.delete_by_key("auth_token")
|
||||
print("Logout successful. Session data cleared.")
|
||||
except KeyError:
|
||||
print("You were not authenticated anyway.")
|
||||
@@ -0,0 +1,12 @@
|
||||
from argenta import Command, Response, Router
|
||||
from argenta.response import ResponseStatus
|
||||
|
||||
router = Router(title="Example")
|
||||
|
||||
|
||||
@router.command(Command("greet", description="Greet the user"))
|
||||
def greet_handler(response: Response):
|
||||
if response.status == ResponseStatus.ALL_FLAGS_VALID:
|
||||
print("Hello! All flags are valid.")
|
||||
else:
|
||||
print("Warning: Some flags have issues.")
|
||||
@@ -0,0 +1,25 @@
|
||||
from argenta import Command, Response, Router
|
||||
|
||||
router = Router(title="Data Example")
|
||||
|
||||
|
||||
@router.command(Command("set", description="Set data"))
|
||||
def set_handler(response: Response):
|
||||
# Update global data storage
|
||||
response.update_data(
|
||||
{
|
||||
"user_name": "John",
|
||||
"timestamp": "2024-01-01",
|
||||
"settings": {"theme": "dark", "language": "ru"},
|
||||
}
|
||||
)
|
||||
print("Data updated successfully")
|
||||
|
||||
|
||||
@router.command(Command("show", description="Show data"))
|
||||
def show_handler(response: Response):
|
||||
# Get data from global storage
|
||||
data = response.get_data()
|
||||
if "user_name" in data:
|
||||
print(f"User: {data['user_name']}")
|
||||
print(f"Settings: {data.get('settings', {})}")
|
||||
@@ -0,0 +1,16 @@
|
||||
from argenta import Command, Response, Router
|
||||
|
||||
router = Router(title="Get Data Example")
|
||||
|
||||
|
||||
@router.command(Command("info", description="Show all stored data"))
|
||||
def info_handler(response: Response):
|
||||
# Get all data from global storage
|
||||
all_data = response.get_data()
|
||||
|
||||
if all_data:
|
||||
print("Stored data:")
|
||||
for key, value in all_data.items():
|
||||
print(f" {key}: {value}")
|
||||
else:
|
||||
print("No data stored")
|
||||
@@ -0,0 +1,19 @@
|
||||
from argenta import Command, Response, Router
|
||||
|
||||
router = Router(title="Clear Data Example")
|
||||
|
||||
|
||||
@router.command(Command("clear", description="Clear all stored data"))
|
||||
def clear_handler(response: Response):
|
||||
# Clear all data storage
|
||||
response.clear_data()
|
||||
print("All data cleared")
|
||||
|
||||
|
||||
@router.command(Command("check", description="Check if data exists"))
|
||||
def check_handler(response: Response):
|
||||
data = response.get_data()
|
||||
if data:
|
||||
print(f"Storage contains {len(data)} item(s)")
|
||||
else:
|
||||
print("Storage is empty")
|
||||
@@ -0,0 +1,29 @@
|
||||
from argenta import Command, Response, Router
|
||||
|
||||
router = Router(title="Delete Data Example")
|
||||
|
||||
|
||||
@router.command(Command("store", description="Store data"))
|
||||
def store_handler(response: Response):
|
||||
response.update_data(
|
||||
{
|
||||
"temp_key": "temporary value",
|
||||
"important_key": "important value",
|
||||
"another_key": "another value",
|
||||
}
|
||||
)
|
||||
print("Data stored")
|
||||
|
||||
|
||||
@router.command(Command("remove", description="Remove specific key"))
|
||||
def remove_handler(response: Response):
|
||||
# Delete specific key from storage
|
||||
try:
|
||||
response.delete_from_data("temp_key")
|
||||
print("Key 'temp_key' deleted")
|
||||
|
||||
# Check what remains
|
||||
remaining = response.get_data()
|
||||
print(f"Remaining keys: {list(remaining.keys())}")
|
||||
except KeyError:
|
||||
print("Key not found")
|
||||
@@ -0,0 +1,38 @@
|
||||
from argenta import Command, Response, Router
|
||||
from argenta.command import Flag, Flags
|
||||
from argenta.command.flag import ValidationStatus
|
||||
from argenta.response import ResponseStatus
|
||||
|
||||
router = Router(title="Flags Example")
|
||||
|
||||
|
||||
@router.command(
|
||||
Command(
|
||||
"process",
|
||||
description="Process with flags",
|
||||
flags=Flags([
|
||||
Flag("format", possible_values=["json", "xml"]),
|
||||
Flag("verbose")
|
||||
]),
|
||||
)
|
||||
)
|
||||
def process_handler(response: Response):
|
||||
print(f"Status: {response.status}")
|
||||
|
||||
format_flag = response.input_flags.get_flag_by_name("format")
|
||||
verbose_flag = response.input_flags.get_flag_by_name("verbose")
|
||||
|
||||
if format_flag:
|
||||
format_value = format_flag.input_value
|
||||
print(f"Format: {format_value}")
|
||||
|
||||
if verbose_flag:
|
||||
print("Verbose mode enabled")
|
||||
|
||||
if response.status == ResponseStatus.ALL_FLAGS_VALID:
|
||||
print("All flags are valid, proceeding...")
|
||||
elif response.status == ResponseStatus.INVALID_VALUE_FLAGS:
|
||||
print("Warning: Some flags have invalid values")
|
||||
for flag in response.input_flags:
|
||||
if flag.status == ValidationStatus.INVALID:
|
||||
print(f" Invalid flag: {flag.string_entity} = {flag.input_value}")
|
||||
Reference in New Issue
Block a user