Skip to main content

A Python package to interact with the Team Fortress 2 Schema

Project description

tf2schema

tf2schema is a Python package for interacting with the Team Fortress 2 (TF2) Schema. It provides an easy-to-use SchemaManager class that allows fetching, updating, and managing the TF2 Schema from Steam's API or from a local file. The package includes features such as automatic updates, file-based schema storage, and async support for better performance.

The library builds on the work from python-tf2-utilities but extends it with additional features, including async fetch operations and more Pythonic naming conventions.

Features

  • Fetch the TF2 schema asynchronously from Steam's API or a local file.
  • Automatic schema updates (optional).
  • Pythonic snake_case naming for schema functions.
  • Integration with file-based schema management for environments where file-only mode is preferred.
  • Uses httpx for async HTTP requests.

Installation

You can install the package using pip:

pip install tf2schema-py

Make sure your environment has the following dependencies installed:

  • httpx
  • python-dotenv
  • pytest
  • pytest-asyncio

Usage

Head to the Examples directory for a quick start guide on how to use the SchemaManager & Schema classes.

Basic Example

By default, when using the async with syntax, the SchemaManager will start the auto-update loop. If you prefer not to have auto-update enabled, you should manually call fetch_schema or get to fetch the schema.

Here’s a basic example of how to use the SchemaManager:

import asyncio
from tf2schema import SchemaManager
from pathlib import Path


async def main():
    steam_api_key = "YOUR_STEAM_API_KEY"

    async with SchemaManager(
            steam_api_key=steam_api_key,
            file_path=Path(__file__).parent / "schema.json",
            save_to_file=True
    ) as manager:
        # Wait until the schema is fetched
        await manager.wait_for_schema()

        # Get the name of an item from the schema using its SKU
        sku = "30911;5;u144"
        item_name = manager.schema.get_name_from_sku(sku)
        print(f"Item name for SKU {sku}: {item_name}")
        # Expected output: "Item name for SKU 30911;5;u144: Snowblinded Fat Man's Field Cap"


if __name__ == "__main__":
    asyncio.run(main())

Disabling Auto-Update

If you do not want auto-update to be enabled, you should avoid using async with to create the SchemaManager. Instead, create an instance and manually fetch the schema.

import asyncio
from tf2schema import SchemaManager
from pathlib import Path


async def main():
    steam_api_key = "YOUR_STEAM_API_KEY"

    # Create the SchemaManager instance
    manager = SchemaManager(
        steam_api_key=steam_api_key,
        file_path=Path(__file__).parent / "schema.json",
        save_to_file=True
    )

    # Manually fetch the schema from Steam's API or file if it exists
    await manager.get()

    # Example: Get the name of an item from the schema using its SKU
    sku = "160;3;u4"
    item_name = manager.schema.get_name_from_sku(sku)
    print(f"Item name for SKU {sku}: {item_name}")
    # Expected output: "Item name for SKU 160;3;u4: Vintage Community Sparkle Lugermorph"


if __name__ == "__main__":
    asyncio.run(main())

Auto-Updating Schema

The SchemaManager supports an auto-update feature that checks for schema updates at regular intervals. If you want to enable the auto-update loop explicitly, you can do so with the run method:

import asyncio
from tf2schema import SchemaManager
from pathlib import Path


async def main():
    steam_api_key = "YOUR_STEAM_API_KEY"

    async with SchemaManager(
            steam_api_key=steam_api_key,
            file_path=Path(__file__).parent / "schema.json",
            save_to_file=True,
            update_interval=timedelta(hours=12)  # Update every 12 hours
    ) as manager:
        # The manager will automatically update the schema in the background
        await manager.wait_for_schema()

        # Example: Get the name for another item from the schema using its SKU
        sku = "817;5;u13"
        item_name = manager.schema.get_name_from_sku(sku)
        print(f"Item name for SKU {sku}: {item_name}")
        # Expected output: "Item name for SKU 817;5;u13: Burning Flames Human Cannonball"


if __name__ == "__main__":
    asyncio.run(main())

File-Only Mode

If you want to use the package in environments where the schema should only be fetched from a file (e.g., in Docker containers), you can enable file_only_mode:

import asyncio
from tf2schema import SchemaManager
from pathlib import Path


async def main():
    async with SchemaManager(
            file_path=Path(__file__).parent / "schema.json",
            file_only_mode=True
    ) as manager:
        try:
            await manager.wait_for_schema()
        except FileNotFoundError:
            print("Schema file not found. Please make sure it exists.")
            return

        # Example: Get the name of an item from the schema using its SKU
        sku = "996;6"
        item_name = manager.schema.get_name_from_sku(sku)
        print(f"Item name for SKU {sku}: {item_name}")
        # Expected output: "Item name for SKU 996;6: The Loose Cannon"


if __name__ == "__main__":
    asyncio.run(main())

Running Tests

To run the tests, you need to set the STEAM_API_KEY as an environment variable:

  1. Create a .env file with your Steam API key:

    STEAM_API_KEY=your_steam_api_key_here
    
  2. Run the tests using pytest:

    pytest
    

The tests include checks for schema fetching, conversion from SKU to name, and vice versa.

Contributing

If you'd like to contribute to this package, feel free to submit a pull request or open an issue. Contributions are always welcome!

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tf2schema_py-0.4.7.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tf2schema_py-0.4.7-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file tf2schema_py-0.4.7.tar.gz.

File metadata

  • Download URL: tf2schema_py-0.4.7.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for tf2schema_py-0.4.7.tar.gz
Algorithm Hash digest
SHA256 18c5261a501e5dc31cb8fb5604955ce09e2c05ec77127199b68c556a9ac80f25
MD5 4db4963e0597b85fecefa013c795cb2a
BLAKE2b-256 ece4713b8f9111b52e34b6b5fd0296435f2aee7221ce9a35c22dd9481da219ed

See more details on using hashes here.

File details

Details for the file tf2schema_py-0.4.7-py3-none-any.whl.

File metadata

  • Download URL: tf2schema_py-0.4.7-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for tf2schema_py-0.4.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d38aa8a305bd12bdf6fa167522704f307757a0e18f83c053f8aad07c745ab5ed
MD5 f872c342644636aab0433f3550662675
BLAKE2b-256 06a225020617e314cabac155dcaca4a91a417d495ec8dcb91f699f4cf06f1e35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page