added windows interaction test
adding more variant stuff
This commit is contained in:
@@ -35,19 +35,39 @@ class VariantManager:
|
||||
"""Load variants from JSON file"""
|
||||
if self.variants_file.exists():
|
||||
with open(self.variants_file, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
data = json.load(f)
|
||||
|
||||
# Migrate from version 2 to version 3
|
||||
if data.get("meta", {}).get("version", 1) < 3:
|
||||
print(f"Migrating variant file from version {data.get('meta', {}).get('version', 1)} to 3")
|
||||
if "base_values" not in data:
|
||||
data["base_values"] = {}
|
||||
for variant in data.get("variants", {}).values():
|
||||
if "part_overrides" not in variant:
|
||||
variant["part_overrides"] = {}
|
||||
data["meta"]["version"] = 3
|
||||
# Save migrated version
|
||||
with open(self.variants_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
return data
|
||||
else:
|
||||
# Default structure
|
||||
return {
|
||||
"meta": {
|
||||
"version": 2, # Version 2 uses UUIDs instead of references
|
||||
"version": 3, # Version 3 adds base_values and part_overrides
|
||||
"active_variant": "default"
|
||||
},
|
||||
"base_values": {
|
||||
# UUID -> {property_name: value}
|
||||
# Stores the base/default values for parts that vary between variants
|
||||
},
|
||||
"variants": {
|
||||
"default": {
|
||||
"name": "default",
|
||||
"description": "Default variant - all parts fitted",
|
||||
"dnp_parts": [] # List of UUIDs that are DNP (Do Not Place)
|
||||
"dnp_parts": [], # List of UUIDs that are DNP (Do Not Place)
|
||||
"part_overrides": {} # UUID -> {property_name: override_value}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,6 +214,88 @@ class VariantManager:
|
||||
|
||||
return uuid in self.variants["variants"][variant_name]["dnp_parts"]
|
||||
|
||||
def get_part_properties(self, variant_name: str, uuid: str) -> Dict[str, str]:
|
||||
"""
|
||||
Get effective property values for a part in a variant.
|
||||
Merges base_values with variant-specific overrides.
|
||||
|
||||
Args:
|
||||
variant_name: Variant name
|
||||
uuid: Component UUID
|
||||
|
||||
Returns:
|
||||
Dict of property_name -> value (merged base + overrides)
|
||||
"""
|
||||
if variant_name not in self.variants["variants"]:
|
||||
return {}
|
||||
|
||||
# Start with base values
|
||||
properties = self.variants.get("base_values", {}).get(uuid, {}).copy()
|
||||
|
||||
# Apply variant overrides
|
||||
overrides = self.variants["variants"][variant_name].get("part_overrides", {}).get(uuid, {})
|
||||
properties.update(overrides)
|
||||
|
||||
return properties
|
||||
|
||||
def set_part_property(self, variant_name: str, uuid: str, property_name: str, value: str) -> bool:
|
||||
"""
|
||||
Set a property override for a part in a variant.
|
||||
|
||||
Args:
|
||||
variant_name: Variant name
|
||||
uuid: Component UUID
|
||||
property_name: Property to set (e.g., "Value", "MPN")
|
||||
value: New value
|
||||
|
||||
Returns:
|
||||
True if successful, False if variant doesn't exist
|
||||
"""
|
||||
if variant_name not in self.variants["variants"]:
|
||||
return False
|
||||
|
||||
# Ensure part_overrides exists for this variant
|
||||
if "part_overrides" not in self.variants["variants"][variant_name]:
|
||||
self.variants["variants"][variant_name]["part_overrides"] = {}
|
||||
|
||||
# Get or create override dict for this UUID
|
||||
if uuid not in self.variants["variants"][variant_name]["part_overrides"]:
|
||||
self.variants["variants"][variant_name]["part_overrides"][uuid] = {}
|
||||
|
||||
# Check if this matches base value - if so, remove override (keep it sparse)
|
||||
base_value = self.variants.get("base_values", {}).get(uuid, {}).get(property_name)
|
||||
if value == base_value:
|
||||
# Remove from overrides since it matches base
|
||||
if property_name in self.variants["variants"][variant_name]["part_overrides"][uuid]:
|
||||
del self.variants["variants"][variant_name]["part_overrides"][uuid][property_name]
|
||||
# Clean up empty override dicts
|
||||
if not self.variants["variants"][variant_name]["part_overrides"][uuid]:
|
||||
del self.variants["variants"][variant_name]["part_overrides"][uuid]
|
||||
else:
|
||||
# Store override
|
||||
self.variants["variants"][variant_name]["part_overrides"][uuid][property_name] = value
|
||||
|
||||
self._save_variants()
|
||||
return True
|
||||
|
||||
def set_base_value(self, uuid: str, property_name: str, value: str):
|
||||
"""
|
||||
Set a base property value for a part.
|
||||
|
||||
Args:
|
||||
uuid: Component UUID
|
||||
property_name: Property name
|
||||
value: Value to set
|
||||
"""
|
||||
if "base_values" not in self.variants:
|
||||
self.variants["base_values"] = {}
|
||||
|
||||
if uuid not in self.variants["base_values"]:
|
||||
self.variants["base_values"][uuid] = {}
|
||||
|
||||
self.variants["base_values"][uuid][property_name] = value
|
||||
self._save_variants()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
|
||||
Reference in New Issue
Block a user