| import os |
| import re |
|
|
| VCF_DIR = "vcf" |
| os.makedirs(VCF_DIR, exist_ok=True) |
|
|
|
|
| def _safe_name(name): |
| |
| name = (name or "contact").strip().replace(" ", "_") |
| return re.sub(r"[^A-Za-z0-9_.-]", "", name) or "contact" |
|
|
|
|
| def generate_vcf(data): |
| filename = os.path.join(VCF_DIR, f"{_safe_name(data.get('name'))}.vcf") |
|
|
| card = f"""BEGIN:VCARD |
| VERSION:3.0 |
| FN:{data.get('name', '')} |
| ORG:{data.get('company', '')} |
| TITLE:{data.get('designation', '')} |
| TEL:{data.get('phone', '')} |
| EMAIL:{data.get('email', '')} |
| URL:{data.get('website', '')} |
| ADR:{data.get('address', '')} |
| END:VCARD |
| """ |
| with open(filename, "w") as f: |
| f.write(card) |
|
|
| return filename |
|
|
|
|
| if __name__ == "__main__": |
| |
| sample = {"name": "John Doe / CEO", "email": "j@abc.com"} |
| path = generate_vcf(sample) |
| assert os.path.basename(path) == "John_Doe__CEO.vcf", path |
| assert "FN:John Doe / CEO" in open(path).read() |
| assert _safe_name("") == "contact" |
| assert _safe_name("../etc/passwd") == "..etcpasswd" |
| print("ok", path) |
|
|