def parse_and_create(self, source, **kwargs): """ Parses and compiles the source. The source should have a component defined with the name 'MainView'. Arguments --------- source : str The enaml source file kwargs : dict The default attribute values to pass to the component. """ enaml_ast = parse(source) enaml_module = types.ModuleType('__tests__') ns = enaml_module.__dict__ code = EnamlCompiler.compile(enaml_ast, '__enaml_tests__') toolkit = self.toolkit with toolkit: exec code in ns view = ns['MainView'] cmpnt = view(**kwargs) toolkit.app.initialize() self.app = toolkit.app.app_object() cmpnt.setup() return cmpnt
def parse_and_create(self, source, **kwargs): """ Parses and compiles the source. The source should have a component defined with the name 'MainView'. Arguments --------- source : str The enaml source file kwargs : dict The default attribute values to pass to the component. Returns ------- The component tree for the 'MainView' component. """ # Start the app instance first. app = MockApplication.instance() if app is None: app = MockApplication() self.app = app enaml_ast = parse(source) enaml_module = types.ModuleType('__tests__') ns = enaml_module.__dict__ code = EnamlCompiler.compile(enaml_ast, '__enaml_tests__') exec code in ns view = ns['MainView'] self.view = view(**kwargs) self.view.prepare() self.client_view = app.builder().root
def compile_source(source, item, filename='<test>'): """ Compile Enaml source code and return the target item. Parameters ---------- source : str The Enaml source code string to compile. item : str The name of the item in the resulting namespace to return. filename : str, optional The filename to use when compiling the code. The default is '<test>'. Returns ------- result : object The named object from the resulting namespace. """ ast = parse(source, filename) code = EnamlCompiler.compile(ast, filename) namespace = {} exec code in namespace return namespace[item]
def main(): usage = 'usage: %prog [options] enaml_file [script arguments]' parser = optparse.OptionParser(usage=usage, description=__doc__) parser.allow_interspersed_args = False parser.add_option('-c', '--component', default='Main', help='The component to view') parser.add_option( '-t', '--toolkit', default='default', help='The GUI toolkit to use [default: qt or ETS_TOOLKIT].') options, args = parser.parse_args() toolkit = prepare_toolkit(options.toolkit) if len(args) == 0: print 'No .enaml file specified' sys.exit() else: enaml_file = args[0] script_argv = args[1:] with open(enaml_file, 'rU') as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType('__main__') module.__file__ = enaml_file ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative # imports can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) # Bung in the command line arguments. sys.argv = [enaml_file] + script_argv with imports(): exec code in ns requested = options.component if requested in ns: component = ns[requested] descr = 'Enaml-run "%s" view' % requested show_simple_view(component(), toolkit, descr) elif 'main' in ns: ns['main']() else: msg = "Could not find component '%s'" % options.component print msg
def main(): usage = 'usage: %prog [options] enaml_file [script arguments]' parser = optparse.OptionParser(usage=usage, description=__doc__) parser.allow_interspersed_args = False parser.add_option('-c', '--component', default='Main', help='The component to view') options, args = parser.parse_args() if len(args) == 0: print('No .enaml file specified') sys.exit() else: enaml_file = args[0] script_argv = args[1:] enaml_code = read_source(enaml_file) # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType('__main__') module.__file__ = os.path.abspath(enaml_file) sys.modules['__main__'] = module ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative # imports can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) # Bung in the command line arguments. sys.argv = [enaml_file] + script_argv with imports(): exec(code, ns) # Make sure ^C keeps working signal.signal(signal.SIGINT, signal.SIG_DFL) requested = options.component if requested in ns: from enaml.qt.qt_application import QtApplication app = QtApplication() window = ns[requested]() window.show() window.send_to_front() app.start() elif 'main' in ns: ns['main']() else: msg = "Could not find component '%s'" % options.component print(msg)
def main(): usage = 'usage: %prog [options] enaml_file [script arguments]' parser = optparse.OptionParser(usage=usage, description=__doc__) parser.allow_interspersed_args = False parser.add_option( '-c', '--component', default='Main', help='The component to view' ) options, args = parser.parse_args() if len(args) == 0: print('No .enaml file specified') sys.exit() else: enaml_file = args[0] script_argv = args[1:] enaml_code = read_source(enaml_file) # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType('__main__') module.__file__ = os.path.abspath(enaml_file) sys.modules['__main__'] = module ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative # imports can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) # Bung in the command line arguments. sys.argv = [enaml_file] + script_argv with imports(): exec_(code, ns) # Make sure ^C keeps working signal.signal(signal.SIGINT, signal.SIG_DFL) requested = options.component if requested in ns: from enaml.qt.qt_application import QtApplication app = QtApplication() window = ns[requested]() window.show() window.send_to_front() app.start() elif 'main' in ns: ns['main']() else: msg = "Could not find component '%s'" % options.component print(msg)
def main(): usage = 'usage: %prog [options] enaml_file [script arguments]' parser = optparse.OptionParser(usage=usage, description=__doc__) parser.allow_interspersed_args = False parser.add_option('-c', '--component', default='Main', help='The component to view') parser.add_option('-t', '--toolkit', default='default', choices=['default', 'wx', 'qt'], help='The toolkit backend to use') options, args = parser.parse_args() # Preapare the toolkit toolkit = prepare_toolkit(options.toolkit) if len(args) == 0: print 'No .enaml file specified' sys.exit() else: enaml_file = args[0] script_argv = args[1:] with open(enaml_file) as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType('__main__') module.__file__ = enaml_file ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative imports # can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) # Bung in the command line arguments. sys.argv = [enaml_file] + script_argv with imports(): exec code in ns with toolkit: requested = options.component if requested in ns: component = ns[requested] window = component() window.show() elif 'main' in ns: ns['main']() else: msg = "Could not find component '%s'" % options.component print msg
def __init__(self, text, code=None, filename=None): self.text = text if code: self.code = code else: try: self.code = EnamlCompiler.compile(parse(text), filename) except SyntaxError as synerr: raise NotPython( u"Couldn't parse '%s' as Enaml source: '%s' at line %d" % ( filename, synerr.msg, synerr.lineno ) )
def main(): usage = "usage: %prog [options] enaml_file [script arguments]" parser = optparse.OptionParser(usage=usage, description=__doc__) parser.allow_interspersed_args = False parser.add_option("-c", "--component", default="Main", help="The component to view") options, args = parser.parse_args() if len(args) == 0: print("No .enaml file specified") sys.exit() else: enaml_file = args[0] script_argv = args[1:] with open(enaml_file, "rU") as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType("__main__") module.__file__ = os.path.abspath(enaml_file) sys.modules["__main__"] = module ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative # imports can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) # Bung in the command line arguments. sys.argv = [enaml_file] + script_argv with imports(): exec(code, ns) requested = options.component if requested in ns: from enaml.qt.qt_application import QtApplication app = QtApplication() window = ns[requested]() window.show() window.send_to_front() app.start() elif "main" in ns: ns["main"]() else: msg = "Could not find component '%s'" % options.component print(msg)
def read_component(enaml_file, requested='Main'): """ Read a component from an .enaml file. Parameters ---------- enaml_file : str The name of the .enaml file. requested : str, optional The name of the MainWindow holding the root Container. Returns ------- factory : callable The factory for the MainWindow, usually a MainWindow EnamlDefinition. module : module The module object from the .enaml file itself. Keep this alive at least until after the component gets constructed. Raises ------ NameError if the requested component does not exist in the module. """ with open(enaml_file) as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType('__main__') module.__file__ = enaml_file ns = module.__dict__ old_path = sys.path[:] sys.path.insert(0, os.path.dirname(enaml_file)) with imports(): exec code in ns sys.path[:] = old_path if requested in ns: factory = ns[requested] else: msg = "Could not find component {0!r}".format(requested) raise NameError(msg) return factory, module
def main(): usage = "usage: %prog [options] enaml_file [script arguments]" parser = optparse.OptionParser(usage=usage, description=__doc__) parser.allow_interspersed_args = False parser.add_option("-c", "--component", default="Main", help="The component to view") parser.add_option("-t", "--toolkit", default="default", help="The GUI toolkit to use [default: qt or ETS_TOOLKIT].") options, args = parser.parse_args() toolkit = prepare_toolkit(options.toolkit) if len(args) == 0: print "No .enaml file specified" sys.exit() else: enaml_file = args[0] script_argv = args[1:] with open(enaml_file) as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType("__main__") module.__file__ = enaml_file ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative # imports can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) # Bung in the command line arguments. sys.argv = [enaml_file] + script_argv with imports(): exec code in ns requested = options.component if requested in ns: component = ns[requested] descr = 'Enaml-run "%s" view' % requested show_simple_view(component(), toolkit, descr) elif "main" in ns: ns["main"]() else: msg = "Could not find component '%s'" % options.component print msg
def main(): usage = 'usage: %prog [options] enaml_file' parser = optparse.OptionParser(usage=usage, description=__doc__) parser.add_option('-c', '--component', default='Main', help='The component to view') parser.add_option('-t', '--toolkit', default='default', choices=['default', 'wx', 'qt'], help='The toolkit backend to use') options, args = parser.parse_args() if len(args) == 0: print 'No .enaml file specified' sys.exit() elif len(args) > 1: print 'Too many files specified' sys.exit() else: enaml_file = args[0] with open(enaml_file) as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning module = types.ModuleType('__main__') module.__file__ = enaml_file ns = module.__dict__ with imports(): exec code in ns with toolkits[options.toolkit](): requested = options.component if requested in ns: component = ns[requested] window = component() window.show() elif 'main' in ns: ns['main']() else: msg = "Could not find component '%s'" % options.component print msg
def parse_and_create(self, source, **kwargs): """ Parses and compiles the source. The source should have a component defined with the name 'MainView'. Arguments --------- source : str The enaml source file kwargs : dict The default attribute values to pass to the component. Returns ------- The component tree for the 'MainView' component. """ enaml_ast = parse(source) enaml_module = types.ModuleType('__tests__') ns = enaml_module.__dict__ code = EnamlCompiler.compile(enaml_ast, '__enaml_tests__') exec code in ns View = ns['MainView'] # Start the app instance first. session_name = get_unique_session_identifier() view_factory = simple_session(session_name, 'test', View) self.app = TestingQtApplication.instance() if self.app is None: self.app = TestingQtApplication([]) self.app.add_factories([view_factory]) session_id = self.app.start_session(session_name) self.app.start() session = self.app._sessions[session_id] # retrieve the enaml server side root widget self.view = session.windows[0] # retrieve the enaml client side root widget self.client_view = self.app._qt_sessions[session_id]._windows[0]
def parse_and_create(self, source, **kwargs): """ Parses and compiles the source. The source should have a component defined with the name 'MainView'. Arguments --------- source : str The enaml source file kwargs : dict The default attribute values to pass to the component. Returns ------- A tuple of the server and client component trees for the 'MainView' component. """ # This replicates what enaml.runner.main does enaml_ast = parse(source, filename='__enaml_tests__') code = EnamlCompiler.compile(enaml_ast, '__enaml_tests__') enaml_module = types.ModuleType('__tests__') ns = enaml_module.__dict__ with traits_enaml.imports(): six.exec_(code, ns, ns) View = ns['MainView'] enaml_view = View(**kwargs) enaml_view.initialize() if not enaml_view.proxy_is_active: enaml_view.activate_proxy() toolkit_view = enaml_view.proxy # We need to keep a reference to the enaml_module to ensure it does not # get collected. If no reference is kept, enaml operators will not be # able to resolve objects properly (eg. validator = IntValidator(...) # will fail) self.enaml_module = enaml_module return enaml_view, toolkit_view
def refresh_view(self): """ Refresh the compiled view object. This method will (re)compile the view for the given view text and update the 'compiled_view' attribute. If a compiled model is available and the view has a member named 'model', the model will be applied to the view. """ text = self.view_text filename = self.view_filename _fake_linecache(text, filename) try: if not text: self.compiled_view = None self._view_module = None else: ast = parse(text, filename=filename) code = EnamlCompiler.compile(ast, filename) module = ModuleType('__main__') module.__file__ = filename namespace = module.__dict__ with enaml.imports(): exec_(code, namespace) view = namespace.get(self.view_item, lambda: None)() if isinstance(view, Object) and 'model' in view.members(): view.model = self.compiled_model # trap any initialization errors and roll back the view old = self.compiled_view try: self.compiled_view = view except Exception: self.compiled_view = old if isinstance(old, Widget): old.show() raise self._view_module = module if old is not None and not old.is_destroyed: old.destroy() except Exception: self.traceback = traceback.format_exc() else: self.traceback = ''
def test_examples(enaml_qtbot, enaml_sleep, path, handler): """ Test the enaml examples. """ dir_path = os.path.abspath(os.path.split(os.path.dirname(__file__))[0]) enaml_file = os.path.join(dir_path, 'examples', os.path.normpath(path)) with open(enaml_file, 'r') as f: enaml_code = f.read() # Parse and compile the Enaml source into a code object ast = parse(enaml_code, filename=enaml_file) code = EnamlCompiler.compile(ast, enaml_file) # Create a proper module in which to execute the compiled code so # that exceptions get reported with better meaning try: module = types.ModuleType('enaml_test') module.__file__ = os.path.abspath(enaml_file) sys.modules['enaml_test'] = module ns = module.__dict__ # Put the directory of the Enaml file first in the path so relative # imports can work. sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file))) with imports(): exec(code, ns) window = ns['Main']() window.show() window.send_to_front() wait_for_window_displayed(enaml_qtbot, window) enaml_qtbot.wait(enaml_sleep*1000) if handler is not None: handler(enaml_qtbot, window) finally: # Make sure we clean up the sys modification before leaving sys.path.pop(0) del sys.modules['enaml_test']
def generate_cache(path): #: Read with open(path, 'rU') as f: enaml_code = f.read() #: Compile ast = parse(enaml_code, filename=path) code = EnamlCompiler.compile(ast, path) #: Generate cache with open('tmp.enamlc', 'wb') as f: f.write(MAGIC) f.write(struct.pack('i', int(os.path.getmtime(path)))) marshal.dump(code, f) with open('tmp.enamlc', 'rb') as f: data = f.read() #: Cleanup if os.path.exists('tmp.enamlc'): os.remove('tmp.enamlc') return data