22# This program is distributed under the MIT license.
33
44import threading
5+ import datetime as datetime_module
6+ import os .path
7+ import pstats
58
69import envelopes
710
11+ from python_toolbox import caching
12+ from python_toolbox import misc_tools
13+
14+ from . import base_profile
15+
816
917class BaseProfileHandler (object ):
1018 __metaclass__ = abc .ABCMeta
1119
1220 @abc .abstractmethod
1321 def __call__ (self ):
1422 pass
23+
24+ default_file_name = caching .CachedProperty (
25+ lambda self : 'profile-%s' % datetime_module .datetime .now ()
26+ )
1527
1628
1729
@@ -28,33 +40,68 @@ def thread_job(self):
2840 pass
2941
3042
31- class EmailProfileHandler (BaseProfileHandler ):
43+ class EmailProfileHandler (AuxiliaryThreadProfileHandler ):
3244 def __init__ (self , email_address , smtp_server , smtp_user , smtp_password ,
3345 use_tls = True ):
3446
47+ if use_tls == 'False' :
48+ use_tls = False
49+
3550 self .email_address = email_address
51+ self .smtp_server = smtp_server
52+ self .smtp_user = smtp_user
53+ self .smtp_password = smtp_password
54+ self .use_tls = use_tls
3655
37- s
56+ def thread_job (self , profile_data ):
57+ envelope = envelopes .Envelope (
58+ to_addr = self .email_address ,
59+ subject = 'Profile data' ,
60+ )
3861
39- def thread_job ( self ):
40- envelope = envelopes . Envelope ( to_addr = self .email_address ,
41- subject = 'Profile data ' )
62+ envelope . add_attachment_from_memory ( profile_data ,
63+ self .default_file_name ,
64+ 'application/octet-stream ' )
4265
43- envelope .add_attachment ('/Users/bilbo/Pictures/helicopter.jpg' )
66+ envelope .send (self .smtp_server , login = self .smtp_user ,
67+ password = self .smtp_password , tls = self .use_tls )
4468
45- envelope .send ('smtp.googlemail.com' , login = 'from@example.com' ,
46- password = 'password' , tls = True )
4769
48- ('profile-%s-%s' % (cleaned_path ,
49- datetime_tools .get_now ()),
50- profile_data ,
51- 'application/octet-stream' ),
5270
5371
72+ class FolderProfileHandler (AuxiliaryThreadProfileHandler ):
73+
74+ def __init__ (self , folder_path ):
75+ self .folder_path = folder_path
76+
77+ def thread_job (self , profile_data ):
78+ with open (os .path .join (self .folder_path , self .default_file_name ), \
79+ 'wb' ) as output_file :
80+ output_file .write (profile_data )
81+
82+
5483
84+ class PrintProfileHandler (BaseProfileHandler ):
85+
86+ def __init__ (self , sort_order ):
87+ self .sort_order = sort_order
88+
89+ def __call__ (self , profile_data ):
90+ pstats .Stats (self ).strip_dirs ().sort_stats (self .sort_order ). \
91+ print_stats ()
92+
5593
5694
5795def get_profile_handler (profile_handler_string ):
58-
59-
60-
96+ if misc_tools .is_legal_email_address (profile_handler_string .split ('\n ' )
97+ [0 ]):
98+ return EmailProfileHandler (* profile_handler_string .split ('\n ' ))
99+ elif os .path .isdir (profile_handler_string ):
100+ return FolderProfileHandler (profile_handler_string )
101+ else :
102+ assert profile_handler_string == '' or int (profile_handler_string )
103+ try :
104+ sort_order = int (profile_handler_string )
105+ except ValueError :
106+ sort_order = - 1
107+ return PrintProfileHandler (sort_order )
0 commit comments