FAQ
Hello all,

This is the first time I have posted to this group. That being said if I am
in the wrong place for this kind of support please let me know.

OK,

So I am writing a log parsing program and wish to allow for the community to
write "parsers". Basically, what I have in place now is a "modules"
directory that contains .py files that are going to be used to parse
specific log types.

The question is, is there a way to dynamically import whatever is in that
modules directory and make is to the import name is actually the file name..

So after the modules directory is 'os.listdir' and it returns a listing of
modules (actually just .py files) IE: ng1-fw.py, cisco_asa_fw.py, etc... I
can just in the next line of code call:

ng1-fw.Parse(long_file) #assuming the ng1-fw.py file has a Parse Function

right now using imp I got it to where i can say like:
x = imp.load_source(md5.new(code_path).hexdigest(), code_path, f)
#assuming that I gave it the path and filename for ng1-fw.py
I can do this:

x.Parse(log_file)

But what I am really after is the ability to do this:
ng1-fw.Parse(log_file)

The reason again behind this is so that there is no need to "hard code"
parser file names.

Did I make myself clear? Please let me know if I need to explain that
better.

Thanks !
--
Riley Porter
Network Security Engineer

Offensive Security - OSCP
SANS GIAC - GCIH
CCNA, Security+
ACSA - Arcsight Security Analyst
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090117/25b1a2dc/attachment-0001.htm>

Search Discussions

  • Laszlo Nagy at Jan 18, 2009 at 11:46 am

    Riley Porter ?rta:
    Hello all,

    This is the first time I have posted to this group. That being said
    if I am in the wrong place for this kind of support please let me know.

    OK,

    So I am writing a log parsing program and wish to allow for the
    community to write "parsers". Basically, what I have in place now is
    a "modules" directory that contains .py files that are going to be
    used to parse specific log types.

    The question is, is there a way to dynamically import whatever is in
    that modules directory and make is to the import name is actually the
    file name..
    So after the modules directory is 'os.listdir' and it returns a
    listing of modules (actually just .py files) IE: ng1-fw.py,
    cisco_asa_fw.py, etc... I can just in the next line of code call:

    ng1-fw.Parse(long_file) #assuming the ng1-fw.py file has a Parse Function

    right now using imp I got it to where i can say like:
    x = imp.load_source(md5.new(code_path).hexdigest(), code_path, f)
    #assuming that I gave it the path and filename for ng1-fw.py
    I can do this:

    x.Parse(log_file)

    But what I am really after is the ability to do this:
    ng1-fw.Parse(log_file)

    The reason again behind this is so that there is no need to "hard
    code" parser file names.

    Did I make myself clear? Please let me know if I need to explain that
    better.
    What you need is the imp module:

    http://docs.python.org/library/imp.html
    This module provides an interface to the mechanisms used to implement
    the import <http://docs.python.org/reference/simple_stmts.html#import>
    statement. It defines the following constants and functions:

    You might find that using classes instead of modules is more handy.

    Regards,

    Laszlo

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 18, '09 at 3:37a
activeJan 18, '09 at 11:46a
posts2
users2
websitepython.org

2 users in discussion

Riley Porter: 1 post Laszlo Nagy: 1 post

People

Translate

site design / logo © 2022 Grokbase