anonymizer - Core Module#
Core anonymization engine for iCalendar files.
Anonymizes personal data while preserving technical properties needed for bug reproduction. Uses deterministic hashing with configurable salt.
- icalendar_anonymizer.anonymizer.anonymize(cal: Calendar, salt: bytes | None = None, preserve: set[str] | None = None, field_modes: dict[str, str] | None = None) Calendar[source]#
Anonymize an iCalendar object.
Removes personal data (names, emails, locations, descriptions) while preserving technical properties (dates, recurrence, timezones). Uses deterministic hashing so the same input produces the same output with the same salt.
- Parameters:
cal – The Calendar object to anonymize
salt – Optional salt for hashing. If None, generates random salt. Pass the same salt to get consistent output across runs.
preserve – Optional set of additional property names to preserve. Case-insensitive. User must ensure these don’t contain sensitive data. Example: {“CATEGORIES”, “COMMENT”} Cannot be used with field_modes.
field_modes –
Optional dict mapping field names to anonymization modes. Modes: “keep”, “remove”, “randomize”, “replace”. Fields: SUMMARY, DESCRIPTION, LOCATION, COMMENT, CONTACT,
RESOURCES, CATEGORIES, ATTENDEE, ORGANIZER, UID.
Cannot be used with preserve.
- Returns:
New anonymized Calendar object
- Raises:
TypeError – If cal is not a Calendar object or salt is not bytes
ValueError – If both preserve and field_modes are specified, or if field_modes contains invalid fields/modes
Usage Example#
from icalendar import Calendar
from icalendar_anonymizer import anonymize
with open('calendar.ics', 'rb') as f:
cal = Calendar.from_ical(f.read())
anonymized_cal = anonymize(cal)
with open('anonymized.ics', 'wb') as f:
f.write(anonymized_cal.to_ical())