/usr/lib/python2.7/dist-packages/pyfits/verify.py is in python-pyfits 1:3.2-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import warnings
from pyfits.util import indent
class VerifyError(Exception):
"""
Verify exception class.
"""
pass
class VerifyWarning(UserWarning):
"""
Verify warning class.
"""
class _Verify(object):
"""
Shared methods for verification.
"""
def run_option(self, option='warn', err_text='', fix_text='Fixed.',
fix=None, fixable=True):
"""
Execute the verification with selected option.
"""
text = err_text
if not fixable:
option = 'unfixable'
if option in ['warn', 'exception']:
pass
# fix the value
elif option == 'unfixable':
text = 'Unfixable error: %s' % text
else:
if fix:
fix()
text += ' ' + fix_text
return text
def verify(self, option='warn'):
"""
Verify all values in the instance.
Parameters
----------
option : str
Output verification option. Must be one of ``"fix"``,
``"silentfix"``, ``"ignore"``, ``"warn"``, or
``"exception"``. See :ref:`verify` for more info.
"""
opt = option.lower()
if opt not in ['fix', 'silentfix', 'ignore', 'warn', 'exception']:
raise ValueError('Option %s not recognized.' % option)
if opt == 'ignore':
return
x = str(self._verify(opt)).rstrip()
if opt in ['fix', 'silentfix'] and 'Unfixable' in x:
raise VerifyError('\n' + x)
if opt not in ['silentfix', 'exception'] and x:
warnings.warn(u'Output verification result:')
for line in x.splitlines():
# Each line contains a single issue that was fixed--issue a
# separate warning for each of those issues
warnings.warn(line, VerifyWarning)
warnings.warn(u'Note: PyFITS uses zero-based indexing.\n')
if opt == 'exception' and x:
raise VerifyError('\n' + x)
class _ErrList(list):
"""
Verification errors list class. It has a nested list structure
constructed by error messages generated by verifications at
different class levels.
"""
def __init__(self, val, unit='Element'):
list.__init__(self, val)
self.unit = unit
def __str__(self):
return self._display()
def _display(self, ind=0):
"""
Print out nested structure with corresponding indentations.
"""
result = []
element = 0
# go through the list twice, first time print out all top level
# messages
for item in self:
if not isinstance(item, _ErrList):
result.append('%s\n' % indent(item, shift=ind))
# second time go through the next level items, each of the next level
# must present, even it has nothing.
for item in self:
if isinstance(item, _ErrList):
tmp = item._display(ind=ind + 1)
# print out a message only if there is something
if tmp.strip():
if self.unit:
result.append(indent('%s %s:\n' % (self.unit, element),
shift=ind))
result.append(tmp)
element += 1
return ''.join(result)
|