1 """
2 Module providing high level functions to manage command line options.
3 """
4 import logging
5
6
7 import sys
8 from logging import debug
9
10 from optparse import OptionParser
11
12 from obitools.utils import universalOpen
13 from obitools.utils import fileSize
14 from obitools.utils import universalTell
15
17 '''
18 Build an option manager fonction. that is able to parse
19 command line options of the script.
20
21 @param optionDefinitions: list of function describing a set of
22 options. Each function must allows as
23 unique parametter an instance of OptionParser.
24 @type optionDefinitions: list of functions.
25
26 @param entryIterator: an iterator generator function returning
27 entries from the data files.
28
29 @type entryIterator: an iterator generator function with only one
30 parametter of type file
31 '''
32 parser = OptionParser()
33 parser.add_option('--DEBUG',
34 action="store_true", dest="debug",
35 default=False,
36 help="Set logging in debug mode")
37
38 parser.add_option('--no-psyco',
39 action="store_true", dest="noPsyco",
40 default=False,
41 help="Don't use psyco even if it installed")
42
43 for f in optionDefinitions:
44 f(parser)
45
46 def commandLineAnalyzer():
47 options,files = parser.parse_args()
48 if options.debug:
49 logging.root.setLevel(logging.DEBUG)
50
51 i = allEntryIterator(files,entryIterator)
52 return options,i
53
54 return commandLineAnalyzer
55
56 _currentInputFileName=None
57 _currentFile = None
58 _currentFileSize = None
59
62
65
68
71
72 -def allEntryIterator(files,entryIterator):
73 global _currentFile
74 global _currentInputFileName
75 global _currentFileSize
76 if files :
77 for f in files:
78 _currentInputFileName=f
79 f = universalOpen(f)
80 _currentFile=f
81 _currentFileSize=fileSize(_currentFile)
82 debug(f)
83 if entryIterator is None:
84 for line in f:
85 yield line
86 else:
87 for entry in entryIterator(f):
88 yield entry
89 else:
90 if entryIterator is None:
91 for line in sys.stdin:
92 yield line
93 else:
94 for entry in entryIterator(sys.stdin):
95 yield entry
96