GNU自動補全模塊readline解析


        readline模塊定義了一系列函數用來讀寫Python解釋器中歷史命令,並提供自動補全命令功能。這個模塊可以通過relcompleter模塊直接調用,模塊中的設置會影響解釋器中的交互提示,以及內置函數raw_input()和input()提供的提示。

        readline模塊定義了以下方法:
        readline.parse_and_bind(string):解析並執行命令行初始化文件。
        readline.get_line_buffer():返回當前命令行緩存的內容。
        readline.insert_text(string):插入到當前行。
        readline.read_init_file([filename]):解析一個命令行初始化文件。
        readline.read_history_file([filename]):讀取歷史命令文件,默認為~/.history
        readline.write_history_file([filename]):保存歷史命令文件,默認為~/.history
        readline.get_history_length():獲取預設的歷史命令條數。負數意味着不限制條數大小。
        readline.set_history_length(length):設置要保存到歷史命令文件中的命令條數,write_history_file()使用這個數字對歷史命令文件進行修改。
        readline.get_current_history_length():返回當前歷史文件中歷史命令的條數。
        readline.get_history_item(index):獲取index索引指定的歷史命令。
        readline.remove_history_item(pos):刪除指定位置的歷史命令。
        readline.replace_history_item(pos, line) :使用給定命令替換指定位置的命令。
        readline.redisplay() :根據命令行緩存實時更新當前屏幕的顯示。
        readline.set_startup_hook([function]) :設置或刪除鈎子函數,如果指定了函數,就將其設為鈎子函數,如果沒有指定或者設置為None,所有已經安裝的鈎子函數將被移除,鈎子函數在命令行輸出提示前執行。
        readline.set_pre_input_hook([function]):跟set_startup_hook()方法類似,但是鈎子函數是在提示輸入完之后,命令行開始讀取字符串之前執行。
        readline.set_completer([function]):如果提供了函數,則用作自動完成命令函數,如果忽略或者設置為None,則移除之前設置的函數。命令自動完成函數形式如function(text,state),text為命令行中輸入的字符串,state為選擇的的補全命令索引。
        readline.get_completer():返回自動完成命令函數。
        readline.get_completion_type() :返回自動完成的類型。
        readline.get_begidx() :獲取命令行tab自動補全范圍的第一個值的索引。
        readline.get_endidx() :獲取命令行tab自動補全范圍的最后一個值的索引。
        readline.set_completer_delims(string) :設置自動補全命令之間的分隔符。
        readline.get_completer_delims() :獲取分隔符。
        readline.set_completion_display_matches_hook([function]) :設置或者移除自動完成顯示函數。
        readline.add_history(line) :添加最后一條輸入的命令到歷史文件中。
        示例:
         下面的例子使用readline模塊從.pyhist中讀取歷史命令,並自動保存歷史命令到這個文件中。
  1. import os
  2. histfile = os.path.join(os.environ["HOME"],".pyhist")
  3. try:
  4. readline.read_history_file(histfile)
  5. exceptIOError:
  6. pass
  7. import atexit
  8. atexit.register(readline.write_history_file, histfile)
  9. del os, histfile
        下面的例子通過繼承code.InteractiveConsole來支持歷史命令的讀寫。
  1. import code
  2. import readline
  3. import atexit
  4. import os
  5. classHistoryConsole(code.InteractiveConsole):
  6. def __init__(self, locals=None, filename="<console>",
  7. histfile=os.path.expanduser("~/.console-history")):
  8. code.InteractiveConsole.__init__(self, locals, filename)
  9. self.init_history(histfile)
  10. def init_history(self, histfile):
  11. readline.parse_and_bind("tab: complete")
  12. if hasattr(readline,"read_history_file"):
  13. try:
  14. readline.read_history_file(histfile)
  15. exceptIOError:
  16. pass
  17. atexit.register(self.save_history, histfile)
  18. def save_history(self, histfile):
  19. readline.write_history_file(histfile)




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM