Advertisement
dyeske

remember

Jun 20th, 2024
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. #
  4. """ this searches history files """
  5. from __future__ import print_function
  6. import glob
  7. import os
  8. import re
  9. import sys
  10. import time
  11. import optparse
  12.  
  13. #                           help='an integer for the accumulator')
  14. #                                              help='sum the integers (default: find the max)')
  15. # print(sys.version_info[0])
  16. #        from importlib import abc
  17.  
  18.  
  19. def main(ignore_case, quiet, query_list):
  20.     """main routine"""
  21.  
  22.     query_regex_list = []
  23.     tmp = []
  24.  
  25.     for pattern in query_list:
  26.         if ignore_case:
  27.             query_regex_list.append(re.compile(pattern, re.IGNORECASE))
  28.         else:
  29.             query_regex_list.append(re.compile(pattern))
  30.  
  31.     for histfile in glob.glob(os.environ['HOME'] + '/.zsh/.zsh_history,*'):
  32.         filestring = re.sub('^.zsh_history,(.*)', r'\1', os.path.split(histfile)[1])
  33.         filestring = re.sub('^(.*).archive$', r'\1', filestring)
  34.  
  35.         if sys.version_info[0] == 3:
  36.             history = open(histfile, 'r', encoding='ISO-8859-1')
  37.         if sys.version_info[0] == 2:
  38.             history = open(histfile, 'r')
  39.         for line in history:
  40.             # print ("%s %s" % (histfile, line))
  41.             match("%s::%s" % (line, filestring), tmp, query_regex_list)
  42.         history.close()
  43.  
  44.     tmp = list(set(tmp))
  45.     tmp.sort()
  46.     timeregex = re.compile(r':.(\d{10}):[0-9]+;(.*)\n::(.*)')
  47.  
  48.     for line in tmp:
  49.         x = timeregex.search(line)
  50.         try:
  51.             if quiet:
  52.                 print(x.group(2))
  53.             else:
  54.                 print(time.strftime("%Y%m%d %H:%M:%S", time.localtime(float(x.group(1)))), x.group(3), x.group(2))
  55.         except AttributeError:
  56.             sys.stderr.write('error parsing the following line\n' + line + '\n')
  57.             sys.exit(1)
  58.  
  59.  
  60. def match(data, tmp, query):
  61.     """this matches the line in the history with the query"""
  62.     for pattern in query:
  63.         if not pattern.search(data):
  64.             return
  65.     tmp.append(data)
  66.  
  67.  
  68. if __name__ == '__main__':
  69.     usage = "usage: %prog -i query1 query2 query3..."
  70.     parser = optparse.OptionParser(usage)
  71.     parser.allow_interspersed_args = False
  72.  
  73.     parser.add_option("-i", "--ignore-case",
  74.                       action="store_true",
  75.                       dest="ignore_case",
  76.                       help="ignore case")
  77.  
  78.     parser.add_option("-q", "--quiet",
  79.                       action="store_true",
  80.                       dest="quiet",
  81.                       help="quiet output")
  82.  
  83.     (options, args) = parser.parse_args()
  84.     # fixme: do not use len(SEQUENCE) to determine if somethin is empty"
  85.     if len(args) == 0:
  86.         parser.error("incorrect number of arguments")
  87.  
  88.     main(options.ignore_case, options. Quiet, args)
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement