Created
June 1, 2025 22:08
-
-
Save chadbaldwin/7fd56eb8f54f9027a008c46f42ed337b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from typing import Dict, List, Optional, Union | |
from dataclasses import dataclass | |
@dataclass | |
class BoxImport: | |
"""Represents a single import statement within box::use()""" | |
module: str | |
alias: Optional[str] = None | |
functions: Optional[List[str]] = None | |
is_path: bool = False | |
@dataclass | |
class BoxUseStatement: | |
"""Represents the entire box::use() statement""" | |
imports: List[BoxImport] | |
def parse_box_use(box_use_string: str) -> BoxUseStatement: | |
""" | |
Parse a box::use() statement string into a structured object. | |
Args: | |
box_use_string: The box::use() statement as a string | |
Returns: | |
BoxUseStatement object containing parsed imports | |
""" | |
# Remove box::use( and closing ) | |
content = box_use_string.strip() | |
if not content.startswith('box::use('): | |
raise ValueError("String must start with 'box::use('") | |
# Extract content between parentheses | |
start_idx = content.find('(') + 1 | |
end_idx = content.rfind(')') | |
if end_idx == -1: | |
raise ValueError("Missing closing parenthesis") | |
inner_content = content[start_idx:end_idx].strip() | |
# Split by commas, but be careful about commas inside brackets | |
imports = [] | |
current_import = "" | |
bracket_depth = 0 | |
for char in inner_content: | |
if char == '[': | |
bracket_depth += 1 | |
elif char == ']': | |
bracket_depth -= 1 | |
elif char == ',' and bracket_depth == 0: | |
if current_import.strip(): | |
imports.append(current_import.strip()) | |
current_import = "" | |
continue | |
current_import += char | |
# Add the last import if it exists | |
if current_import.strip(): | |
imports.append(current_import.strip()) | |
# Parse each import | |
parsed_imports = [] | |
for import_str in imports: | |
parsed_import = _parse_single_import(import_str) | |
if parsed_import: # Skip empty imports | |
parsed_imports.append(parsed_import) | |
return BoxUseStatement(imports=parsed_imports) | |
def _parse_single_import(import_str: str) -> Optional[BoxImport]: | |
"""Parse a single import statement""" | |
import_str = import_str.strip() | |
if not import_str: | |
return None | |
# Check if it's an aliased import (contains =) | |
if '=' in import_str: | |
alias, module = import_str.split('=', 1) | |
alias = alias.strip() | |
module = module.strip() | |
is_path = '/' in module | |
return BoxImport(module=module, alias=alias, is_path=is_path) | |
# Check if it has function specifications in brackets | |
if '[' in import_str and ']' in import_str: | |
# Extract module name and functions | |
bracket_start = import_str.find('[') | |
module = import_str[:bracket_start].strip() | |
# Extract functions from brackets | |
bracket_end = import_str.rfind(']') | |
functions_str = import_str[bracket_start + 1:bracket_end] | |
# Parse functions, handling spaces and commas | |
functions = [] | |
for func in functions_str.split(','): | |
func = func.strip() | |
if func: # Skip empty strings | |
functions.append(func) | |
is_path = '/' in module | |
return BoxImport(module=module, functions=functions, is_path=is_path) | |
# Simple module import | |
module = import_str.strip() | |
is_path = '/' in module | |
return BoxImport(module=module, is_path=is_path) | |
# Example usage and test | |
if __name__ == "__main__": | |
test_string = """box::use( | |
DBI[dbListTables, dbExecute], | |
Yessir[this_one, that one, | |
and_this_one], | |
Maybesir[ | |
func_one, | |
func_two, | |
], | |
Nosir, | |
database = logic/database, | |
log = logic/log, | |
options = logic/options, | |
utilities = logic/utilities, | |
)""" | |
result = parse_box_use(test_string) | |
print("Parsed box::use statement:") | |
print(f"Number of imports: {len(result.imports)}") | |
print() | |
for i, import_obj in enumerate(result.imports, 1): | |
print(f"Import {i}:") | |
print(f" Module: {import_obj.module}") | |
if import_obj.alias: | |
print(f" Alias: {import_obj.alias}") | |
if import_obj.functions: | |
print(f" Functions: {import_obj.functions}") | |
print(f" Is path: {import_obj.is_path}") | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment