Python

Parsing

Overview

The parsing module in ducpy provides functions to read and parse duc files into Python objects. These functions convert the FlatBuffers binary format into structured Python objects that you can easily work with in your code.

Main Functions

Parse Duc File

The main function for parsing a complete duc file:

from ducpy import parse
 
# Open a duc file
with open('example.duc', 'rb') as f:
    # Parse the entire file
    duc_data = parse.parse_duc_flatbuffers(f)
    
    # Access the parsed data
    elements = duc_data['elements']  # List of DucElement objects
    app_state = duc_data['appState']  # AppState object
    files = duc_data['files']  # BinaryFiles object
    file_type = duc_data['type']  # File type string
    version = duc_data['version']  # File version number
    source = duc_data['source']  # Source string

Parse Individual Components

You can also parse individual components of a duc file:

Parse Duc Elements

from ducpy.parse import parse_duc_element
 
# Parse a single element from the file
element = parse_duc_element(data.Elements(0))

Parse App State

from ducpy.parse import parse_app_state
 
# Parse the application state
app_state = parse_app_state(data.AppState())

Parse Binary Files

from ducpy.parse import parse_binary_files
 
# Parse binary files included in the duc file
files = parse_binary_files(data.Files())

Returned Data Structure

When parsing a duc file, you get a dictionary with the following keys:

  • elements: A list of DucElementUnion objects representing all elements in the file
  • appState: An AppState object containing application state information
  • files: A BinaryFiles object containing any binary files embedded in the duc file
  • type: The file type string
  • version: The file version number
  • source: The source of the file

Element Types

The duc format supports several element types, each with specific properties:

  • Text elements
  • Line elements
  • Freedraw elements
  • Image elements
  • Frame elements
  • Group elements
  • Magic frame elements
  • Iframe elements

Each element type is parsed into a corresponding Python object with appropriate properties.

Edit on GitHub

Last updated on