File size: 1,172 Bytes
aa93eef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import os
import re
VCF_DIR = "vcf"
os.makedirs(VCF_DIR, exist_ok=True)
def _safe_name(name):
# ponytail: keep filenames filesystem-safe; fall back to "contact"
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__":
# ponytail: one runnable check — the filename-safety path is the only real logic
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)
|