# 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](https://github.com/dixon2004/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`:
```bash
pip install tf2schema-py
```
Make sure your environment has the following dependencies installed:
- `httpx`
- `python-dotenv`
- `pytest`
- `pytest-asyncio`
## Usage
Head to the [Examples](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`:
```python
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.
```python
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:
```python
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`:
```python
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`:
```bash
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/Osc44r/tf2schema/",
"name": "tf2schema-py",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "Team Fortress 2, TF2, Steam, Schema, API",
"author": "Osc44r",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ca/ef/218c4406fe22bbb8bb723ce59322fe8d1378d15184820a577a590634b703/tf2schema_py-0.4.5.tar.gz",
"platform": null,
"description": "# tf2schema\n\ntf2schema is a Python package for interacting with the Team Fortress 2 (TF2) Schema. It provides an easy-to-use\n`SchemaManager` class that allows fetching, updating, and managing the TF2 Schema from Steam's API or from a local file.\nThe package includes features such as automatic updates, file-based schema storage, and async support for better\nperformance.\n\nThe library builds on the work from [python-tf2-utilities](https://github.com/dixon2004/python-tf2-utilities) but\nextends it with additional features, including async fetch operations and more Pythonic naming conventions.\n\n## Features\n\n- Fetch the TF2 schema asynchronously from Steam's API or a local file.\n- Automatic schema updates (optional).\n- Pythonic snake_case naming for schema functions.\n- Integration with file-based schema management for environments where file-only mode is preferred.\n- Uses `httpx` for async HTTP requests.\n\n## Installation\n\nYou can install the package using `pip`:\n\n```bash\npip install tf2schema-py\n```\n\nMake sure your environment has the following dependencies installed:\n\n- `httpx`\n- `python-dotenv`\n- `pytest`\n- `pytest-asyncio`\n\n## Usage\n\nHead to the [Examples](examples) directory for a quick start guide on how to use the `SchemaManager` & `Schema` classes.\n\n### Basic Example\n\nBy default, when using the `async with` syntax, the `SchemaManager` will start the auto-update loop. If you prefer not\nto have auto-update enabled, you should manually call `fetch_schema` or `get` to fetch the schema.\n\nHere\u2019s a basic example of how to use the `SchemaManager`:\n\n```python\nimport asyncio\nfrom tf2schema import SchemaManager\nfrom pathlib import Path\n\n\nasync def main():\n steam_api_key = \"YOUR_STEAM_API_KEY\"\n\n async with SchemaManager(\n steam_api_key=steam_api_key,\n file_path=Path(__file__).parent / \"schema.json\",\n save_to_file=True\n ) as manager:\n # Wait until the schema is fetched\n await manager.wait_for_schema()\n\n # Get the name of an item from the schema using its SKU\n sku = \"30911;5;u144\"\n item_name = manager.schema.get_name_from_sku(sku)\n print(f\"Item name for SKU {sku}: {item_name}\")\n # Expected output: \"Item name for SKU 30911;5;u144: Snowblinded Fat Man's Field Cap\"\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n### Disabling Auto-Update\n\nIf you do **not** want auto-update to be enabled, you should avoid using `async with` to create the `SchemaManager`.\nInstead, create an instance and manually fetch the schema.\n\n```python\nimport asyncio\nfrom tf2schema import SchemaManager\nfrom pathlib import Path\n\n\nasync def main():\n steam_api_key = \"YOUR_STEAM_API_KEY\"\n\n # Create the SchemaManager instance\n manager = SchemaManager(\n steam_api_key=steam_api_key,\n file_path=Path(__file__).parent / \"schema.json\",\n save_to_file=True\n )\n\n # Manually fetch the schema from Steam's API or file if it exists\n await manager.get()\n\n # Example: Get the name of an item from the schema using its SKU\n sku = \"160;3;u4\"\n item_name = manager.schema.get_name_from_sku(sku)\n print(f\"Item name for SKU {sku}: {item_name}\")\n # Expected output: \"Item name for SKU 160;3;u4: Vintage Community Sparkle Lugermorph\"\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n### Auto-Updating Schema\n\nThe `SchemaManager` supports an auto-update feature that checks for schema updates at regular intervals. If you want to\nenable the auto-update loop explicitly, you can do so with the `run` method:\n\n```python\nimport asyncio\nfrom tf2schema import SchemaManager\nfrom pathlib import Path\n\n\nasync def main():\n steam_api_key = \"YOUR_STEAM_API_KEY\"\n\n async with SchemaManager(\n steam_api_key=steam_api_key,\n file_path=Path(__file__).parent / \"schema.json\",\n save_to_file=True,\n update_interval=timedelta(hours=12) # Update every 12 hours\n ) as manager:\n # The manager will automatically update the schema in the background\n await manager.wait_for_schema()\n\n # Example: Get the name for another item from the schema using its SKU\n sku = \"817;5;u13\"\n item_name = manager.schema.get_name_from_sku(sku)\n print(f\"Item name for SKU {sku}: {item_name}\")\n # Expected output: \"Item name for SKU 817;5;u13: Burning Flames Human Cannonball\"\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n### File-Only Mode\n\nIf you want to use the package in environments where the schema should only be fetched from a file (e.g., in Docker\ncontainers), you can enable `file_only_mode`:\n\n```python\nimport asyncio\nfrom tf2schema import SchemaManager\nfrom pathlib import Path\n\n\nasync def main():\n async with SchemaManager(\n file_path=Path(__file__).parent / \"schema.json\",\n file_only_mode=True\n ) as manager:\n try:\n await manager.wait_for_schema()\n except FileNotFoundError:\n print(\"Schema file not found. Please make sure it exists.\")\n return\n\n # Example: Get the name of an item from the schema using its SKU\n sku = \"996;6\"\n item_name = manager.schema.get_name_from_sku(sku)\n print(f\"Item name for SKU {sku}: {item_name}\")\n # Expected output: \"Item name for SKU 996;6: The Loose Cannon\"\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n## Running Tests\n\nTo run the tests, you need to set the `STEAM_API_KEY` as an environment variable:\n\n1. Create a `.env` file with your Steam API key:\n\n ```\n STEAM_API_KEY=your_steam_api_key_here\n ```\n\n2. Run the tests using `pytest`:\n\n ```bash\n pytest\n ```\n\nThe tests include checks for schema fetching, conversion from SKU to name, and vice versa.\n\n## Contributing\n\nIf you'd like to contribute to this package, feel free to submit a pull request or open an issue. Contributions are\nalways welcome!\n\n## License\n\nThis project is licensed under the MIT License.\n",
"bugtrack_url": null,
"license": null,
"summary": "A Python package to interact with the Team Fortress 2 Schema",
"version": "0.4.5",
"project_urls": {
"Homepage": "https://github.com/Osc44r/tf2schema/"
},
"split_keywords": [
"team fortress 2",
" tf2",
" steam",
" schema",
" api"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1336c26029718c63858809e22a5c24c2a0d052b41fe248dc3c48d128bdf4b0e0",
"md5": "8263b5d8b8de22e38ef4ef897dee5be8",
"sha256": "b734926aa0cac1319f3dc05f2ea45c6d0317fc2c275abb2c41cb938c8a74d8df"
},
"downloads": -1,
"filename": "tf2schema_py-0.4.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8263b5d8b8de22e38ef4ef897dee5be8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 22106,
"upload_time": "2024-10-25T12:56:21",
"upload_time_iso_8601": "2024-10-25T12:56:21.889924Z",
"url": "https://files.pythonhosted.org/packages/13/36/c26029718c63858809e22a5c24c2a0d052b41fe248dc3c48d128bdf4b0e0/tf2schema_py-0.4.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "caef218c4406fe22bbb8bb723ce59322fe8d1378d15184820a577a590634b703",
"md5": "e9e9b997014a86a14f7e1742156b1b4e",
"sha256": "4422204f2b1af91e2ee84503797bd2bf20a11d354040bdd97b290f348a52c359"
},
"downloads": -1,
"filename": "tf2schema_py-0.4.5.tar.gz",
"has_sig": false,
"md5_digest": "e9e9b997014a86a14f7e1742156b1b4e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 22074,
"upload_time": "2024-10-25T12:56:23",
"upload_time_iso_8601": "2024-10-25T12:56:23.309642Z",
"url": "https://files.pythonhosted.org/packages/ca/ef/218c4406fe22bbb8bb723ce59322fe8d1378d15184820a577a590634b703/tf2schema_py-0.4.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-25 12:56:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Osc44r",
"github_project": "tf2schema",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "tf2schema-py"
}