class TestPackageResource(Common, unittest.TestCase): def setUp(self): super(TestPackageResource, self).setUp() sys.path.append(self.base) self.res = PackageResource(self.mod) def tearDown(self): del self.res del sys.modules[self.mod] sys.path.remove(self.base) super(TestPackageResource, self).tearDown() def test_filename(self): with self.res.filename('a.yml') as filename: with open(filename, encoding='utf-8') as f: self.assertEqual( load(f.read())['path'], 'a.yml') self.assertTrue(os.path.exists(filename)) with self.res.get(['subdir', 'b.yml']).filename() as filename: with open(filename, encoding='utf-8') as f: self.assertEqual( load(f.read())['path'], 'subdir/b.yml') self.assertTrue(os.path.exists(filename))
def __init__(self, panel): """Add toolbar tool to panel and subscribe to capture events.""" self.hook = HookCollection( start='madgui.component.matching.start') self.cid = None self.segment = panel.view.segment self.panel = panel self.view = panel.view self.matcher = None # toolbar tool res = PackageResource('madgui.data') with res.open('cursor.xpm') as xpm: img = wx.ImageFromStream(xpm, wx.BITMAP_TYPE_XPM) bmp = wx.BitmapFromImage(img) self.toolbar = panel.toolbar self.tool = panel.toolbar.AddCheckTool( wx.ID_ANY, bitmap=bmp, shortHelp='Beam matching', longHelp='Match by specifying constraints for envelope x(s), y(s).') panel.Bind(wx.EVT_TOOL, self.OnMatchClick, self.tool) # setup mouse capture panel.hook.capture_mouse.connect(self.stop_match)
def setUp(self): super(TestEggResource, self).setUp() cwd = os.getcwd() os.chdir(self.base) with captured_output('stdout'): with captured_output('stderr'): setuptools.setup( name=self.mod, packages=[self.mod], script_args=['bdist_egg', '--quiet'], package_data={self.mod:[ 'a.yml', os.path.join('subdir', 'b.yml')]}) os.chdir(cwd) self.eggs = os.listdir(os.path.join(self.base, 'dist')) for egg in self.eggs: sys.path.append(os.path.join(self.base, 'dist', egg)) self.res = PackageResource(self.mod)
class TestEggResource(Common, unittest.TestCase): def setUp(self): super(TestEggResource, self).setUp() cwd = os.getcwd() os.chdir(self.base) with captured_output('stdout'): with captured_output('stderr'): setuptools.setup( name=self.mod, packages=[self.mod], script_args=['bdist_egg', '--quiet'], package_data={self.mod:[ 'a.yml', os.path.join('subdir', 'b.yml')]}) os.chdir(cwd) self.eggs = os.listdir(os.path.join(self.base, 'dist')) for egg in self.eggs: sys.path.append(os.path.join(self.base, 'dist', egg)) self.res = PackageResource(self.mod) def tearDown(self): del self.res del sys.modules[self.mod] for egg in self.eggs: sys.path.remove(os.path.join(self.base, 'dist', egg)) super(TestEggResource, self).tearDown() def test_is_valid(self): """ Check that the test uses a .egg resource. Only then the test results confirm that the package is working. """ self.assertTrue(self.res._is_extracted) def test_filename(self): with self.res.filename('a.yml') as filename: with open(filename, encoding='utf-8') as f: self.assertEqual( load(f.read())['path'], 'a.yml') self.assertFalse(os.path.exists(filename)) with self.res.get(['subdir', 'b.yml']).filename() as filename: with open(filename, encoding='utf-8') as f: self.assertEqual( load(f.read())['path'], 'subdir/b.yml') self.assertFalse(os.path.exists(filename)) # The resource should be accessible again, even though the file was # deleted when exiting the context manager: with self.res.get(['subdir', 'b.yml']).filename() as filename: with open(filename, encoding='utf-8') as f: self.assertEqual( load(f.read())['path'], 'subdir/b.yml') self.assertFalse(os.path.exists(filename)) def test_use_filename_twice(self): with self.res.filename('a.yml') as filename: self.assertTrue(os.path.exists(filename)) self.assertFalse(os.path.exists(filename)) with self.res.filename('a.yml') as filename: self.assertTrue(os.path.exists(filename)) with open(filename, encoding='utf-8') as f: self.assertEqual( load(f.read())['path'], 'a.yml')
def setUp(self): super(TestPackageResource, self).setUp() sys.path.append(self.base) self.res = PackageResource(self.mod)