BGP-Ranking/bin/parser.py

42 lines
1.7 KiB
Python
Raw Normal View History

2018-07-13 14:51:00 +02:00
#!/usr/bin/env python3
2018-03-29 22:37:28 +02:00
# -*- coding: utf-8 -*-
import logging
from pathlib import Path
2018-03-30 14:33:33 +02:00
from bgpranking.abstractmanager import AbstractManager
2018-03-29 22:37:28 +02:00
from bgpranking.parser import RawFilesParser
from bgpranking.libs.helpers import get_config_path, get_homedir
2018-03-29 22:37:28 +02:00
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s:%(message)s',
level=logging.INFO, datefmt='%I:%M:%S')
2018-03-30 14:33:33 +02:00
class ParserManager(AbstractManager):
2018-03-29 22:37:28 +02:00
def __init__(self, config_dir: Path=None, storage_directory: Path=None, loglevel: int=logging.DEBUG):
2018-03-30 14:33:33 +02:00
super().__init__(loglevel)
2018-03-29 22:37:28 +02:00
if not config_dir:
config_dir = get_config_path()
if not storage_directory:
2018-07-13 14:51:00 +02:00
self.storage_directory = get_homedir() / 'rawdata'
self.modules_config = config_dir / 'modules'
self.modules_paths = [modulepath for modulepath in self.modules_config.glob('*.json')]
self.modules = [RawFilesParser(path, self.storage_directory, loglevel) for path in self.modules_paths]
2018-03-29 22:37:28 +02:00
2018-03-30 14:33:33 +02:00
def _to_run_forever(self):
2018-07-13 14:51:00 +02:00
# Check if there are new config files
new_modules_paths = [modulepath for modulepath in self.modules_config.glob('*.json') if modulepath not in self.modules_paths]
self.modules += [RawFilesParser(path, self.storage_directory, self.loglevel) for path in new_modules_paths]
self.modules_paths += new_modules_paths
if self.modules:
[module.parse_raw_files() for module in self.modules]
else:
self.logger.warning('No config files were found so there are no parsers running yet. Will try again later.')
2018-03-29 22:37:28 +02:00
if __name__ == '__main__':
parser_manager = ParserManager()
2018-03-30 14:33:33 +02:00
parser_manager.run(sleep_in_sec=120)