def begin(self): dlg = ArbyFitDialog(self.plot) ret = dlg.exec_() if ret != QDialog.Accepted: return fcnstr, params, values, xmin, xmax = dlg.getFunction() ns = {} exec_('from numpy import *', ns) try: model = eval('lambda x, %s: %s' % (', '.join(params), fcnstr), ns) except SyntaxError as e: self.plot.showInfo('Syntax error in function: %s' % e) return f = Fit('fit', model, params, values, xmin, xmax) res = f.run(*self.data) if res._failed: self.plot.showInfo('Fitting failed: %s.' % res._message) return res.label_x = res.curve_x[0] res.label_y = max(res.curve_y) res.label_contents = list(zip(*res._pars)) self.plot._plotFit(res)
def processGuiConfig(configcode): ns = prepareGuiNamespace() exec_(configcode, ns) gui_conf = gui_config(ns['main_window'], ns.get('windows', []), ns.get('tools', []), ns.get('name', 'NICOS'), ns.get('options', {})) if gui_conf.name != 'NICOS': SettingGroup.global_group = gui_conf.name return gui_conf
def _RunScript(filename, statdevices, debug=False): fn = _scriptfilename(filename) if not path.isfile(fn) and os.access(fn, os.R_OK): raise UsageError('The file %r does not exist or is not readable' % fn) if session.mode == SIMULATION: starttime = session.clock.time for dev in statdevices: if not isinstance(dev, Readable): session.log.warning('unable to collect statistics on %r', dev) continue dev._sim_min = None dev._sim_max = None session.log.info('running user script: %s', fn) try: fp = io.open(fn, 'r', encoding='utf-8') except Exception as e: if session.mode == SIMULATION: session.log.exception('Dry run: error opening script') return raise NicosError('cannot open script %r: %s' % (filename, e)) with fp: code = fp.read() # guard against bare excepts code = fixupScript(code) # quick guard against self-recursion if session.experiment and session.experiment.scripts and \ code.strip() == session.experiment.scripts[-1].strip(): raise NicosError('script %r would call itself, aborting' % filename) def compiler(src): return compile(src + '\n', fn, 'exec', CO_DIVISION) compiled = session.scriptHandler(code, fn, compiler) with _ScriptScope(path.basename(fn), code): try: exec_(compiled, session.namespace) except Exception: if debug: traceback.print_exc() raise session.log.info('finished user script: %s', fn) if session.mode == SIMULATION: session.log.info('simulated minimum runtime: %s', formatDuration(session.clock.time - starttime, precise=False)) for dev in statdevices: if not isinstance(dev, Readable): continue session.log.info('%s: min %s, max %s, last %s %s', dev.name, dev.format(dev._sim_min), dev.format(dev._sim_max), dev.format(dev._sim_value), dev.unit)
def exec_script(self, code, user, handler): # execute code in the script namespace (this is called not from # the script thread, but from a handle thread) self.thread_data.user = user temp_request = ScriptRequest(code, None, user) temp_request.parse() session.log.log(INPUT, formatScript(temp_request, '---')) self.last_handler = weakref.ref(handler) try: for block in temp_request.code: exec_(block, self.namespace) finally: self.last_handler = None
def _RunCode(code, debug=False): if session.mode == SIMULATION: starttime = session.clock.time code = fixupScript(code) try: exec_(code, session.namespace) except Exception: if debug: traceback.print_exc() raise if session.mode == SIMULATION: session.log.info('simulated minimum runtime: %s', formatDuration(session.clock.time - starttime, precise=False))
def parse_sampleconf(filename): builtin_ns = vars(builtins).copy() for name in ('__import__', 'open', 'exec', 'execfile'): builtin_ns.pop(name, None) mocksample = MockSample() ns = {'__builtins__': builtin_ns, 'ClearSamples': mocksample.reset, 'SetSample': mocksample.define} with open(filename, 'r') as fp: exec_(fp, ns) # The script needs to call this, if it doesn't it is not a sample file. if not mocksample.reset_called: raise ValueError('the script never calls ClearSamples()') return mocksample.configs
def my_runcode(self, codeobj, source=None): """Mostly copied from code.InteractiveInterpreter, but added better exception handling. """ session.scriptEvent('start', ('', source)) try: exec_(codeobj, self.globals) except NicosInteractiveStop: pass except KeyboardInterrupt: # "immediate stop" chosen session.immediateStop() except Exception: session.scriptEvent('exception', sys.exc_info()) if hasattr(code, 'softspace') and code.softspace(sys.stdout, 0): print() session.scriptEvent('finish', None)
def run(cls, setup, code, mode=SLAVE, appname='script'): session.__class__ = cls try: session.__init__(appname) except Exception as err: try: session.log.exception('Fatal error while initializing') finally: print('Fatal error while initializing:', err, file=sys.stderr) return 1 # Load the initial setup and handle becoming master. session.handleInitialSetup(setup, mode) # Execute the script code and shut down. exec_(code, session.namespace) session.shutdown()
def configdata(name): from nicos.core.errors import ConfigurationError try: setupname, element = name.split('.') except ValueError: raise ConfigurationError('configdata() argument must be in the ' 'form \'module.valuename\'') if setupname not in all_setups: raise ConfigurationError('config setup "%s" not found' % setupname) else: fullname = all_setups[setupname] ns = {} with open(fullname) as fp: exec_(fp.read(), ns) dep_files.add(fullname) try: return ns[element] except KeyError: raise ConfigurationError('value named %s not found in config ' 'setup "%s"' % (element, setupname))
def _update(self): try: configcode = self.client.eval( '__import__("nicos_mlz").kws1._get_instr_config()') config = {'__builtins__': None} exec_(configcode, config) except Exception: self.showError('Could not determine current config.') self.frame.setDisabled(True) return includes = set(config['includes']) for (part, widgets) in zip(self._parts, self._widgets): if 'virtual_' + part in includes: widgets[3].setChecked(True) includes.discard('virtual_' + part) else: widgets[2].setChecked(True) includes.discard(part) self.additionalBox.setPlainText('\n'.join(includes)) self._modules = config.get('modules', []) self._description = config.get('description', 'instrument setup')
def runCommand(self): command = self.getCommand() self.addToHistory(command) tmp_stdout = sys.stdout sys.stdout = self.stdout try: command = self.getConstruct(command) if not command: return exec_(command, self.namespace) except SystemExit: self.closeConsole.emit() except: # pylint: disable=W0702 traceback_lines = traceback.format_exc().split('\n') # Remove traceback mentioning this file, and a linebreak for i in (2, 1, -1): traceback_lines.pop(i) self.appendPlainText('\n'.join(traceback_lines)) finally: sys.stdout = tmp_stdout self.newPrompt()
def breakpoint(self, level): exec_(self._bpcode[level])
def check(self): # check syntax try: with open(self.filename) as fp: self.code = fp.read() exec_(self.code, self.ns) self.ast = ast.parse(self.code) except SyntaxError as e: msg = 'SyntaxError:\t%s' % e.msg msg += '\n|line: %s : %s ' % (e.lineno, e.text.strip() if e.text else '') self.log_error(msg, extra={'line': e.lineno}) return self.good except Exception as e: self.log_exception(e) return self.good self.log.info('syntax ok') self.setup_info[self.setupname] = self.ns if self.is_guiconfig: return self.check_guiconfig() # check for valid group group = self.ns.get('group', 'optional') if group not in SETUP_GROUPS: self.log_error('invalid setup group %r', group, extra=self.find_global('group')) # check for a description description = self.ns.get('description', None) if description in (None, ''): self.log_error('missing user-friendly setup description', extra=self.find_global('description')) self.ns['devices'] = fixup_stacked_devices(self, self.ns.get('devices', {})) # check if devices are duplicated if group != 'special': devs = self.ns.get('devices', {}) for devname in devs: if devname not in self.devs_seen: self.devs_seen[devname] = self.setupname continue # we have a duplicate: it's okay if we exclude the other setup # or if we are both basic setups other = self.devs_seen[devname] self_group = self.ns.get('group', 'optional') other_group = self.setup_info[other].get('group', 'optional') if self_group == 'basic' and other_group == 'basic': continue if other in self.ns.get('excludes', []) or \ self.setupname in self.setup_info[other].get('excludes', []): continue # it's also ok if it is a sample, experiment, or instrument # device if devname in ['Sample', 'Exp'] or \ 'instrument' in devs[devname][1]: continue self.log.warning('device name %s duplicate: also in %s', devname, self.devs_seen[devname], extra=self.find_deventry(devname)) # check for common misspelling of "includes" if 'include' in self.ns: self.log_error("'include' list should be called 'includes'", extra=self.find_global('include')) # check for common misspelling of "excludes" if 'exclude' in self.ns: self.log_error("'exclude' list should be called 'excludes'", extra=self.find_global('exclude')) if os.path.basename(self.filename) == 'startup.py': if self.ns.get('includes', []): self.log_error("The 'includes' in 'startup.py' must be empty!", extra=self.find_global('includes')) # check for types of recognized variables for (vname, vtype) in [ ('description', string_types), # group is already checked against a fixed list ('sysconfig', dict), ('includes', list), ('excludes', list), ('modules', list), ('devices', dict), ('alias_config', dict), ('startupcode', str), ('extended', dict) ]: if vname in self.ns and not isinstance(self.ns[vname], vtype): self.log_error('%r must be of type %s (but is %s)' % (vname, vtype, type(self.ns[vname])), extra=self.find_global(vname)) # check for importability of modules for module in self.ns.get('modules', []): # try to import the device class try: importString(module) except Exception as err: self.log_error('module %r not importable: %s', module, err, extra=self.find_global('modules')) # check for validity of alias_config aliascfg = self.ns.get('alias_config', {}) if isinstance(aliascfg, dict): # else we complained above already for aliasname, entrydict in aliascfg.items(): if not (isinstance(aliasname, string_types) and isinstance(entrydict, dict)): self.log_error( 'alias_config entries should map alias ' 'device names to a dictionary', extra=self.find_global('alias_config')) continue for target, prio in entrydict.items(): if not (isinstance(target, string_types) and isinstance(prio, integer_types) and not (isinstance(prio, bool))): self.log_error( 'alias_config entries should map device ' 'names to integer priorities', extra=self.find_global('alias_config')) break if target not in self.ns.get('devices', {}): basedev = target.partition('.')[0] if basedev not in self.ns.get('devices'): self.log_error( 'alias_config device target should ' 'be a device from the current setup', extra=self.find_global('alias_config')) break # check for validity of display_order display_order = self.ns.get('display_order', 50) if not isinstance(display_order, integer_types) or \ not 0 <= display_order <= 100: self.log_error( 'display_order should be an integer between ' '0 and 100', extra=self.find_global('display_order')) # check for validity of extended representative representative = self.ns.get('extended', {}).get('representative') if representative is not None: if representative not in self.ns.get('devices', {}): self.log_error( 'extended["representative"] should be a device ' 'defined in the current setup', extra=self.find_global('extended')) # check for valid device classes (if importable) and parameters for devname, devconfig in self.ns.get('devices', {}).items(): self.check_device(devname, devconfig, group in ('special', 'configdata')) # return overall "ok" flag return self.good
def run_script_session(session, setup, code): session.handleInitialSetup(setup) try: exec_(code, session.namespace) finally: session.shutdown()
def runsource(self, source, filename='<input>', symbol='single'): code = self.commandHandler(source, lambda src: compile(src, filename, symbol)) if code is None: return exec_(code, self.namespace)
def run(cls, sock, uuid, setups, user, code, quiet=False, debug=False): session.__class__ = cls session._is_sandboxed = sock.startswith('ipc://') session._debug_log = debug socket = nicos_zmq_ctx.socket(zmq.DEALER) socket.connect(sock) # we either get an empty message (retrieve cache data ourselves) # or a pickled key-value database data = socket.recv() db = pickle.loads(data) if data else None # send log messages back to daemon if requested session.log_sender = SimLogSender(socket, session, uuid, quiet) username, level = user.rsplit(',', 1) session._user = User(username, int(level)) try: session.__init__(SIMULATION) except Exception as err: try: session.log.exception('Fatal error while initializing') finally: print('Fatal error while initializing:', err, file=sys.stderr) return 1 # Give a sign of life and then tell the log handler to only log # errors during setup. session.log.info('setting up dry run...') session.begin_setup() # Handle "print" statements in the script. sys.stdout = LoggingStdout(sys.stdout) try: # Initialize the session in simulation mode. session._mode = SIMULATION # Load the setups from the original system, this should give the # information about the cache address. session.log.info('loading simulation mode setups: %s', ', '.join(setups)) session.loadSetup(setups, allow_startupcode=False) # Synchronize setups and cache values. session.log.info('synchronizing to master session') session.simulationSync(db) # Set session to always abort on errors. session.experiment.errorbehavior = 'abort' except: # really *all* exceptions -- pylint: disable=W0702 session.log.exception('Exception in dry run setup') session.log_sender.finish() session.shutdown() return 1 # Set up log handlers to output everything. session.log_sender.begin_exec() # Execute the script code. exception = False try: last_clock = session.clock.time code, _ = parseScript(code) for i, c in enumerate(code): exec_(c, session.namespace) time = session.clock.time - last_clock last_clock = session.clock.time session.log_sender.send_block_result(i, time) except Abort: session.log.info('Dry run finished by abort()') except: # pylint: disable=W0702 session.log.exception('Exception in dry run') exception = True else: session.log.info('Dry run finished') finally: session.log_sender.finish(exception) # Shut down. session.shutdown()
def readSetup(infodict, modname, filepath, all_setups, logger): try: with open(filepath, 'rb') as modfile: code = modfile.read() except IOError as err: logger.exception('Could not read setup ' 'module %r: %s', filepath, err) return ns = prepareNamespace(modname, filepath, all_setups) try: exec_(code, ns) except Exception as err: logger.exception('An error occurred while processing ' 'setup %r: %s', filepath, err) return devices = fixup_stacked_devices(logger, ns.get('devices', {})) for devname in devices: if not nicosdev_re.match(devname): logger.exception('While processing setup %r: device name %r is ' 'invalid, names must be Python identifiers', filepath, devname) return info = { 'description': ns.get('description', modname), 'group': ns.get('group', 'optional'), 'sysconfig': ns.get('sysconfig', {}), 'includes': ns.get('includes', []), 'excludes': ns.get('excludes', []), 'modules': ns.get('modules', []), 'devices': devices, 'alias_config': ns.get('alias_config', {}), 'startupcode': ns.get('startupcode', ''), 'display_order': ns.get('display_order', 50), 'extended': ns.get('extended', {}), 'filenames': [filepath] + list(ns.get('_dep_files', ())), 'monitor_blocks': ns.get('monitor_blocks', {}), 'watch_conditions': ns.get('watch_conditions', []), } if info['group'] not in SETUP_GROUPS: logger.warning('Setup %s has an invalid group (valid groups ' 'are: %s)', modname, ', '.join(SETUP_GROUPS)) info['group'] = 'optional' if modname in infodict: # setup already exists; override/extend with new values oldinfo = infodict[modname] or {} oldinfo['description'] = ns.get('description', oldinfo['description']) oldinfo['group'] = ns.get('group', oldinfo['group']) oldinfo['sysconfig'].update(info['sysconfig']) oldinfo['includes'].extend(info['includes']) oldinfo['excludes'].extend(info['excludes']) oldinfo['modules'].extend(info['modules']) oldinfo['devices'].update(info['devices']) # remove devices overridden by "None" entries completely for devname, value in listitems(oldinfo['devices']): if value is None: del oldinfo['devices'][devname] oldinfo['startupcode'] += '\n' + info['startupcode'] oldinfo['alias_config'].update(info['alias_config']) oldinfo['display_order'] = ns.get('display_order', oldinfo['display_order']) oldinfo['extended'].update(info['extended']) oldinfo['filenames'].extend(info['filenames']) oldinfo['monitor_blocks'].update(info['monitor_blocks']) oldinfo['watch_conditions'].extend(info['watch_conditions']) logger.debug('%r setup partially merged with version ' 'from parent directory', modname) else: infodict[modname] = info