Skip to content

Instantly share code, notes, and snippets.

@beacrea
Created October 11, 2024 20:45
Show Gist options
  • Save beacrea/2bb3614be9ed9338b8152d86823f540e to your computer and use it in GitHub Desktop.
Save beacrea/2bb3614be9ed9338b8152d86823f540e to your computer and use it in GitHub Desktop.
Redactor Task for Visual Studio
import json
def load_config(config_file):
with open(config_file, 'r') as file:
return json.load(file)
def redact_pii(file_path, config_file='redaction_config.json'):
config = load_config(config_file)
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Apply ignore patterns before redaction
for pattern in config['ignore_patterns']:
content = re.sub(pattern['regexp'], pattern['replace_with'], content)
# Analyze and anonymize (after applying ignore patterns)
results = analyzer.analyze(text=content, entities=["PHONE_NUMBER", "EMAIL_ADDRESS", "PERSON"], language='en')
redacted_content = anonymizer.anonymize(text=content, analyzer_results=results)
# Save redacted content
with open(file_path, 'w', encoding='utf-8') as file:
file.write(redacted_content)
print(f"Redacted file saved at {file_path}")
{
"ignore_patterns": [
{
"regexp": "\\bexample.com\\b",
"replace_with": "example.com"
},
{
"regexp": "\\b(?:specificString|testAccount)\\b",
"replace_with": ""
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"label": "Redact PII in open file",
"type": "shell",
"command": "python",
"args": [
"${workspaceFolder}/redact_vscode.py"
],
"problemMatcher": [],
"options": {
"env": {
"OPEN_FILE_PATH": "${file}"
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment