at dantic 3.6 kB view raw
1"""Basic usage examples for ATProto data and lexicon modules.""" 2 3import json 4from atpasser.data import serializer, CIDLink, DateTimeString 5from atpasser.lexicon import parser, registry 6 7 8def demonstrate_data_serialization(): 9 """Demonstrate basic data serialization.""" 10 print("=== Data Serialization Demo ===") 11 12 # Create some sample data 13 sample_data = { 14 "title": "Hello ATProto", 15 "content": "This is a test post", 16 "createdAt": "2024-01-15T10:30:00.000Z", 17 "tags": ["atproto", "test", "demo"], 18 "cidLink": CIDLink( 19 "bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a" 20 ), 21 } 22 23 # Serialize to JSON 24 json_output = serializer.to_json(sample_data, indent=2) 25 print("JSON Output:") 26 print(json_output) 27 28 # Deserialize back 29 deserialized = serializer.from_json(json_output) 30 print("\nDeserialized:") 31 print(deserialized) 32 33 print() 34 35 36def demonstrate_lexicon_parsing(): 37 """Demonstrate Lexicon parsing.""" 38 print("=== Lexicon Parsing Demo ===") 39 40 # Sample Lexicon definition 41 sample_lexicon = { 42 "lexicon": 1, 43 "id": "com.example.blog.post", 44 "description": "A simple blog post record", 45 "defs": { 46 "main": { 47 "type": "record", 48 "key": "literal:post", 49 "record": { 50 "type": "object", 51 "properties": { 52 "title": {"type": "string", "maxLength": 300}, 53 "content": {"type": "string"}, 54 "createdAt": {"type": "string", "format": "datetime"}, 55 "tags": { 56 "type": "array", 57 "items": {"type": "string"}, 58 "maxLength": 10, 59 }, 60 }, 61 "required": ["title", "content", "createdAt"], 62 }, 63 } 64 }, 65 } 66 67 try: 68 # Parse and register the Lexicon 69 parser.parse_and_register(sample_lexicon) 70 print("Lexicon parsed and registered successfully!") 71 72 # Get the generated model 73 model_class = registry.get_model("com.example.blog.post") 74 if model_class: 75 print(f"Generated model: {model_class.__name__}") 76 77 # Create an instance using the model 78 post_data = { 79 "title": "Test Post", 80 "content": "This is a test post content", 81 "createdAt": "2024-01-15T10:30:00.000Z", 82 "tags": ["test", "demo"], 83 } 84 85 validated_post = model_class(**post_data) 86 print(f"Validated post: {validated_post.model_dump()}") 87 88 except Exception as e: 89 print(f"Error: {e}") 90 91 print() 92 93 94def demonstrate_custom_types(): 95 """Demonstrate custom type validation.""" 96 print("=== Custom Type Validation Demo ===") 97 98 # DateTimeString validation 99 try: 100 valid_dt = DateTimeString("2024-01-15T10:30:00.000Z") 101 print(f"Valid datetime: {valid_dt}") 102 except Exception as e: 103 print(f"DateTime validation error: {e}") 104 105 # Invalid datetime 106 try: 107 invalid_dt = DateTimeString("invalid-date") 108 print(f"Invalid datetime: {invalid_dt}") 109 except Exception as e: 110 print(f"DateTime validation caught: {e}") 111 112 print() 113 114 115if __name__ == "__main__": 116 demonstrate_data_serialization() 117 demonstrate_lexicon_parsing() 118 demonstrate_custom_types() 119 print("Demo completed!")