1"""Utility functions for outputting messages, diagnostics and errors' 2 3Copyright (C) 2013, Joshua More and Michele Ceriotti 4 5This program is free software: you can redistribute it and/or modify 6it under the terms of the GNU General Public License as published by 7the Free Software Foundation, either version 3 of the License, or 8(at your option) any later version. 9 10This program is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with this program. If not, see <http.//www.gnu.org/licenses/>. 17 18 19Classes: 20 Verbosity: Concise class to check the selected level of output 21 22Functions: 23 banner: Prints the program welcome "screen" 24 help: Prints the input syntax help 25 info: Prints some information to standard output, depending on the level of verbosity 26 warning: Same as info, but with a "!W!" prefix and optionally printing a stack trace 27""" 28 29import traceback, sys 30 31__all__ = ['Verbosity', 'verbosity',' help', 'banner', 'info', 'warning'] 32 33 34VERB_QUIET = 0 35VERB_LOW = 1 36VERB_MEDIUM = 2 37VERB_HIGH = 3 38VERB_DEBUG = 4 39 40class Verbosity(object): 41 """Class used to determine what to print to standard output. 42 43 Attributes: 44 level: Determines what level of output to print. 45 """ 46 47 level = "low" 48 49 def __getattr__(self, name): 50 """Determines whether a certain verbosity level is 51 less than or greater than the stored value. 52 53 Used to decide whether or not a certain info or warning string 54 should be output. 55 56 Args: 57 name: The verbosity level at which the info/warning string 58 will be output. 59 """ 60 61 if name is "quiet": 62 return self.level >= VERB_QUIET 63 elif name is "low": 64 return self.level >= VERB_LOW 65 elif name is "medium": 66 return self.level >= VERB_MEDIUM 67 elif name is "high": 68 return self.level >= VERB_HIGH 69 elif name is "debug": 70 return self.level >= VERB_DEBUG 71 72 def __setattr__(self, name, value): 73 """Sets the verbosity level 74 75 Args: 76 name: The name of what to set. Should always be 'level'. 77 value: The value to set the verbosity to. 78 79 Raises: 80 ValueError: Raised if either the name or the level is not 81 a valid option. 82 """ 83 84 if name == "level": 85 if value == "quiet": 86 level = VERB_QUIET 87 elif value == "low": 88 level = VERB_LOW 89 elif value == "medium": 90 level = VERB_MEDIUM 91 elif value == "high": 92 level = VERB_HIGH 93 elif value == "debug": 94 level = VERB_DEBUG 95 else: 96 raise ValueError("Invalid verbosity level " + str(value) + " specified.") 97 super(Verbosity,self).__setattr__("level", level) 98 99 100verbosity = Verbosity() 101 102def help(): 103 """Prints out a help string.""" 104 105 print """usage: %s input """%sys.argv[0] 106 107def banner(): 108 """Prints out a banner.""" 109 110 print """ 111 ____ ____ ____ ____ 112/ \ / \ / \ / \ 113| ################################# | 114\__#_/ \____/ \____/ \_#__/ 115 # _ _______ _____ # 116 # (_) |_ __ \|_ _| # v. 1.0 117 # __ ______ | |__) | | | # 118 Y [ ||______|| ___/ | | # A Python interface for (ab initio) 119 0 0 | | _| |_ _| |_ # (path integral) molecular dynamics. 120 # [___] |_____| |_____| # 121 __#_ ____ ____ _#__ 122/ # \ / \ / \ / # \ 123| ################################# | 124\____/ \____/ \____/ \____/ 125 126 """ 127 128 129def info(text="", show=True ): 130 """Prints a warning message. 131 132 Args: 133 text: The text of the information message. 134 show: A boolean describing whether or not the message should be 135 printed. 136 """ 137 138 if not show: 139 return 140 print text 141 142def warning(text="", show=True): 143 """Prints a warning message. 144 145 Args: 146 text: The text of the information message. 147 show: A boolean describing whether or not the message should be 148 printed. 149 """ 150 151 if not show: 152 return 153 if verbosity.debug: 154 traceback.print_stack(file=sys.stdout) 155 print " !W! " + text 156