forked from schemaorg/schemaorg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdoutil.py
83 lines (64 loc) · 2.34 KB
/
sdoutil.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
import logging
logging.basicConfig(level=logging.INFO) # dev_appserver.py --log_level debug .
log = logging.getLogger(__name__)
import io
import threading
import os
import os.path
import fnmatch
SDOCONFIG=None
from google.appengine.api import app_identity
from google.appengine.api import mail
def sdo_send_mail(to=None,subject=None,msg=None):
if not to:
log.error("No mail recipient!")
return
if not subject:
subject = "Infomation from " + app_identity.get_application_id()
if not msg:
msg = "Empty message"
sender = 'manager@{}.appspotmail.com'.format(app_identity.get_application_id())
log.info("sdo_send_mail To: %s From: %s Subject: %s Msg: %s" % (to,sender,subject,msg))
try:
mail.send_mail(sender=sender,to=to,subject=subject,body=msg)
except Exception as e:
log.error("Exception from within mail.send_mail: %s: %s" % (e,e.message))
# Wrap io.SringIO to ensure that all things written are of type unicode
class sdoStringIO(io.StringIO):
def write(self,s):
if isinstance(s,str):
s = s.decode('utf-8')
if not isinstance(s,unicode):
s = unicode(s)
ret = super(sdoStringIO,self).write(s)
return ret
#On thread varible storage
ThreadVars = threading.local()
def getAppVar(var):
ret = getattr(ThreadVars, var, None)
#log.debug("got var %s as %s" % (var,ret))
return ret
def setAppVar(var,val):
#log.debug("Setting var %s to %s" % (var,val))
setattr(ThreadVars,var,val)
CLOUDSTAT = "CloudStat"
CLOUDEXTRAMETA = "CloudExtraMeta"
def full_path(filename):
"""convert local file name to full path."""
import os.path
folder = os.path.dirname(os.path.realpath(__file__))
return os.path.join(folder, filename)
def glob_from_dir(adir, pattern, source="local"):
log.info("glob-from-dir '%s', '%s', %s" % (adir,pattern,source))
files = []
if os.path.isdir(adir):
try:
if source == "local":
for file in os.listdir(adir):
if fnmatch.fnmatch(file,pattern):
files.append(adir + "/" + file)
except Exception as e:
log.error("Exception from within glob_from_dir: %s: %s" % (e,e.message))
else:
log.error("No such directory: %s" % adir)
return files