history.py :  » Development » Lyntin » lyntin-4.2 » lyntin » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Lyntin 
Lyntin » lyntin 4.2 » lyntin » history.py
#########################################################################
# This file is part of Lyntin.
#
# Lyntin 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 3 of the License, or
# (at your option) any later version.
#
# Lyntin 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.  If not, see <http://www.gnu.org/licenses/>.
#
# copyright (c) Free Software Foundation 2001-2007
#
# $Id: history.py,v 1.4 2007/07/24 00:39:03 willhelm Exp $
#########################################################################
"""
The HistoryManager keeps track of the last 1000 lines of user input 
X{history}. The HistoryManager is a singleton and it's on an engine 
scoping thus we don't keep track of history per session.
"""
import manager

class HistoryManager(manager.Manager):
  """
  Manages user data history.

  This manages the user input history by storing the commands the
  user entered.  This module also handles manipulating that history
  letting the user to recall and edit those commands to fix mistakes
  they may have typed.
  """
  def __init__(self, e):
    self._history = [""]
    self._config = e.getManager("config")
    self._engine = e

  def getHistoryItem(self, userinput):
    """
    This retrieves the item (if it exists) and performs the 
    substitutions (if we need to).

    @param userinput: what the user typed--we'll use this to figure
        out which item they're referring to and whether to apply a 
        substitution
    @type  userinput: string

    @returns: None if we didn't discover anything or the command 
        string at the history index
    """
    tokens = userinput.split(" ", 1)

    # grab the first (and possibly only) token and remove the !
    index = tokens[0][1:]

    # if it's very short, we're looking at the last thing typed
    # (prior to this thing they typed)
    if len(index) == 0:
      returninput = self._history[0]
    else:
      try:
        returninput = self._history[int(index)]
      except:
        for h in self._history:
          if h[0:len(index)] == index:
            return h
        return None

    # check to see if they want to do a substitution
    if len(tokens) > 1:
      i = tokens[1].find("=")
      if i != -1:
        returninput = returninput.replace(tokens[1][:i], tokens[1][i+1:])

    return returninput

  def getHistory(self, count):
    """
    Returns everything in the history buffer as a list of strings

    @return: everything in the history buffer
    @rtype: list of strings
    """
    return self._history[:count]

  def recordHistory(self, input):
    """
    Records an item in the history (which is a queue).

    @param input: the line to record
    @type  input: string
    """
    if not input:
      return

    if input == self._history[0] and not self._config.get("repeathistory"):
      return

    self._history.insert(0, input)
    if len(self._history) > 1000:
      del self._history[-1]

# Local variables:
# mode:python
# py-indent-offset:2
# tab-width:2
# End:
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.