/usr/share/pyshared/fabio/fabioutils.py is in python-fabio 0.0.8-1.
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | import re, os
def construct_filename(*args, **kwds):
raise Exception("You probably want fabio.jump_filename")
FILETYPES = {
# extension NNNimage fabioclass
# type consistency - always use a list if one case is
'edf' : ['edf'],
'cor' : ['edf'],
'pnm' : ['pnm'],
'pgm' : ['pnm'],
'pbm' : ['pnm'],
'tif' : ['tif'],
'tiff' : ['tif'],
'img' : ['adsc', 'OXD', 'HiPiC'],
'mccd' : ['marccd'],
'mar2300': ['mar345'],
'sfrm' : ['bruker100'],
'msk' : ['fit2dmask'],
'spr' : ['fit2dspreadsheet'],
'dm3' : ['dm3'],
'kcd' : ['kcd'],
'cbf' : ['cbf'],
'xml' : ["xsd"],
'xsd' : ["xsd"],
}
# Add bzipped and gzipped
for key in FILETYPES.keys():
FILETYPES[key + ".bz2"] = FILETYPES[key]
FILETYPES[key + ".gz"] = FILETYPES[key]
# Compressors
COMPRESSORS = {}
try:
lines = os.popen("gzip -h 2>&1").read()
# Looking for "usage"
if "sage" in lines:
COMPRESSORS['.gz'] = 'gzip -dc '
else:
COMPRESSORS['.gz'] = None
except:
COMPRESSORS['.gz'] = None
try:
lines = os.popen("bzip2 -h 2>&1").read()
# Looking for "usage"
if "sage" in lines:
COMPRESSORS['.bz2'] = 'bzip2 -dc '
else:
COMPRESSORS['.bz2'] = None
except:
COMPRESSORS['.bz2'] = None
def getnum(name):
"""
# try to figure out a file number
# guess it starts at the back
"""
stem , num, post_num = numstem(name)
try:
return int(num)
except ValueError:
return None
class filename_object:
"""
The 'meaning' of a filename
"""
def __init__(self, stem,
num=None,
directory=None,
format=None,
extension=None,
postnum=None,
digits=4):
self.stem = stem
self.num = num
self.format = format
self.extension = extension
self.digits = digits
self.postnum = postnum
self.directory = directory
def str(self):
""" Return a string representation """
fmt = "stem %s, num %s format %s extension %s " + \
"postnum = %s digits %s dir %s"
return fmt % tuple([str(x) for x in [
self.stem ,
self.num ,
self.format ,
self.extension ,
self.postnum ,
self.digits ,
self.directory ] ])
def tostring(self):
"""
convert yourself to a string
"""
name = self.stem
if self.digits is not None and self.num is not None:
fmt = "%0" + str(self.digits) + "d"
name += fmt % self.num
if self.postnum is not None:
name += self.postnum
if self.extension is not None:
name += self.extension
if self.directory is not None:
name = os.path.join(self.directory, name)
return name
def numstem(name):
""" cant see how to do without reversing strings
Match 1 or more digits going backwards from the end of the string
"""
reg = re.compile(r"^(.*?)(-?[0-9]{0,9})(\D*)$")
#reg = re.compile("""(\D*)(\d\d*)(\w*)""")
try:
res = reg.match(name).groups()
#res = reg.match(name[::-1]).groups()
#return [ r[::-1] for r in res[::-1]]
if len(res[0]) == len(res[1]) == 0: # Hack for file without number
return [res[2], '', '']
return [ r for r in res]
except AttributeError: # no digits found
return [name, "", ""]
def deconstruct_filename(filename):
"""
Break up a filename to get image type and number
"""
direc , name = os.path.split(filename)
if len(direc) == 0:
direc = None
parts = os.path.split(name)[-1].split(".")
# loop back from end
compressed = False
extn = ""
postnum = ""
ndigit = 4
if parts[-1] in ["gz", "bz2"]:
extn = "." + parts[-1]
parts = parts[:-1]
compressed = True
if parts[-1] in FILETYPES.keys():
typ = FILETYPES[parts[-1]]
extn = "." + parts[-1] + extn
try:
stem , numstring, postnum = numstem(".".join(parts[:-1]))
num = int(numstring)
ndigit = len(numstring)
except:
# There is no number - hence make num be None, not 0
num = None
stem = "".join(parts[:-1])
else:
# Probably two type left
if len(parts) == 1:
# Probably GE format stem_numb
parts2 = parts[0].split("_")
try:
num = int(parts2[-1])
ndigit = len(parts2[-1])
typ = ['GE']
stem = "_".join(parts2[:-1]) + "_"
except:
pass
else:
try:
num = int(parts[-1])
ndigit = len(parts[-1])
typ = ['bruker']
stem = ".".join(parts[:-1]) + "."
except:
typ = None
extn = "." + parts[-1] + extn
try:
stem , numstring, postnum = numstem(".".join(parts[:-1]))
num = int(numstring)
ndigit = len(numstring)
except:
raise
# raise Exception("Cannot decode "+filename)
obj = filename_object(stem,
num=num,
directory=direc,
format=typ,
extension=extn,
postnum=postnum,
digits=ndigit)
return obj
def next_filename(name, padding=True):
""" increment number """
obj = deconstruct_filename(name)
obj.num += 1
if not padding:
obj.digits = 0
return obj.tostring()
def previous_filename(name, padding=True):
""" decrement number """
obj = deconstruct_filename(name)
obj.num -= 1
if not padding:
obj.digits = 0
return obj.tostring()
def jump_filename(name, num, padding=True):
""" jump to number """
obj = deconstruct_filename(name)
obj.num = num
if not padding:
obj.digits = 0
return obj.tostring()
def extract_filenumber(name):
""" extract file number """
obj = deconstruct_filename(name)
return obj.num
def isAscii(name, listExcluded=None):
"""
@param name: string to check
@param listExcluded: list of char or string excluded.
@return: True of False whether name is pure ascii or not
"""
isascii = None
try:
name.decode("ascii")
except UnicodeDecodeError:
isascii = False
else:
if listExcluded:
isascii = not(any(bad in name for bad in listExcluded))
else:
isascii = True
return isascii
|