Code refactor and clean

This commit is contained in:
Paul 2016-03-15 10:52:43 +01:00
parent 61d9186789
commit 5f9f66265d

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python2.7
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import imaplib import imaplib
@ -14,6 +14,11 @@ from messaging.sms import SmsSubmit
def csv_config_parser(mailboxes): def csv_config_parser(mailboxes):
"""
Reads CSV config, returns as a list
:param mailboxes:
:return:
"""
params = [] params = []
with open(mailboxes) as f: with open(mailboxes) as f:
for line in f: for line in f:
@ -27,7 +32,7 @@ def csv_config_parser(mailboxes):
def fetch_unread_mails(mailboxserver, mailboxlogin, mailboxpassword): def fetch_unread_mails(mailboxserver, mailboxlogin, mailboxpassword):
""" """
Fetch unread emails on specific mailbox, and returns some fields Fetch unread emails on specific mailbox, and returns some fields
:param mailboxserver: :param mailboxserver:
:param mailboxlogin: :param mailboxlogin:
:param mailboxpassword: :param mailboxpassword:
@ -87,6 +92,8 @@ def clear_all_sms():
def resize_ascii_sms(message): def resize_ascii_sms(message):
""" """
Strip message if longer than config.smssize Strip message if longer than config.smssize
:param message: Message to resize
:return message: Resized message
""" """
value = config.smssize-3 value = config.smssize-3
if len(message) > value: if len(message) > value:
@ -97,8 +104,8 @@ def resize_ascii_sms(message):
def resize_pdu_sms(message): def resize_pdu_sms(message):
""" """
Strip SMS if longer than config.smssize Strip SMS if longer than config.smssize
:param message: :param message: Message to resize
:return: str :return message: Resized message
""" """
if len(message) > config.smssize: if len(message) > config.smssize:
message = message[:config.smssize] message = message[:config.smssize]
@ -117,14 +124,19 @@ def sms_template(sender, subject):
def imap2sms(conf): def imap2sms(conf):
for list in conf: """
username = list[0] Send a sms
password = list[1] :param conf:
mailserver = list[2] :return:
"""
for l in conf:
username = l[0]
password = l[1]
mailserver = l[2]
numbers = [] numbers = []
i = 3 i = 3
while i < len(list): while i < len(l):
numbers.append(list[i]) numbers.append(l[i])
i += 1 i += 1
mails = fetch_unread_mails(username, password, mailserver) mails = fetch_unread_mails(username, password, mailserver)
@ -140,9 +152,13 @@ def imap2sms(conf):
sms = resize_ascii_sms(sms_template(sender, subject)) sms = resize_ascii_sms(sms_template(sender, subject))
send_ascii_sms(number, sms) send_ascii_sms(number, sms)
def pduformat(phonenumber, message): def pduformat(phonenumber, message):
""" """
Formats SMS using pdu encoding Formats SMS using pdu encoding
:param phonenumber: Phone number to insert in pdu
:param message: Text message
:return: pdustring, pdulenght
""" """
sms = SmsSubmit(phonenumber, message) sms = SmsSubmit(phonenumber, message)
pdu = sms.to_pdu()[0] pdu = sms.to_pdu()[0]
@ -156,7 +172,9 @@ def pduformat(phonenumber, message):
def send_ascii_sms(phonenumber, sms): def send_ascii_sms(phonenumber, sms):
""" """
Send SMS using telnetlib, returns exception when issues with telnet communication Send SMS using telnetlib, returns exception when issues with telnet communication
:param phonenumber: Phone number to insert in pdu
:param sms: Text message
""" """
decoded_sms = sms.encode("ascii", "ignore") decoded_sms = sms.encode("ascii", "ignore")
try: try:
@ -184,9 +202,9 @@ def send_ascii_sms(phonenumber, sms):
def send_pdu_sms(pdustring, pdulenght): def send_pdu_sms(pdustring, pdulenght):
""" """
Send SMS using telnetlib, returns exception when issues with telnet communication Send SMS using telnetlib, returns exception when issues with telnet communication
:param pdustring: is the converted sms to pdu format :param pdustring: is the converted sms to pdu format
:param pdulenght: is the size of the pdustring :param pdulenght: is the size of the pdustring
""" """
try: try:
time.sleep(2) time.sleep(2)
@ -213,7 +231,8 @@ def send_pdu_sms(pdustring, pdulenght):
def usage(): def usage():
""" """
Prints usage Prints usage
:return: str
""" """
usagetext = "smsgateway.py subcommands : \n\n%s imap2sms\n%s sms <number> <message>\n%s clearallsms\n" % ( usagetext = "smsgateway.py subcommands : \n\n%s imap2sms\n%s sms <number> <message>\n%s clearallsms\n" % (
sys.argv[0], sys.argv[0], sys.argv[0]) sys.argv[0], sys.argv[0], sys.argv[0])
@ -222,8 +241,8 @@ def usage():
def debug(): def debug():
""" """
Debug Function Debug Function
:return: :return: bool
""" """
return True return True