def install_make(): click.secho("We need to install make ...\n", fg="red") if platform.system() != "Windows": call_cmd("sudo -k -H apt install make") else: click.fail("As a windows user, you must install `make` manually. " "See: http://bfy.tw/NI8r")
def cli(ctx): """ Interface with a YubiKey via the command line. """ subcmd = next(c for c in COMMANDS if c.name == ctx.invoked_subcommand) transports = getattr(subcmd, 'transports', TRANSPORT.usb_transports()) if transports: try: descriptors = list(get_descriptors()) except usb.core.NoBackendError: click.fail('No PyUSB backend detected!') n_keys = len(descriptors) if n_keys == 0: ctx.fail('No YubiKey detected!') if n_keys > 1: ctx.fail('Multiple YubiKeys detected. Only a single YubiKey at a ' 'time is supported.') descriptor = descriptors[0] if descriptor.mode.transports & transports: try: ctx.obj['dev'] = descriptor.open_device(transports) if not ctx.obj['dev']: # Key should be there, busy? raise FailedOpeningDeviceException() except FailedOpeningDeviceException: ctx.fail('Failed connecting to the YubiKey. Is it in use by ' 'another process?') else: req = ', '.join((t.name for t in TRANSPORT if t & transports)) click.echo("Command '{}' requires one of the following transports " "to be enabled: '{}'.".format(subcmd.name, req)) ctx.fail("Use 'ykman mode' to set the enabled connections.")
def create_api_key(): """ Interactively create a new API key via Zotero's OAuth API. Requires the user to enter a verification key displayed in the browser. :returns: API key and the user's library ID """ auth = OAuth1Service( name='zotero', consumer_key=CLIENT_KEY, consumer_secret=CLIENT_SECRET, request_token_url=REQUEST_TOKEN_URL, access_token_url=ACCESS_TOKEN_URL, authorize_url=AUTH_URL, base_url=BASE_URL) token, secret = auth.get_request_token( params={'oauth_callback': 'oob'}) auth_url = auth.get_authorize_url(token) auth_url += '&' + urlencode({ 'name': 'zotero-cli', 'library_access': 1, 'notes_access': 1, 'write_access': 1, 'all_groups': 'read'}) click.echo("Opening {} in browser, please confirm.".format(auth_url)) click.launch(auth_url) verification = click.prompt("Enter verification code") token_resp = auth.get_raw_access_token( token, secret, method='POST', data={'oauth_verifier': verification}) if not token_resp: logging.debug(token_resp.content) click.fail("Error during API key generation.") access = urlparse.parse_qs(token_resp.text) return access['oauth_token'][0], access['userID'][0]
def state(ctx, module, identifier, db, table, key_map, verbose, namespace): """ Dump the current state of the identifier for the specified module from Redis DB or CONFIG_FILE """ if not multi_asic.is_multi_asic() and namespace != DEFAULT_NAMESPACE: click.echo("Namespace option is not valid for a single-ASIC device") ctx.exit() if multi_asic.is_multi_asic() and (namespace != DEFAULT_NAMESPACE and namespace not in multi_asic.get_namespace_list()): click.echo("Namespace option is not valid. Choose one of {}".format( multi_asic.get_namespace_list())) ctx.exit() if module not in plugins.dump_modules: click.echo("No Matching Plugin has been Implemented") ctx.exit() if verbose: os.environ["VERBOSE"] = "1" else: os.environ["VERBOSE"] = "0" ctx.module = module obj = plugins.dump_modules[module]() if identifier == "all": ids = obj.get_all_args(namespace) else: ids = identifier.split(",") params = {} collected_info = {} params['namespace'] = namespace for arg in ids: params[plugins.dump_modules[module].ARG_NAME] = arg try: collected_info[arg] = obj.execute(params) except ValueError as err: click.fail(f"Failed to execute plugin: {err}") if len(db) > 0: collected_info = filter_out_dbs(db, collected_info) vidtorid = extract_rid(collected_info, namespace) if not key_map: collected_info = populate_fv(collected_info, module, namespace) for id in vidtorid.keys(): collected_info[id]["ASIC_DB"]["vidtorid"] = vidtorid[id] print_dump(collected_info, table, module, identifier, key_map) return
def get_confluence_cmd(): """Assemble mandatory parameters for the Confluence command.""" try: command_path = environ['CONFLUENCE_COMMAND_PATH'] except KeyError as error: click.fail('Unable to access {}'.format(str(error))) credentials = get_credentials() return [ command_path, '--server', CONFLUENCE_URL, '--user', credentials['username'], '--password', credentials['password'] ]
def growth(ctx, **kwargs): """Attempts to compute growth rate (i.e. fit e^(2x)) from DynVector data, typically an integrated quantity like electric or magnetic field energy. """ vlog(ctx, 'Starting growth') pushChain( ctx, 'growth', **kwargs) data = ctx.obj['data'] for dat in data.iterator(kwargs['use']): time = dat.getGrid() values = dat.getValues() numDims = dat.getNumDims() if numDims > 1: click.fail(click.style("'growth' is available only for 1D data (used on {:d}D data)".format(numDims), fg='red')) #end bestParams, bestR2, bestN = fitGrowth(time[0], values[..., 0], minN=kwargs['minn'], maxN=kwargs['maxn'], p0=kwargs['guess']) if kwargs['plot'] is True: vlog(ctx, 'growth: Plotting data and fit') plt.style.use(os.path.dirname(os.path.realpath(__file__)) \ + "/../output/postgkyl.mplstyle") fig, ax = plt.subplots() ax.plot(time[0], values[..., 0], '.') ax.set_autoscale_on(False) ax.plot(time[0], exp2(time[0], *bestParams)) ax.grid(True) plt.show() #end if kwargs['instantaneous'] is True: vlog(ctx, 'growth: Plotting instantaneous growth rate') gammas = [] for i in range(1,len(time[0])-1): gamma = (values[i+1,0] - values[i-1,0])/(2*values[i,0]*(time[0][i+1] - time[0][i-1])) gammas.append(gamma) plt.style.use(os.path.dirname(os.path.realpath(__file__)) \ + "/../output/postgkyl.mplstyle") fig, ax = plt.subplots() ax.plot(time[0][1:-1], gammas) #ax.set_autoscale_on(False) ax.grid(True) plt.show() #end #end vlog(ctx, 'Finishing growth')
def import_(bundleid): for path, plist in alfredcli.util.workflows(): if plist.get("bundleid") == bundleid: break else: click.fail("Unable to find bundle id: {0}".format(bundleid)) source_dir = os.path.dirname(path) dest_dir = os.getcwd() click.echo("Importing {0} from {1} to {2}".format(bundleid, source_dir, dest_dir)) for root, dirs, files in os.walk(source_dir): for f in files: # if f != 'info.plist': # continue source_path = os.path.join(root, f) dest_path = os.path.join(dest_dir, f) click.echo("Copying {0} to {1}".format(source_path, dest_path)) shutil.copy(source_path, dest_path)
def motion(input: hansel.Crumb, extra: pd.DataFrame, out_file: hansel.Crumb): """ Create in `out_path` an Excel spreadsheet with some of the motion statistics obtained from the `statistics_files` output of the nipype.RapidArt found in the hansel.Crumb `motion_file_cr`. Examples: \n nitap motion -i "/data/hansel/cobre/{sid}/{session}/rest/artifact_stats/motion_stats.json" -o motion.xls\n nitap motion -i "/data/nuk/out/{group}/{sid}/session_0/rest/artifact_stats/motion_stats.json" -o motion.xls\n """ from neuro_pypes.fmri.utils import motion_stats_sheet crumb_args = list(input.open_args()) df = motion_stats_sheet(input, crumb_args) if extra: extra_columns = set(extra.columns.values) matched_args = extra_columns.intersection(crumb_args) if not matched_args: click.fail('Found no matches in the spreadsheet file between: ' '"{}" and "{}".'.format(extra_columns, crumb_args)) df.join(extra, on=matched_args) df.to_excel(out_file) print( 'Successfully wrote the motions spreadsheet in "{}".'.format(out_file))
def main(group_id, location, time_boundary, event_status, pandoc, force): key_path = os.path.normpath(os.path.expanduser('~/.meetup.com-key')) if os.path.exists(key_path): with io.open(key_path, encoding='utf8') as fh: key = fh.read().strip() else: key = None cache = FileCache('.web_cache', forever=True) requests = CacheControl(Session(), cache, cache_etags=False, heuristic=ExpiresAfter(days=1)) while True: resp = requests.get('https://api.meetup.com/status', params=dict(key=key)) if resp.status_code == 200 and resp.json().get('status') == 'ok': break elif resp.status_code == 200 and any( 'auth_fail' == e.code for e in resp.json().get('errors', [])): click.echo( 'Your meetup.com key is required. You can get it from https://secure.meetup.com/meetup_api/key/\n' ) if click.confirm( 'Open https://secure.meetup.com/meetup_api/key/ in your web browser?' ): click.launch('https://secure.meetup.com/meetup_api/key/') click.echo('') key = click.prompt('Key', hide_input=True) else: raise click.ClickException( 'Failed to get meetup.com status. Response was {!r} {!r}'. format(resp.status_code, resp.text)) click.secho( 'For convenience your key is saved in `{}`.\n'.format(key_path), fg='magenta') with open(key_path, 'w') as fh: fh.write(key) while not location: location = location or get_input( u'Location: ', completer=WordCompleter( [u'cluj', u'iasi', u'timisoara', u'bucuresti'], ignore_case=True)) while True: group_id = group_id or get_input( u'Group ID: ', completer=WordCompleter([ u'RoPython-Bucuresti', u'RoPython-Cluj', u'RoPython_Iasi', u'RoPython-Timisoara' ], ignore_case=True)) resp = requests.get('https://api.meetup.com/2/events', params=dict( key=key, group_urlname=group_id, time=time_boundary, status=event_status, )) if resp.status_code == 200: json = resp.json() if json['results']: break else: click.secho( 'Invalid group `{}`. It has no events!'.format(group_id), fg='red') group_id = None if resp.status_code == '400': click.fail( 'Failed to get make correct request. Response was {!r}'.format( resp.text)) else: click.secho('Invalid group `{}`. Response was [{}] {!r}'.format( group_id, resp.status_code, resp.text), fg='red') # click.echo(pformat(dict(resp.headers))) for event in json['results']: dt = datetime.fromtimestamp(event['time'] / 1000) event['duration'] = format_duration( event.get('duration', 3600000) / 1000) event['time'] = dt.strftime('%Y-%m-%d %H:%M') if 'how_to_find_us' in event: address = event['how_to_find_us'], else: address = () if 'venue' in event: address_1 = event['venue'].get('address_1') if address_1: address += address_1, event['venue']['address_1'] = ', '.join(address) else: event['venue'] = {'address_1': address} click.echo("{time}: {name}".format(**event)) click.echo("\t{}".format(pformat(event))) existing_path = glob( os.path.join('content', '*', dt.strftime('%Y-%m-%d*'), 'index.rst')) if existing_path and not force: if len(existing_path) > 1: click.secho('\tERROR: multiple paths matched: {}'.format( existing_path)) else: click.secho('\t`{}` already exists. Not importing.'.format( *existing_path), fg='yellow') else: target_dir = os.path.join( 'content', location, '{}-{}'.format(dt.strftime('%Y-%m-%d'), slugify(event['name']))) target_path = os.path.join(target_dir, 'index.rst') if not os.path.exists(target_dir): os.makedirs(target_dir) if pandoc: with tempfile.NamedTemporaryFile(delete=False) as fh: fh.write(event['description'].encode('utf-8')) rst = subprocess.check_output( ['pandoc', '--from=html', '--to=rst', fh.name]).decode('utf-8') os.unlink(fh.name) else: rst = html2rest(event['description']) doc = u'''{name} ############################################################### :tags: prezentari :registration: meetup.com: {event_url} :start: {time} :duration: {duration} :location: {venue[address_1]}, {venue[city]}, {venue[localized_country_name]} {rst}'''.format(rst=rst, **event) with io.open(target_path, 'w', encoding='utf-8') as fh: fh.write(doc) click.secho('\tWrote `{}`.'.format(target_path), fg='green')
def main(group_id, location, time_boundary, event_status, pandoc): key_path = os.path.normpath(os.path.expanduser('~/.meetup.com-key')) if os.path.exists(key_path): with open(key_path) as fh: key = fh.read().strip() cache = FileCache('.web_cache', forever=True) requests = CacheControl( Session(), cache, cache_etags=False, heuristic=ExpiresAfter(days=1) ) while True: resp = requests.get('https://api.meetup.com/status', params=dict(key=key)) if resp.status_code == 200: break elif resp.status_code == 401: click.echo('Your meetup.com key is required. You can get it from https://secure.meetup.com/meetup_api/key/\n') if click.confirm('Open https://secure.meetup.com/meetup_api/key/ in your web browser?'): click.launch('https://secure.meetup.com/meetup_api/key/') click.echo('') key = click.prompt('Key', hide_input=True) else: click.fail('Failed to get meetup.com status. Response was {!r}'.format(resp.text)) click.secho('For convenience your key is saved in `{}`.\n'.format(key_path), fg='magenta') with open(key_path, 'w') as fh: fh.write(key) while not location: location = location or get_input('Location: ', completer=WordCompleter(['cluj', 'iasi', 'timisoara'], ignore_case=True)) while True: group_id = group_id or get_input('Group ID: ', completer=WordCompleter(['Cluj-py', 'RoPython-Timisoara'], ignore_case=True)) resp = requests.get('https://api.meetup.com/2/events', params=dict( key=key, group_urlname=group_id, time=time_boundary, status=event_status, )) if resp.status_code == 200: json = resp.json() if json['results']: break else: click.secho('Invalid group `{}`. It has no events!'.format(group_id), fg='red') group_id = None if resp.status_code == '400': click.fail('Failed to get make correct request. Response was {!r}'.format(resp.text)) else: click.secho('Invalid group `{}`. Response was [{}] {!r}'.format(group_id, resp.status_code, resp.text), fg='red') # click.echo(pformat(dict(resp.headers))) for event in json['results']: dt = datetime.fromtimestamp(event['time']/1000) click.echo("{}: {}".format( dt.strftime('%Y-%m-%d %H:%M:%S'), event['name'] )) existing_path = glob(os.path.join('content', '*', dt.strftime('%Y-%m-%d*'), 'index.rst')) if existing_path: if len(existing_path) > 1: click.secho('\tERROR: multiple paths matched: {}'.format(existing_path)) else: click.secho('\t`{}` already exists. Not importing.'.format(*existing_path), fg='yellow') else: target_dir = os.path.join('content', location, '{}-{}'.format(dt.strftime('%Y-%m-%d'), slugify(event['name']))) target_path = os.path.join(target_dir, 'index.rst') if not os.path.exists(target_dir): os.makedirs(target_dir) if pandoc: with tempfile.NamedTemporaryFile(delete=False) as fh: fh.write(event['description'].encode('utf-8')) rst = subprocess.check_output(['pandoc', '--from=html', '--to=rst', fh.name]).decode('utf-8') print fh.name #os.unlink(fh.name) else: stream = StringIO() html2rest(event['description'].encode('utf-8'), writer=stream) rst = stream.getvalue().decode('utf-8') with io.open(target_path, 'w', encoding='utf-8') as fh: fh.write('''{name} ############################################################### :tags: unknown :registration: meetup.com: {event_url} {rst}'''.format(rst=rst, **event)) click.secho('\tWrote `{}`.'.format(target_path), fg='green')
def standard(ct_file_name, rods_file_name, density_file_name, densities, csv_file_name): '''Determine the calibration equation for a standard phantom.''' click.echo(echo_arguments('Standard Calibration', locals())) # Read click.echo('Reading input {}'.format(ct_file_name)) ct = sitk.ReadImage(ct_file_name) click.echo('Reading input {}'.format(rods_file_name)) rods = sitk.ReadImage(rods_file_name) # Determine number of components click.echo('Determining mean intensities') filt = sitk.LabelStatisticsImageFilter() filt.Execute(ct, rods) n_labels = len(filt.GetLabels()) click.echo('Found {} labels'.format(n_labels)) if len(densities) < 2: click.fail('Need at least 2 samples to fit a function.' ' Only found {}'.format(len(densities))) # Get the HU samples HU = [] for label in filt.GetLabels(): # Skip background label if label == 0: continue HU.append(filt.GetMean(label)) click.echo('Fitting parameters:') click.echo(' Densities: {}'.format(densities)) click.echo(' HU: {}'.format(HU)) # Perform calibration calibrator = StandardCalibration() calibrator.fit(HU, densities) click.echo('Found fit:') click.echo(' Slope: {}'.format(calibrator.slope)) click.echo(' Intercept: {}'.format(calibrator.intercept)) click.echo(' R^2: {}'.format(calibrator.r_value**2)) header = [ 'CT File Name', 'Rods File Name', 'Density File Name', 'slope', 'intercept', 'r_value', 'p_value', 'std_err' ] data = { 'CT File Name': ct_file_name, 'Rods File Name': rods_file_name, 'Density File Name': density_file_name, 'slope': round(calibrator.slope, 16), 'intercept': round(calibrator.intercept, 16), 'r_value': round(calibrator.r_value, 16), 'p_value': round(calibrator.p_value, 16), 'std_err': round(calibrator.std_err, 16) } click.echo('Writing complete results to {}'.format(csv_file_name)) write_csv(data, csv_file_name, header) click.echo('Calibrating density file') density = calibrator.slope * sitk.Cast(ct, sitk.sitkFloat32) + \ calibrator.intercept click.echo('Writing density file to {}'.format(density_file_name)) sitk.ReadImage(density, density_file_name)
def mindways(ct_file_name, rods_file_name, density_file_name, csv_file_name, water, densities): '''Determine the calibration equation for a Mindways phantom. Water and K2HPO4 densities are taken from the QC documentation accompanying your specific phantom.''' click.echo(echo_arguments('Mindways Calibration', locals())) # Read click.echo('Reading input {}'.format(ct_file_name)) ct = sitk.ReadImage(ct_file_name) click.echo('Reading input {}'.format(rods_file_name)) rods = sitk.ReadImage(rods_file_name) # Determine number of components click.echo('Determining mean intensities') filt = sitk.LabelStatisticsImageFilter() filt.Execute(ct, rods) n_labels = len(filt.GetLabels()) click.echo('Found {} labels'.format(n_labels)) if len(water) != len(densities) or len(water) != n_labels - 1: click.fail( 'Number of water, K2HPO4, and segmented labels are not the same ' '({}, {}, and {})'.format(len(water), len(densities), n_labels - 1)) if len(water) < 2: click.fail('Need at least 2 samples to fit a function.' ' Only found {}'.format(len(water))) # Get the HU samples HU = [] for label in filt.GetLabels(): # Skip background label if label == 0: continue HU.append(filt.GetMean(label)) click.echo('Fitting parameters:') click.echo(' Water: {}'.format(water)) click.echo(' K2HPO4: {}'.format(densities)) click.echo(' HU: {}'.format(HU)) # Perform calibration calibrator = MindwaysCalibration() calibrator.fit(HU, densities, water) click.echo('Found fit:') click.echo(' Slope: {}'.format(calibrator.slope)) click.echo(' Intercept: {}'.format(calibrator.intercept)) click.echo(' R^2: {}'.format(calibrator.r_value**2)) header = [ 'CT File Name', 'Rods File Name', 'Density File Name', 'sigma_ref', 'beta_ref', 'sigma_ct', 'beta_ct', 'slope', 'intercept', 'r_value', 'p_value', 'std_err' ] data = { 'CT File Name': ct_file_name, 'Rods File Name': rods_file_name, 'Density File Name': density_file_name, 'sigma_ref': round(calibrator.sigma_ref, 16), 'beta_ref': round(calibrator.beta_ref, 16), 'sigma_ct': round(calibrator.sigma_ct, 16), 'beta_ct': round(calibrator.beta_ct, 16), 'slope': round(calibrator.slope, 16), 'intercept': round(calibrator.intercept, 16), 'r_value': round(calibrator.r_value, 16), 'p_value': round(calibrator.p_value, 16), 'std_err': round(calibrator.std_err, 16) } click.echo('Writing complete results to {}'.format(csv_file_name)) write_csv(data, csv_file_name, header) click.echo('Calibrating density file') density = calibrator.slope * sitk.Cast(ct, sitk.sitkFloat32) + \ calibrator.intercept click.echo('Writing density file to {}'.format(density_file_name)) sitk.ReadImage(density, density_file_name)