1 2# constant objects 3 4from .parsing import QuietException 5from .shortcut import Shortcut 6from .constants_palette import palette_dict 7import re 8 9class _loadable: 10 pdb = 0 11 mol = 1 12 molstr = 3 13 mmod = 4 14 mmodstr = 6 15 xplor = 7 16 model = 8 17 pdbstr = 9 18 brick = 10 # chempy.brick object 19 chempymap = 11 # chempy.map object (only used by cctbx/examples/view_fft_map.py) 20 callback = 12 # pymol callback obejct 21 cgo = 13 # compiled graphic object 22 xyz = 15 # xyz, tinker format 23 ccp4 = 18 # CCP4 map, under development 24 pmo = 19 # pmo, experimental molecular object format 25 top = 21 # AMBER topology 26 trj = 22 # AMBER trajectory 27 crd = 23 # AMBER coordinate 28 rst = 24 # AMBER restart 29 xplorstr = 26 # XPLOR map as string 30 phi = 27 # Delphi/Grasp 31 fld = 28 # AVS field format (not yet general -- only uniform allowed) 32 brix = 29 # BRIX/DSN6/O map format 33 grd = 30 # Insight II Grid format 34 pqr = 31 # PQR file (modified PDB file for APBS) 35 dx = 32 # DX file (APBS) 36 mol2 = 33 # MOL2 file (TRIPOS) 37 mol2str = 34 # MOL2 file string (TRIPOS) 38 p1m = 35 # P1M file (combined data & secure commands) 39 ccp4str = 36 # CCP4 map string 40 sdf = 37 # new default... 41 sdf2 = 37 # SDF using C-based SDF parser (instead of Python) 42 sdf2str = 38 # SDF ditto 43 trj2 = 45 # trj trajectroy format (via plugin) 44 xyzstr = 49 # 45 phistr = 51 # electrostatic map as a string 46 acnt = 56 # Tripos/Sybyl acnt grid file (proposed) 47 cif = 60 # C++ based CIF parser 48 cifstr = 61 49 plugin = 64 50 mae = 65 51 maestr = 66 52 pdbqt = 67 53 vdbstr = 69 54 vdb = 70 55 mmtf = 71 56 mmtfstr = 72 57 map = 73 # unspecified CCP4 or MRC map 58 mrc = 74 59 60class loadable(_loadable): 61 @classmethod 62 def _reverse_lookup(cls, number): 63 for name in dir(cls): 64 if getattr(cls, name) == number: 65 return name 66 return '' 67 68_load2str = { loadable.pdb : loadable.pdbstr, 69 loadable.vdb: loadable.vdbstr, 70 loadable.cif : loadable.cifstr, 71 loadable.mmtf : loadable.mmtfstr, 72 loadable.mae : loadable.maestr, 73 loadable.mol : loadable.molstr, 74 loadable.xplor : loadable.xplorstr, 75 loadable.mol2 : loadable.mol2str, 76 loadable.mmod : loadable.mmodstr, 77 loadable.ccp4 : loadable.ccp4str, 78 loadable.xyz : loadable.xyzstr, 79 loadable.sdf2 : loadable.sdf2str} 80 81sanitize_alpha_list_re = re.compile(r"[^a-zA-Z0-9_\'\"\.\-\[\]\,]+") 82nt_hidden_path_re = re.compile(r"\$[\/\\]") 83 84def safe_alpha_list_eval(st): 85 '''Like `safe_eval` but removes most non-alpha-numeric characters. 86 87 >>> safe_alpha_list_eval("[A B/C, D+E:F]") 88 ['ABC', 'DEF'] 89 ''' 90 st = sanitize_alpha_list_re.sub('',st) 91 return safe_list_eval(st) 92 93class SafeEvalNS(object): 94 def __getitem__(self, name): 95 return name 96 97def safe_eval(st): 98 '''Safe version of "eval" which evaluates names to strings. 99 100 # "foo" is a string 101 >>> safe_eval('foo, 123, 4 + 5, "A B C", {}, "{}"') 102 ('foo', 123, 9, 'A B C', {}, '{}') 103 104 # no harmful code possible 105 >>> safe_eval('__import__("os").unlink("foo.txt")') 106 TypeError: 'str' object is not callable 107 ''' 108 return eval(st, {}, SafeEvalNS()) 109 110safe_list_eval = safe_eval 111 112DEFAULT_ERROR = -1 113DEFAULT_SUCCESS = None 114 115#-------------------------------------------------------------------- 116# shortcuts... 117 118toggle_dict = {'on':1,'off':0,'1':1,'0':0,'toggle':-1, '-1':-1} 119toggle_sc = Shortcut(toggle_dict.keys()) 120 121stereo_dict = {'on':-2,'off':0,'0':0,'1':-2,'swap':-1, 122 'chromadepth': -3, 123 'quadbuffer':1,'crosseye':2, 124 'walleye':3,'geowall':4,'sidebyside':5, 125 'byrow':6, 'bycolumn':7, 'checkerboard':8, 126 'custom': 9, 'anaglyph' : 10, 127 'dynamic' : 11, 'clonedynamic': 12, 128 'openvr' : 13 } 129 130stereo_sc = Shortcut(stereo_dict.keys()) 131 132space_sc = Shortcut(['cmyk','rgb','pymol']) 133 134window_dict = { 'show' : 1, 'hide' : 0, 'position' : 2, 'size' : 3, 135 'box' : 4, 'maximize' : 5, 'fit' : 6, 'focus' : 7, 136 'defocus' : 8 } 137window_sc = Shortcut(window_dict.keys()) 138 139repres = { 140 'everything' : -1, 141 'sticks' : 0, 142 'spheres' : 1, 143 'surface' : 2, 144 'labels' : 3, 145 'nb_spheres' : 4, 146 'cartoon' : 5, 147 'ribbon' : 6, 148 'lines' : 7, 149 'mesh' : 8, 150 'dots' : 9, 151 'dashes' :10, 152 'nonbonded' :11, 153 'cell' :12, 154 'cgo' :13, 155 'callback' :14, 156 'extent' :15, 157 'slice' :16, 158 'angles' :17, 159 'dihedrals' :18, 160 'ellipsoids' :19, 161 'volume' :20, 162} 163 164repmasks = { 165 'everything' : 0b000111111111111111111111, 166 'sticks' : 0b000000000000000000000001, 167 'spheres' : 0b000000000000000000000010, 168 'surface' : 0b000000000000000000000100, 169 'labels' : 0b000000000000000000001000, 170 'nb_spheres' : 0b000000000000000000010000, 171 'cartoon' : 0b000000000000000000100000, 172 'ribbon' : 0b000000000000000001000000, 173 'lines' : 0b000000000000000010000000, 174 'mesh' : 0b000000000000000100000000, 175 'dots' : 0b000000000000001000000000, 176 'dashes' : 0b000000000000010000000000, 177 'nonbonded' : 0b000000000000100000000000, 178 'cell' : 0b000000000001000000000000, 179 'cgo' : 0b000000000010000000000000, 180 'callback' : 0b000000000100000000000000, 181 'extent' : 0b000000001000000000000000, 182 'slice' : 0b000000010000000000000000, 183 'angles' : 0b000000100000000000000000, 184 'dihedrals' : 0b000001000000000000000000, 185 'ellipsoids' : 0b000010000000000000000000, 186 'volume' : 0b000100000000000000000000, 187 188 # combinations 189 'licorice' : 0b000000000000000000010001, # sticks | nb_spheres 190 'wire' : 0b000000000000100010000000, # lines | nonbonded 191} 192 193repmasks_sc = Shortcut(repmasks) 194repres_sc = Shortcut(repres.keys()) 195 196boolean_dict = { 197 'yes' : 1, 198 'no' : 0, 199 '1' : 1, 200 '0' : 0, 201 'on' : 1, 202 'off' : 0 203 } 204 205boolean_sc = Shortcut(boolean_dict.keys()) 206 207palette_sc = Shortcut(palette_dict.keys()) 208 209 210location_code = { 211 'first' : -1, 212 'top' : -1, 213 'upper' : -2, 214 'current' : 0, 215 'bottom' : 1, 216 'last' : 1 217 } 218location_sc = Shortcut(location_code.keys()) 219 220class fb_action: 221 set = 0 222 enable = 1 223 disable = 2 224 push = 3 225 pop = 4 226 227class fb_module: 228 229# This first set represents internal C systems 230 231 all =0 232 isomesh =1 233 map =2 234 matrix =3 235 mypng =4 236 triangle =5 237 match =6 238 raw =7 239 isosurface =8 240 opengl =9 241 242 color =10 243 cgo =11 244 feedback =12 245 scene =13 246 threads =14 247 symmetry =15 248 ray =16 249 setting =17 250 object =18 251 ortho =19 252 movie =20 253 python =21 254 extrude =22 255 rep =23 256 shaker =24 257 258 coordset =25 259 distset =26 260 gadgetset =27 261 262 objectmolecule =30 263 objectmap =31 264 objectmesh =32 265 objectdist =33 266 objectcgo =34 267 objectcallback =35 268 objectsurface =36 269 objectgadget =37 270 objectslice =38 271 objectvolume =39 272 273 repangle =43 274 repdihederal =44 275 repwirebond =45 276 repcylbond =46 277 replabel =47 278 repsphere =49 279 repsurface =50 280 repmesh =51 281 repdot =52 282 repnonbonded =53 283 repnonbondedsphere =54 284 repdistdash =55 285 repdistlabel =56 286 repribbon =57 287 repcartoon =58 288 sculpt =59 289 vfont =60 290 # in layer0 291 shader =61 292 shadermgr =62 293 shaderprg =63 294 session =64 295 # in layer1 296 property =65 297 298 executive =70 299 selector =71 300 editor =72 301 nag =73 302 303 export =75 304 ccmd =76 305 api =77 306 307 main =80 308 309# This second set, with negative indices 310# represent "python-only" subsystems 311 312 parser =-1 313 cmd =-2 314 315class fb_mask: 316 output = 0x01 # Python/text output 317 results = 0x02 318 errors = 0x04 319 actions = 0x08 320 warnings = 0x10 321 details = 0x20 322 blather = 0x40 323 debugging = 0x80 324 everything = 0xFF 325 326 327# State count starts at 1. Values <=0 have special meaning. 328ALL_STATES = 0 329CURRENT_STATE = -1 330