def testMultifilesave(self): import glob for name in ['ala', 'gly', 'his', 'arg']: cmd.fragment(name) # multistate for i in range(2, 11): cmd.create('ala', 'ala', 1, i) for fmt in ['{}-{:02}.cif', '{}-{state}.cif']: with testing.mkdtemp() as dirname: cmd.multifilesave(os.path.join(dirname, fmt), 'ala', 0) filenames = [os.path.basename(p) for p in glob.glob(os.path.join(dirname, '*.cif'))] self.assertEqual(len(filenames), 10) self.assertTrue('ala-03.cif' in filenames) with testing.mkdtemp() as dirname: cmd.multifilesave(os.path.join(dirname, '{}.pdb'), 'a* g*') filenames_full = sorted(glob.glob(os.path.join(dirname, '*.pdb'))) filenames = [os.path.basename(p) for p in filenames_full] self.assertEqual(filenames, ['ala.pdb', 'arg.pdb', 'gly.pdb']) cmd.delete('*') cmd.load(filenames_full[0]) self.assertEqual(cmd.count_atoms(), 10) cmd.delete('*') cmd.load(filenames_full[1]) self.assertEqual(cmd.count_atoms(), 24)
def test_unc(self): ''' UNC-Paths (PYMOL-2954) ''' pymolwin = self._get_pymolwin() with testing.mkdtemp() as base: if base[1] != ':': self.skipTest('no drive letter: ' + base) return if base.startswith('\\\\'): uncpath = base else: uncpath = '\\\\localhost\\%s$%s' % (base[0], base[2:]) pml_filename = uncpath + '\\in.pml' out_filename = uncpath + '\\out.pdb' with open(pml_filename, 'w') as handle: handle.write('fragment gly\n') handle.write('save %s\n' % out_filename) subprocess.call([pymolwin, '+2', '-kcq', pml_filename]) # wait for spawned process time.sleep(0.5) cmd.load(out_filename) self.assertEqual(7, cmd.count_atoms())
def testFetchLocal(self): try: import urllib.parse as urlparse except ImportError: import urlparse with testing.mkdtemp() as fetch_path: names = [] cmd.set('fetch_path', fetch_path) cmd.set( 'fetch_host', urlparse.urlunsplit( ['file', '', self.datafile('pdb.mirror'), '', ''])) cmd.fetch('1avy', type='pdb') names += ['1avy'] self.assertItemsEqual(cmd.get_names(), names) cmd.fetch('1avyB', type='pdb') names += ['1avyB'] self.assertItemsEqual(cmd.get_names(), names) self.assertEqual(cmd.get_chains('1avyB'), ['B']) cmd.fetch('1aq5', type='pdb', multiplex=1) names += ['1aq5_%04d' % (i + 1) for i in range(20)] self.assertItemsEqual(cmd.get_names(), names)
def testFetchLocal(self): try: import urllib.parse as urlparse except ImportError: import urlparse # PyMOL 1.8.6 adds full URLs, remove them import pymol pdbpaths = pymol.importing.hostPaths['pdb'] pdbpaths[:] = [p for p in pdbpaths if '://' not in p] with testing.mkdtemp() as fetch_path: names = [] cmd.set('fetch_path', fetch_path) cmd.set('fetch_host', urlparse.urlunsplit(['file', '', self.datafile('pdb.mirror'), '', ''])) cmd.fetch('1avy', type='pdb') names += ['1avy'] self.assertItemsEqual(cmd.get_names(), names) cmd.fetch('1avyB', type='pdb') names += ['1avyB'] self.assertItemsEqual(cmd.get_names(), names) self.assertEqual(cmd.get_chains('1avyB'), ['B']) cmd.fetch('1aq5', type='pdb', multiplex=1) names += ['1aq5_%04d' % (i+1) for i in range(20)] self.assertItemsEqual(cmd.get_names(), names)
def testFetchLocal(self): try: import urllib.parse as urlparse except ImportError: import urlparse # PyMOL 1.8.6 adds full URLs, remove them import pymol pdbpaths = pymol.importing.hostPaths['pdb'] pdbpaths[:] = [p for p in pdbpaths if '://' not in p] with testing.mkdtemp() as fetch_path: names = [] cmd.set('fetch_path', fetch_path) cmd.set( 'fetch_host', urlparse.urlunsplit( ['file', '', self.datafile('pdb.mirror'), '', ''])) cmd.fetch('1avy', type='pdb') names += ['1avy'] self.assertItemsEqual(cmd.get_names(), names) cmd.fetch('1avyB', type='pdb') names += ['1avyB'] self.assertItemsEqual(cmd.get_names(), names) self.assertEqual(cmd.get_chains('1avyB'), ['B']) cmd.fetch('1aq5', type='pdb', multiplex=1) names += ['1aq5_%04d' % (i + 1) for i in range(20)] self.assertItemsEqual(cmd.get_names(), names)
def test(self, use_shaders, cgo_shader_ub, rep="cartoon"): self._setup_movie(rep) cmd.set("use_shaders", use_shaders) cmd.set("cgo_shader_ub_color", cgo_shader_ub) cmd.set("cgo_shader_ub_flags", cgo_shader_ub) cmd.set("cgo_shader_ub_normal", cgo_shader_ub) with testing.mkdtemp() as tempdir, self.timing(): cmd.mpng(os.path.join(tempdir, "f"))
def testFetch(self): with testing.mkdtemp() as fetch_path: names = [] cmd.set('fetch_path', fetch_path) cmd.set('fetch_host', 'pdb pdbe') cmd.fetch('1avy', '1avy1', type='pdb1') names += ['1avy1'] self.assertItemsEqual(cmd.get_names(), names) self.assertEqual(cmd.count_states('1avy1'), 3) cmd.fetch('1avy', type='2fofc') names += ['1avy_2fofc'] self.assertItemsEqual(cmd.get_names(), names) self.assertEqual(cmd.get_type('1avy_2fofc'), 'object:map')
def testCdLsPwd(self): with testing.mkdtemp() as path: cmd.cd(path) self.assertEqual(os.getcwd(), os.path.realpath(path)) touch('foo1.txt') touch('foo2.txt') touch('foo3.bin') cmd.feedback("disable", "python", "output") cmd.pwd() # no test of output possible cmd.ls('*.txt')
def test_get_user_config(self, basename): ''' pymolrc finding test ''' orig = dict((key, os.getenv(key)) for key in ['HOME', 'HOMEDRIVE', 'HOMEPATH', 'PYMOL_PATH']) with testing.mkdtemp() as tmpdir: os.chdir(tmpdir) os.mkdir('abc') pwd = os.getcwd() if sys.platform.startswith('win'): if pwd[1] != ':': raise ValueError('no drive letter') os.environ.pop('HOME', None) os.environ['HOMEDRIVE'] = pwd[:2] os.environ['HOMEPATH'] = pwd[2:] + r'\abc' else: os.environ['HOME'] = pwd + '/abc' pymolrc = os.path.join(pwd, 'abc', basename) with open(pymolrc, 'w') as handle: print('# hello', file=handle) a = invocation.get_user_config() self.assertEqual(pymolrc, os.path.normpath(a[0])) pymolrc = os.path.join(pwd, basename) with open(pymolrc, 'w') as handle: print('# hello', file=handle) a = invocation.get_user_config() self.assertEqual(pymolrc, os.path.normpath(a[0])) for key, value in orig.items(): if value is None: os.environ.pop(key, None) else: os.environ[key] = value
def testFetchLocal(self): import urlparse with testing.mkdtemp() as fetch_path: names = [] cmd.set('fetch_path', fetch_path) cmd.set('fetch_host', urlparse.urlunsplit(['file', '', self.datafile('pdb.mirror'), '', ''])) cmd.fetch('1avy') names += ['1avy'] self.assertItemsEqual(cmd.get_names(), names) cmd.fetch('1avyB') names += ['1avyB'] self.assertItemsEqual(cmd.get_names(), names) self.assertEqual(cmd.get_chains('1avyB'), ['B']) cmd.fetch('1aq5', multiplex=1) names += ['1aq5_%04d' % (i+1) for i in range(20)] self.assertItemsEqual(cmd.get_names(), names)
def test_scenes(self): self._load_example() cmd.scene('s1', 'store') cmd.enable('g1') cmd.scene('s2', 'store') cmd.mset('1x4') cmd.mview('store', 1, scene='s2') cmd.mview('store', 4, scene='s1') with testing.mkdtemp() as tempdir: # export prefix = os.path.join(tempdir, 'frame') cmd.mpng(prefix, width=100, height=100) img = self.get_imagearray(prefix + '0002.png') self.assertImageHasColor('red', img) self.assertImageHasColor('blue', img) img = self.get_imagearray(prefix + '0003.png') self.assertImageHasNotColor('red', img) self.assertImageHasColor('blue', img)
def testLoadPWG(self): if sys.version_info[0] < 3: import urllib2 else: import urllib.request as urllib2 content_index = b'Hello World\n' port = 8084 baseurl = 'http://localhost:%d' % port def urlread(url): handle = urllib2.urlopen(url, timeout=3.0) try: return handle.info(), handle.read() finally: handle.close() with testing.mkdtemp() as rootdir: filename = os.path.join(rootdir, 'index.html') with open(filename, 'wb') as handle: handle.write(content_index) filename = os.path.join(rootdir, 'start.pwg') with open(filename, 'w') as handle: handle.write('root %s\n' % rootdir) handle.write('port %d\n' % port) handle.write('header add Test-Key Test-Value\n') # new in 2.4 cmd.load(filename) header, content = urlread(baseurl) self.assertEqual(header['Test-Key'], 'Test-Value') self.assertEqual(content, content_index) # Warning: can't call locking functions here, will dead-lock # get_version is non-locking since 1.7.4 header, content = urlread(baseurl + '/apply/pymol.cmd.get_version') content = content.decode('ascii', errors='ignore') self.assertTrue(content, cmd.get_version()[0] in content)
def testMpng(self, modal): import glob, os shape2 = (100, 100) cmd.mset("1x4") cmd.mdo(1, 'bg_color red') cmd.mdo(3, 'bg_color blue') with testing.mkdtemp() as dirname: cmd.mpng(os.path.join(dirname, 'image'), width=shape2[0], height=shape2[1]) filenames = glob.glob(os.path.join(dirname, 'image*.png')) filenames.sort() self.assertEqual(4, len(filenames)) img = self.get_imagearray(filenames[0]) self.assertEqual(img.shape[:2], shape2) self.assertImageHasColor('red', img) img = self.get_imagearray(filenames[-1]) self.assertEqual(img.shape[:2], shape2) self.assertImageHasColor('blue', img)
def testLoadPWG(self): if sys.version_info[0] < 3: import urllib2 else: import urllib.request as urllib2 content_index = b'Hello World\n' port = 8084 baseurl = 'http://localhost:%d' % port def urlread(url): handle = urllib2.urlopen(url, timeout=3.0) try: return handle.read() finally: handle.close() with testing.mkdtemp() as rootdir: filename = os.path.join(rootdir, 'index.html') with open(filename, 'wb') as handle: handle.write(content_index) filename = os.path.join(rootdir, 'start.pwg') with open(filename, 'w') as handle: handle.write('root %s\n' % rootdir) handle.write('port %d\n' % port) cmd.load(filename) content = urlread(baseurl) self.assertEqual(content, content_index) # Warning: can't call locking functions here, will dead-lock # get_version is non-locking since 1.7.4 content = urlread(baseurl + '/apply/pymol.cmd.get_version') content = content.decode('ascii', errors='ignore') self.assertTrue(content, cmd.get_version()[0] in content)
def test_wchar(self): ''' Wide-char file names ''' pymolwin = self._get_pymolwin() with testing.mkdtemp() as base: dirname = u'Japanese \u65e5\u672c\u8a9e' dirname = os.path.join(base, dirname) os.mkdir(dirname) out_filename = base + '\\out.pdb' bat_filename = base + '\\in.bat' pml_filename = dirname + u'\\in.pml' in_filename = dirname + u'\\in.pdb' command = u'"%s" -kcq "%s" "%s"' % (pymolwin, in_filename, pml_filename) cmd.fragment('gly') cmd.save(in_filename) cmd.delete('*') with open(pml_filename, 'w') as handle: handle.write('save %s\n' % out_filename) with open(bat_filename, 'wb') as handle: handle.write(b'chcp 65001\r\n') # UTF-8 code page handle.write(b'set PYTHONIOENCODING=UTF-8\r\n') # avoid LookupError handle.write(command.encode('utf-8') + b'\r\n') handle.write(b'chcp 437\r\n') subprocess.call([bat_filename]) # wait for spawned process time.sleep(0.5) cmd.load(out_filename) self.assertEqual(7, cmd.count_atoms())
def test_mdo(self): self._load_example() cmd.mset('1x3') cmd.mdo(1, 'disable m1') cmd.mdo(2, 'enable m1') cmd.mdo(3, 'enable g1') with testing.mkdtemp() as tempdir: # export prefix = os.path.join(tempdir, 'frame') cmd.mpng(prefix, width=100, height=100) img = self.get_imagearray(prefix + '0001.png') self.assertImageHasNotColor('red', img) self.assertImageHasColor('blue', img) img = self.get_imagearray(prefix + '0002.png') self.assertImageHasNotColor('red', img) self.assertImageHasColor('blue', img) img = self.get_imagearray(prefix + '0003.png') self.assertImageHasColor('red', img) self.assertImageHasColor('blue', img)