13
13
import pytds
14
14
import mysql .connector
15
15
import psycopg2
16
- from dbjob import DBJob
16
+ from dbjobs . dbjob import DBJob
17
17
18
- # Trick to make it work with both Python 2 and 3:
19
- try :
20
- import configparser
21
- except ImportError :
22
- import ConfigParser as configparser
18
+ import configparser
23
19
24
20
class DBReport (DBJob ):
25
21
"""Utility methods to create a report and send it as en email attachment"""
26
22
27
- def __init__ (self , log_level = logging .DEBUG ):
28
- super ().__init__ (log_level = log_level )
29
- self .get_parameters ()
30
-
31
- nowstr = str (datetime .now ().strftime ('%Y%m%d-%H%M%S' ))
23
+ def __init__ (self , job_section = None , conf_dir = None , log_level = logging .DEBUG ):
24
+ super ().__init__ (job_section = job_section , conf_dir = conf_dir , log_level = log_level )
32
25
self .filesuffix = self .job ['file_suffix' ]
33
- self .filename = '%s%s_%s-%s_%s%s' % (self .fileprefix , self .interval , self .start_year , self .start_month , nowstr , self .filesuffix )
34
- self .set_logging (level = log_level , filepath = os .path .join (self .working_dir , '%s%s_%s-%s.log' % (self .fileprefix , self .interval , self .start_year , self .start_month )))
35
26
36
- def get_parameters (self ):
27
+ def get_start_date (self , interval = None , start_year = None , start_month = None ):
37
28
# Get input parameters, otherwise use default values
38
- self .interval = 'month'
39
-
40
29
today = date .today ()
41
30
first = today .replace (day = 1 )
42
31
prev_date = first - timedelta (days = 1 )
43
32
44
- if len (sys .argv ) > 2 :
45
- self .interval = sys .argv [2 ]
33
+ if interval :
34
+ self .interval = interval
35
+ else :
36
+ self .interval = "month"
46
37
47
- if len (sys .argv ) >= 2 :
48
- if self .interval == 'month' :
49
- self .start_year = prev_date .year
50
- self .start_month = prev_date .month
51
- elif self .interval == 'year' :
52
- self .start_year = prev_date .year - 1
53
- self .start_month = prev_date .month
54
- else :
55
- err_msg = 'interval must be "month" or "year"'
56
- logging .getLogger ().error (err_msg )
57
- raise AttributeError (err_msg )
38
+ if self .interval == "month" :
39
+ self .start_year = prev_date .year
40
+ self .start_month = prev_date .month
58
41
59
- if len (sys .argv ) > 3 :
60
- self .start_year = sys .argv [3 ] # e.g. 2018
61
- if len (sys .argv ) > 4 :
62
- self .start_month = sys .argv [4 ] # e.g. 02
42
+ elif self .interval == "year" :
43
+ self .start_year = prev_date .year - 1
44
+ self .start_month = prev_date .month
63
45
64
- self .start_date = '%s/%s/01' % (self .start_year , self .start_month )
46
+ else :
47
+ self .error ('interval must be "month" or "year"' )
48
+
49
+ if start_year :
50
+ self .start_year = start_year
51
+ if start_month :
52
+ self .start_month = start_month
53
+
54
+ return f'{ self .start_year } /{ self .start_month } /01'
65
55
66
- def read_config (self , job_section ):
67
- super ().read_config (job_section )
56
+ def read_config (self , job_section , conf_dir ):
57
+ super ().read_config (job_section , conf_dir = conf_dir )
68
58
self .sender = self .config ['sender' ]
69
59
self .recipients = self .config ['recipients' ]
70
60
71
- def make_sql (self ):
61
+ def make_sql (self , interval , start_year , start_month ):
72
62
"""Create proper SQL from the template - merge the headers in as aliases"""
73
63
self .headers = self .job ['sql_headers' ].split (',' )
74
64
fmt = copy .deepcopy (self .headers )
65
+
66
+ self .start_date = self .get_start_date (interval , start_year , start_month )
75
67
fmt .append (self .start_date )
76
- fmt .append (self .interval )
68
+ fmt .append (interval )
69
+ nowstr = str (datetime .now ().strftime ('%Y%m%d-%H%M%S' ))
70
+ self .filename = '%s%s_%s-%s_%s%s' % (self .fileprefix , interval , start_year , start_month , nowstr , self .filesuffix )
77
71
self .sql = self .job ['sql' ].format (* fmt )
78
72
79
73
def run_job (self ):
@@ -171,7 +165,7 @@ def create_csv(self, result_set):
171
165
logging .getLogger ().debug (msg )
172
166
173
167
174
- def send_email (self ):
168
+ def send_email (self , subject ):
175
169
report_name = self .job ["fullname" ]
176
170
attach_report = True
177
171
if self .config ["attach" ].lower () == "no" :
@@ -180,7 +174,7 @@ def send_email(self):
180
174
filepath = os .path .join (self .working_dir , self .filename )
181
175
182
176
message = MIMEMultipart ()
183
- message ['Subject' ] = '%s for %s starting %s' % ( report_name , self . interval , self . start_date )
177
+ message ['Subject' ] = subject
184
178
message ['From' ] = self .sender
185
179
message ['To' ] = self .recipients
186
180
0 commit comments