def main(): """Validates config file""" config = ConfigParser() config.read(CONFIG_PATH) # Type validate each entry not in the keymap for section in filter( lambda s: s != 'BIT2KEY_MAP', config.sections() ): for option in filter( lambda o: not o.startswith('type_'), config.options(section) ): if "GPIO_attr" in config.get(section, 'type_' + option): assert hasattr(GPIO, config.get(section, option)), ( "Config validation failed. GPIO does not have " "attr {}.".format(GPIO, config.get(section, option)) ) assert str(type(safe_eval(config.get(section, option)))) == \ "<type '{}'>".format(config.get(section, 'type_' + option)),\ ("Config validation failed. {} expected type was <type '{}'>" ", got {} instead.".format( option, config.get(section, 'type_' + option), str(type(safe_eval( config.get(section, option)))))) # Validate keymap length actual = sorted([int(option) for option in config.options('BIT2KEY_MAP')]) expected = range(int(config.get( 'RASPBERRY_CEREAL', 'bus_width')) * int(config.get( 'RASPBERRY_CEREAL', 'shift_registers'))) assert actual == expected, \ ("KEY2BIT_MAP does not have items matching specification from" " bus_width and shift_registers.\nactual: {}\nexpected: {}".format( actual, expected)) # Ensure keys in keymap actually exist for key in [ config.get('BIT2KEY_MAP', key) for key in config.options('BIT2KEY_MAP')]: assert hasattr(uinput, key), ("The key, {}, is not valid." .format(key))
def _parse_args(func, args, usage=None): """Parse the arguments given to a magic function""" if isinstance(args, list): args = ' '.join(args) args = _split_args(args) kwargs = dict() if getattr(func, 'has_options', False): parser = MagicOptionParser(usage=usage, conflict_handler="resolve") parser.add_options(func.options) left = [] value = None if '--' in args: left = args[:args.index('--')] value, args = parser.parse_args(args[args.index('--') + 1:]) else: while args: try: value, args = parser.parse_args(args) except Exception: left.append(args.pop(0)) else: break args = left + args if value: kwargs = value.__dict__ new_args = [] for arg in args: try: new_args.append(safe_eval(arg)) except: new_args.append(arg) for (key, value) in kwargs.items(): try: kwargs[key] = safe_eval(value) except: pass return new_args, kwargs
def _split_args(args): try: # do not use posix mode, to avoid eating quote characters args = shlex.split(args, posix=False) except: # parse error; let's pass args along rather than crashing args = args.split() new_args = [] temp = '' for arg in args: if arg.startswith('-'): new_args.append(arg) elif temp: arg = temp + ' ' + arg try: safe_eval(arg) except: temp = arg else: new_args.append(arg) temp = '' elif arg.startswith(('(', '[', '{')) or '(' in arg: try: safe_eval(arg) except: temp = arg else: new_args.append(arg) else: new_args.append(arg) if temp: new_args.append(temp) return new_args
def main(): """Polls shift register for serial data and emit_clicks HIGHs""" if os.geteuid() != 0: exit("Must be run as root!") print "[WAIT] Setting up..." # argparse parser = argparse.ArgumentParser() for arg in ARGS.keys(): parser.add_argument(*ARGS[arg][0], dest=arg, action='store_true', help=ARGS[arg][1]) args = parser.parse_args() # Validate config validate_config() # Read config file config = ConfigParser() config.read(CONFIG_PATH) # active_low = int(safe_eval(config.get('RASPBERRY_CEREAL', 'active_low'))) # Create device events = [] for key in [config.get('BIT2KEY_MAP', num) for num in config.options('BIT2KEY_MAP') if config.get('BIT2KEY_MAP', num) != "NONE"]: events.append(getattr(uinput, key.upper())) device = uinput.Device(events) print "[OK] Configuration options type-validated." # Setup GPIO sr_config = gpio_setup() # Set poll time poll_time = float(config.get('RASPBERRY_CEREAL', 'poll_time')) print ("[OK] If you opened {0} with {1}, you may safely hit Ctrl-C and" " {0} will continue to run in the background. Remember to kill" " the job when you are done. Polling every {2} ms.".format( "raspberry-cereal", "'sudo raspberry-cereal &'", int(poll_time * 1000))) # PERFORMANCE: Moved main_loop so that there are no longer # two loops. try: main_loop(sr_config, active_low, poll_time, device, config, args) except KeyboardInterrupt: exit("[OK] raspberry-cereal bids you adieu.")
def _get_search_engines(self): try: lang = MyConfig.get("document_retrieval", "lang") engines = safe_eval(MyConfig.get("document_retrieval", "engines")) throttle = MyConfig.get("document_retrieval", "throttle") l = [] for (engine, license) in engines: # Eval to something like this: # Google(google_license, throttle, lang) l.append(eval(engine + "(\"" + license + "\", " + throttle + ", " + lang + ")")) return l except MyConfigException: sys.exit("_get_search_engines: config error") except: logger = logging.getLogger("qa_logger") logger.exception("_get_search_engines: fatal error") sys.exit(2)
def _get_search_engines(self): try: lang = MyConfig.get("document_retrieval", "lang") engines = safe_eval(MyConfig.get("document_retrieval", "engines")) throttle = MyConfig.get("document_retrieval", "throttle") l = [] for (engine, license) in engines: # Eval to something like this: # Google(google_license, throttle, lang) l.append( eval(engine + "(\"" + license + "\", " + throttle + ", " + lang + ")")) return l except MyConfigException: sys.exit("_get_search_engines: config error") except: logger = logging.getLogger("qa_logger") logger.exception("_get_search_engines: fatal error") sys.exit(2)
def fix_code_style_list(key, data, errors, context): """Grant's code style fix converter""" from ast import literal_eval as safe_eval # As per method signature this is for editing data, not returning a value raw = data.get(key) try: py_list = safe_eval(raw) except ValueError as e: # only warnings seem to make it through the CKAN logging (and appear # in /var/log/apache2/ckan_default.error.log) log.info( "Unable to clean value {} - already a clean string? {}".format( raw, e)) return None except Exception as e: log.info(u"Unable to clean value {} - {}".format(raw, e)) return None else: if isinstance(py_list, list): log.info( u"Converted code-style list '{}' into text format".format(raw)) data[key] = u' & '.join(py_list)
def gpio_setup(): """Performs GPIO setup returns: Shift register config """ sr_config = {} config = ConfigParser() config.read(CONFIG_PATH) sr_config['ploadpin'] = int(config.get('74HC165N', 'ploadpin')) sr_config['datapin'] = int(config.get('74HC165N', 'datapin')) sr_config['clockpin'] = int(config.get('74HC165N', 'clockpin')) sr_config['triggerpulsewidth'] = float( config.get('74HC165N', 'triggerpulsewidth') ) sr_config['bus_width'] = int(config.get('RASPBERRY_CEREAL', 'bus_width')) sr_config['shift_registers'] = int(config.get( 'RASPBERRY_CEREAL', 'shift_registers' ) ) # PERFORMANCE: Perform this operation here so it only needs to be # performed once sr_config['full_width'] = sr_config[ 'bus_width'] * sr_config['shift_registers'] GPIO.setmode(getattr(GPIO, config.get('GPIO', 'setmode'))) GPIO.setwarnings(safe_eval(config.get('GPIO', 'setwarnings'))) GPIO.setup(sr_config['ploadpin'], GPIO.OUT) GPIO.setup(sr_config['datapin'], GPIO.IN) GPIO.setup(sr_config['clockpin'], GPIO.OUT) GPIO.output(sr_config['clockpin'], 0) GPIO.output(sr_config['ploadpin'], 1) return sr_config
def server_version(self): detected = tuple( int(x) for x in self._server_version.relVersion.replace( 'rods', '').split('.')) return (safe_eval(os.environ.get('IRODS_SERVER_VERSION', '()')) or detected)
def parse_strelka(self, fname): ''' extracts all the information about each mutations from line ''' mode = self.__parse_header_strelka(fname) if self.is_gzip(fname): freader = gzip.open(fname, 'rb') else: freader = open(fname) for line in freader: if line[0] == '#': continue line = line.strip().split('\t') line[7] = line[7].split(';') line[8] = Utils.build_indices(line[8].split(':')) #normal line[9] = [safe_eval(val) for val in line[9].split(':')] #tumour line[10] = [safe_eval(val) for val in line[10].split(':')] if mode == 'strelka_indel': #TR = (DP+DP2)-sum(TIR) tum_alt = line[10][line[8]['TIR']][0] tum_ref = line[10][line[8]['DP']] - tum_alt norm_alt = line[9][line[8]['TIR']][0] norm_ref = line[9][line[8]['DP']] - norm_alt else: tum_ref = line[10][line[8][line[3] + 'U']][0] norm_ref = line[9][line[8][line[3] + 'U']][0] if line[4] == '.': tum_alt = 'N/A' norm_alt = 'N/A' else: tum_alt = max([ line[10][line[8][val + 'U']][0] for val in line[4].split(',') ]) norm_alt = max([ line[9][line[8][val + 'U']][0] for val in line[4].split(',') ]) subpat, _ = Utils.get_sub_pattern(line[3], line[4]) #get snpeff annotations snpeff = self.parse_snpeff(line[7]) snpeff = self.sort_snpeff(snpeff) #only keep the highest ranked annotation when removing duplicates if self.rmdups: snpeff = [snpeff[0]] anns = self.get_annotations(line[7]) for snpval in snpeff: outval = [ self.case_id, self.normal_id, self.tumour_id, line[0], line[1], line[1], snpval[1], snpval[2], snpval[0], line[3], line[4], tum_ref, tum_alt, norm_ref, norm_alt, anns[0], anns[1], anns[2], mode, snpval[4], snpval[5], snpval[6], self.project, snpval[7], snpval[8], line[6], snpval[3], subpat ] + [None] * 5 + self.lab_vals outval = [val if val else 'N/A' for val in outval] yield outval freader.close()