Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ pyyaml
jsonschema < 4.18.0
GitPython
sphinx-lint
google-auth-oauthlib
google-api-python-client
pytz
tools/yaml2x/get_yaml_data
14 changes: 14 additions & 0 deletions tools/yaml2x/get_yaml_data/get_yaml_data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
YAML to JSON converter package for accessibility guidelines.

This package provides functionality to convert YAML-based accessibility
guidelines into JSON format, handling RST markup and multilingual content.
"""

from .process_yaml import get_yaml_data
from .process_yaml import convert_yaml_to_json

__all__ = ['get_yaml_data', 'convert_yaml_to_json']

# Version information
__version__ = '0.1.0'
31 changes: 31 additions & 0 deletions tools/yaml2x/get_yaml_data/get_yaml_data/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# yaml2json/cli.py
"""
Command-line interface for YAML to JSON conversion tool.
"""

import sys
from pathlib import Path
from get_yaml_data.config import setup_configuration
from get_yaml_data.process_yaml import convert_yaml_to_json

def main() -> None:
"""
Main CLI function that processes YAML files and generates JSON output.
"""
try:
# Get configuration
settings = setup_configuration()

# Convert YAML to JSON
convert_yaml_to_json(
basedir=settings['basedir'],
base_url=settings['base_url'],
output_file=settings['output_file'],
publish=settings['publish']
)

except Exception as e:
sys.exit(f"Error during conversion process: {str(e)}")

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# get_yaml_data.py
"""
Main module for YAML to JSON conversion.
Main module for YAML processing and conversion to JSON.

This module orchestrates the conversion process, handling configuration,
data processing, and output generation for accessibility guidelines.
This module provides the core functionality for converting YAML files to JSON format,
focusing on accessibility guidelines processing.
"""

import sys
import json
from pathlib import Path
from typing import Dict, Any
sys.path.append(str(Path(__file__).resolve().parent.parent))
from typing import Dict, Any, Optional
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))

from a11y_guidelines import setup_instances, InfoRef, Check
import config, utils, rst_processor
from .config import setup_configuration
from . import utils
from . import rst_processor

def get_yaml_data(basedir: Path, base_url: str, publish: bool = False) -> Dict[str, Any]:
"""
Expand All @@ -21,6 +24,7 @@ def get_yaml_data(basedir: Path, base_url: str, publish: bool = False) -> Dict[s
Args:
basedir (Path): Base directory containing YAML files
base_url (str): Base URL for links
publish (bool): Flag to indicate if this is a publication build

Returns:
Dict[str, Any]: Processed data including version info, checks, and conditions
Expand Down Expand Up @@ -61,23 +65,31 @@ def get_yaml_data(basedir: Path, base_url: str, publish: bool = False) -> Dict[s
'checks': checks
}

def main() -> None:
def convert_yaml_to_json(
basedir: Path,
base_url: str,
output_file: Path,
publish: bool = False
) -> None:
"""
Main CLI function that processes YAML files and generates JSON output.
Convert YAML files to JSON and write to specified output file.

Args:
basedir (Path): Base directory containing YAML files
base_url (str): Base URL for links
output_file (Path): Path to output JSON file
publish (bool): Flag to indicate if this is a publication build

Raises:
Exception: If there's an error during the conversion process
"""
try:
# Get configuration
settings: Dict[str, Any] = config.setup_configuration()

# Process YAML data
output_data = get_yaml_data(settings['basedir'], settings['base_url'], settings['publish'])
output_data = get_yaml_data(basedir, base_url, publish)

# Write to JSON file
with open(settings['output_file'], mode="w", encoding="utf-8", newline="\n") as f:
with open(output_file, mode="w", encoding="utf-8", newline="\n") as f:
json.dump(output_data, f, indent=2, ensure_ascii=False)

except Exception as e:
sys.exit(f"Error during conversion process: {str(e)}")

if __name__ == "__main__":
main()
raise Exception(f"Error during conversion process: {str(e)}")
18 changes: 18 additions & 0 deletions tools/yaml2x/get_yaml_data/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[build-system]
requires = ["setuptools>=64", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "get_yaml_data"
version = "0.1.0"
description = "A module to process YAML data"
authors = [
{name = "Masafumi NAKANE", email = "[email protected]"}
]
requires-python = ">=3.8"

[project.scripts]
yaml2json = "get_yaml_data.cli:main"

[tool.setuptools]
packages = { find = {} }
19 changes: 0 additions & 19 deletions tools/yaml2x/yaml2json/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions tools/yaml2x/yaml2sheet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.*
!config.*.sample
83 changes: 83 additions & 0 deletions tools/yaml2x/yaml2sheet/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os.path
import logging
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

logger = logging.getLogger(__name__)

class GoogleAuthManager:
"""Manages Google API authentication and token management"""

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

def __init__(self, credentials_path: str = 'credentials.json',
token_path: str = 'token.json'):
"""Initialize the auth manager with paths for credentials and token

Args:
credentials_path: Path to client secrets JSON file
token_path: Path to save/load authentication token
"""
self.credentials_path = credentials_path
self.token_path = token_path

def get_credentials(self) -> Credentials:
"""Retrieve Google API credentials, refreshing or creating if needed

Returns:
Credentials: Valid Google API credentials

Raises:
FileNotFoundError: If credentials file is missing
Exception: If authentication fails
"""
creds = None

# Try to load saved token
if os.path.exists(self.token_path):
try:
creds = Credentials.from_authorized_user_file(self.token_path, self.SCOPES)
logger.info("Loaded existing token")
except Exception as e:
logger.warning(f"Failed to load existing token: {e}")

# Handle invalid/expired token
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
logger.info("Refreshed expired token")
except Exception as e:
logger.warning(f"Failed to refresh token: {e}")
creds = None

# Create new token if needed
if not creds:
if not os.path.exists(self.credentials_path):
raise FileNotFoundError(
f"Credentials file not found at {self.credentials_path}. "
"Please download it from GCP Console."
)

try:
flow = InstalledAppFlow.from_client_secrets_file(
self.credentials_path,
self.SCOPES
)
creds = flow.run_local_server(port=0)
logger.info("Created new token through authentication flow")
except Exception as e:
logger.error(f"Failed to authenticate: {e}")
raise

# Save new token
try:
with open(self.token_path, 'w') as token:
token.write(creds.to_json())
logger.info(f"Saved token to {self.token_path}")
except Exception as e:
logger.error(f"Failed to save token: {e}")
# Continue even if token save fails since auth succeeded

return creds
76 changes: 76 additions & 0 deletions tools/yaml2x/yaml2sheet/cell_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import Any, Dict, Optional
from enum import Enum

class CellType(Enum):
"""Cell data type enumeration"""
PLAIN = "plain"
RICH_TEXT = "rich_text"
FORMULA = "formula"

class CellData:
"""Represents a cell's data structure and formatting"""

def __init__(
self,
value: Any,
type: CellType,
formatting: Optional[Dict] = None,
validation: Optional[Dict] = None,
protection: bool = False,
note: Optional[str] = None
):
"""Initialize cell data with value and formatting options

Args:
value: Cell value (string, number, formula, or rich text object)
type: CellType enum indicating data type
formatting: Optional dictionary of cell formatting options
validation: Optional dictionary of data validation rules
protection: Whether the cell is protected
note: Optional cell comment/note
"""
self.value = value
self.type = type
self.formatting = formatting
self.validation = validation
self.protection = protection
self.note = note

def to_sheets_value(self) -> Dict:
"""Convert cell data to Google Sheets API format

Returns:
Dict: Cell data in Google Sheets API format
"""
result = {}

# Set cell value based on type
if self.type == CellType.PLAIN:
result["userEnteredValue"] = {"stringValue": str(self.value)}
elif self.type == CellType.RICH_TEXT:
result["userEnteredValue"] = {"stringValue": self.value['text']}
# Process text format runs if present
if self.value['format_runs']:
result["textFormatRuns"] = [
{
"startIndex": run['startIndex'],
"format": {
"link": run['format'].get('link'),
"foregroundColor": run['format'].get('foregroundColor'),
"underline": run['format'].get('underline')
}
}
for run in self.value['format_runs']
]
elif self.type == CellType.FORMULA:
result["userEnteredValue"] = {"formulaValue": self.value}

# Add formatting if specified
if self.formatting:
result["userEnteredFormat"] = self.formatting

# Add data validation rules if specified
if self.validation:
result["dataValidation"] = self.validation

return result
Loading