-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
106 lines (87 loc) · 2.5 KB
/
utils.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
100
101
102
103
104
105
import os
import json
import gspread
import smtplib
import logging as log
from consts import *
from oauth2client.service_account import ServiceAccountCredentials
# to get configuration values
def getConfigValue(args, name, default=False):
# first check args
if args and name in vars(args).keys():
value = vars(args)[name]
if value:
return value
# now environment variable
value = os.getenv(name)
if value:
return value
# if not found look in config
config = loadJson(CONFIG_FILE)
if config and name in config:
value = config[name]
return value
# fallback to default
return default
# get google sheer url
def getGoogleSheetUrl(sheetId):
return 'https://docs.google.com/spreadsheets/d/{0}'.format(sheetId)
# to open a google sheet
def openGoogleSheet(sheetId):
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name(GSHEET_SECRET, scope)
client = gspread.authorize(creds)
# now open
book = client.open_by_key(sheetId)
return book
# send mail
def sendMail(mailFrom, mailTo, subject, body):
smtpUser = getConfigValue(None, XFINITY_SMTP_USER)
smtpHost = getConfigValue(None, XFINITY_SMTP_HOST) or SMTP_GMAIL if smtpUser.endswith('@gmail.com') else ''
if not smtpHost:
log.warn('Should warn but SMTP host not defined')
return False
try:
# connect to smtp server
smtpPort = getConfigValue(None, XFINITY_SMTP_PORT)
smtpPort = int(smtpPort) if smtpPort else (SMTP_PORT_SSL if smtpHost == SMTP_GMAIL else SMTP_PORT)
log.debug('SMTP: Connecting to server {}:{}'.format(smtpHost, smtpPort))
server = smtplib.SMTP_SSL(smtpHost, smtpPort)
server.ehlo()
if smtpPort == SMTP_PORT_TLS:
log.debug('SMTP: Enabling TLS')
server.starttls()
server.ehlo()
if smtpUser:
log.debug('SMTP: Authenticating')
server.login(smtpUser, getConfigValue(None, XFINITY_SMTP_PASS))
# build and send email
emailText = 'From: {0}\n'.format(mailFrom)
emailText += 'To: {0}\n'.format(mailTo)
emailText += 'Subject: {0}\n\n{1}'.format(subject, body)
server.sendmail(mailFrom, mailTo, emailText)
# done
server.close()
log.info('Warning mail sent')
except Exception as e:
log.error(e)
pass
def deleteFile(file):
try:
os.remove(file)
return True
except:
return False
def loadJson(file):
try:
return json.load(open(file))
except:
return None
def saveJson(file, data):
try:
with open(file, 'w') as f:
json.dump(data, f)
return True
except:
return False