-
Notifications
You must be signed in to change notification settings - Fork 0
/
clicktoshell.py
59 lines (40 loc) · 1.6 KB
/
clicktoshell.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
import os
import re
def clean_phone(data):
""" Returns an international phone number for many common formats """
simplified = data.strip()
# Us phone numbers: (217) 352-1913
if re.search(r"^\([0-9]{3}\)\s*[0-9]{3}\s*\-\s*[0-9]{4}$", simplified):
simplified = "+1" + simplified
# Us phone numbers: 217-352-1913
if re.search(r"^[0-9]{3}\s*\-\s*[0-9]{3}\s*\-\s*[0-9]{4}$", simplified):
simplified = "+1" + simplified
# Manage phones like +32 (0) 80 41 81 54
if simplified.startswith("+"):
simplified = re.sub(r"\(\s*0\s*\)", "", simplified)
simplified = re.sub(r"[\(\)\-\.\s]", "", simplified)
# Special case for phones like 33 (0)4 89 06 58 00
if simplified.startswith("330") and len(simplified) == 12:
simplified = "+33" + simplified[3:]
# 33 1 346 678 32
if simplified.startswith("33") and len(simplified) == 11:
simplified = "+33" + simplified[2:]
# 44 7918708773
if simplified.startswith("44") and len(simplified) == 12:
simplified = "+" + simplified
# (+33) 09 81 01 99 31
if simplified.startswith("+330"):
simplified = "+33" + simplified[4:]
if re.search(r"^\+?[0-9]{6,15}$", simplified):
if simplified.startswith("00"):
simplified = "+" + simplified[2:]
elif simplified.startswith("0"):
simplified = "+33" + simplified[1:] # Historical french default
return simplified
return None
protocol, raw_phone = sys.argv[1].split(":", 1)
template_file = os.path.expanduser("~/clicktoshell-%s.sh" % protocol)
phone = clean_phone(raw_phone)
if os.path.isfile(template_file):
os.system("sh %s '%s'" % (template_file, phone))