Cat
cat.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 """
4 CAT : package for electronics and data processing
5 
6 """
7 import sys, os
8 from arguments import *
9 from shell import *
10 
11 hist_file = os.path.expandvars("$CATPATH/.cathistory")
12 max_size_bytes = 10000
13 max_size_lines = 100
14 
15 #Code section, no need to modify this
16 
17 import readline
18 import rlcompleter
19 import atexit
20 from os.path import getsize
21 
22 def reset_file(size,max_size,reason):
23  try:
24  print "Resetting history file %s because it exceeded %s %s; it has %s." % (hist_file,max_size,reason,size,)
25  f = open(hist_file,'w')
26  f.close()
27  except IOError, e:
28  print "Couldn't reset history file %s [%s]." % (hist_file,e,)
29 
30 def safe_getsize(hist_file):
31  try:
32  size = getsize(hist_file)
33  except OSError:
34  size = 0
35  return size
36 
37 lines = 0
38 size = safe_getsize(hist_file)
39 
40 if size > max_size_bytes:
41  reset_file(size,max_size_bytes,"bytes")
42 else:
43  try:
44  readline.read_history_file(hist_file)
45  lines = readline.get_current_history_length()
46  if lines > max_size_lines:
47  try:
48  readline.clear_history()
49  except NameError, e:
50  print "readline.clear_history() not supported (%s), please delete history file %s by hand." % (e,hist_file,)
51  reset_file(lines,max_size_lines,"lines")
52  except IOError:
53  try:
54  f = open(hist_file,'a')
55  f.close()
56  except IOError:
57  print "The file %s can't be created, check your hist_file variable." % hist_file
58 
59 size = safe_getsize(hist_file)
60 
61 print "Current history file (%s) size: %s bytes, %s lines." % (hist_file,size,readline.get_current_history_length(),)
62 
63 readline.parse_and_bind("tab: complete")
64 
65 atexit.register(readline.write_history_file,hist_file)
66 
67 def main(argv=None):
68  if argv is None:
69  argv = sys.argv
70  args=arguments(argv)
71 
72  if (args.shellMode):
73  print 'starting shell...'
74  cat=Application()
75  shell(cat,args)
76  else:
77  import gui
78  print 'starting gui...'
79  gui.main(args)
80 
81 if __name__ == "__main__":
82  main()
def reset_file(size, max_size, reason)
Definition: cat.py:22
def main(argsCat=None)
Definition: gui.py:93
def main(argv=None)
Definition: cat.py:67
Definition: shell.py:1
def safe_getsize(hist_file)
Definition: cat.py:30