Extract shared named style constant and use keyword args
- Extract VALID_NAMED_STYLE_TYPES constant in docs_helpers.py, reuse in validation_manager.py - Switch build_paragraph_style call to keyword arguments for clarity and resilience Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,18 @@ from typing import Dict, Any, Optional
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
VALID_NAMED_STYLE_TYPES = (
|
||||||
|
"NORMAL_TEXT",
|
||||||
|
"TITLE",
|
||||||
|
"SUBTITLE",
|
||||||
|
"HEADING_1",
|
||||||
|
"HEADING_2",
|
||||||
|
"HEADING_3",
|
||||||
|
"HEADING_4",
|
||||||
|
"HEADING_5",
|
||||||
|
"HEADING_6",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _normalize_color(
|
def _normalize_color(
|
||||||
color: Optional[str], param_name: str
|
color: Optional[str], param_name: str
|
||||||
@@ -137,15 +149,10 @@ def build_paragraph_style(
|
|||||||
fields = []
|
fields = []
|
||||||
|
|
||||||
if named_style_type is not None:
|
if named_style_type is not None:
|
||||||
valid_styles = [
|
if named_style_type not in VALID_NAMED_STYLE_TYPES:
|
||||||
"NORMAL_TEXT", "TITLE", "SUBTITLE",
|
|
||||||
"HEADING_1", "HEADING_2", "HEADING_3",
|
|
||||||
"HEADING_4", "HEADING_5", "HEADING_6",
|
|
||||||
]
|
|
||||||
if named_style_type not in valid_styles:
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Invalid named_style_type '{named_style_type}'. "
|
f"Invalid named_style_type '{named_style_type}'. "
|
||||||
f"Must be one of: {', '.join(valid_styles)}"
|
f"Must be one of: {', '.join(VALID_NAMED_STYLE_TYPES)}"
|
||||||
)
|
)
|
||||||
paragraph_style["namedStyleType"] = named_style_type
|
paragraph_style["namedStyleType"] = named_style_type
|
||||||
fields.append("namedStyleType")
|
fields.append("namedStyleType")
|
||||||
@@ -360,15 +367,15 @@ def create_update_paragraph_style_request(
|
|||||||
Dictionary representing the updateParagraphStyle request, or None if no styles provided
|
Dictionary representing the updateParagraphStyle request, or None if no styles provided
|
||||||
"""
|
"""
|
||||||
paragraph_style, fields = build_paragraph_style(
|
paragraph_style, fields = build_paragraph_style(
|
||||||
heading_level,
|
heading_level=heading_level,
|
||||||
alignment,
|
alignment=alignment,
|
||||||
line_spacing,
|
line_spacing=line_spacing,
|
||||||
indent_first_line,
|
indent_first_line=indent_first_line,
|
||||||
indent_start,
|
indent_start=indent_start,
|
||||||
indent_end,
|
indent_end=indent_end,
|
||||||
space_above,
|
space_above=space_above,
|
||||||
space_below,
|
space_below=space_below,
|
||||||
named_style_type,
|
named_style_type=named_style_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not paragraph_style:
|
if not paragraph_style:
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import logging
|
|||||||
from typing import Dict, Any, List, Tuple, Optional
|
from typing import Dict, Any, List, Tuple, Optional
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from gdocs.docs_helpers import validate_operation
|
from gdocs.docs_helpers import validate_operation, VALID_NAMED_STYLE_TYPES
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -317,15 +317,10 @@ class ValidationManager:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if named_style_type is not None:
|
if named_style_type is not None:
|
||||||
valid_styles = [
|
if named_style_type not in VALID_NAMED_STYLE_TYPES:
|
||||||
"NORMAL_TEXT", "TITLE", "SUBTITLE",
|
|
||||||
"HEADING_1", "HEADING_2", "HEADING_3",
|
|
||||||
"HEADING_4", "HEADING_5", "HEADING_6",
|
|
||||||
]
|
|
||||||
if named_style_type not in valid_styles:
|
|
||||||
return (
|
return (
|
||||||
False,
|
False,
|
||||||
f"Invalid named_style_type '{named_style_type}'. Must be one of: {', '.join(valid_styles)}",
|
f"Invalid named_style_type '{named_style_type}'. Must be one of: {', '.join(VALID_NAMED_STYLE_TYPES)}",
|
||||||
)
|
)
|
||||||
|
|
||||||
if heading_level is not None:
|
if heading_level is not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user