Last active
July 14, 2025 14:12
-
-
Save abits/b10f7fb5d37761254db195233a57a4a9 to your computer and use it in GitHub Desktop.
Demo for ArgParse and YML in Python and Zenity dialogs in Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Demo - zeigt die wichtigsten Features von argparse, sowie | |
| Konfigurationsdateien | |
| """ | |
| import argparse | |
| import sys | |
| import yaml | |
| def parse_arguments(): | |
| # Parser erstellen | |
| parser = argparse.ArgumentParser( | |
| description="Demonstriert die wichtigsten argparse Features", | |
| epilog="Beispiel: %(prog)s datei.txt -v --count 5 --format json", | |
| ) | |
| # Positionales Argument (erforderlich) | |
| parser.add_argument("filename", help="Dateiname zum Verarbeiten") | |
| # Optionales Argument mit Standardwert | |
| parser.add_argument( | |
| "-c", | |
| "--count", | |
| type=int, | |
| default=1, | |
| help="Anzahl der Wiederholungen (Standard: 1)", | |
| ) | |
| # Boolean Flag (store_true) | |
| parser.add_argument( | |
| "-v", "--verbose", action="store_true", help="Ausführliche Ausgabe aktivieren" | |
| ) | |
| # Argument mit Auswahlmöglichkeiten | |
| parser.add_argument( | |
| "--format", | |
| choices=["json", "xml", "csv"], | |
| default="json", | |
| help="Ausgabeformat (Standard: json)", | |
| ) | |
| # Argumente parsen | |
| args = parser.parse_args() | |
| # Ausgabe der geparsten Argumente | |
| print(f"Dateiname: {args.filename}") | |
| print(f"Count: {args.count}") | |
| print(f"Verbose: {args.verbose}") | |
| print(f"Format: {args.format}") | |
| # Verhalten basierend auf Argumenten | |
| if args.verbose: | |
| print("Ausführliche Ausgabe aktiviert") | |
| # Simulation der Verarbeitung | |
| for i in range(args.count): | |
| print(f"Verarbeitung {i+1}/{args.count}: {args.filename}") | |
| def create_demo_yaml(filename="demo_config.yml"): | |
| """Erstellt eine YAML-Datei mit Demo-Werten""" | |
| demo_data = { | |
| "application": { | |
| "name": "Demo App", | |
| "version": "1.2.3", | |
| "description": "Eine Beispielanwendung für YAML-Demonstration", | |
| "author": "Max Mustermann", | |
| }, | |
| } | |
| with open(filename, "w", encoding="utf-8") as file: | |
| yaml.dump( | |
| demo_data, file, default_flow_style=False, allow_unicode=True, indent=2 | |
| ) | |
| print(f"YAML-Datei '{filename}' erfolgreich erstellt!") | |
| def demonstrate_yaml_access(filename): | |
| """Demonstriert verschiedene Zugriffsmethoden auf YAML-Daten""" | |
| with open(filename, "r", encoding="utf-8") as file: | |
| data = yaml.safe_load(file) | |
| print("\n Beispiele für Datenzugriff:") | |
| print("=" * 40) | |
| # Einfacher Zugriff | |
| print(f"App Name: {data['application']['name']}") | |
| def main(): | |
| parse_arguments() | |
| filename = "demo_config.yml" | |
| create_demo_yaml(filename) | |
| demonstrate_yaml_access(filename) | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Zenity Demo Script - Demonstriert die wichtigsten zenity Features | |
| # sudo pacman -S zenity | |
| # Temporäre Dateien | |
| TEMP_FILE=$(mktemp) | |
| RESULT_FILE=$(mktemp) | |
| # Cleanup-Funktion | |
| cleanup() { | |
| rm -f "$TEMP_FILE" "$RESULT_FILE" | |
| } | |
| # Cleanup bei Script-Ende | |
| trap cleanup EXIT | |
| # Hauptmenü | |
| show_main_menu() { | |
| while true; do | |
| choice=$(zenity --list --title="Zenity Demo" \ | |
| --text="Wählen Sie eine Demo:" \ | |
| --column="Nr" --column="Beschreibung" \ | |
| --width=500 --height=400 \ | |
| "1" "Einfache Nachricht (info)" \ | |
| "2" "Warnung (warning)" \ | |
| "3" "Fehler (error)" \ | |
| "4" "Ja/Nein Frage (question)" \ | |
| "5" "Texteingabe (entry)" \ | |
| "6" "Passwort-Eingabe (password)" \ | |
| "7" "Datei öffnen (file-selection)" \ | |
| "8" "Datei speichern (save)" \ | |
| "9" "Ordner auswählen (directory)" \ | |
| "10" "Farbe auswählen (color-selection)" \ | |
| "11" "Fortschrittsbalken (progress)" \ | |
| "12" "Auswahlliste (list)" \ | |
| "13" "Texteditor (text-info)" \ | |
| "14" "Kalender (calendar)" \ | |
| "15" "Skalierung (scale)" \ | |
| "16" "Formulare (forms)" \ | |
| "17" "Benachrichtigung (notification)" \ | |
| "0" "Beenden") | |
| case $choice in | |
| 1) demo_info ;; | |
| 2) demo_warning ;; | |
| 3) demo_error ;; | |
| 4) demo_question ;; | |
| 5) demo_entry ;; | |
| 6) demo_password ;; | |
| 7) demo_file_open ;; | |
| 8) demo_file_save ;; | |
| 9) demo_directory ;; | |
| 10) demo_color ;; | |
| 11) demo_progress ;; | |
| 12) demo_list ;; | |
| 13) demo_text ;; | |
| 14) demo_calendar ;; | |
| 15) demo_scale ;; | |
| 16) demo_forms ;; | |
| 17) demo_notification ;; | |
| 0 | "") exit 0 ;; | |
| *) | |
| zenity --error --text="Ungültige Auswahl!" | |
| ;; | |
| esac | |
| done | |
| } | |
| # Demo 1: Info-Dialog | |
| demo_info() { | |
| zenity --info --title="Information" \ | |
| --text="Dies ist eine einfache Informationsmeldung.\n\nZenity erstellt schöne GTK-Dialoge!" \ | |
| --width=300 | |
| } | |
| # Demo 2: Warnung | |
| demo_warning() { | |
| zenity --warning --title="Warnung" \ | |
| --text="Dies ist eine Warnung!\n\nSeien Sie vorsichtig mit dieser Aktion." \ | |
| --width=300 | |
| } | |
| # Demo 3: Fehler | |
| demo_error() { | |
| zenity --error --title="Fehler" \ | |
| --text="Ein Fehler ist aufgetreten!\n\nÜberprüfen Sie Ihre Eingaben." \ | |
| --width=300 | |
| } | |
| # Demo 4: Ja/Nein Frage | |
| demo_question() { | |
| if zenity --question --title="Bestätigung" \ | |
| --text="Möchten Sie wirklich fortfahren?\n\nDiese Aktion kann nicht rückgängig gemacht werden." \ | |
| --width=350; then | |
| zenity --info --text="Sie haben JA gewählt!" | |
| else | |
| zenity --info --text="Sie haben NEIN gewählt!" | |
| fi | |
| } | |
| # Demo 5: Texteingabe | |
| demo_entry() { | |
| name=$(zenity --entry --title="Texteingabe" \ | |
| --text="Geben Sie Ihren Namen ein:" \ | |
| --entry-text="Max Mustermann" \ | |
| --width=300) | |
| if [ $? -eq 0 ] && [ -n "$name" ]; then | |
| zenity --info --text="Hallo $name!\n\nSchön Sie kennenzulernen." --width=300 | |
| fi | |
| } | |
| # Demo 6: Passwort-Eingabe | |
| demo_password() { | |
| password=$(zenity --password --title="Passwort" \ | |
| --text="Geben Sie Ihr Passwort ein:") | |
| if [ $? -eq 0 ] && [ -n "$password" ]; then | |
| zenity --info --text="Passwort erhalten!\nLänge: ${#password} Zeichen" --width=300 | |
| fi | |
| } | |
| # Demo 7: Datei öffnen | |
| demo_file_open() { | |
| file=$(zenity --file-selection --title="Datei öffnen" \ | |
| --file-filter="Textdateien | *.txt *.log" \ | |
| --file-filter="Alle Dateien | *") | |
| if [ $? -eq 0 ] && [ -n "$file" ]; then | |
| zenity --info --text="Datei ausgewählt:\n$file\n\nGröße: $(stat -c%s "$file" 2>/dev/null || echo "Unbekannt") Bytes" \ | |
| --width=400 | |
| fi | |
| } | |
| # Demo 8: Datei speichern | |
| demo_file_save() { | |
| file=$(zenity --file-selection --save --title="Datei speichern" \ | |
| --filename="beispiel.txt" \ | |
| --file-filter="Textdateien | *.txt") | |
| if [ $? -eq 0 ] && [ -n "$file" ]; then | |
| echo "Dies ist ein Beispieltext aus dem Zenity-Demo." >"$file" | |
| zenity --info --text="Datei gespeichert:\n$file" --width=400 | |
| fi | |
| } | |
| # Demo 9: Ordner auswählen | |
| demo_directory() { | |
| dir=$(zenity --file-selection --directory --title="Ordner auswählen") | |
| if [ $? -eq 0 ] && [ -n "$dir" ]; then | |
| file_count=$(find "$dir" -maxdepth 1 -type f | wc -l) | |
| zenity --info --text="Ordner ausgewählt:\n$dir\n\nEnthält $file_count Dateien" --width=400 | |
| fi | |
| } | |
| # Demo 10: Farbe auswählen | |
| demo_color() { | |
| color=$(zenity --color-selection --title="Farbe auswählen" \ | |
| --color='#FF0000') | |
| if [ $? -eq 0 ] && [ -n "$color" ]; then | |
| zenity --info --text="Farbe ausgewählt:\n$color" --width=300 | |
| fi | |
| } | |
| # Demo 11: Fortschrittsbalken | |
| demo_progress() { | |
| { | |
| echo "10" | |
| sleep 1 | |
| echo "# Initialisierung..." | |
| echo "25" | |
| sleep 1 | |
| echo "# Lade Daten..." | |
| echo "50" | |
| sleep 1 | |
| echo "# Verarbeite Daten..." | |
| echo "75" | |
| sleep 1 | |
| echo "# Speichere Ergebnisse..." | |
| echo "100" | |
| sleep 1 | |
| echo "# Fertig!" | |
| } | zenity --progress --title="Fortschritt" \ | |
| --text="Verarbeitung läuft..." \ | |
| --percentage=0 \ | |
| --width=400 | |
| } | |
| # Demo 12: Auswahlliste | |
| demo_list() { | |
| choice=$(zenity --list --title="Programmiersprachen" \ | |
| --text="Wählen Sie Ihre Lieblings-Programmiersprache:" \ | |
| --column="Sprache" --column="Beschreibung" \ | |
| --width=500 --height=300 \ | |
| "Python" "Einfach und vielseitig" \ | |
| "JavaScript" "Für Web-Entwicklung" \ | |
| "Java" "Enterprise-Anwendungen" \ | |
| "C++" "Systemnahe Programmierung" \ | |
| "Go" "Moderne Sprache von Google" \ | |
| "Rust" "Sicherheit und Performance" \ | |
| "PHP" "Web-Backend" \ | |
| "Ruby" "Elegante Syntax") | |
| if [ $? -eq 0 ] && [ -n "$choice" ]; then | |
| zenity --info --text="Ihre Wahl: $choice" --width=300 | |
| fi | |
| } | |
| # Demo 13: Texteditor/Textanzeige | |
| demo_text() { | |
| # Erstelle Beispieltext | |
| cat >"$TEMP_FILE" <<'EOF' | |
| Dies ist eine Demonstration der zenity --text-info Funktion. | |
| Zenity kann längere Texte in einem scrollbaren Fenster anzeigen. | |
| Funktionen: | |
| - Scrollbare Anzeige | |
| - Checkbox zum Editieren | |
| - Schriftart-Auswahl | |
| - Suche im Text (Strg+F) | |
| Sie können diesen Text bearbeiten, wenn Sie die --editable Option verwenden. | |
| Das ist eine längere Zeile, die zeigt, wie zenity mit Text umgeht, der über die Fenstergröße hinausgeht und automatisch umgebrochen wird. | |
| Ende der Beispieldatei. | |
| EOF | |
| if zenity --text-info --title="Textanzeige" \ | |
| --filename="$TEMP_FILE" \ | |
| --width=600 --height=400 \ | |
| --checkbox="Ich habe den Text gelesen"; then | |
| zenity --info --text="Danke fürs Lesen!" --width=300 | |
| fi | |
| } | |
| # Demo 14: Kalender | |
| demo_calendar() { | |
| date=$(zenity --calendar --title="Datum auswählen" \ | |
| --text="Wählen Sie ein Datum:" \ | |
| --date-format="%Y-%m-%d") | |
| if [ $? -eq 0 ] && [ -n "$date" ]; then | |
| zenity --info --text="Ausgewähltes Datum:\n$date" --width=300 | |
| fi | |
| } | |
| # Demo 15: Skalierung (Slider) | |
| demo_scale() { | |
| value=$(zenity --scale --title="Lautstärke" \ | |
| --text="Wählen Sie die Lautstärke:" \ | |
| --min-value=0 --max-value=100 \ | |
| --value=50 --step=5) | |
| if [ $? -eq 0 ] && [ -n "$value" ]; then | |
| zenity --info --text="Lautstärke eingestellt auf: $value%" --width=300 | |
| fi | |
| } | |
| # Demo 16: Formulare | |
| demo_forms() { | |
| result=$(zenity --forms --title="Benutzer-Formular" \ | |
| --text="Geben Sie Ihre Daten ein:" \ | |
| --add-entry="Vorname" \ | |
| --add-entry="Nachname" \ | |
| --add-entry="Email" \ | |
| --add-entry="Telefon" \ | |
| --add-combo="Land" --combo-values="Deutschland|Österreich|Schweiz|Andere" \ | |
| --add-calendar="Geburtsdatum" \ | |
| --separator="|" \ | |
| --width=400) | |
| if [ $? -eq 0 ] && [ -n "$result" ]; then | |
| # Ergebnis formatieren | |
| IFS='|' read -r vorname nachname email telefon land geburt <<<"$result" | |
| formatted_result="Formular-Ergebnis:\n\n" | |
| formatted_result+="Vorname: $vorname\n" | |
| formatted_result+="Nachname: $nachname\n" | |
| formatted_result+="Email: $email\n" | |
| formatted_result+="Telefon: $telefon\n" | |
| formatted_result+="Land: $land\n" | |
| formatted_result+="Geburtsdatum: $geburt" | |
| zenity --info --text="$formatted_result" --width=400 | |
| fi | |
| } | |
| # Demo 17: Benachrichtigung | |
| demo_notification() { | |
| zenity --notification --text="Dies ist eine Desktop-Benachrichtigung!" \ | |
| --window-icon="info" | |
| sleep 2 | |
| zenity --info --text="Benachrichtigung wurde gesendet!\n\nSie sollten sie in der Systemleiste gesehen haben." \ | |
| --width=350 | |
| } | |
| # Überprüfung ob zenity installiert ist | |
| if ! command -v zenity &>/dev/null; then | |
| echo "Fehler: 'zenity' ist nicht installiert!" | |
| echo "Installieren Sie es mit:" | |
| echo " Ubuntu/Debian: sudo apt-get install zenity" | |
| echo " CentOS/RHEL: sudo yum install zenity" | |
| echo " Arch Linux: sudo pacman -S zenity" | |
| exit 1 | |
| fi | |
| # Überprüfung ob X11/Wayland läuft | |
| if [ -z "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ]; then | |
| echo "Fehler: Keine grafische Umgebung erkannt!" | |
| echo "Zenity benötigt eine laufende X11- oder Wayland-Sitzung." | |
| exit 1 | |
| fi | |
| # Willkommensnachricht | |
| zenity --info --title="Willkommen" \ | |
| --text="Willkommen zur Zenity Demo!\n\nDieses Script demonstriert verschiedene zenity-Features für grafische Dialoge in Bash-Scripts." \ | |
| --width=400 | |
| # Hauptmenü starten | |
| show_main_menu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment