def start_logging (self): if self._logger is not None: return self._logger = logging.getLogger('Logger1') self._logger.setLevel(logging.DEBUG) log_full_path_file_name = '%s/log.log' % os.path.dirname(os.path.realpath(__file__)) log_file_info = QFileInfo(log_full_path_file_name) if log_file_info.exists(): log_created_qdatetime = log_file_info.created() current_qdatetime = QDateTime.currentDateTime() max_difference_secs = 60 * 60 * 24 * 7 # 7 giorni current_difference_secs = log_created_qdatetime.secsTo(current_qdatetime) if current_difference_secs > max_difference_secs: QFile.remove(log_full_path_file_name) self._log_handler = LogHandler(log_full_path_file_name) formatter = logging.Formatter('%(asctime)s - %(thread)d - %(name)s - %(levelname)s - %(message)s') self._log_handler.setFormatter(formatter) self._logger.addHandler(self._log_handler) # Do not raise logging system exceptions logging.raiseExceptions = False # Connect log signal self._log_handler.logged.connect (self.message_logged) self.logging_enabled = True
def select_log(): tkinter.Tk().withdraw() filename = tkinter.filedialog.askopenfilename( initialdir=LogHandler.get_log_dir()) assert bool(filename), 'file not selected' logs = LogHandler.load_month_logs(os.path.basename(filename)) LogHandler.check_logs(logs) return logs
def main(): level = ArgumentParser().start() if level is not None: LogHandler(log_level=level) else: LogHandler() process_data = ProcessData() #process_data.test_jitbit() process_data.start()
def setUp(self): """Initialization of the tests""" # Create temporary log file for testing the methods self.logPath = "tmp.log" self.generatingRate = 60 self.entryGenerator = EntryGenerator(self.logPath, self.generatingRate) now = datetime.now() # Add a 10 hours old entry self.entryGenerator.write_entry(now - timedelta(hours=10)) # Add two entries to the current time self.entryGenerator.write_entry(now) self.entryGenerator.write_entry(now) # Setup the LogHandler object prior testing self.refreshPeriod = 2 self.alertThreshold = 20 self.monitorDuration = 10 self.logHandler = LogHandler(self.logPath, self.refreshPeriod, self.alertThreshold, self.monitorDuration) # Disable logHandler console output for the tests self.logHandler.printStatus = False
def main(args): try: logging.basicConfig(level=args.log_level) except ValueError as e: print('Unable to initialise logging: %s' % e) raise SystemExit logging.info('Starting the server...') logging.info('Version: %s.', server_version()) config.config = config.Config.load(args.config_file) from db import load_db, dump_db load_db() from networking import factory listener = reactor.listenTCP(config.config.port, factory, interface=config.config.interface) logging.info('Listening on %s:%d.', listener.interface, listener.port) from log_handler import LogHandler logging.getLogger().addHandler(LogHandler()) reactor.run() logging.info('Server shutting down.') dump_db() config.config.dump(args.config_file)
def format_row(log): date_id = LogHandler.get_date_id(log['from']) date = date_id if date_id not in done else '' start = LogHandler.format_clocktime(log['from']) end = LogHandler.format_clocktime(log['to']) elapsed = LogHandler.format_duration(log['to'] - log['from']) elapsed_in_day = LogHandler.format_duration( duration_in_day[date_id]) if date_id not in done else '' delta = LogHandler.format_duration_difference( duration_in_day[date_id], timedelta_should_per_day) if date_id not in done else '' content = os.linesep.join([ format_cell(elem) for elem in [date, start, end, elapsed, elapsed_in_day, delta] ]) done.add(date_id) return content
def main(): LogHandler.merge_session() LogHandler.backup() handler = LogHandler() try: while True: handler.update_session() LogHandler.sleep() except: handler.update_session() LogHandler.merge_session() handler.print_progress_today(force=True)
def game(): # load logs and lithology log_info = LogHandler() # game parameters and initial settings clock = pg.time.Clock() done = False selected_user_lith = "Unknown" # start drawing # Fill the background with DARK GREY screen.fill(art.DARK_GREY) # draw chart background pg.draw.rect(screen, art.GREY, [20, 20, 685, 710], 0) # draw log chart # background pg.draw.rect(screen, art.WHITE, [40, 40, 645, 670], 0) well_names = [ 'BLUE MAVERICK 34-1', 'BIG CRAWDAD 25-3-34', 'BIG MONEY HONEY 6-43-5H', 'BEANS STATION 15-8-1H', 'AUNT MABEL 25-9-23', 'LIONS GRASP 4-32-50', 'BOBS COUCH 10-5-23 4', 'PETERSON RANCH 4-25-3 1H', 'BLACK GOLD 2-45', 'FINGERS CROSSED 9-12', ] # draw well name and info header_text = header_font.render(well_names[random.randint(0, 9)], True, art.BLACK) screen.blit(header_text, (int(370 - (.5 * header_text.get_rect().width)), 60)) # log labels and scale gr_text = font.render('0 —— Gamma Ray (API) —— 150', True, art.BLACK) pe_text = font.render('0 ———— Pe (barns/e) ———— 10', True, art.RED) dp_text = font.render( '30 ————-——— Density Porosity (ls eq. units) ———————— -10', True, art.GREEN) np_text = font.render( '30 ————-——— Neutron Porosity (ls eq. units) ———————— -10', True, art.BLACK) # track 1 (Gamma Ray) # track1_surface_size = (180, 500) track1_surface_location = (60, 190) # draw log label and scale screen.blit( gr_text, (track1_surface_location[0] - 3, track1_surface_location[1] - 25)) # draw the track surface draw_track(screen, log_info, track1_surface_size, track1_surface_location, 1) # track 2 (Depth) # track2_surface_size = (65, 500) track2_surface_location = (240, 190) track2 = pg.Surface(track2_surface_size) track2.fill(art.WHITE) # draw depth values and markers draw_depth(track2, log_info.n_zones, log_info.logs['DEPTH']) screen.blit(track2, track2_surface_location) # track 3 (PE, Density, Neutron) # track3_surface_size = (360, 500) track3_surface_location = (305, 190) # draw log label and scale screen.blit( pe_text, (track3_surface_location[0] - 3, track3_surface_location[1] - 75)) screen.blit( dp_text, (track3_surface_location[0] - 3, track3_surface_location[1] - 50)) screen.blit( np_text, (track3_surface_location[0] - 3, track3_surface_location[1] - 25)) # draw the track surface draw_track(screen, log_info, track3_surface_size, track3_surface_location, 3) # create buttons # depth track lith buttons lith_buttons = [] for x in range(0, log_info.n_zones): height = track2_surface_size[1] / log_info.n_zones lith_buttons.append( LithButton(track2_surface_location, 0, int(x * height), track2_surface_size[0], height)) # button screen buttons_surface = pg.Surface((300, 710)) buttons_surface_location = (725, 20) buttons_surface.fill(art.GREY) # create user lith buttons halite_button = Button(buttons_surface_location, 20, 105, 80, 50, 'Halite') gypsum_button = Button(buttons_surface_location, 110, 105, 80, 50, 'Gypsum') anhydrite_button = Button(buttons_surface_location, 200, 105, 80, 50, 'Anhydrite') dolomite_button = Button(buttons_surface_location, 110, 165, 80, 50, 'Dolomite') dolomitcls_button = Button(buttons_surface_location, 20, 225, 80, 50, 'Dolomitic LS') chertydolls_button = Button(buttons_surface_location, 110, 225, 80, 50, 'Cherty Dol LS') chertydol_button = Button(buttons_surface_location, 200, 225, 80, 50, 'Cherty Dol') limestone_button = Button(buttons_surface_location, 20, 285, 80, 50, 'Limestone') chertyls_button = Button(buttons_surface_location, 110, 285, 80, 50, 'Cherty LS') chert_button = Button(buttons_surface_location, 200, 285, 80, 50, 'Chert') shale_button = Button(buttons_surface_location, 110, 345, 80, 50, 'Shale') sandstone_button = Button(buttons_surface_location, 20, 405, 80, 50, 'Sandstone') ironstone_button = Button(buttons_surface_location, 110, 405, 80, 50, 'Ironstone') coal_button = Button(buttons_surface_location, 200, 405, 80, 50, 'Coal') newlognolith_button = Button(buttons_surface_location, 75, 485, 150, 30, 'New log without lithology') newloglith_button = Button(buttons_surface_location, 75, 525, 150, 30, 'New log with lithology') checklith_button = OnOffButton(buttons_surface_location, 75, 565, 150, 30, 'Check lithology', False) pg.display.update() while not done: # draw user lith buttons halite_button.draw(buttons_surface, selected_user_lith) gypsum_button.draw(buttons_surface, selected_user_lith) anhydrite_button.draw(buttons_surface, selected_user_lith) dolomite_button.draw(buttons_surface, selected_user_lith) dolomitcls_button.draw(buttons_surface, selected_user_lith) chertydolls_button.draw(buttons_surface, selected_user_lith) chertydol_button.draw(buttons_surface, selected_user_lith) limestone_button.draw(buttons_surface, selected_user_lith) chertyls_button.draw(buttons_surface, selected_user_lith) chert_button.draw(buttons_surface, selected_user_lith) shale_button.draw(buttons_surface, selected_user_lith) sandstone_button.draw(buttons_surface, selected_user_lith) ironstone_button.draw(buttons_surface, selected_user_lith) coal_button.draw(buttons_surface, selected_user_lith) newlognolith_button.draw(buttons_surface) newloglith_button.draw(buttons_surface) checklith_button.draw(buttons_surface) # check for button clicks for event in pg.event.get(): if event.type == pg.QUIT: done = True if event.type == pg.MOUSEBUTTONDOWN: if halite_button.selected(event.pos): selected_user_lith = 'Halite' break if gypsum_button.selected(event.pos): selected_user_lith = "Gypsum" break if anhydrite_button.selected(event.pos): selected_user_lith = "Anhydrite" break if dolomite_button.selected(event.pos): selected_user_lith = "Dolomite" break if dolomitcls_button.selected(event.pos): selected_user_lith = "Dolomitic LS" break if chertydolls_button.selected(event.pos): selected_user_lith = "Cherty Dol LS" break if chertydol_button.selected(event.pos): selected_user_lith = "Cherty Dol" break if limestone_button.selected(event.pos): selected_user_lith = "Limestone" break if chertyls_button.selected(event.pos): selected_user_lith = "Cherty LS" break if chert_button.selected(event.pos): selected_user_lith = "Chert" break if shale_button.selected(event.pos): selected_user_lith = "Shale" break if sandstone_button.selected(event.pos): selected_user_lith = "Sandstone" break if ironstone_button.selected(event.pos): selected_user_lith = "Ironstone" break if coal_button.selected(event.pos): selected_user_lith = "Coal" break if newlognolith_button.selected(event.pos): log_info = LogHandler() draw_track(screen, log_info, track1_surface_size, track1_surface_location, 1) draw_track(screen, log_info, track3_surface_size, track3_surface_location, 3) track2.fill(art.WHITE) draw_depth(track2, log_info.n_zones, log_info.logs['DEPTH']) for button in lith_buttons: button.lith_status = 'Unknown' break if newloglith_button.selected(event.pos): log_info = LogHandler() draw_track(screen, log_info, track1_surface_size, track1_surface_location, 1) draw_track(screen, log_info, track3_surface_size, track3_surface_location, 3) for i, button in enumerate(lith_buttons): button.lith_status = log_info.get_lith( log_info.true_lith[i]) track2.blit(log_info.get_lith_art(button.lith_status), (button.x, button.y)) draw_depth(track2, log_info.n_zones, log_info.logs['DEPTH']) break checklith_button.selected(event.pos) # draw lith buttons for button in lith_buttons: if button.selected(event.pos, selected_user_lith): for button_2 in lith_buttons: track2.blit( log_info.get_lith_art(button_2.lith_status), (button_2.x, button_2.y)) draw_depth(track2, log_info.n_zones, log_info.logs['DEPTH']) break if checklith_button.status: text = font.render('x', True, art.RED) for i, button in enumerate(lith_buttons): if button.lith_status != 'Unknown': if log_info.get_lith(button.lith_status) != int( log_info.true_lith[i]): track2.blit(text, (button.x + 3, button.y - 2)) screen.blit(track2, track2_surface_location) screen.blit(buttons_surface, buttons_surface_location) pg.display.update() clock.tick(30)
logger.error("Invalid date/time format: {0}.".format(dt)) sys.exit(1) return '%d-%02d-%02d %02d:%02d:%02d' % (year, month, day, hour, min, sec) #------------------------------------------------------------------------------ """ Main """ #------------------------------------------------------------------------------ if __name__ == "__main__": # subscribe for signal from version import __version__ logger = LogHandler('hcdiag_query', logging.DEBUG) #logger.open_console(logging.DEBUG) # now we can start logging! logger.info('{0}, version {1}, running on {2} {3}, {4} machine.' \ .format( __file__, __version__, os.uname()[0], os.uname()[2], os.uname()[1].split(".")[0])) parser = argparse.ArgumentParser() parser.add_argument( '--runid', '-r', type=str, nargs='+', metavar='id', help= 'unique udentifier of the diagnostic run. All other arguments will be ignored.'
def main(): LogHandler.extend_until_now()
from resources.user_agent import USER_AGENT_FIREFOX from lxml import etree from mongo.mongo import db from redisclient.redis_pool import redis_client from log_handler import LogHandler import time import os import urllib import asyncio import aiohttp from compress_util import Compress as compress log = LogHandler("baidubaike") class BaiduBaike: def __init__(self): # 百科首页 self.baike_home = "http://baike.baidu.com" # 请求头 self.header = { "user-agent": USER_AGENT_FIREFOX, "host": "baike.baidu.com", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "accept-language": "zh-CN,en-US;q=0.8,zh;q=0.7,zh-TW;q=0.5,zh-HK;q=0.3,en;q=0.2", "accept-encoding": "gzip, deflate, br", "connection": "keep-alive", "upgrade-insecure_requests": "1", "cache-control": 'max-age=0',
""" Copyright(c) 2018 Gang Zhang All rights reserved. Author:Gang Zhang Date:2018.02.01 """ from flask import Flask, jsonify, request from log_handler import LogHandler from proxy_manager import ProxyManager app = Flask(__name__) logger = LogHandler('proxy_api') api_list = { 'get': u'get an usable proxy', 'get_all': u'get all proxy from proxy pool', 'delete?proxy=127.0.0.1:8080': u'delete an unable proxy', 'get_status': u'proxy statistics' } @app.route('/') def index(): return jsonify(api_list) @app.route('/get/') def get(): proxy = ProxyManager(logger).get()
def func_update(): while True: handler.update_session() handler.print_progress_today(force=False) LogHandler.sleep()
print 'Can not find csmi directory {0}. Is csmi installed?'.format( csmidir) sys.exit(1) usecsm = True runid = datetime.now().strftime("%y%m%d%H%M%S%f") logd = '%s/%s' % (mconfig['logdir'], runid) logf = '%s/%s-%s.log' % (mconfig['logdir'], me, runid) console_level = VERBOSE_LEVEL[mconfig['console_verbose'].lower()] log_level = VERBOSE_LEVEL[mconfig['log_verbose'].lower()] bds = True if mconfig['bds'].lower() == 'yes' else False try: logger = LogHandler(thismodule, console_level, log_level, True, bds, logf) except IOError as e: print e print 'Error creating the log handlers. Exiting...' sys.exit(1) thisuser = getpass.getuser() csmii = csmi.CsmiInterface(logger, csmidir, usecsm, mconfig['allocation_id'], runid, thisuser) logger.set_csmi(csmii) if mgmt_mode: import harness tharness = harness.Harness(mconfig, tconfig, logger, runid, logd, csmii, thisuser, args.target) else:
def __init__(self): log = LogHandler('valid_schedule') ProxyManager.__init__(self, log) Thread.__init__(self)
def __init__(self): log = LogHandler('refresh_schedule') ProxyManager.__init__(self, log)
class QEpanet: """QGIS Plugin Implementation.""" def __init__(self, iface): """Constructor. :param iface: An interface instance that will be passed to this class which provides the hook by which you can manipulate the QGIS application at run time. :type iface: QgsInterface """ # Save reference to the QGIS interface self.iface = iface self.params = Parameters() # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # initialize locale locale = QSettings().value('locale/userLocale')[0:2] locale_path = os.path.join( self.plugin_dir, 'i18n', 'QEpanet_{}.qm'.format(locale)) if os.path.exists(locale_path): self.translator = QTranslator() self.translator.load(locale_path) if qVersion() > '4.3.3': QCoreApplication.installTranslator(self.translator) # Declare instance attributes self.actions = [] self.menu = self.tr(u'&QEPANET') # TODO: We are going to let the user set this up in a future iteration self.toolbar = self.iface.addToolBar(u'QEpanet') self.toolbar.setObjectName(u'QEpanet') self.pluginIsActive = False self.dockwidget = None self._logger = None self._log_handler = None # self.start_logging() # noinspection PyMethodMayBeStatic def tr(self, message): """Get the translation for a string using Qt translation API. We implement this ourselves since we do not inherit QObject. :param message: String for translation. :type message: str, QString :returns: Translated version of message. :rtype: QString """ # noinspection PyTypeChecker,PyArgumentList,PyCallByClass return QCoreApplication.translate('QEpanet', message) def add_action( self, icon_path, text, callback, enabled_flag=True, add_to_menu=True, add_to_toolbar=True, status_tip=None, whats_this=None, parent=None): """Add a toolbar icon to the toolbar. :param icon_path: Path to the icon for this action. Can be a resource path (e.g. ':/plugins/foo/bar.png') or a normal file system path. :type icon_path: str :param text: Text that should be shown in menu items for this action. :type text: str :param callback: Function to be called when the action is triggered. :type callback: function :param enabled_flag: A flag indicating if the action should be enabled by default. Defaults to True. :type enabled_flag: bool :param add_to_menu: Flag indicating whether the action should also be added to the menu. Defaults to True. :type add_to_menu: bool :param add_to_toolbar: Flag indicating whether the action should also be added to the toolbar. Defaults to True. :type add_to_toolbar: bool :param status_tip: Optional text to show in a popup when mouse pointer hovers over the action. :type status_tip: str :param parent: Parent widget for the new action. Defaults None. :type parent: QWidget :param whats_this: Optional text to show in the status bar when the mouse pointer hovers over the action. :returns: The action that was created. Note that the action is also added to self.actions list. :rtype: QAction """ icon = QIcon(icon_path) action = QAction(icon, text, parent) action.triggered.connect(callback) action.setEnabled(enabled_flag) if status_tip is not None: action.setStatusTip(status_tip) if whats_this is not None: action.setWhatsThis(whats_this) if add_to_toolbar: self.toolbar.addAction(action) if add_to_menu: self.iface.addPluginToMenu( self.menu, action) self.actions.append(action) return action def initGui(self): """Create the menu entries and toolbar icons inside the QGIS GUI.""" icon_path = ':/plugins/QEpanet/icon.png' self.add_action( icon_path, text=self.tr(u'QEPANET'), callback=self.run, parent=self.iface.mainWindow()) #-------------------------------------------------------------------------- def onClosePlugin(self): """Cleanup necessary items here when plugin dockwidget is closed""" # disconnects self.dockwidget.closingPlugin.disconnect(self.onClosePlugin) # remove this statement if dockwidget is to remain # for reuse if plugin is reopened # Commented next statement since it causes QGIS crashe # when closing the docked window: # self.dockwidget = None self.pluginIsActive = False self.stop_logging() def unload(self): """Removes the plugin menu item and icon from QGIS GUI.""" for action in self.actions: self.iface.removePluginMenu( self.tr(u'&QEPANET'), action) self.iface.removeToolBarIcon(action) # remove the toolbar del self.toolbar if self.dockwidget is not None: self.iface.removeDockWidget(self.dockwidget) self.stop_logging() # -------------------------------------------------------------------------- def run(self): """Run method that loads and starts the plugin""" # # Restore cursor # QApplication.setOverrideCursor(Qt.ArrowCursor) # # Check for config file existance if not os.path.exists(Parameters.config_file_path): config_file = ConfigFile(Parameters.config_file_path) config_file.create_config() # Clear vertex markers (there should be none, just in case) vertex_items = [i for i in self.iface.mapCanvas().scene().items() if issubclass(type(i), QgsVertexMarker)] for ver in vertex_items: if ver in self.iface.mapCanvas().scene().items(): self.iface.mapCanvas().scene().removeItem(ver) file_dialog = MyQFileDialog() file_dialog.setWindowTitle('Select an INP file or create a new one') # TODO: Softcode file_dialog.setLabelText(QFileDialog.Accept, 'Select') # TODO: sofcode file_dialog.setFileMode(QFileDialog.AnyFile) file_dialog.setFilter("INP files (*.inp)") inp_file_path = None if file_dialog.exec_(): inp_file_path = file_dialog.selectedFiles()[0] if not inp_file_path.lower().endswith('.inp'): inp_file_path += '.inp' self.params.last_project_dir = os.path.dirname(inp_file_path) if inp_file_path is None: return if not self.pluginIsActive: self.pluginIsActive = True if self.dockwidget is None: # Create the dockwidget (after translation) and keep reference self.dockwidget = QEpanetDockWidget(self.iface, self.params, inp_file_path) self.params.attach(self.dockwidget) # Connect to provide cleanup on closing of dockwidget self.dockwidget.closingPlugin.connect(self.onClosePlugin) # Show the dockwidget self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget) self.dockwidget.show() def start_logging (self): if self._logger is not None: return self._logger = logging.getLogger('Logger1') self._logger.setLevel(logging.DEBUG) log_full_path_file_name = '%s/log.log' % os.path.dirname(os.path.realpath(__file__)) log_file_info = QFileInfo(log_full_path_file_name) if log_file_info.exists(): log_created_qdatetime = log_file_info.created() current_qdatetime = QDateTime.currentDateTime() max_difference_secs = 60 * 60 * 24 * 7 # 7 giorni current_difference_secs = log_created_qdatetime.secsTo(current_qdatetime) if current_difference_secs > max_difference_secs: QFile.remove(log_full_path_file_name) self._log_handler = LogHandler(log_full_path_file_name) formatter = logging.Formatter('%(asctime)s - %(thread)d - %(name)s - %(levelname)s - %(message)s') self._log_handler.setFormatter(formatter) self._logger.addHandler(self._log_handler) # Do not raise logging system exceptions logging.raiseExceptions = False # Connect log signal self._log_handler.logged.connect (self.message_logged) self.logging_enabled = True def stop_logging (self): if self._logger is not None and self._log_handler is not None: self._logger.debug('---------------------------------------------------------') self._logger.removeHandler(self._log_handler) logging.shutdown() # disconnessione al segnale logged emesso ad ogni log QObject.disconnect(self._log_handler, SIGNAL('logged(QString)'),self.message_logged) self.logging_enabled = False self._logger = None self._log_handler = None def message_logged (self, message): if not self.logging_enabled: return # if self.widget is not None: # self.widget.log_list_view_model.appendRow(QStandardItem(message))
class TestLogHandler(unittest.TestCase): """Test the LogHandler class""" def setUp(self): """Initialization of the tests""" # Create temporary log file for testing the methods self.logPath = "tmp.log" self.generatingRate = 60 self.entryGenerator = EntryGenerator(self.logPath, self.generatingRate) now = datetime.now() # Add a 10 hours old entry self.entryGenerator.write_entry(now - timedelta(hours=10)) # Add two entries to the current time self.entryGenerator.write_entry(now) self.entryGenerator.write_entry(now) # Setup the LogHandler object prior testing self.refreshPeriod = 2 self.alertThreshold = 20 self.monitorDuration = 10 self.logHandler = LogHandler(self.logPath, self.refreshPeriod, self.alertThreshold, self.monitorDuration) # Disable logHandler console output for the tests self.logHandler.printStatus = False def test_add_entry(self): """Tests adding entries to the LogHandler""" print("********************************") print("test_add_entry()") unparsedEntry = self.entryGenerator.generate_entry(datetime.now()) logEntry = LogEntry(unparsedEntry) self.logHandler.add_entry(logEntry) # Check that all the attributes of the entry have been processed self.assertEqual(self.logHandler.log[0], logEntry) self.assertEqual(self.logHandler.hits, 1) self.assertEqual(self.logHandler.size, logEntry.size) self.assertEqual(self.logHandler.sections, {logEntry.section: 1}) self.assertEqual(self.logHandler.ips, {logEntry.ip: 1}) self.assertEqual(self.logHandler.methods, {logEntry.method: 1}) self.assertEqual(self.logHandler.codes, {logEntry.code: 1}) # Put an entry that is not formatted # to check if it's correctly dropped logEntry = LogEntry("This is not a formatted entry\n") self.logHandler.add_entry(logEntry) self.assertEqual(len(self.logHandler.log), 1) self.assertEqual(self.logHandler.hits, 1) def test_delete_entry(self): """Tests deleting entries from the LogHandler""" print("********************************") print("test_delete_entry()") # Add 2 entries and delete the oldest unparsedEntry = self.entryGenerator.generate_entry(datetime.now()) logEntry1 = LogEntry(unparsedEntry) unparsedEntry = self.entryGenerator.generate_entry(datetime.now()) logEntry2 = LogEntry(unparsedEntry) self.logHandler.add_entry(logEntry1) self.logHandler.add_entry(logEntry2) self.logHandler.delete_entry() # logEntry1 should be deleted (oldest) # Check that only logEntry2 is left self.assertEqual(self.logHandler.log[0], logEntry2) self.assertEqual(self.logHandler.hits, 1) self.assertEqual(self.logHandler.size, logEntry2.size) self.assertEqual(self.logHandler.sections, {logEntry2.section: 1}) self.assertEqual(self.logHandler.ips, {logEntry2.ip: 1}) self.assertEqual(self.logHandler.methods, {logEntry2.method: 1}) self.assertEqual(self.logHandler.codes, {logEntry2.code: 1}) def test_read(self): """Test that the LogHandler correctly reads the log and creates a corresponding list of LogEntry objects""" print("********************************") print("test_read()") # Add a non formatted line to the log to check if it's dropped self.entryGenerator.write("this is not a formatted entry\n") self.logHandler.read() # Check that only the two most recent entries have been processed self.assertEqual(len(self.logHandler.log), 2) self.assertEqual(self.logHandler.hits, 2) # Check that all entries are of type LogEntry for entry in self.logHandler.log: self.assertIsInstance(entry, LogEntry) def test_several_reads(self): """Test behaviour of LogHandler when reading several times in a row""" print("********************************") print("test_several_reads()") self.logHandler.read() # Check that only the two most recententries have been processed self.assertEqual(len(self.logHandler.log), 2) # Add a new entry self.entryGenerator.write_entry(datetime.now()) self.logHandler.read() # Check that logHandler has 3 entries in total self.assertEqual(len(self.logHandler.log), 3) def test_run(self): """Test the main monitoring loop of the LogHandler""" print("********************************") print("test_run()") # Start the thread (checks the log every refreshPeriod) self.logHandler.start() # Wait a bit so that logHandler has time to read the log once sleep(0.1*self.refreshPeriod) updateTime1 = self.logHandler.lastReadTime # Let's wait one and a half refreshPeriod for another read to happen sleep(self.refreshPeriod*1.5) updateTime2 = self.logHandler.lastReadTime # Time between the 2 reads delta = (updateTime2 - updateTime1).total_seconds() self.logHandler.stop() self.logHandler.join() # Check delta between two log reads is within a 10% error margin # from the specified refreshPeriod self.assertTrue(abs((delta-self.refreshPeriod) / self.refreshPeriod) < 0.1) def test_drop_old_entries(self): """Test the removal of entries older than the monitored period""" print("********************************") print("test_drop_old_entries()") # Create an old entry object in the log oldEntryStr = self.entryGenerator.generate_entry(datetime.now() - timedelta(hours=10)) oldEntry = LogEntry(oldEntryStr) # Create a recent entry newEntryString = self.entryGenerator.generate_entry(datetime.now()) newEntry = LogEntry(newEntryString) # Add them manually to the LogHandler self.logHandler.add_entry(oldEntry) self.logHandler.add_entry(newEntry) self.assertEqual(len(self.logHandler.log), 2) self.logHandler.drop_old_entries() # Only one of the entries should be left self.assertEqual(len(self.logHandler.log), 1) self.assertEqual(self.logHandler.hits, 1) def test_alert(self): """Test alert triggering""" print("********************************") print("test_alert()") self.logHandler.refreshPeriod = 2 self.logHandler.start() self.assertEqual(self.logHandler.alertStatus, False) # Add twice as much entries than the threshold to trigger the alert now = datetime.now() entryCount = int(2 * self.logHandler.alertThreshold * self.logHandler.monitorDuration / 60) for i in range(0, entryCount): self.entryGenerator.write_entry(now) # Wait for the LogHandler to read the log sleep(1.5*self.logHandler.refreshPeriod) self.logHandler.stop() self.logHandler.join() self.assertTrue(self.logHandler.alertStatus) def test_end_alert(self): """Test the alert ending went traffic went back to normal""" print("********************************") print("test_end_alert()") # Set time frame of monitoring to 1 second to test faster self.logHandler.monitorDuration = 2 self.logHandler.refreshPeriod = 1 self.logHandler.start() self.assertEqual(self.logHandler.alertStatus, False) # Add twice as much entries than the threshold to trigger the alert now = datetime.now() entryCount = int(2 * self.logHandler.alertThreshold * self.logHandler.monitorDuration / 60) for i in range(0, entryCount): self.entryGenerator.write_entry(now) # Wait for the LogHandler to read the log sleep(self.refreshPeriod) self.assertTrue(self.logHandler.alertStatus) # Wait for the LogHandler to remove the entries sleep(1.5*self.logHandler.monitorDuration) self.logHandler.stop() self.logHandler.join() self.assertFalse(self.logHandler.alertStatus) def test_summary(self): """Test the processing of information contained in the entries""" print("********************************") print("test_summary()") # Write some predefined entries to the log file self.logHandler.monitorDuration = 2 now = datetime.now() # Truncate current datetime to remove microseconds # (for the test to succeed) now = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) # Disposition required to satisfy PEP8 entries = ('127.0.0.1 - - [%s +1000] "GET /icons/blank.gif HTTP/1.1" \ 200 100\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '289.8.42.1 - - [%s +1000] "POST /index.html HTTP/1.1" \ 200 1000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '127.0.0.1 - - [%s +1000] "GET /icons/blank.gif HTTP/1.1" \ 200 900\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '289.8.42.1 - - [%s +1000] "GET /css/display.css HTTP/1.1" \ 403 4000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '127.0.0.1 - - [%s +1000] "GET /index.php HTTP/1.1" \ 404 1000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '289.8.42.1 - - [%s +1000] "POST /icons/blank.gif HTTP/1.1" \ 200 9000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '127.0.0.1 - - [%s +1000] "GET /icons/blank.gif HTTP/1.1" \ 403 4000\n' % now.strftime("%d/%b/%Y:%H:%M:%S")) self.entryGenerator.clear_log() self.entryGenerator.write(entries) self.logHandler.read() # Check that summary information are correct self.assertEqual(self.logHandler.hits, 7) self.assertEqual(self.logHandler.size, 20000) self.assertEqual(self.logHandler.sections, {"icons": 4, "root": 2, "css": 1}) self.assertEqual(self.logHandler.ips, {"127.0.0.1": 4, "289.8.42.1": 3}) self.assertEqual(self.logHandler.methods, {"GET": 5, "POST": 2}) self.assertEqual(self.logHandler.codes, {"200": 4, "403": 2, "404": 1})
# Run the HTTP monitor with parameters from parameters.cfg from log_handler import LogHandler import configparser config = configparser.ConfigParser() config.read("parameters.cfg") logPath = str(config.get("Monitor", "logPath")) refreshPeriod = float(config.get("Monitor", "refreshPeriod")) treshold = float(config.get("Monitor", "treshold")) monitorDuration = float(config.get("Monitor", "monitorDuration")) logHandler = LogHandler(logPath, refreshPeriod, treshold, monitorDuration) logHandler.start() # Wait for the logHandler to finish to end the program logHandler.join()
import tkinter import multiprocessing from log_handler import LogHandler LogHandler.merge_session() LogHandler.backup() handler = LogHandler() def close(): thread_update.terminate() handler.update_session() LogHandler.merge_session() handler.print_progress_today(force=True) base_window.destroy() def func_update(): while True: handler.update_session() handler.print_progress_today(force=False) LogHandler.sleep() if __name__ == '__main__': base_window = tkinter.Tk() base_window.title('clock-in') thread_update = multiprocessing.Process(target=func_update) thread_update.start()
def report(path: str, logs: list, name: str, should_hour_per_day: float): assert len(logs) > 0, 'empty log' done = set() timedelta_should_per_day = datetime.timedelta(hours=should_hour_per_day) duration_in_day = LogHandler.count_duration_per_day(logs) total_duration = functools.reduce( lambda s, d: s + d, (duration for duration in duration_in_day.values())) def format_cell(content: str): return f'\\intbl {content} \\cell ' def format_row(log): date_id = LogHandler.get_date_id(log['from']) date = date_id if date_id not in done else '' start = LogHandler.format_clocktime(log['from']) end = LogHandler.format_clocktime(log['to']) elapsed = LogHandler.format_duration(log['to'] - log['from']) elapsed_in_day = LogHandler.format_duration( duration_in_day[date_id]) if date_id not in done else '' delta = LogHandler.format_duration_difference( duration_in_day[date_id], timedelta_should_per_day) if date_id not in done else '' content = os.linesep.join([ format_cell(elem) for elem in [date, start, end, elapsed, elapsed_in_day, delta] ]) done.add(date_id) return content cells = [f'\\cellx{r}' for r in (800, 1800, 2800, 3800, 4900, 6000)] table_head = '\\trowd\\pard\\trqc ' + ' '.join(cells) with open(path, 'w') as fs: fs.write('{\\rtf1\\ansi\\deff0' + os.linesep) fs.write('\\qr \\sb300 {\\loch %s}' % (LogHandler.format_date(LogHandler.get_now()), ) + os.linesep) fs.write('\\par\\pard\\sb300\\plain {\\loch Dear %s,}' % (name, ) + os.linesep) fs.write( '\\par\\pard\\sb300\\sa300\\plain {\\loch your working time in %s from %s to %s is as follows:}' % (LogHandler.get_month_id(logs[0]['from']), LogHandler.format_date(logs[0]['from']), LogHandler.format_date(logs[-1]['from'])) + os.linesep) fs.write('\\par\\pard\\sb100\\sa100\\qc' + os.linesep) fs.write( table_head.replace( '\\cellx', '\\clbrdrt\\brdrth\\clbrdrb\\brdrs\\cellx').replace( '\\pard', '') + os.linesep) fs.write(''.join([ f'\\intbl {title} \\cell ' for title in ['Date', 'Start', 'End', 'Elapsed', 'Sum', 'Change'] ]) + '\\row' + os.linesep) for id_log, log in enumerate(logs, 1): fs.write((table_head if id_log < len(logs) else table_head. replace('\\cellx', '\\clbrdrb\\brdrth\\cellx')) + os.linesep) fs.write(format_row(log) + os.linesep) fs.write('\\row\\pard' + os.linesep) fs.write( '\\par\\pard\\sb100\\plain {\\loch The total working time for the %d days with time tracking is %s, the balance for this period is %s (with %s planned per day).}' % (len(duration_in_day), LogHandler.format_duration(total_duration), LogHandler.format_duration_difference( total_duration, datetime.timedelta(hours=(should_hour_per_day * len(duration_in_day)))), LogHandler.format_duration(timedelta_should_per_day)) + os.linesep) fs.write('\\par\\pard\\sb300\\plain {\\loch Sincerely yours}' + os.linesep) fs.write('\\par\\pard\\sb300\\plain {\\loch clock_in}' + os.linesep) fs.write('}' + os.linesep)
def close(): thread_update.terminate() handler.update_session() LogHandler.merge_session() handler.print_progress_today(force=True) base_window.destroy()
class TestLogHandler(unittest.TestCase): """Test the LogHandler class""" def setUp(self): """Initialization of the tests""" # Create temporary log file for testing the methods self.logPath = "tmp.log" self.generatingRate = 60 self.entryGenerator = EntryGenerator(self.logPath, self.generatingRate) now = datetime.now() # Add a 10 hours old entry self.entryGenerator.write_entry(now - timedelta(hours=10)) # Add two entries to the current time self.entryGenerator.write_entry(now) self.entryGenerator.write_entry(now) # Setup the LogHandler object prior testing self.refreshPeriod = 2 self.alertThreshold = 20 self.monitorDuration = 10 self.logHandler = LogHandler(self.logPath, self.refreshPeriod, self.alertThreshold, self.monitorDuration) # Disable logHandler console output for the tests self.logHandler.printStatus = False def test_add_entry(self): """Tests adding entries to the LogHandler""" print("********************************") print("test_add_entry()") unparsedEntry = self.entryGenerator.generate_entry(datetime.now()) logEntry = LogEntry(unparsedEntry) self.logHandler.add_entry(logEntry) # Check that all the attributes of the entry have been processed self.assertEqual(self.logHandler.log[0], logEntry) self.assertEqual(self.logHandler.hits, 1) self.assertEqual(self.logHandler.size, logEntry.size) self.assertEqual(self.logHandler.sections, {logEntry.section: 1}) self.assertEqual(self.logHandler.ips, {logEntry.ip: 1}) self.assertEqual(self.logHandler.methods, {logEntry.method: 1}) self.assertEqual(self.logHandler.codes, {logEntry.code: 1}) # Put an entry that is not formatted # to check if it's correctly dropped logEntry = LogEntry("This is not a formatted entry\n") self.logHandler.add_entry(logEntry) self.assertEqual(len(self.logHandler.log), 1) self.assertEqual(self.logHandler.hits, 1) def test_delete_entry(self): """Tests deleting entries from the LogHandler""" print("********************************") print("test_delete_entry()") # Add 2 entries and delete the oldest unparsedEntry = self.entryGenerator.generate_entry(datetime.now()) logEntry1 = LogEntry(unparsedEntry) unparsedEntry = self.entryGenerator.generate_entry(datetime.now()) logEntry2 = LogEntry(unparsedEntry) self.logHandler.add_entry(logEntry1) self.logHandler.add_entry(logEntry2) self.logHandler.delete_entry() # logEntry1 should be deleted (oldest) # Check that only logEntry2 is left self.assertEqual(self.logHandler.log[0], logEntry2) self.assertEqual(self.logHandler.hits, 1) self.assertEqual(self.logHandler.size, logEntry2.size) self.assertEqual(self.logHandler.sections, {logEntry2.section: 1}) self.assertEqual(self.logHandler.ips, {logEntry2.ip: 1}) self.assertEqual(self.logHandler.methods, {logEntry2.method: 1}) self.assertEqual(self.logHandler.codes, {logEntry2.code: 1}) def test_read(self): """Test that the LogHandler correctly reads the log and creates a corresponding list of LogEntry objects""" print("********************************") print("test_read()") # Add a non formatted line to the log to check if it's dropped self.entryGenerator.write("this is not a formatted entry\n") self.logHandler.read() # Check that only the two most recent entries have been processed self.assertEqual(len(self.logHandler.log), 2) self.assertEqual(self.logHandler.hits, 2) # Check that all entries are of type LogEntry for entry in self.logHandler.log: self.assertIsInstance(entry, LogEntry) def test_several_reads(self): """Test behaviour of LogHandler when reading several times in a row""" print("********************************") print("test_several_reads()") self.logHandler.read() # Check that only the two most recententries have been processed self.assertEqual(len(self.logHandler.log), 2) # Add a new entry self.entryGenerator.write_entry(datetime.now()) self.logHandler.read() # Check that logHandler has 3 entries in total self.assertEqual(len(self.logHandler.log), 3) def test_run(self): """Test the main monitoring loop of the LogHandler""" print("********************************") print("test_run()") # Start the thread (checks the log every refreshPeriod) self.logHandler.start() # Wait a bit so that logHandler has time to read the log once sleep(0.1 * self.refreshPeriod) updateTime1 = self.logHandler.lastReadTime # Let's wait one and a half refreshPeriod for another read to happen sleep(self.refreshPeriod * 1.5) updateTime2 = self.logHandler.lastReadTime # Time between the 2 reads delta = (updateTime2 - updateTime1).total_seconds() self.logHandler.stop() self.logHandler.join() # Check delta between two log reads is within a 10% error margin # from the specified refreshPeriod self.assertTrue( abs((delta - self.refreshPeriod) / self.refreshPeriod) < 0.1) def test_drop_old_entries(self): """Test the removal of entries older than the monitored period""" print("********************************") print("test_drop_old_entries()") # Create an old entry object in the log oldEntryStr = self.entryGenerator.generate_entry(datetime.now() - timedelta(hours=10)) oldEntry = LogEntry(oldEntryStr) # Create a recent entry newEntryString = self.entryGenerator.generate_entry(datetime.now()) newEntry = LogEntry(newEntryString) # Add them manually to the LogHandler self.logHandler.add_entry(oldEntry) self.logHandler.add_entry(newEntry) self.assertEqual(len(self.logHandler.log), 2) self.logHandler.drop_old_entries() # Only one of the entries should be left self.assertEqual(len(self.logHandler.log), 1) self.assertEqual(self.logHandler.hits, 1) def test_alert(self): """Test alert triggering""" print("********************************") print("test_alert()") self.logHandler.refreshPeriod = 2 self.logHandler.start() self.assertEqual(self.logHandler.alertStatus, False) # Add twice as much entries than the threshold to trigger the alert now = datetime.now() entryCount = int(2 * self.logHandler.alertThreshold * self.logHandler.monitorDuration / 60) for i in range(0, entryCount): self.entryGenerator.write_entry(now) # Wait for the LogHandler to read the log sleep(1.5 * self.logHandler.refreshPeriod) self.logHandler.stop() self.logHandler.join() self.assertTrue(self.logHandler.alertStatus) def test_end_alert(self): """Test the alert ending went traffic went back to normal""" print("********************************") print("test_end_alert()") # Set time frame of monitoring to 1 second to test faster self.logHandler.monitorDuration = 2 self.logHandler.refreshPeriod = 1 self.logHandler.start() self.assertEqual(self.logHandler.alertStatus, False) # Add twice as much entries than the threshold to trigger the alert now = datetime.now() entryCount = int(2 * self.logHandler.alertThreshold * self.logHandler.monitorDuration / 60) for i in range(0, entryCount): self.entryGenerator.write_entry(now) # Wait for the LogHandler to read the log sleep(self.refreshPeriod) self.assertTrue(self.logHandler.alertStatus) # Wait for the LogHandler to remove the entries sleep(1.5 * self.logHandler.monitorDuration) self.logHandler.stop() self.logHandler.join() self.assertFalse(self.logHandler.alertStatus) def test_summary(self): """Test the processing of information contained in the entries""" print("********************************") print("test_summary()") # Write some predefined entries to the log file self.logHandler.monitorDuration = 2 now = datetime.now() # Truncate current datetime to remove microseconds # (for the test to succeed) now = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) # Disposition required to satisfy PEP8 entries = ( '127.0.0.1 - - [%s +1000] "GET /icons/blank.gif HTTP/1.1" \ 200 100\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '289.8.42.1 - - [%s +1000] "POST /index.html HTTP/1.1" \ 200 1000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '127.0.0.1 - - [%s +1000] "GET /icons/blank.gif HTTP/1.1" \ 200 900\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '289.8.42.1 - - [%s +1000] "GET /css/display.css HTTP/1.1" \ 403 4000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '127.0.0.1 - - [%s +1000] "GET /index.php HTTP/1.1" \ 404 1000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '289.8.42.1 - - [%s +1000] "POST /icons/blank.gif HTTP/1.1" \ 200 9000\n' % now.strftime("%d/%b/%Y:%H:%M:%S") + '127.0.0.1 - - [%s +1000] "GET /icons/blank.gif HTTP/1.1" \ 403 4000\n' % now.strftime("%d/%b/%Y:%H:%M:%S")) self.entryGenerator.clear_log() self.entryGenerator.write(entries) self.logHandler.read() # Check that summary information are correct self.assertEqual(self.logHandler.hits, 7) self.assertEqual(self.logHandler.size, 20000) self.assertEqual(self.logHandler.sections, { "icons": 4, "root": 2, "css": 1 }) self.assertEqual(self.logHandler.ips, { "127.0.0.1": 4, "289.8.42.1": 3 }) self.assertEqual(self.logHandler.methods, {"GET": 5, "POST": 2}) self.assertEqual(self.logHandler.codes, {"200": 4, "403": 2, "404": 1})
def _init_loghandler(log_path): return LogHandler(log_path)
# -*- coding: utf-8 -*- import time from car_parts.abstract_car_part_handler import AbstractCarPartHandler from log_handler import LogHandler logger = LogHandler().get_logger(__name__) class Lambda(AbstractCarPartHandler): """ Wraps a function into a vehicle part. """ def __init__(self, f): """ Accepts the function to use. """ self.f = f def start(self, *args, **kwargs): return def update(self, *args, **kwargs): return def run_threaded(self, *args, **kwargs): return def run(self, *args, **kwargs): return self.f(*args, **kwargs)
from log_handler import LogHandler from ssh_handler import SSHHandler from celery import Celery app = Flask(__name__, instance_relative_config=True) app.config.from_object('config') app.config.from_pyfile('settings.py', silent=True) db = SQLAlchemy(app) Bootstrap(app) try: datahandler = DataHandler(app.config['DATALOCATION'], netboxURL=app.config['NETBOXSERVER']) except KeyError: datahandler = DataHandler('local') logger = LogHandler(app.config['SYSLOGFILE']) sshhandler = SSHHandler() # Celery celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'], backend=app.config['CELERY_RESULT_BACKEND']) celery.conf.update(app.config) from app import views, models manager = Manager(app) if __name__ == "__main__": app.secret_key = os.urandom(25) manager.run()
COUNTRIES, SCORE_THRESHOLD, RUMOR_THRESHOLD, USEFUL_THRESHOLD, SENTIMENT_THRESHOLD, ITOPICS, ECOUNTRY_ICOUNTRIES_MAP, ) logger = logging.getLogger(__file__) cfg = load_config() meta_data_handler = MetaDataHandler() db_handler = DBHandler(**cfg["db_handler"]) log_handler = LogHandler(**cfg["log_handler"]) twitter_handler = TwitterHandler(**cfg["twitter_handler"]) def update_database(do_tweet: bool = False): logger.debug("Add automatically categorized pages.") data_path = cfg["data"]["article_list"] cache_file = f'{cfg["log_handler"]["log_dir"]}/offset.txt' if os.path.exists(cache_file): with open(cache_file) as f: offset = int(f.read().strip()) else: offset = 0 logger.debug(f"Skip the first {offset} lines.") maybe_tweeted_ds = [] with open(data_path, mode="r", encoding="utf-8", errors="ignore") as f:
if os.name == 'nt': os.system('cls') else: os.system('clear') def menuloop(csv_file): while True: clear() print("Work Log Terminal: Main Menu\n") print("1. Add new entry") print("2. View and/or edit Data") print("3. Clear/Reset Work Log Data") print("4. Quit\n") print("Please enter the number of the option you wish to choose.") choice = input("> ") if choice == '1': csv_file.write_to_log() elif choice == '2': print(csv_file.search_log()) elif choice == '3': csv_file.reset_log() elif choice == '4': clear() break else: print("Invalid Input") input("Press Enter") csv_file = LogHandler('log.csv') menuloop(csv_file)