本文整理汇总了Python中msg_db_connector.MSGDBConnector.connectDB方法的典型用法代码示例。如果您正苦于以下问题:Python MSGDBConnector.connectDB方法的具体用法?Python MSGDBConnector.connectDB怎么用?Python MSGDBConnector.connectDB使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msg_db_connector.MSGDBConnector
的用法示例。
在下文中一共展示了MSGDBConnector.connectDB方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestMSGDBConnect
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class TestMSGDBConnect(unittest.TestCase):
"""
These tests require a database connection to be available.
"""
def setUp(self):
self.connector = MSGDBConnector(True)
self.conn = self.connector.connectDB()
self.configer = MSGConfiger()
def test_init(self):
self.assertTrue(
isinstance(self.connector, msg_db_connector.MSGDBConnector),
"self.connection is an instance of MECODBConnector.")
def test_db_connection(self):
"""
DB can be connected to.
"""
self.assertIsNotNone(self.conn, 'DB connection not available.')
# Get the name of the database.
self.assertEqual(
self.configer.configOptionValue('Database', 'testing_db_name'),
self.connector.dbName, 'Testing DB name is not correct.')
def tearDown(self):
self.connector.closeDB(self.conn)
示例2: AllDataAggregator
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class AllDataAggregator(object):
"""
Perform aggregation of all data for a set of predefined data types.
In this class, the raw types are left as empty to allow manual
specification.
"""
def __init__(self):
"""
Constructor.
"""
self.logger = SEKLogger(__name__, 'DEBUG')
self.aggregator = MSGDataAggregator(exitOnError = False,
commitOnEveryInsert = True)
self.notifier = MSGNotifier()
# Available types are in ['weather', 'egauge', 'circuit', 'irradiance'].
self.rawTypes = ['']
self.connector = MSGDBConnector()
self.conn = self.connector.connectDB()
self.cursor = self.conn.cursor()
self.dbUtil = MSGDBUtil()
def aggregateAllData(self):
"""
:return: Nothing.
"""
map(self.aggregator.aggregateAllData, self.rawTypes)
示例3: TestMECODupeChecker
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class TestMECODupeChecker(unittest.TestCase):
"""
Unit tests for duplicate checking.
"""
def setUp(self):
self.dupeChecker = MECODupeChecker()
self.p = MECOXMLParser(True) # run in testing mode
self.dbConnect = MSGDBConnector(True)
self.dbUtil = MSGDBUtil()
self.conn = self.dbConnect.connectDB()
self.cur = self.conn.cursor()
def testInit(self):
self.assertEqual(self.dupeChecker.__class__.__name__, "MECODupeChecker",
"Dupe checker has been created.")
def testFindIndividualDupe(self):
"""
Find a duplicate record when only one exists.
"""
self.dbUtil.eraseTestMeco()
self.p.filename = "../../test-data/meco_v3-energy-test-data.xml"
fileObject = open(self.p.filename, "rb")
self.p.parseXML(fileObject, True)
self.assertTrue(
self.dupeChecker.readingBranchDupeExists(self.conn, '100000',
'2013-04-08 00:30:00',
'1', True),
"Record should already exist")
def testLoadOnTop(self):
"""
If the same data set is loaded in succession,
all values will be duplicated. Verify that this is true.
This is no longer possible as
duplicates are dropped before insertion.
"""
pass
def testLoadSingleMissingEntry(self):
"""
A reading will be inserted into the database where the reading does
not currently exist as determined by the
MeterName-IntervalEndTime-Channel tuple.
"""
pass
def tearDown(self):
self.dbConnect.closeDB(self.conn)
示例4: TestMECOXMLParser
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class TestMECOXMLParser(unittest.TestCase):
"""
Unit tests for MECO XML Parser.
"""
def setUp(self):
self.p = MECOXMLParser(True) # run in testing mode
self.dbConnect = MSGDBConnector(True)
self.dbUtil = MSGDBUtil()
self.conn = self.dbConnect.connectDB()
self.cur = self.conn.cursor()
def testMECOXMLParserCanBeInited(self):
self.assertIsNotNone(self.p)
def testEveryElementIsVisited(self):
self.dbUtil.eraseTestMeco()
self.p.filename = "../../test-data/meco_v3-energy-test-data.xml"
fileObject = open(self.p.filename, "rb")
expectedCount = 125
self.p.parseXML(fileObject, True)
print "element count = %s" % self.p.processForInsertElementCount
self.assertEqual(self.p.processForInsertElementCount, expectedCount)
def testAllTableNamesArePresent(self):
self.dbUtil.eraseTestMeco()
self.p.filename = "../../test-data/meco_v3-energy-test-data.xml"
fileObject = open(self.p.filename, "rb")
self.p.parseXML(fileObject, True)
fail = False
for key in self.p.tableNameCount.keys():
print key + ": ",
print self.p.tableNameCount[key]
if self.p.tableNameCount[key] < 1:
if key != 'ChannelStatus' and key != 'IntervalStatus' and key \
!= 'EventData' and key != 'Event':
print "table = %s" % key
fail = True
self.assertFalse(fail,
"At least one table of each type should have been "
"encountered.")
def tearDown(self):
self.dbConnect.closeDB(self.conn)
示例5: MECONonPVinMLHNotifier
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class MECONonPVinMLHNotifier(object):
def __init__(self):
"""
Constructor.
"""
self.logger = SEKLogger(__name__)
self.viewPVReadingsinNonMLH = ''
self.lastDateProcessed = None
self.connector = MSGDBConnector()
self.conn = self.connector.connectDB()
def sendNewReadingsNotification(self):
pass
def checkForNewReadings(self, lastDate = None):
sql = """SELECT * FROM %s """ % (self.viewPVReadingsinNonMLH)
开发者ID:Hawaii-Smart-Energy-Project,项目名称:Maui-Smart-Grid,代码行数:19,代码来源:meco_pv_readings_in_nonpv_mlh_notifier.py
示例6: TestMECODBRead
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class TestMECODBRead(unittest.TestCase):
def setUp(self):
self.reader = MECODBReader()
self.connector = MSGDBConnector(True)
self.conn = self.connector.connectDB()
self.inserter = MECODBInserter()
self.util = MSGDBUtil()
self.lastSeqVal = None
self.tableName = 'MeterData'
self.colName = 'meter_data_id'
self.deleter = MECODBDeleter()
def testMECODBReadCanBeInited(self):
self.assertIsNotNone(self.reader)
def testSelectRecord(self):
"""
Insert and retrieve a record to test the ability to select a record.
"""
print "testSelectRecord:"
print "self.conn = %s" % self.conn
sampleDict = {'MeterName': '100001', 'UtilDeviceID': '100001',
'MacID': '00:00:00:00:00:00:00:00'}
self.inserter.insertData(self.conn, self.tableName, sampleDict)
self.lastSeqVal = self.util.getLastSequenceID(self.conn, self.tableName,
self.colName)
print "lastSeqVal = %s" % self.lastSeqVal
row = self.reader.selectRecord(self.conn, self.tableName, self.colName,
self.lastSeqVal)
self.assertEqual(row[self.colName], self.lastSeqVal)
def tearDown(self):
# Delete the record that was inserted.
if self.lastSeqVal != None:
self.deleter.deleteRecord(self.conn, self.tableName, self.colName,
self.lastSeqVal)
self.connector.closeDB(self.conn)
示例7: insertData
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
def insertData(files, table, cols, testing = False):
"""
Insert aggregated data generated by this script into a database table.
:param files: A list of the filenames to be processed.
:param table: The name of the table in the DB.
:param cols: A list of the columns (as strings) in the table.
:param testing: Specify whether to use test (false by default).
"""
connector = MSGDBConnector()
conn = connector.connectDB()
dbUtil = MSGDBUtil()
cursor = conn.cursor()
cnt = 0
for file in files:
with open(file, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter = ',')
# Skip the header line.
reader.next()
for row in reader:
sql = """INSERT INTO "%s" (%s) VALUES (%s)""" % (
table, ','.join(cols),
','.join("'" + item.strip() + "'" for item in row))
sql = sql.replace("'NULL'", 'NULL')
dbUtil.executeSQL(cursor, sql)
cnt += 1
if cnt % 10000 == 0:
conn.commit()
conn.commit()
cnt = 0
开发者ID:Hawaii-Smart-Energy-Project,项目名称:Maui-Smart-Grid,代码行数:39,代码来源:insertCleanSCADAVoltageAndTapData.py
示例8: MSGDBExporter
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class MSGDBExporter(object):
"""
Export MSG DBs as SQL scripts.
Supports export to local storage and to cloud storage.
Usage:
from msg_db_exporter import MSGDBExporter
exporter = MSGDBExporter()
Public API:
exportDB(databases:List,
toCloud:Boolean,
testing:Boolean,
numChunks:Integer,
deleteOutdated:Boolean): Export a list of DBs to the cloud.
"""
# List of cloud files.
@property
def cloudFiles(self):
self._cloudFiles = self.driveService.files().list().execute()
return self._cloudFiles
@property
def driveService(self):
if self._driveService:
return self._driveService
if not self.credentialPath:
raise Exception("Credential path is required.")
storage = Storage(
'{}/google_api_credentials'.format(self.credentialPath))
self.googleAPICredentials = storage.get()
self.logger.log("Authorizing credentials.", 'info')
http = httplib2.Http()
http = self.googleAPICredentials.authorize(http)
self.logger.log("Authorized.", 'info')
self._driveService = build('drive', 'v2', http = http)
return self._driveService
def __init__(self):
"""
Constructor.
"""
self.logger = SEKLogger(__name__, 'DEBUG', useColor = False)
self.timeUtil = MSGTimeUtil()
self.configer = MSGConfiger()
self.fileUtil = MSGFileUtil()
self.pythonUtil = MSGPythonUtil() # for debugging
self.connector = MSGDBConnector()
self.conn = self.connector.connectDB()
self.cursor = self.conn.cursor()
self.dbUtil = MSGDBUtil()
self.notifier = SEKNotifier(connector = self.connector,
dbUtil = self.dbUtil,
user = self.configer.configOptionValue(
'Notifications', 'email_username'),
password = self.configer.configOptionValue(
'Notifications', 'email_password'),
fromaddr = self.configer.configOptionValue(
'Notifications', 'email_from_address'),
toaddr = self.configer.configOptionValue(
'Notifications', 'email_recipients'),
testing_toaddr =
self.configer.configOptionValue(
'Notifications',
'testing_email_recipients'),
smtp_server_and_port =
self.configer.configOptionValue(
'Notifications',
'smtp_server_and_port'))
# Google Drive parameters.
self.clientID = self.configer.configOptionValue('Export',
'google_api_client_id')
self.clientSecret = self.configer.configOptionValue('Export',
'google_api_client_secret')
self.oauthScope = 'https://www.googleapis.com/auth/drive'
self.oauthConsent = 'urn:ietf:wg:oauth:2.0:oob'
self.googleAPICredentials = ''
self.exportTempWorkPath = self.configer.configOptionValue('Export',
'db_export_work_path')
self.credentialPath = self.configer.configOptionValue('Export',
'google_api_credentials_path')
self.credentialStorage = Storage(
'{}/google_api_credentials'.format(self.credentialPath))
self._driveService = None
self._cloudFiles = None
#.........这里部分代码省略.........
示例9: NewDataAggregator
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class NewDataAggregator(object):
"""
Perform aggregation of new data for a set of predefined data types (self
.rawTypes).
"""
def __init__(self):
"""
Constructor.
"""
self.logger = SEKLogger(__name__, 'DEBUG')
self.aggregator = MSGDataAggregator()
self.notifier = MSGNotifier()
self.rawTypes = [x.name for x in list(MSGAggregationTypes)]
self.connector = MSGDBConnector()
self.conn = self.connector.connectDB()
self.cursor = self.conn.cursor()
self.dbUtil = MSGDBUtil()
def sendNewDataNotification(self, result = None, testing = False):
"""
Sending notification reporting on new data being available since the
last time new data was reported.
:param result: list of dicts containing aggregation results as
provided by MSGDataAggregator::aggregateNewData.
:param testing: Use testing mode when True.
"""
self.logger.log('result {}'.format(result), 'debug')
lastReportDate = self.notifier.lastReportDate(
MSGNotificationHistoryTypes.MSG_DATA_AGGREGATOR)
if not lastReportDate:
lastReportDate = "never"
if not result:
msgBody = '\nNew data has NOT been aggregated in {}. No result ' \
'was obtained. This is an error that should be ' \
'investigated.'.format(self.connector.dbName)
else:
msgBody = '\nNew data has been aggregated in {}.'.format(
self.connector.dbName)
msgBody += '\n\n'
for i in range(len(result)):
msgBody += 'The new data count for type {} is {} readings' \
'.\n'.format(result[i].keys()[0],
result[i].values()[0])
msgBody += '\n\n'
msgBody += 'The last report date was %s.' % lastReportDate
msgBody += '\n\n'
self.notifier.sendNotificationEmail(msgBody, testing = testing)
self.notifier.recordNotificationEvent(
MSGNotificationHistoryTypes.MSG_DATA_AGGREGATOR)
def aggregateNewData(self):
"""
:return: list of dicts obtained from
MSGDataAggregator::aggregateNewData.
"""
result = map(self.aggregator.aggregateNewData, self.rawTypes)
self.logger.log('result {}'.format(result))
return result
示例10: processCommandLineArguments
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
argParser.add_argument('--testing', action = 'store_true', default = False,
help = 'If this flag is on, '
'insert data to the testing database as '
'specified in the local configuration file.')
commandLineArgs = argParser.parse_args()
if __name__ == '__main__':
processCommandLineArguments()
filename = COMMAND_LINE_ARGS.filename
success = True
anyFailure = False
connector = MSGDBConnector(testing = COMMAND_LINE_ARGS.testing)
conn = connector.connectDB()
cur = conn.cursor()
dbUtil = MSGDBUtil()
notifier = MSGNotifier()
msgBody = ''
configer = MSGConfiger()
if COMMAND_LINE_ARGS.testing:
dbName = configer.configOptionValue("Database", "testing_db_name")
else:
dbName = configer.configOptionValue("Database", "db_name")
msg = ("Loading Meter Location History data in file %s to database %s.\n" % (
filename, dbName))
sys.stderr.write(msg)
msgBody += msg
开发者ID:Hawaii-Smart-Energy-Project,项目名称:Maui-Smart-Grid,代码行数:33,代码来源:insertMECOMeterLocationHistoryData.py
示例11: MSGDBUtilTester
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class MSGDBUtilTester(unittest.TestCase):
"""
Unit tests for MECO DB Utils.
"""
def setUp(self):
self.i = MECODBInserter()
# Connect to the testing database.
self.connector = MSGDBConnector(testing = True)
self.conn = self.connector.connectDB()
self.lastSeqVal = None
# Does this work having the dictCur be in another class?
self.dictCur = self.connector.dictCur
self.cursor = self.conn.cursor()
self.deleter = MECODBDeleter()
self.tableName = 'MeterData'
self.columnName = 'meter_data_id'
self.configer = MSGConfiger()
self.logger = MSGLogger(__name__, 'debug')
self.dbUtil = MSGDBUtil()
def testMECODBUtilCanBeInited(self):
self.assertIsNotNone(self.dbUtil)
def testLastSequenceNumberIsCorrect(self):
"""
Test if last sequence ID value is generated correctly. Do this by
inserting and deleting a DB record.
"""
# Insert some values.
sampleDict = {'MeterName': '100001', 'UtilDeviceID': '100001',
'MacID': '00:00:00:00:00:00:00:00'}
self.i.insertData(self.conn, self.tableName, sampleDict)
self.lastSeqVal = self.dbUtil.getLastSequenceID(self.conn,
self.tableName,
self.columnName)
print "lastSeqVal = %s" % self.lastSeqVal
sql = """SELECT * FROM "%s" WHERE %s = %s""" % (
self.tableName, self.columnName, self.lastSeqVal)
dictCur = self.connector.dictCur
self.dbUtil.executeSQL(dictCur, sql)
row = dictCur.fetchone()
meterDataID = row[self.columnName]
self.assertEqual(self.lastSeqVal, meterDataID)
def testGetDBName(self):
dbName = self.dbUtil.getDBName(self.cursor)[0]
self.logger.log("DB name is %s" % dbName, 'info')
self.assertEqual(dbName, "test_meco",
"Testing DB name should be set correctly.")
def testEraseTestingDatabase(self):
"""
Test that calls to eraseTestMeco() work correctly.
"""
dbName = self.dbUtil.getDBName(self.cursor)[0]
self.logger.log("DB name is %s" % dbName, 'info')
self.assertEqual(dbName, "test_meco",
"Testing DB name should be set correctly.")
self.dbUtil.eraseTestMeco()
# Check all of the tables for the presence of records.
for table in self.configer.insertTables:
sql = """select count(*) from "%s";""" % table
self.dbUtil.executeSQL(self.dictCur, sql)
row = self.dictCur.fetchone()
self.assertEqual(row[0], 0,
"No records should be present in the %s table."
% table)
def testColumns(self):
"""
Test the ability to retrieve the column names from a database.
"""
print self.dbUtil.columns(self.cursor, 'Event')
def tearDown(self):
"""
Delete the record that was inserted.
"""
if self.lastSeqVal != None:
self.deleter.deleteRecord(self.conn, self.tableName,
self.columnName, self.lastSeqVal)
self.connector.closeDB(self.conn)
示例12: MECOXMLParser
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class MECOXMLParser(object):
"""
Parses XML for MECO data.
"""
tableName = ""
def __init__(self, testing=False):
"""
Constructor.
:param testing: (optional) Boolean indicating if Testing Mode is on.
"""
self.logger = SEKLogger(__name__, "silent")
if testing:
self.logger.log("Testing Mode is ON.", "info")
self.debug = False
self.configer = MSGConfiger()
if self.configer.configOptionValue("Debugging", "debug") == True:
self.debug = True
self.util = MSGDBUtil()
self.mapper = MECOMapper()
self.connector = MSGDBConnector(testing)
self.conn = self.connector.connectDB()
self.filename = None
self.fileObject = None
self.processForInsertElementCount = 0
self.inserter = MECODBInserter()
self.insertDataIntoDatabase = False
# Count number of times sections in source data are encountered.
self.tableNameCount = {
"SSNExportDocument": 0,
"MeterData": 0,
"RegisterData": 0,
"RegisterRead": 0,
"Tier": 0,
"Register": 0,
"IntervalReadData": 0,
"Interval": 0,
"Reading": 0,
"IntervalStatus": 0,
"ChannelStatus": 0,
"EventData": 0,
"Event": 0,
}
# Use this dictionary to track which channels were processed when
# readings are being processed. this is to prevent duplicate channel
# data from being inserted.
self.channelProcessed = {}
self.initChannelProcessed()
# Tables to be inserted to.
self.insertTables = self.configer.insertTables
self.lastSeqVal = None
self.fKeyVal = None
self.lastTable = None
self.fkDeterminer = MECOFKDeterminer()
self.dupeChecker = MECODupeChecker()
self.currentMeterName = None
self.currentIntervalEndTime = None
self.currentRegisterReadReadTime = None
self.dupesExist = False
self.channelDupeExists = False # For Reading dupes.
self.numberDupeExists = False # For Register dupes.
self.eventTimeDupeExists = False # For Event dupes.
self.commitCount = 0
self.readingDupeOnInsertCount = 0 # For Reading dupes.
self.registerDupeOnInsertCount = 0 # For Register dupes.
self.eventDupeOnInsertCount = 0 # For Event dupes.
self.dataProcessCount = 0
self.readingDupeCheckCount = 0 # For Reading dupes.
self.registerDupeCheckCount = 0 # For Register dupes.
self.eventDupeCheckCount = 0 # For Event dupes.
self.insertCount = 0
self.cumulativeInsertCount = 0
self.nonProcessForInsertElementCount = 0
self.readingInsertCount = 0
self.registerInsertCount = 0
self.eventInsertCount = 0
self.totalReadingInsertCount = 0
self.totalRegisterInsertCount = 0
self.totalEventInsertCount = 0
self.totalReadingDupeOnInsertCount = 0
self.totalRegisterDupeOnInsertCount = 0
self.totalEventDupeOnInsertCount = 0
def parseXML(self, fileObject, insert=False, jobID=""):
"""
Parse an XML file.
:param fileObject: a file object referencing an XML file.
:param insert: (optional) True to insert to the database | False to
#.........这里部分代码省略.........
示例13: MSGNotifier
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class MSGNotifier(object):
"""
Provides notification service functionality for MSG data processing.
Email settings are stored in the local configuration.
Usage:
from msg_notifier import MSGNotifier
self.notifier = MSGNotifier()
Public API:
sendNotificationEmail(msgBody, testing = False):
Send msgBody as a notification to the mailing list defined in the
config file.
sendMailWithAttachments(msgBody, files = None, testing = False)
Send msgBody with files attached as a notification to the mailing
list defined in the config file.
lastReportDate(noticeType):
The last date where a notification of the given type was reported.
recordNotificationEvent(noticeType):
Record an event in the notification history.
"""
def __init__(self):
"""
Constructor.
"""
warnings.simplefilter('default')
warnings.warn("This module is deprecated in favor of SEKNotifier.",
DeprecationWarning)
self.config = MSGConfiger()
self.logger = SEKLogger(__name__, 'info')
self.connector = MSGDBConnector()
self.conn = self.connector.connectDB()
self.cursor = self.conn.cursor()
self.dbUtil = MSGDBUtil()
self.noticeTable = 'NotificationHistory'
self.notificationHeader = "This is a message from the Hawaii Smart " \
"Energy Project MSG Project notification " \
"system.\n\n"
self.noReplyNotice = '\n\nThis email account is not monitored. No ' \
'replies will originate from this ' \
'account.\n\nYou are receiving this message ' \
'because you are on the recipient list for ' \
'notifications for the Hawaii Smart Energy ' \
'Project.'
def sendNotificationEmail(self, msgBody, testing = False):
"""
This method is an alternative to the multipart method in
sendMailWithAttachments.
:param msgBody: The body of the message to be sent.
:param testing: True if running in testing mode.
:returns: True for success, False for an error.
"""
errorOccurred = False
user = self.config.configOptionValue('Notifications', 'email_username')
password = self.config.configOptionValue('Notifications',
'email_password')
fromaddr = self.config.configOptionValue('Notifications',
'email_from_address')
if testing:
toaddr = self.config.configOptionValue('Notifications',
'testing_email_recipients')
else:
toaddr = self.config.configOptionValue('Notifications',
'email_recipients')
server = smtplib.SMTP(self.config.configOptionValue('Notifications',
'smtp_server_and_port'))
try:
server.starttls()
except smtplib.SMTPException as detail:
errorOccurred = True
self.logger.log("Exception during SMTP STARTTLS: {}".format(detail),
'ERROR')
try:
server.login(user, password)
except smtplib.SMTPException as detail:
errorOccurred = True
self.logger.log("Exception during SMTP login: %s" % detail, 'ERROR')
senddate = datetime.now().strftime('%Y-%m-%d')
subject = "HISEP Notification"
msgHeader = "Date: {}\r\nFrom: {}\r\nTo: {}\r\nSubject: {" \
#.........这里部分代码省略.........
示例14: NewDataAggregator
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class NewDataAggregator(object):
"""
Perform aggregation of new data for a set of predefined data types (self
.rawTypes).
"""
def __init__(self):
"""
Constructor.
"""
self.logger = MSGLogger(__name__, 'DEBUG')
self.aggregator = MSGDataAggregator()
self.notifier = MSGNotifier()
self.rawTypes = ['weather', 'egauge', 'circuit', 'irradiance']
self.connector = MSGDBConnector()
self.conn = self.connector.connectDB()
self.cursor = self.conn.cursor()
self.dbUtil = MSGDBUtil()
def lastReportDate(self, notificationType):
"""
Get the last time a notification was reported.
:param notificationType: string indicating the type of the
notification. It is stored in the event history.
:returns: datetime of last report date.
"""
cursor = self.cursor
sql = """SELECT MAX("notificationTime") FROM "{}" WHERE
"notificationType" = '{}'""".format(NOTIFICATION_HISTORY_TABLE,
notificationType)
success = self.dbUtil.executeSQL(cursor, sql)
if success:
rows = cursor.fetchall()
if not rows[0][0]:
return None
else:
return rows[0][0]
else:
raise Exception('Exception during getting last report date.')
def sendNewDataNotification(self, result = None, testing = False):
"""
Sending notification reporting on new data being available since the
last time new data was reported.
:param result: list of dicts containing aggregation results as
provided by MSGDataAggregator::aggregateNewData.
:param testing: Use testing mode when True.
"""
self.logger.log('result {}'.format(result), 'debug')
lastReportDate = self.lastReportDate(NOTIFICATION_HISTORY_TYPE)
if not lastReportDate:
lastReportDate = "never"
if not result:
msgBody = '\nNew data has NOT been aggregated in {}. No result ' \
'was obtained. This is an error that should be ' \
'investigated.'.format(self.connector.dbName)
else:
msgBody = '\nNew data has been aggregated in {}.'.format(
self.connector.dbName)
msgBody += '\n\n'
for i in range(len(result)):
msgBody += 'The new data count for type {} is {} readings' \
'.\n'.format(result[i].keys()[0],
result[i].values()[0])
msgBody += '\n\n'
msgBody += 'The last report date was %s.' % lastReportDate
msgBody += '\n\n'
self.notifier.sendNotificationEmail(msgBody, testing = testing)
self.saveNotificationTime()
def saveNotificationTime(self):
"""
Save a notification event to the notification history.
"""
cursor = self.cursor
sql = """INSERT INTO "{}" ("notificationType", "notificationTime")
VALUES ('{}', NOW())""".format(NOTIFICATION_HISTORY_TABLE,
NOTIFICATION_HISTORY_TYPE)
success = self.dbUtil.executeSQL(cursor, sql)
self.conn.commit()
if not success:
raise Exception('Exception while saving the notification time.')
def aggregateNewData(self):
"""
:return: list of dicts obtained from
#.........这里部分代码省略.........
示例15: TestMECODBInserter
# 需要导入模块: from msg_db_connector import MSGDBConnector [as 别名]
# 或者: from msg_db_connector.MSGDBConnector import connectDB [as 别名]
class TestMECODBInserter(unittest.TestCase):
"""
Unit tests for the MECO XML Parser.
"""
def setUp(self):
self.i = MECODBInserter()
self.util = MSGDBUtil()
self.connector = MSGDBConnector(True)
self.deleter = MECODBDeleter()
self.reader = MECODBReader()
self.lastSeqVal = None
self.conn = self.connector.connectDB()
self.sampleTableName = 'MeterData'
self.sampleDict = {'MeterName': '100001', 'UtilDeviceID': '100001',
'MacID': '00:00:00:00:00:00:00:00'}
self.keyName = 'meter_data_id'
def testMECODBInserterCanBeInited(self):
localInserter = MECODBInserter()
self.assertIsInstance(self.i, type(localInserter))
def testInsertionToMeterDataTable(self):
"""
Data can be written to the Meter Data table.
"""
# Insert some values.
self.i.insertData(self.conn, self.sampleTableName, self.sampleDict)
# Retrieve the last fetched value.
self.lastSeqVal = self.util.getLastSequenceID(self.conn,
self.sampleTableName,
self.keyName)
print "lastSeqVal = %s" % self.lastSeqVal
row = self.reader.selectRecord(self.conn, self.sampleTableName,
self.keyName, self.lastSeqVal)
self.assertEqual(row[self.keyName], self.lastSeqVal)
def test_fkey_value_is_correct(self):
"""
Verify that the fkey value used during insertion is correct.
"""
def testInsertionsSums(self):
"""
"""
pass
def tearDown(self):
# Delete the record that was inserted.
if self.lastSeqVal != None:
self.deleter.deleteRecord(self.conn, self.sampleTableName,
self.keyName, self.lastSeqVal)
self.connector.closeDB(self.conn)