Source

vasm / vasm

#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with VASM.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

# vasm launcher script.
import os
import logging
from optparse import OptionParser

def start_logger():
    logger = logging.getLogger('vasm')
    hdlr = logging.FileHandler('/var/log/vasm.log')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.ERROR)

def get_logger_level(level):
    """ return the proper logger level for the specified level"""
    levels = {'0':logging.CRITICAL,
        '1':logging.FATAL,
        '2':logging.ERROR,
        '3':logging.INFO,
        '4':logging.DEBUG}
    return levels[level] or levels['4']

def launch_tui():
    import tvasm
    return tvasm.main()

def launch_gui():
    import panedui
    return panedui.main()

def main():
    if os.getenv("DISPLAY"):
        return launch_gui()
    return launch_tui()

if __name__ == "__main__":
     # process commandline options
    start_logger()
    logger = logging.getLogger('vasm')
    parser = OptionParser()
    parser.add_option("-v", "--verbose", dest="verbose",
                      help = "Verbosity level [ 0 - 4 ].")
    parser.add_option('-i','--interface', dest='interface',
                      help=''.join(('Interface to be  launched [ gui or tui ].', '  '
                                   'If nothing is specified, it will default to whatever is available')))
    (options, args) = parser.parse_args()
    
    if options.verbose is not None:
        logger.setLevel(get_logger_level(options.verbose))
    if options.interface is None:
        main()
    elif options.interface == 'tui':
        launch_tui()
    elif options.interface == 'gui':
        launch_gui()