Simple pseudo-console interface in python script


#!/usr/bin/env python

# copyright (c) 2010 vprokofyev.blogspot.com <v.prokofyev@gmail.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program 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 this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA.

import sys
import string

class Console:
"""Create psedo-console terminal"""
 def f_prompt(self):
  print "Type 'help' or '?' to list available commands"
  done = False
  while not done:
   self.cmd = raw_input("> ")
   Parser.f_read_prompt()


class Parser:
"""Parse input from user"""
 def f_read_prompt(self):
  command_list = {'h':'help', '?':'?',
      's':'something',
      'q':'quit', 'e':'exit'}
  if Console.cmd in (command_list["h"], command_list["?"]):
   Info.f_usage()

  elif Console.cmd == command_list["s"]:
   CommandOutput.f_somefunction()

  elif Console.cmd in (command_list["q"], command_list["e"]):
   sys.exit()

  elif Console.cmd:
   print "Command not found"

class CommandOutput:
"""Init output"""
 def f_somefunction(self):
  print "Some output"

class Info:
"""Info"""
 def f_usage(self):
  print """
help/?        shows this help
something     print something
quit/exit     close program
"""

Console = Console()
Parser = Parser()
Info = Info()

Console.f_prompt()

No comments:

Post a Comment