testdata imgs

This commit is contained in:
j3d1 2026-09-05 16:23:36 +02:00
parent 197f173097
commit 5cd3ae5a13
35 changed files with 47 additions and 6 deletions

View file

@ -8,6 +8,7 @@ who are friends with each other.
import csv
import io
import json
import mimetypes
import os
import random
import zipfile
@ -15,6 +16,9 @@ from hashlib import sha256
from nacl.signing import SigningKey
from nacl.encoding import HexEncoder
IMGS_DIR = os.path.join(os.path.dirname(__file__), 'imgs')
IMAGE_ATTACHMENT_PROBABILITY = 0.9
# Realistic item database with shared_data category/tag references using fully qualified handles
# Format: (name, category_handle, tags_handles, policy, description, qty, properties_list)
@ -250,6 +254,21 @@ def load_shared_data():
return all_tags, all_categories, all_properties
def load_available_images():
"""Read every file in imgs/ and return a list of (data, mime_type) tuples."""
images = []
for filename in sorted(os.listdir(IMGS_DIR)):
filepath = os.path.join(IMGS_DIR, filename)
if not os.path.isfile(filepath):
continue
mime_type, _ = mimetypes.guess_type(filename)
if not mime_type:
continue
with open(filepath, 'rb') as f:
images.append((f.read(), mime_type))
return images
def generate_keypair():
"""Generate a signing key pair and return (hex_private_key, hex_public_key)."""
signing_key = SigningKey.generate()
@ -341,7 +360,7 @@ def _quote_value_if_needed(value):
return value
def generate_inventory_csv(count, num_locations, location_id_map, item_range=None):
def generate_inventory_csv(count, num_locations, location_id_map, item_range=None, available_images=None):
"""Generate a realistic inventory.csv with varied items using shared_data references and properties.
Args:
@ -349,6 +368,12 @@ def generate_inventory_csv(count, num_locations, location_id_map, item_range=Non
num_locations: Number of locations
location_id_map: Map of location names to IDs
item_range: Tuple of (start_idx, end_idx) to partition the ITEMS_DATABASE, or None for all items
available_images: List of (data, mime_type) tuples to randomly attach to items, or None to skip
Returns:
Tuple of (inventory_csv_bytes, files) where files maps a zip arcname (matching
`toolshed.offlinedata.inventory_files()`'s 'files/<hash><ext>' convention) to file bytes,
for every image actually referenced by an item.
"""
# Get location names from the map
location_names = list(location_id_map.keys())
@ -365,6 +390,7 @@ def generate_inventory_csv(count, num_locations, location_id_map, item_range=Non
selected_items = random.sample(available_items, min(count, len(available_items)))
rows = []
files = {}
for i, item_tuple in enumerate(selected_items, 1):
# Handle both old format (6 elements) and new format (7 elements with properties)
if len(item_tuple) == 7:
@ -382,6 +408,16 @@ def generate_inventory_csv(count, num_locations, location_id_map, item_range=Non
properties_str = ', '.join(
f"{prop_handle}={_quote_value_if_needed(value)}" for prop_handle, value in properties_list)
# Attach a random image to most items, matching `toolshed.offlinedata.inventory_files()`'s
# 'files/<hash><ext>' arcname convention so the import side resolves it as the same file.
files_str = ''
if available_images and random.random() < IMAGE_ATTACHMENT_PROBABILITY:
data, mime_type = random.choice(available_images)
extension = mimetypes.guess_extension(mime_type) or ''
arcname = f'files/{sha256(data).hexdigest()}{extension}'
files[arcname] = data
files_str = arcname
rows.append({
'id': str(i),
'name': name,
@ -393,7 +429,7 @@ def generate_inventory_csv(count, num_locations, location_id_map, item_range=Non
'storage_location': location,
'tags': ', '.join(tags_for_item),
'properties': properties_str,
'files': '',
'files': files_str,
'created_at': '2026-08-01T00:00:00+00:00',
})
@ -403,7 +439,7 @@ def generate_inventory_csv(count, num_locations, location_id_map, item_range=Non
writer = csv.DictWriter(output, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
return output.getvalue().encode('utf-8')
return output.getvalue().encode('utf-8'), files
def generate_sample_files():
@ -447,9 +483,14 @@ def generate_export_zip(username, domain, friend_username, friend_domain, friend
friends_csv = generate_friends_csv(friend_username, friend_domain, friend_public_key)
zip_file.writestr('friends.csv', friends_csv)
inventory_csv = generate_inventory_csv(num_items, num_locations, location_id_map, item_range)
available_images = load_available_images()
inventory_csv, item_files = generate_inventory_csv(
num_items, num_locations, location_id_map, item_range, available_images)
zip_file.writestr('inventory.csv', inventory_csv)
for arcname, content in item_files.items():
zip_file.writestr(arcname, content)
sample_files = generate_sample_files()
for arcname, content in sample_files.items():
zip_file.writestr(arcname, content)