Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
options_class.py
Go to the documentation of this file.
1 # :noTabs=true:
2 # (c) Copyright Rosetta Commons Member Institutions.
3 # (c) This file is part of the Rosetta software suite and is made available under license.
4 # (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
5 # (c) For more information, see http://www.rosettacommons.org. Questions about this can be
6 # (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
7 
8 ## @file basic/options/options_class.py
9 ## @brief Program options generation script classes
10 ## @author Sergey Lyskov (Sergey.Lyskov@jhu.edu)
11 
12 
13 import sys
14 import time
15 
16 KnownTypes=['Boolean', 'Integer', 'Real', 'String', 'File', 'Path', 'BooleanVector', 'IntegerVector', 'ResidueChainVector', 'RealVector', 'StringVector', 'FileVector', 'PathVector']
17 
18 # argument should be string or list<string>
19 # - wrap strings with "".
20 def wrapCStrig(s):
21  if s is not None:
22  if type(s) == list:
23  for i, e in enumerate(s): s[i] = wrapCStrig(e)
24  return s
25 
26  if len(s) < 2 :
27  return '"' + s + '"'
28  else:
29  if s[0] == '"' and s[-1] == '"': return s
30  else: return '"' + s + '"'
31  return s
32 
33 
34 class Option:
35  def __init__(self, name=None, ctype=None, group=None, desc="No description", short="",
36  oldName="-",
37  lower=None, upper=None, default=None, legal=None, n=None, n_lower=None, n_upper=None, restrict_access=False):
38 
39  if ctype not in KnownTypes:
40  print 'Unknown type:%s!!!' % ctype
41  sys.exit()
42  if default == 'none' or default == 'None':
43  print '*** Option %(name)s will default to the *string* "%(default)s"' % vars()
44  print ' If you want no default, write default=None (no quotes)'
45 
46  self.ctype = ctype; self.name = name; self.group = group
47  self.desc = desc; self.short = short; self.oldName = oldName
48  self.lower = lower; self.upper = upper; self.default = default; self.legal=legal
49  self.restrict_access = restrict_access;
50  self.n = n; self.n_lower = n_lower; self.n_upper = n_upper
51  #Is this the synonymous member of an option group (e.g. -in:file:file)
52  self.is_option_group = False
53  # Wraping c-strings in "
54  if ctype == 'String' or ctype == 'Path' or ctype == 'File' or \
55  ctype == 'StringVector' or ctype == 'PathVector' or ctype == 'FileVector':
56  if ctype == 'StringVector':
57  self.default = self.default
58  else:
59  self.default = wrapCStrig( self.default )
60  self.lower = wrapCStrig( self.lower )
61  self.upper = wrapCStrig( self.upper )
62  if type( self.legal ) == type([]):
63  for i in range(0, len(self.legal) ):
64  self.legal[i] = wrapCStrig( self.legal[i] )
65  else: self.legal = wrapCStrig( self.legal )
66 
67 
68  def get_namespace(self,level):
69  if self.group:
70  namespaces = self.group.split(':')
71  #print namespaces
72  if len(namespaces) > level:
73  return namespaces[level]
74 
75  # return C++ name of option object
76  def getCName(self):
77  if str( self.name[:1] ).isdigit() : return 'n' + self.name
78  else: return self.name
79 
80 
81  def getOptionCC(self):
82  s = ''
83  s += 'option.add( basic::options::OptionKeys::'
84  if self.group:
85  s += self.group.replace(':','::')+'::'
86  s += self.getCName()+', "'+self.desc+'" )'
87  if self.short: s+= '.shortd( "' + self.short + '" )'
88  if self.lower : s+= '.lower(' + self.lower + ')'
89  if self.upper : s+= '.upper(' + self.upper + ')'
90  if self.n: s+= '.n(' + self.n + ')'
91  if self.n_lower: s+= '.n_lower(' + self.n_lower + ')'
92  if self.n_upper: s+= '.n_upper(' + self.n_upper + ')'
93  if self.legal :
94  if type(self.legal) == type(''):
95  s+= '.legal(' + self.legal + ')'
96  else:
97  for l in self.legal:
98  s+= '.legal(' + l + ')'
99  if self.default is not None :
100  if type(self.default) == type(''): s+= '.def(' + self.default + ')'
101  else:
102  if len( self.default ) > 0:
103  for d in self.default:
104  s+= '.def(' + d + ')'
105  else:
106  s+='.def()'
107  if self.restrict_access: s+= '.restrict_access(true)'
108  #These have to be last, as they're in the base class
109  if self.is_option_group: s+= '.is_group(true)'
110  return s + ';\n'
111 
112 
113  def getOptionKeysHH(self):
114  s = ''; se = ''
115  if self.group:
116  for ns in self.group.split(':'):
117  s += 'namespace ' + ns + ' { '
118  se += ' }'
119  s += 'extern '+ self.ctype+'OptionKey const '+self.getCName()+';'
120  return s + se + '\n'
121 
122 
123  def getOptionKeysCC(self):
124  s = ''; se = ''
125  if self.group:
126  for ns in self.group.split(':'):
127  s += 'namespace ' + ns + ' { '
128  se += ' }'
129 
130  s += self.ctype+'OptionKey const '+self.getCName()+'( "'
131 
132  if self.group:
133  s += self.group
134  if self.name != self.group.split(':')[-1] : s += ':' + self.name
135  else: s += self.name
136  s+= '" ); ' + se + '\n'
137  return s
138 
139 
140  def getWikiTableRow(self):
141  def smStr(s): return s or ''
142 
143  s = ' |-\n'
144  s += ' | -%(name)s <%(ctype)s>\n' % {'name':self.name, 'ctype':self.ctype}
145  s += ' | ' + self.desc + '\n'
146  s += ' | ' + smStr(self.lower) + '-' + smStr(self.upper) + '\n'
147  if self.legal=='true' and self.default=='true': s += ' |\n'
148  else:
149  if type(self.default) == type( [] ):
150  s += ' | ' + str( self.default ) + '\n'
151  else: s += ' | ' + smStr(self.default) + '\n'
152  s += ' | -' + self.oldName + '\n'
153  s += ' |-\n'
154  return s
155 
156 
157  def getDoxygenRow(self):
158  def smStr(s): return s or ''
159 
160  s = "<dl>\n"
161  s += '<dt><b>-%(name)s</b> \\<%(ctype)s\\><dt>\n' % {'name':self.name, 'ctype':self.ctype}
162  s += '<dd>' + self.desc + '</dd><br>\n'
163  if self.lower or self.upper:
164  s += '<dd>Range: ' + smStr(self.lower) + '-' + smStr(self.upper) + '</dd><br>\n'
165  if self.legal=='true' and self.default=='true': pass #s += ' |\n'
166  else:
167  if type(self.default) == type( [] ): df = str( self.default )
168  else: df = smStr(self.default)
169 
170  if df: s += '<dd>Default: ' + df + '</dd><br>\n'
171 
172  #s += ' | -' + self.oldName + '\n'
173  s += '</dl>\n'
174  return s
175 
176 
177  def getMarkdownRow(self):
178  #Unfortunately, pure markdown doesn't have a defintion list element - steal HTML
179  def smStr(s): return s or ''
180 
181  s = '<dt><b>-%(name)s</b> \\<%(ctype)s\\></dt>\n' % {'name':self.name, 'ctype':self.ctype}
182  s += '<dd>' + self.desc + '<br/>'
183  if self.lower or self.upper:
184  s += 'Range: ' + smStr(self.lower) + '-' + smStr(self.upper) + '<br/>'
185  if self.legal=='true' and self.default=='true': pass #s += ' |\n'
186  else:
187  if type(self.default) == type( [] ): df = str( self.default )
188  else: df = smStr(self.default)
189 
190  if df: s += 'Default: ' + df + '<br/>'
191 
192  s += '</dd>\n'
193  return s
194 
195 
196 def Option_Group(group, *args):
197  res = []
198 
199  for o in args: # first concat all lists
200  if type(o) == type([]): res += o
201  else: res.append( o )
202 
203  if group:
204  found_option_for_group = False
205  for o in res:
206  if o.group: o.group = group + ':' + o.group
207  else: o.group = group
208  if o.name == group:
209  found_option_for_group = True
210  o.is_option_group = True
211  # In order to nest option specifiers in a flags file (passed with @flags.txt),
212  # there must be a boolean option with the same name as each option group.
213  if not found_option_for_group:
214  o = Option( group, 'Boolean', group=group, desc=group+" option group", legal='true', default='true' )
215  o.is_option_group = True
216  res.insert( 0, o )
217  return res
218 
219 
220 def writeToFile(opt_list, fileName, mapFunction):
221  l = map(mapFunction, opt_list)
222  f = file(fileName, 'wb'); f.write( "".join(l) ); f.close()
223 
224 def printWikiTable(opt_list):
225  s = ""
226  prevGroup = None
227  for o in opt_list:
228  if prevGroup != o.group: # Generating new table
229  if prevGroup : s += ' |}\n' # Closing previos table if any
230  s += """{| border="1" cellpadding="10" width="100%"\n |+ '''""" + (o.group or '')
231  s += " Option Group'''\n"
232  s += ' ! Option name\n' # width="10%" |
233  s += ' ! Description\n'
234  s += ' ! Range\n'
235  s += ' ! Default\n'
236  s += ' ! Old name\n'
237  s += ' |-\n'
238  s += o.getWikiTableRow()
239  prevGroup = o.group
240  return s + ' |}\n'
241 
242 
243 def getDoxygenPage(opt_list):
244  s = "/*!\n@page full_options_list Rosetta command line option descriptions.\n"
245  s += "<i>(This is an automatically generated file, do not edit!)</i> Generated: "+time.strftime("%Y-%m-%d")+"\n"
246  s += "<ul>\n"
247  prevGroup = None
248  for o in opt_list:
249  if prevGroup != o.group: # Generating new table
250  if prevGroup : s += ' </li>\n' # Closing previos table if any
251  s += "<li><h2>" + (o.group or '') + "</h2>\n"
252  s += o.getDoxygenRow()
253  prevGroup = o.group
254  return s + "</ul>\n */\n"
255 
256 
257 def getMarkdownPage(opt_list):
258  s = "# List of Rosetta command line options.\n\n"
259  s += "_(This is an automatically generated file, do not edit!)_\n"
260  s += "Generated: " + time.strftime("%Y-%m-%d") + "\n\n"
261  s += "_Note that some application specific options may not be present in this list._\n\n"
262  s += "[[_TOC_]]\n"
263  in_dl = False
264  prevGroup = []
265  for o in opt_list:
266  if prevGroup != o.group: # Generating new group
267  hlevel=min(6, len(o.group.split(":"))+1)
268  if in_dl:
269  s += "</dl>\n"
270  s += "+ <h"+str(hlevel)+">-" + (o.group or '') + "</h"+str(hlevel)+">\n"
271  s += "<dl>\n"
272  in_dl = True
273  s += o.getMarkdownRow()
274  prevGroup = o.group
275  if in_dl:
276  s += "</dl>"
277  return s + "\n"
static T min(T x, T y)
Definition: Svm.cc:16
BooleanOptionKey const enumerate
Fstring::size_type len(Fstring const &s)
Length.
Definition: Fstring.hh:2207