-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
executable file
·99 lines (81 loc) · 2.56 KB
/
utilities.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
This the utilities python module.
All the commonly used function to be
implemented in this utilities module
"""
import os
import subprocess
import yaml
from prettytable import PrettyTable
#--------#
##########
# Mailer #
##########
#--------#
class Mailer:
"""
User needs to configure sendmail sever on which the
automation framework is running.
"""
command = "/usr/sbin/sendmail -oi -t"
def __init__(self, recipient, sender, subject=None, reply=None):
self.msg = []
self.recipient = recipient
self.sender = sender
self.reply = reply and reply or sender
self.subject = subject and subject or 'No Subject'
def __str__(self):
return '%s --> %s: "%s" [...]x%d' % \
(self.sender, self.recipient, self.subject, len(self.msg))
def header(self):
head = []
head.append('To: %s' % self.recipient)
head.append('From: %s' % self.sender)
head.append('Reply-to: %s' % self.reply)
head.append('Subject: %s' % self.subject)
head.append('')
return head
def write(self, msg):
self.msg.append(msg)
def send(self):
mail = subprocess.Popen(self.command, stdout=subprocess.PIPE, \
stdin=subprocess.PIPE, shell=True)
mail.stdin.write("\n".join(self.header() + self.msg))
mail.stdin.write("\n\0")
mail.stdin.close()
def show(self):
print "\n".join(self.header() + self.msg)
def read_yaml(path):
"""
This function will read the yaml file
and read the structure in the dictionary format
"""
if not is_file_valid(path):
raise RuntimeError("Yaml file %s not present" % path)
with open(path, 'r') as fh:
return yaml.load(fh)
def sms_email(number, provider):
provider = provider.lower()
n_fmt = {'att': lambda n: '%d' % n}
p_fmt = {'att': '%s@txt.att.net'}
return p_fmt[provider] % n_fmt(number)
def send_sms(number, provider, sender, title, msg):
sms_limit = 140
m = Mailer(sms_email(number, provider), sender, title)
if len(msg) > sms_limit:
print ("WARN: SMS is %d (>%d) characters, might be truncated" %
(len(msg), sms_limit))
m.write(msg)
m.send()
def is_file_valid(file_path):
return os.path.isfile(file_path)
def is_path(dir):
return os.path.isdir(dir)
def create_dir(path):
return os.makedirs(path)
def prettytable(padding_w, l_dict ,arg):
x = PrettyTable(arg)
x.padding_width = padding_w
for k,v in l_dict.items():
x.add_row([k,v])
return x