From 968db636251c397021b9b546f7b595969604a885 Mon Sep 17 00:00:00 2001 From: Claudine Date: Fri, 16 May 2025 14:13:14 -0400 Subject: [PATCH] use javaproperties library to parse --- requirements.txt | 1 + scripts/parse_and_validate_properties_txt.py | 31 ++------------------ 2 files changed, 4 insertions(+), 28 deletions(-) diff --git a/requirements.txt b/requirements.txt index 43815ab..337ae4e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ ruamel.yaml tenacity~=9.0.0 requests~=2.32.3 pydantic~=2.9.2 +javaproperties~=0.8.2 diff --git a/scripts/parse_and_validate_properties_txt.py b/scripts/parse_and_validate_properties_txt.py index 08fd2f6..a0931bc 100644 --- a/scripts/parse_and_validate_properties_txt.py +++ b/scripts/parse_and_validate_properties_txt.py @@ -14,6 +14,7 @@ import os from typing import Optional, Union from pydantic import BaseModel, Field, ConfigDict, field_validator +import javaproperties as jp class PropertiesBase(BaseModel): @@ -71,32 +72,6 @@ def read_properties_txt(properties_url): return r.text -def parse_text(properties_raw): - field_pattern = re.compile(r"^([a-zA-z]+)\s*=(.*)") - - properties_dict = {} - field_name = "" - field_value = "" - properties_lines = properties_raw.split('\n') - for line in properties_lines: - if line.startswith('#') or not line.strip(): - continue - if line_match := field_pattern.match(line): - # store previous key-value pair - if field_name: - properties_dict[field_name] = field_value - # process current line - field_name = line_match[1].strip() - field_value = line_match[2].strip() - field_value = field_value.split('#')[0].strip() - else: - field_value += " " + line.strip() - # store last key-pair - if field_name: - properties_dict[field_name] = field_value - - return properties_dict - def validate_existing(properties_dict): # validation on existing contribution is weaker properties = PropertiesExisting.model_validate(properties_dict) @@ -151,9 +126,9 @@ def set_output_error(msg): try: if type_ == 'library': - props = validate_new_library(parse_text(properties_raw)) + props = validate_new_library(jp.loads(properties_raw)) else: - props = validate_new(parse_text(properties_raw)) + props = validate_new(jp.loads(properties_raw)) except Exception as e: set_output_error(f'Errors when parsing file. Please check all required fields, and file format.\n\n{e}') raise e