From e7a41c1e0c8f044d4975a05396f9c916c43fa476 Mon Sep 17 00:00:00 2001 From: Mohammad Aboelnour Date: Mon, 3 Nov 2025 15:12:59 +0200 Subject: [PATCH 1/3] Implement YAML constructor and modify attribute handling Added a YAML constructor to ignore Ruby-specific tags and updated attribute extraction logic. --- pyworker/job.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/pyworker/job.py b/pyworker/job.py index 27a82a1..59c421f 100644 --- a/pyworker/job.py +++ b/pyworker/job.py @@ -14,6 +14,12 @@ def __new__(meta, name, bases, class_dict): _register_class(cls) return cls +# Add a YAML constructor to ignore Ruby-specific tags (required once at module load time) +def no_ruby_objects(loader, tag_suffix, node): + # Construct mapping normally, ignoring Ruby-specific tags + return loader.construct_mapping(node) + +yaml.SafeLoader.add_multi_constructor("!ruby/object:", no_ruby_objects) class Job(object): """docstring for Job""" @@ -52,19 +58,6 @@ def extract_class_name(line): else: return None - def extract_attributes(lines): - attributes = [] - collect = False - for line in lines: - if line.startswith(' raw_attributes:'): - collect = True - elif not line.startswith(' '): - if collect: - break - elif collect: - attributes.append(line) - return attributes - def extract_extra_fields(extra_fields, extra_field_values): if extra_fields is None or extra_field_values is None: return None @@ -91,18 +84,18 @@ def extract_extra_fields(extra_fields, extra_field_values): abstract=True, extra_fields=extra_fields_dict, reporter=reporter) - attributes = extract_attributes(handler[2:]) + attributes = handler[3:] logger.debug("Found attributes: %s" % str(attributes)) - stripped = '\n'.join(['object:', ' attributes:'] + attributes) - payload = yaml.load(stripped, Loader=yaml.FullLoader) + stripped = '\n'.join(['object:', ' raw_attributes:'] + attributes) + payload = yaml.load(stripped, Loader=yaml.SafeLoader) logger.debug("payload object: %s" % str(payload)) return target_class(class_name=class_name, logger=logger, job_id=job_id, attempts=attempts, run_at=run_at, queue=queue, database=database, max_attempts=max_attempts, - attributes=payload['object']['attributes'], + attributes=payload['object']['raw_attributes'], abstract=False, extra_fields=extra_fields_dict, reporter=reporter) From 08643f531429ab3f02830a56f7b93bbd9050741a Mon Sep 17 00:00:00 2001 From: Mohammad Aboelnour Date: Mon, 3 Nov 2025 15:14:00 +0200 Subject: [PATCH 2/3] Add multiline and newline attributes to YAML fixture --- tests/fixtures/handler_registered.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/fixtures/handler_registered.yaml b/tests/fixtures/handler_registered.yaml index 6ae53f6..a4aa7f5 100644 --- a/tests/fixtures/handler_registered.yaml +++ b/tests/fixtures/handler_registered.yaml @@ -8,6 +8,16 @@ object: !ruby/object:RegisteredJob multiline total_articles: 1000 is_blind: true + proper_multiline: |- + line one + line two + + line four + collapsed_first_newline: 'line one + abc' + extra_new_line: 'line one + + line two' attributes: !ruby/object:ActiveRecord::AttributeSet attributes: !ruby/object:ActiveRecord::LazyAttributeHash types: {} From 583f1c51efb3b471b77f98524ba7b2741ac80ae5 Mon Sep 17 00:00:00 2001 From: Mohammad Aboelnour Date: Mon, 3 Nov 2025 15:14:55 +0200 Subject: [PATCH 3/3] Add extra fields to job report in test case --- tests/test_job.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_job.py b/tests/test_job.py index 59968a4..b721065 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -146,7 +146,10 @@ def test_from_row_when_registered_class_returns_job_instance_with_attributes(sel 'title': 'review title', 'description': 'review description\nmultiline\n', 'total_articles': 1000, - 'is_blind': True + 'is_blind': True, + 'collapsed_first_newline': 'line one abc', + 'extra_new_line': 'line one\nline two', + 'proper_multiline': 'line one\nline two\n\nline four' }) self.assertIsNone(job.reporter)