Skip to content

Commit

Permalink
Ref #290 - Removed changed: line manipulation from #242
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsasha committed Jul 9, 2020
1 parent 458b492 commit e0bbbd6
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 87 deletions.
39 changes: 0 additions & 39 deletions irrd/rpsl/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,45 +416,6 @@ def _update_attribute_value(self, attribute, new_values):
self._object_data.insert(insert_idx, (attribute, new_value, []))
insert_idx += 1

def overwrite_date_new_changed_attributes(self, existing_obj=None) -> None:
"""
Overwrite the date in any newly added changed: attributes per #242.
Which changed: lines are new is determined by comparing to existing_obj,
which should be another RPSLObject, or None if all changed: lines
should be considered new.
"""
parsed_values_to_overwrite = set(self.parsed_data['changed'])
if existing_obj:
parsed_values_to_overwrite -= set(existing_obj.parsed_data['changed'])

# As the value is already validated by RPSLChangedField,
# we can safely make assumptions on the format.
new_object_data = []
removed_values_with_comment: List[Tuple[int, str]] = []
for idx, (attr_name, attr_value, continuation_chars) in enumerate(self._object_data):
if attr_name == 'changed':
attr_value_clean = attr_value.split('#')[0].strip()
if attr_value_clean in parsed_values_to_overwrite:
removed_values_with_comment.append((idx, attr_value))
continue
new_object_data.append((attr_name, attr_value, continuation_chars))
self._object_data = new_object_data

current_date = datetime.datetime.now().strftime('%Y%m%d')
for idx, value in removed_values_with_comment:
try:
content, comment = map(str.strip, value.split('#'))
except ValueError:
content = value.strip()
comment = ''
email = content.split(' ')[0] # Ignore existing date
if comment:
new_value = f'{email} {current_date} # {comment}'
else:
new_value = f'{email} {current_date}'
self._object_data.insert(idx, ('changed', new_value, []))
self.messages.info(f'Set date in changed line "{value}" to today.')

def __repr__(self):
source = self.parsed_data.get('source', '')
return f'{self.rpsl_object_class}/{self.pk()}/{source}'
Expand Down
33 changes: 0 additions & 33 deletions irrd/rpsl/tests/test_rpsl_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,36 +497,3 @@ def test_parse(self):
]
assert obj.references_strong_inbound() == set()
assert obj.render_rpsl_text() == rpsl_text


class TestOverwriteDateNewChangedAttributes:
expected_date = datetime.datetime.now().strftime('%Y%m%d')

# This applies to all objects identically - only one test needed
def test_changed_line_overwrite_with_date_and_comment(self):
new_rpsl_text = self._generate_old_new_object('changed: new1@example.com 19980101 # comment')
assert 'changed: changed@example.com 20190701 # comment' in new_rpsl_text
assert f'changed: new1@example.com {self.expected_date} # comment' in new_rpsl_text

def test_changed_line_overwrite_without_comment(self):
new_rpsl_text = self._generate_old_new_object('changed: new1@example.com 19980101')
assert 'changed: changed@example.com 20190701 # comment' in new_rpsl_text
assert f'changed: new1@example.com {self.expected_date}' in new_rpsl_text

def test_changed_line_overwrite_without_date_with_comment(self):
new_rpsl_text = self._generate_old_new_object('changed: new1@example.com#comment')
assert 'changed: changed@example.com 20190701 # comment' in new_rpsl_text
assert f'changed: new1@example.com {self.expected_date} # comment' in new_rpsl_text

def _generate_old_new_object(self, new_changed_line):
rpsl_text = object_sample_mapping[RPSLRouteSet().rpsl_object_class]
obj_current = rpsl_object_from_text(rpsl_text, strict_validation=True)

lines = rpsl_text.splitlines()
lines.insert(4, new_changed_line)
rpsl_text = '\n'.join(lines)
obj_new = rpsl_object_from_text(rpsl_text, strict_validation=True)

obj_new.overwrite_date_new_changed_attributes(obj_current)
assert 'Set date in changed line' in obj_new.messages.infos()[1]
return obj_new.render_rpsl_text()
15 changes: 0 additions & 15 deletions irrd/updates/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ def save(self, database_handler: DatabaseHandler) -> None:
logger.info(f'{id(self)}: Saving change for {self.rpsl_obj_new}: deleting current object')
database_handler.delete_rpsl_object(rpsl_object=self.rpsl_obj_current, origin=JournalEntryOrigin.auth_change)
else:
if not self.used_override:
self.rpsl_obj_new.overwrite_date_new_changed_attributes(self.rpsl_obj_current)
# This call may have emitted a new info message.
self._import_new_rpsl_obj_info_messages()
logger.info(f'{id(self)}: Saving change for {self.rpsl_obj_new}: inserting/updating current object')
database_handler.upsert_rpsl_object(self.rpsl_obj_new, JournalEntryOrigin.auth_change)
self.status = UpdateRequestStatus.SAVED
Expand Down Expand Up @@ -304,17 +300,6 @@ def _check_conflicting_roa(self) -> bool:
self._cached_roa_validity = True
return True

def _import_new_rpsl_obj_info_messages(self):
"""
Import new info messages from self.rpsl_obj_new.
This is used after overwrite_date_new_changed_attributes()
is called, as it's called just before saving, but may
emit a new info message.
"""
for info_message in self.rpsl_obj_new.messages.infos():
if info_message not in self.info_messages:
self.info_messages.append(info_message)


def parse_change_requests(requests_text: str,
database_handler: DatabaseHandler,
Expand Down

0 comments on commit e0bbbd6

Please sign in to comment.