Skip to content

Base Parser

jfuruness edited this page Nov 29, 2020 · 3 revisions

Table of Contents

Short Description

The purpose of this parser is to template all the other collectors and modules, for faster code writing, and so that others can use the library easier.

Long Description

This class is the base class for most collectors and classes. It contains many useful features that make it easy to interact with this package.

To start with we have slots. This is a python way of confining the classes attributes to just those mentioned here, which allows for faster access and imo more readability.

Then we have the init subclass method. This overrides inheritance, and adds all classes that inherit this base class to a class attribute (that is a list) called parsers.

In the init function, we first get/set the global section header. By default this is bgp. This is just the section of the database that we will be using, and is global across all files.

Then we configure logging. Default is info.

We create and clear two folders, self.path and self.csv_dir. Note that csv directory is in /dev/shm for faster access.

You'll notice this class has a run method. This calls _run, which should be overriden by the subclass. This run method runs the code, logs the time, and catches errors. Once it's done, it ends the parser and removes unnessecary files.

No need to worry about the argparse_call method. The short version is that this method (along with code written in __main__.py file) will allow you to call any parser from the command line, using it's class name.

Usage

Inherit this class for a collector:

from ...utils.base_classes import Parser
class My_New_Parser(Parser):
    # Note that you do not need **kwargs, but you always need self and args
    def _run(self, *args, **kwargs):
        pass  # My function stuff here

Initializing the parser in a script:

These are the arguments you can change for any subclass when it gets initialized

To initialize My_New_Parser with default values:

from lib_bgp_data import My_New_Parser
parser = My_New_Parser()

To initialize My_New_Parser with custom path, CSV directory, and logging level, and database section:

from logging import DEBUG
from lib_bgp_data import My_New_Parser
parser = My_New_Parser(path="/my_custom_path",
                           csv_dir="/my_custom_csv_dir",
                           stream_level=DEBUG,
                           section="mydbsection")

To run the My_New_Parser with defaults:

from lib_bgp_data import My_New_Parser
My_New_Parser().run()

From the command line:

There are two ways you can run this parser. It will run with all the defaults. Later in the future it is possible more parameters will be added, but for now this is unnecessary. This will call the run method.

Best way:

lib_bgp_data --my_new_parser

For debugging:

lib_bgp_data --my_new_parser --debug

This example must be run over the package, so cd into one directory above that package

python3 -m lib_bgp_data --my_new_parser

Design Choices

  • Table of contents
  • Classes use underscores instead of camelcase that inherit this so that you can call them from the command line with their class name using underscores.