{"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck the current state of the experiment to see whether it has been completed.", "response": "def experiment_completed(self):\n \"\"\"Checks the current state of the experiment to see whether it has\n completed. This makes use of the experiment server `/summary` route,\n which in turn uses :meth:`~Experiment.is_complete`.\n \"\"\"\n heroku_app = HerokuApp(self.app_id)\n status_url = \"{}/summary\".format(heroku_app.url)\n data = {}\n try:\n resp = requests.get(status_url)\n data = resp.json()\n except (ValueError, requests.exceptions.RequestException):\n logger.exception(\"Error fetching experiment status.\")\n logger.debug(\"Current application state: {}\".format(data))\n return data.get(\"completed\", False)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef retrieve_data(self):\n local = False\n if self.exp_config.get(\"mode\") == \"debug\":\n local = True\n filename = export(self.app_id, local=local)\n logger.debug(\"Data exported to %s\" % filename)\n return Data(filename)", "response": "Retrieves and saves data from a running experiment"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef end_experiment(self):\n if self.exp_config.get(\"mode\") != \"debug\":\n HerokuApp(self.app_id).destroy()\n return True", "response": "Terminates a running experiment"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef events_for_replay(self, session=None, target=None):\n if session is None:\n session = self.session\n return session.query(Info).order_by(Info.creation_time)", "response": "Returns an ordered list of events for replaying."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndisplay Jupyter Notebook widget", "response": "def _ipython_display_(self):\n \"\"\"Display Jupyter Notebook widget\"\"\"\n from IPython.display import display\n\n self.build_widget()\n display(self.widget())"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef from_config(config):\n debug_mode = config.get(\"mode\") == \"debug\"\n name = config.get(\"recruiter\", None)\n recruiter = None\n\n # Special case 1: Don't use a configured recruiter in replay mode\n if config.get(\"replay\"):\n return HotAirRecruiter()\n\n if name is not None:\n recruiter = by_name(name)\n\n # Special case 2: may run BotRecruiter or MultiRecruiter in any mode\n # (debug or not), so it trumps everything else:\n if isinstance(recruiter, (BotRecruiter, MultiRecruiter)):\n return recruiter\n\n # Special case 3: if we're not using bots and we're in debug mode,\n # ignore any configured recruiter:\n if debug_mode:\n return HotAirRecruiter()\n\n # Configured recruiter:\n if recruiter is not None:\n return recruiter\n\n if name and recruiter is None:\n raise NotImplementedError(\"No such recruiter {}\".format(name))\n\n # Default if we're not in debug mode:\n return MTurkRecruiter()", "response": "Return a Recruiter instance based on the configuration."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn initial experiment URL list plus instructions for finding subsequent recruitment events in experiemnt logs.", "response": "def open_recruitment(self, n=1):\n \"\"\"Return initial experiment URL list, plus instructions\n for finding subsequent recruitment events in experiemnt logs.\n \"\"\"\n logger.info(\"Opening CLI recruitment for {} participants\".format(n))\n recruitments = self.recruit(n)\n message = (\n 'Search for \"{}\" in the logs for subsequent recruitment URLs.\\n'\n \"Open the logs for this experiment with \"\n '\"dallinger logs --app {}\"'.format(\n NEW_RECRUIT_LOG_PREFIX, self.config.get(\"id\")\n )\n )\n return {\"items\": recruitments, \"message\": message}"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngenerates experiemnt URLs and print them to the console.", "response": "def recruit(self, n=1):\n \"\"\"Generate experiemnt URLs and print them to the console.\"\"\"\n logger.info(\"Recruiting {} CLI participants\".format(n))\n urls = []\n template = \"{}/ad?recruiter={}&assignmentId={}&hitId={}&workerId={}&mode={}\"\n for i in range(n):\n ad_url = template.format(\n get_base_url(),\n self.nickname,\n generate_random_id(),\n generate_random_id(),\n generate_random_id(),\n self._get_mode(),\n )\n logger.info(\"{} {}\".format(NEW_RECRUIT_LOG_PREFIX, ad_url))\n urls.append(ad_url)\n\n return urls"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef reward_bonus(self, assignment_id, amount, reason):\n logger.info(\n 'Award ${} for assignment {}, with reason \"{}\"'.format(\n amount, assignment_id, reason\n )\n )", "response": "Print out bonus info for the assignment"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns initial experiment URL list plus instructions for finding subsequent recruitment events in experiemnt logs.", "response": "def open_recruitment(self, n=1):\n \"\"\"Return initial experiment URL list, plus instructions\n for finding subsequent recruitment events in experiemnt logs.\n \"\"\"\n logger.info(\"Opening HotAir recruitment for {} participants\".format(n))\n recruitments = self.recruit(n)\n message = \"Recruitment requests will open browser windows automatically.\"\n\n return {\"items\": recruitments, \"message\": message}"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef open_recruitment(self, n=1):\n logger.info(\"Opening Sim recruitment for {} participants\".format(n))\n return {\"items\": self.recruit(n), \"message\": \"Simulated recruitment only\"}", "response": "Open recruitment for n participants."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef open_recruitment(self, n=1):\n logger.info(\"Opening MTurk recruitment for {} participants\".format(n))\n if self.is_in_progress:\n raise MTurkRecruiterException(\n \"Tried to open_recruitment on already open recruiter.\"\n )\n\n if self.hit_domain is None:\n raise MTurkRecruiterException(\"Can't run a HIT from localhost\")\n\n self.mturkservice.check_credentials()\n\n if self.config.get(\"assign_qualifications\"):\n self._create_mturk_qualifications()\n\n hit_request = {\n \"max_assignments\": n,\n \"title\": self.config.get(\"title\"),\n \"description\": self.config.get(\"description\"),\n \"keywords\": self._config_to_list(\"keywords\"),\n \"reward\": self.config.get(\"base_payment\"),\n \"duration_hours\": self.config.get(\"duration\"),\n \"lifetime_days\": self.config.get(\"lifetime\"),\n \"ad_url\": self.ad_url,\n \"notification_url\": self.config.get(\"notification_url\"),\n \"approve_requirement\": self.config.get(\"approve_requirement\"),\n \"us_only\": self.config.get(\"us_only\"),\n \"blacklist\": self._config_to_list(\"qualification_blacklist\"),\n \"annotation\": self.config.get(\"id\"),\n }\n hit_info = self.mturkservice.create_hit(**hit_request)\n if self.config.get(\"mode\") == \"sandbox\":\n lookup_url = (\n \"https://workersandbox.mturk.com/mturk/preview?groupId={type_id}\"\n )\n else:\n lookup_url = \"https://worker.mturk.com/mturk/preview?groupId={type_id}\"\n\n return {\n \"items\": [lookup_url.format(**hit_info)],\n \"message\": \"HIT now published to Amazon Mechanical Turk\",\n }", "response": "Open a connection to AWS MTurk and create a HIT."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrecruits n new participants to an existing HIT", "response": "def recruit(self, n=1):\n \"\"\"Recruit n new participants to an existing HIT\"\"\"\n logger.info(\"Recruiting {} MTurk participants\".format(n))\n if not self.config.get(\"auto_recruit\"):\n logger.info(\"auto_recruit is False: recruitment suppressed\")\n return\n\n hit_id = self.current_hit_id()\n if hit_id is None:\n logger.info(\"no HIT in progress: recruitment aborted\")\n return\n\n try:\n return self.mturkservice.extend_hit(\n hit_id, number=n, duration_hours=self.config.get(\"duration\")\n )\n except MTurkServiceException as ex:\n logger.exception(str(ex))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nassigns a Qualification to the Participant for the experiment ID and for the configured group_name.", "response": "def notify_completed(self, participant):\n \"\"\"Assign a Qualification to the Participant for the experiment ID,\n and for the configured group_name, if it's been set.\n\n Overrecruited participants don't receive qualifications, since they\n haven't actually completed the experiment. This allows them to remain\n eligible for future runs.\n \"\"\"\n if participant.status == \"overrecruited\" or not self.qualification_active:\n return\n\n worker_id = participant.worker_id\n\n for name in self.qualifications:\n try:\n self.mturkservice.increment_qualification_score(name, worker_id)\n except QualificationNotFoundException as ex:\n logger.exception(ex)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef reward_bonus(self, assignment_id, amount, reason):\n try:\n return self.mturkservice.grant_bonus(assignment_id, amount, reason)\n except MTurkServiceException as ex:\n logger.exception(str(ex))", "response": "Reward the Turker for a specified assignment with a bonus."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating MTurk Qualification for experiment ID and for group_name .", "response": "def _create_mturk_qualifications(self):\n \"\"\"Create MTurk Qualification for experiment ID, and for group_name\n if it's been set. Qualifications with these names already exist, but\n it's faster to try and fail than to check, then try.\n \"\"\"\n for name, desc in self.qualifications.items():\n try:\n self.mturkservice.create_qualification_type(name, desc)\n except DuplicateQualificationNameError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nstarts recruiting right away.", "response": "def open_recruitment(self, n=1):\n \"\"\"Start recruiting right away.\"\"\"\n logger.info(\"Opening Bot recruitment for {} participants\".format(n))\n factory = self._get_bot_factory()\n bot_class_name = factory(\"\", \"\", \"\").__class__.__name__\n return {\n \"items\": self.recruit(n),\n \"message\": \"Bot recruitment started using {}\".format(bot_class_name),\n }"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrecruiting n new participant bots to the queue", "response": "def recruit(self, n=1):\n \"\"\"Recruit n new participant bots to the queue\"\"\"\n logger.info(\"Recruiting {} Bot participants\".format(n))\n factory = self._get_bot_factory()\n urls = []\n q = _get_queue()\n for _ in range(n):\n base_url = get_base_url()\n worker = generate_random_id()\n hit = generate_random_id()\n assignment = generate_random_id()\n ad_parameters = (\n \"recruiter={}&assignmentId={}&hitId={}&workerId={}&mode=sandbox\"\n )\n ad_parameters = ad_parameters.format(self.nickname, assignment, hit, worker)\n url = \"{}/ad?{}\".format(base_url, ad_parameters)\n urls.append(url)\n bot = factory(url, assignment_id=assignment, worker_id=worker, hit_id=hit)\n job = q.enqueue(bot.run_experiment, timeout=self._timeout)\n logger.warning(\"Created job {} for url {}.\".format(job.id, url))\n\n return urls"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef notify_duration_exceeded(self, participants, reference_time):\n for participant in participants:\n participant.status = \"rejected\"\n session.commit()", "response": "Notify the user that the duration of the participants has been exceeded."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing the specification of how to recruit participants.", "response": "def parse_spec(self):\n \"\"\"Parse the specification of how to recruit participants.\n\n Example: recruiters = bots: 5, mturk: 1\n \"\"\"\n recruiters = []\n spec = get_config().get(\"recruiters\")\n for match in self.SPEC_RE.finditer(spec):\n name = match.group(1)\n count = int(match.group(2))\n recruiters.append((name, count))\n return recruiters"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef recruiters(self, n=1):\n recruit_count = 0\n while recruit_count <= n:\n counts = dict(\n session.query(Recruitment.recruiter_id, func.count(Recruitment.id))\n .group_by(Recruitment.recruiter_id)\n .all()\n )\n for recruiter_id, target_count in self.spec:\n remaining = 0\n count = counts.get(recruiter_id, 0)\n if count >= target_count:\n # This recruiter quota was reached;\n # move on to the next one.\n counts[recruiter_id] = count - target_count\n continue\n else:\n # Quota is still available; let's use it.\n remaining = target_count - count\n break\n else:\n return\n\n num_recruits = min(n - recruit_count, remaining)\n # record the recruitments and commit\n for i in range(num_recruits):\n session.add(Recruitment(recruiter_id=recruiter_id))\n session.commit()\n\n recruit_count += num_recruits\n yield by_name(recruiter_id), num_recruits", "response": "Iterator that provides the participant\n count to be recruited for up to n participants."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef open_recruitment(self, n=1):\n logger.info(\"Multi recruitment running for {} participants\".format(n))\n recruitments = []\n messages = {}\n remaining = n\n for recruiter, count in self.recruiters(n):\n if not count:\n break\n if recruiter.nickname in messages:\n result = recruiter.recruit(count)\n recruitments.extend(result)\n else:\n result = recruiter.open_recruitment(count)\n recruitments.extend(result[\"items\"])\n messages[recruiter.nickname] = result[\"message\"]\n\n remaining -= count\n if remaining <= 0:\n break\n\n logger.info(\n (\n \"Multi-recruited {} out of {} participants, \" \"using {} recruiters.\"\n ).format(n - remaining, n, len(messages))\n )\n\n return {\"items\": recruitments, \"message\": \"\\n\".join(messages.values())}", "response": "Return initial experiment URL list."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run_check(participants, config, reference_time):\n recruiters_with_late_participants = defaultdict(list)\n for p in participants:\n timeline = ParticipationTime(p, reference_time, config)\n if timeline.is_overdue:\n print(\n \"Error: participant {} with status {} has been playing for too \"\n \"long - their recruiter will be notified.\".format(p.id, p.status)\n )\n recruiters_with_late_participants[p.recruiter_id].append(p)\n\n for recruiter_id, participants in recruiters_with_late_participants.items():\n recruiter = recruiters.by_name(recruiter_id)\n recruiter.notify_duration_exceeded(participants, reference_time)", "response": "Check if the participants have been playing for too long."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef check_db_for_missing_notifications():\n config = dallinger.config.get_config()\n participants = Participant.query.filter_by(status=\"working\").all()\n reference_time = datetime.now()\n\n run_check(participants, config, reference_time)", "response": "Check the database for missing notifications."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _work(self, burst=False, logging_level=logging.INFO):\n setup_loghandlers(logging_level)\n self._install_signal_handlers()\n\n self.did_perform_work = False\n self.register_birth()\n self.log.info(\n \"RQ GEVENT worker (Greenlet pool size={0}) {1!r} started, version {2}\".format(\n self.gevent_pool.size, self.key, VERSION\n )\n )\n self.set_state(WorkerStatus.STARTED)\n\n try:\n while True:\n try:\n self.check_for_suspension(burst)\n\n if self.should_run_maintenance_tasks:\n self.clean_registries()\n\n if self._stop_requested:\n self.log.info(\"Stopping on request.\")\n break\n\n timeout = None if burst else max(1, self.default_worker_ttl - 60)\n\n result = self.dequeue_job_and_maintain_ttl(timeout)\n if result is None and burst:\n self.log.info(\"RQ worker {0!r} done, quitting\".format(self.key))\n\n try:\n # Make sure dependented jobs are enqueued.\n gevent.wait(self.children)\n except LoopExit:\n pass\n result = self.dequeue_job_and_maintain_ttl(timeout)\n\n if result is None:\n break\n except StopRequested:\n break\n\n job, queue = result\n self.execute_job(job, queue)\n\n finally:\n if not self.is_horse:\n self.register_death()\n return self.did_perform_work", "response": "Starts the work loop."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nspawns a greenlet to be able to kill it when it s blocked dequeueing the job.", "response": "def work(self, burst=False, logging_level=logging.INFO):\n \"\"\"\n Spawning a greenlet to be able to kill it when it's blocked dequeueing job\n :param burst: if it's burst worker don't need to spawn a greenlet\n \"\"\"\n # If the is a burst worker it's not needed to spawn greenlet\n if burst:\n return self._work(burst, logging_level=logging_level)\n\n self.gevent_worker = gevent.spawn(self._work, burst)\n self.gevent_worker.join()\n return self.gevent_worker.value"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef register(dlgr_id, snapshot=None):\n try:\n config.get(\"osf_access_token\")\n except KeyError:\n pass\n else:\n osf_id = _create_osf_project(dlgr_id)\n _upload_assets_to_OSF(dlgr_id, osf_id)", "response": "Register the experiment using configured services."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a project on the OSF.", "response": "def _create_osf_project(dlgr_id, description=None):\n \"\"\"Create a project on the OSF.\"\"\"\n\n if not description:\n description = \"Experiment {} registered by Dallinger.\".format(dlgr_id)\n\n r = requests.post(\n \"{}/nodes/\".format(root),\n data={\n \"type\": \"nodes\",\n \"category\": \"project\",\n \"title\": \"Experiment dlgr-{}\".format(dlgr_id[0:8]),\n \"description\": description,\n },\n headers={\"Authorization\": \"Bearer {}\".format(config.get(\"osf_access_token\"))},\n )\n r.raise_for_status()\n osf_id = r.json()[\"data\"][\"id\"]\n\n logger.info(\"Project registered on OSF at http://osf.io/{}\".format(osf_id))\n\n return osf_id"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _upload_assets_to_OSF(dlgr_id, osf_id, provider=\"osfstorage\"):\n root = \"https://files.osf.io/v1\"\n snapshot_filename = \"{}-code.zip\".format(dlgr_id)\n snapshot_path = os.path.join(\"snapshots\", snapshot_filename)\n r = requests.put(\n \"{}/resources/{}/providers/{}/\".format(root, osf_id, provider),\n params={\"kind\": \"file\", \"name\": snapshot_filename},\n headers={\n \"Authorization\": \"Bearer {}\".format(config.get(\"osf_access_token\")),\n \"Content-Type\": \"text/plain\",\n },\n data=open(snapshot_path, \"rb\"),\n )\n r.raise_for_status()", "response": "Upload experimental assets to the OSF."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef outformat_is_json():\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n return state.outformat_is_json()", "response": "Check if the current click context is json"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchecking if the current click context is Unix.", "response": "def outformat_is_unix():\n \"\"\"\n Only safe to call within a click context.\n \"\"\"\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n return state.outformat_is_unix()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef outformat_is_text():\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n return state.outformat_is_text()", "response": "Check if the outformat of the current click context is text."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the JMESPATH expression for the current click context.", "response": "def get_jmespath_expression():\n \"\"\"\n Only safe to call within a click context.\n \"\"\"\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n return state.jmespath_expr"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef verbosity():\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n return state.verbosity", "response": "Get the current verbosity of the current node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef is_verbose():\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n return state.is_verbose()", "response": "Returns True if the current click context is verbose."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef show_task(successful_transfers, task_id):\n client = get_client()\n\n if successful_transfers:\n print_successful_transfers(client, task_id)\n else:\n print_task_detail(client, task_id)", "response": "Executor for globus task show"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bookmark_create(endpoint_plus_path, bookmark_name):\n endpoint_id, path = endpoint_plus_path\n client = get_client()\n\n submit_data = {\"endpoint_id\": str(endpoint_id), \"path\": path, \"name\": bookmark_name}\n\n res = client.create_bookmark(submit_data)\n formatted_print(res, simple_text=\"Bookmark ID: {}\".format(res[\"id\"]))", "response": "Executor for globus bookmark create"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef server_list(endpoint_id):\n # raises usage error on shares for us\n endpoint, server_list = get_endpoint_w_server_list(endpoint_id)\n\n if server_list == \"S3\": # not GCS -- this is an S3 endpoint\n server_list = {\"s3_url\": endpoint[\"s3_url\"]}\n fields = [(\"S3 URL\", \"s3_url\")]\n text_format = FORMAT_TEXT_RECORD\n else: # regular GCS host endpoint\n fields = (\n (\"ID\", \"id\"),\n (\"URI\", lambda s: (s[\"uri\"] or \"none (Globus Connect Personal)\")),\n )\n text_format = FORMAT_TEXT_TABLE\n formatted_print(server_list, text_format=text_format, fields=fields)", "response": "Executor for globus endpoint server list"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef task_list(\n limit,\n filter_task_id,\n filter_status,\n filter_type,\n filter_label,\n filter_not_label,\n inexact,\n filter_requested_after,\n filter_requested_before,\n filter_completed_after,\n filter_completed_before,\n):\n \"\"\"\n Executor for `globus task-list`\n \"\"\"\n\n def _process_filterval(prefix, value, default=None):\n if value:\n if isinstance(value, six.string_types):\n return \"{}:{}/\".format(prefix, value)\n return \"{}:{}/\".format(prefix, \",\".join(str(x) for x in value))\n else:\n return default or \"\"\n\n # make filter string\n filter_string = \"\"\n filter_string += _process_filterval(\"task_id\", filter_task_id)\n filter_string += _process_filterval(\"status\", filter_status)\n filter_string += _process_filterval(\n \"type\", filter_type, default=\"type:TRANSFER,DELETE/\"\n )\n\n # combine data into one list for easier processing\n if inexact:\n label_data = [\"~\" + s for s in filter_label] + [\n \"!~\" + s for s in filter_not_label\n ]\n else:\n label_data = [\"=\" + s for s in filter_label] + [\n \"!\" + s for s in filter_not_label\n ]\n filter_string += _process_filterval(\"label\", label_data)\n\n filter_string += _process_filterval(\n \"request_time\",\n [(filter_requested_after or \"\"), (filter_requested_before or \"\")],\n )\n filter_string += _process_filterval(\n \"completion_time\",\n [(filter_completed_after or \"\"), (filter_completed_before or \"\")],\n )\n\n client = get_client()\n task_iterator = client.task_list(\n num_results=limit, filter=filter_string[:-1]\n ) # ignore trailing /\n\n fields = [\n (\"Task ID\", \"task_id\"),\n (\"Status\", \"status\"),\n (\"Type\", \"type\"),\n (\"Source Display Name\", \"source_endpoint_display_name\"),\n (\"Dest Display Name\", \"destination_endpoint_display_name\"),\n (\"Label\", \"label\"),\n ]\n formatted_print(\n task_iterator, fields=fields, json_converter=iterable_response_to_dict\n )", "response": "Executor for globus task - list"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete_command(\n batch,\n ignore_missing,\n star_silent,\n recursive,\n enable_globs,\n endpoint_plus_path,\n label,\n submission_id,\n dry_run,\n deadline,\n skip_activation_check,\n notify,\n):\n \"\"\"\n Executor for `globus delete`\n \"\"\"\n endpoint_id, path = endpoint_plus_path\n if path is None and (not batch):\n raise click.UsageError(\"delete requires either a PATH OR --batch\")\n\n client = get_client()\n\n # attempt to activate unless --skip-activation-check is given\n if not skip_activation_check:\n autoactivate(client, endpoint_id, if_expires_in=60)\n\n delete_data = DeleteData(\n client,\n endpoint_id,\n label=label,\n recursive=recursive,\n ignore_missing=ignore_missing,\n submission_id=submission_id,\n deadline=deadline,\n skip_activation_check=skip_activation_check,\n interpret_globs=enable_globs,\n **notify\n )\n\n if batch:\n # although this sophisticated structure (like that in transfer)\n # isn't strictly necessary, it gives us the ability to add options in\n # the future to these lines with trivial modifications\n @click.command()\n @click.argument(\"path\", type=TaskPath(base_dir=path))\n def process_batch_line(path):\n \"\"\"\n Parse a line of batch input and add it to the delete submission\n item.\n \"\"\"\n delete_data.add_item(str(path))\n\n shlex_process_stdin(process_batch_line, \"Enter paths to delete, line by line.\")\n else:\n if not star_silent and enable_globs and path.endswith(\"*\"):\n # not intuitive, but `click.confirm(abort=True)` prints to stdout\n # unnecessarily, which we don't really want...\n # only do this check if stderr is a pty\n if (\n err_is_terminal()\n and term_is_interactive()\n and not click.confirm(\n 'Are you sure you want to delete all files matching \"{}\"?'.format(\n path\n ),\n err=True,\n )\n ):\n safeprint(\"Aborted.\", write_to_stderr=True)\n click.get_current_context().exit(1)\n delete_data.add_item(path)\n\n if dry_run:\n formatted_print(delete_data, response_key=\"DATA\", fields=[(\"Path\", \"path\")])\n # exit safely\n return\n\n res = client.submit_delete(delete_data)\n formatted_print(\n res,\n text_format=FORMAT_TEXT_RECORD,\n fields=((\"Message\", \"message\"), (\"Task ID\", \"task_id\")),\n )", "response": "Executor for globus delete"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_command(value, parameter):\n conf = get_config_obj()\n\n section = \"cli\"\n if \".\" in parameter:\n section, parameter = parameter.split(\".\", 1)\n\n # ensure that the section exists\n if section not in conf:\n conf[section] = {}\n # set the value for the given parameter\n conf[section][parameter] = value\n\n # write to disk\n safeprint(\"Writing updated config to {}\".format(conf.filename))\n conf.write()", "response": "Executor for globus config set"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprocesses the stdin line by line.", "response": "def shlex_process_stdin(process_command, helptext):\n \"\"\"\n Use shlex to process stdin line-by-line.\n Also prints help text.\n\n Requires that @process_command be a Click command object, used for\n processing single lines of input. helptext is prepended to the standard\n message printed to interactive sessions.\n \"\"\"\n # if input is interactive, print help to stderr\n if sys.stdin.isatty():\n safeprint(\n (\n \"{}\\n\".format(helptext) + \"Lines are split with shlex in POSIX mode: \"\n \"https://docs.python.org/library/shlex.html#parsing-rules\\n\"\n \"Terminate input with Ctrl+D or \\n\"\n ),\n write_to_stderr=True,\n )\n\n # use readlines() rather than implicit file read line looping to force\n # python to properly capture EOF (otherwise, EOF acts as a flush and\n # things get weird)\n for line in sys.stdin.readlines():\n # get the argument vector:\n # do a shlex split to handle quoted paths with spaces in them\n # also lets us have comments with #\n argv = shlex.split(line, comments=True)\n if argv:\n try:\n process_command.main(args=argv)\n except SystemExit as e:\n if e.code != 0:\n raise"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef globus_group(*args, **kwargs):\n\n def inner_decorator(f):\n f = click.group(*args, cls=GlobusCommandGroup, **kwargs)(f)\n f = common_options(f)\n return f\n\n return inner_decorator", "response": "Decorator for globus group"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_package_data():\n moddata = []\n modlist = (\n \"click\",\n \"configobj\",\n \"cryptography\",\n \"globus_cli\",\n \"globus_sdk\",\n \"jmespath\",\n \"requests\",\n \"six\",\n )\n if verbosity() < 2:\n modlist = (\"globus_cli\", \"globus_sdk\", \"requests\")\n\n for mod in modlist:\n cur = [mod]\n try:\n loaded_mod = __import__(mod)\n except ImportError:\n loaded_mod = None\n\n for attr in (\"__version__\", \"__file__\", \"__path__\"):\n # if loading failed, be sure to pad with error messages\n if loaded_mod is None:\n cur.append(\"[import failed]\")\n continue\n\n try:\n attrval = getattr(loaded_mod, attr)\n except AttributeError:\n attrval = \"\"\n cur.append(attrval)\n moddata.append(cur)\n\n return moddata", "response": "Imports a set of important packages and returns relevant data about them in a\n dict."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef print_version():\n latest, current = get_versions()\n if latest is None:\n safeprint(\n (\"Installed Version: {0}\\n\" \"Failed to lookup latest version.\").format(\n current\n )\n )\n else:\n safeprint(\n (\"Installed Version: {0}\\n\" \"Latest Version: {1}\\n\" \"\\n{2}\").format(\n current,\n latest,\n \"You are running the latest version of the Globus CLI\"\n if current == latest\n else (\n \"You should update your version of the Globus CLI with\\n\"\n \" globus update\"\n )\n if current < latest\n else \"You are running a preview version of the Globus CLI\",\n )\n )\n\n # verbose shows more platform and python info\n # it also includes versions of some CLI dependencies\n if is_verbose():\n moddata = _get_package_data()\n\n safeprint(\"\\nVerbose Data\\n---\")\n\n safeprint(\"platform:\")\n safeprint(\" platform: {}\".format(platform.platform()))\n safeprint(\" py_implementation: {}\".format(platform.python_implementation()))\n safeprint(\" py_version: {}\".format(platform.python_version()))\n safeprint(\" sys.executable: {}\".format(sys.executable))\n safeprint(\" site.USER_BASE: {}\".format(site.USER_BASE))\n\n safeprint(\"modules:\")\n for mod, modversion, modfile, modpath in moddata:\n safeprint(\" {}:\".format(mod))\n safeprint(\" __version__: {}\".format(modversion))\n safeprint(\" __file__: {}\".format(modfile))\n safeprint(\" __path__: {}\".format(modpath))", "response": "Print out the current version of the Globus CLI."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ninvoke pip and print the output.", "response": "def _call_pip(*args):\n \"\"\"\n Invoke pip *safely* and in the *supported* way:\n https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program\n \"\"\"\n all_args = [sys.executable, \"-m\", \"pip\"] + list(args)\n print(\"> {}\".format(\" \".join(all_args)))\n subprocess.check_call(all_args)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _check_pip_installed():\n try:\n subprocess.check_output(\n [sys.executable, \"-m\", \"pip\", \"--version\"], stderr=subprocess.STDOUT\n )\n return True\n except subprocess.CalledProcessError:\n return False", "response": "Check if pip is installed and return True if it is installed False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_command(yes, development, development_version):\n # enforce that pip MUST be installed\n # Why not just include it in the setup.py requirements? Mostly weak\n # reasons, but it shouldn't matter much.\n # - if someone has installed the CLI without pip, then they haven't\n # followed our install instructions, so it's mostly a non-issue\n # - we don't want to have `pip install -U globus-cli` upgrade pip -- that's\n # a little bit invasive and easy to do by accident on modern versions of\n # pip where `--upgrade-strategy` defaults to `eager`\n # - we may want to do distributions in the future with dependencies baked\n # into a package, but we'd never want to do that with pip. More changes\n # would be needed to support that use-case, which we've discussed, but\n # not depending directly on pip gives us a better escape hatch\n # - if we depend on pip, we need to start thinking about what versions we\n # support. In point of fact, that becomes an issue as soon as we add this\n # command, but not being explicit about it lets us punt for now (maybe\n # indefinitely) on figuring out version requirements. All of that is to\n # say: not including it is bad, and from that badness we reap the rewards\n # of procrastination and non-explicit requirements\n # - Advanced usage, like `pip install -t` can produce an installed version\n # of the CLI which can't import its installing `pip`. If we depend on\n # pip, anyone doing this is forced to get two copies of pip, which seems\n # kind of nasty (even if \"they're asking for it\")\n if not _check_pip_installed():\n safeprint(\"`globus update` requires pip. \" \"Please install pip and try again\")\n click.get_current_context().exit(1)\n\n # --development-version implies --development\n development = development or (development_version is not None)\n\n # if we're running with `--development`, then the target version is a\n # tarball from GitHub, and we can skip out on the safety checks\n if development:\n # default to master\n development_version = development_version or \"master\"\n target_version = (\n \"https://github.com/globus/globus-cli/archive/{}\" \".tar.gz#egg=globus-cli\"\n ).format(development_version)\n else:\n # lookup version from PyPi, abort if we can't get it\n latest, current = get_versions()\n if latest is None:\n safeprint(\"Failed to lookup latest version. Aborting.\")\n click.get_current_context().exit(1)\n\n # in the case where we're already up to date, do nothing and exit\n if current == latest:\n safeprint(\"You are already running the latest version: {}\".format(current))\n return\n\n # if we're up to date (or ahead, meaning a dev version was installed)\n # then prompt before continuing, respecting `--yes`\n else:\n safeprint(\n (\n \"You are already running version {0}\\n\"\n \"The latest version is {1}\"\n ).format(current, latest)\n )\n if not yes and (\n not click.confirm(\"Continue with the upgrade?\", default=True)\n ):\n click.get_current_context().exit(1)\n\n # if we make it through to here, it means we didn't hit any safe (or\n # unsafe) abort conditions, so set the target version for upgrade to\n # the latest\n target_version = \"globus-cli=={}\".format(latest)\n\n # print verbose warning/help message, to guide less fortunate souls who hit\n # Ctrl+C at a foolish time, lose connectivity, or don't invoke with `sudo`\n # on a global install of the CLI\n safeprint(\n (\n \"The Globus CLI will now update itself.\\n\"\n \"In the event that an error occurs or the update is interrupted, we \"\n \"recommend uninstalling and reinstalling the CLI.\\n\"\n \"Update Target: {}\\n\"\n ).format(target_version)\n )\n\n # register the upgrade activity as an atexit function\n # this ensures that most library teardown (other than whatever libs might\n # jam into atexit themselves...) has already run, and therefore protects us\n # against most potential bugs resulting from upgrading click while a click\n # command is running\n #\n # NOTE: there is a risk that we will see bugs on upgrade if the act of\n # doing a pip upgrade install changes state on disk and we (or a lib we\n # use) rely on that via pkg_resources, lazy/deferred imports, or good\n # old-fashioned direct inspection of `__file__` and the like DURING an\n # atexit method. Anything outside of atexit methods remains safe!\n @atexit.register\n def do_upgrade():\n install_args = [\"install\", \"--upgrade\", target_version]\n if IS_USER_INSTALL:\n install_args.insert(1, \"--user\")\n _call_pip(*install_args)", "response": "Executor for globus update"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef whoami_command(linked_identities):\n client = get_auth_client()\n\n # get userinfo from auth.\n # if we get back an error the user likely needs to log in again\n try:\n res = client.oauth2_userinfo()\n except AuthAPIError:\n safeprint(\n \"Unable to get user information. Please try \" \"logging in again.\",\n write_to_stderr=True,\n )\n click.get_current_context().exit(1)\n\n print_command_hint(\n \"For information on which identities are in session see\\n\"\n \" globus session show\\n\"\n )\n\n # --linked-identities either displays all usernames or a table if verbose\n if linked_identities:\n try:\n formatted_print(\n res[\"identity_set\"],\n fields=[\n (\"Username\", \"username\"),\n (\"Name\", \"name\"),\n (\"ID\", \"sub\"),\n (\"Email\", \"email\"),\n ],\n simple_text=(\n None\n if is_verbose()\n else \"\\n\".join([x[\"username\"] for x in res[\"identity_set\"]])\n ),\n )\n except KeyError:\n safeprint(\n \"Your current login does not have the consents required \"\n \"to view your full identity set. Please log in again \"\n \"to agree to the required consents.\",\n write_to_stderr=True,\n )\n\n # Default output is the top level data\n else:\n formatted_print(\n res,\n text_format=FORMAT_TEXT_RECORD,\n fields=[\n (\"Username\", \"preferred_username\"),\n (\"Name\", \"name\"),\n (\"ID\", \"sub\"),\n (\"Email\", \"email\"),\n ],\n simple_text=(None if is_verbose() else res[\"preferred_username\"]),\n )", "response": "Executor for globus whoami"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwalks the tree of commands to a terminal command or multicommand and returns a new context object that can be used to perform completion work on that command.", "response": "def get_completion_context(args):\n \"\"\"\n Walk the tree of commands to a terminal command or multicommand, using the\n Click Context system.\n Effectively, we'll be using the resilient_parsing mode of commands to stop\n evaluation, then having them capture their options and arguments, passing\n us on to the next subcommand. If we walk \"off the tree\" with a command that\n we don't recognize, we have a hardstop condition, but otherwise, we walk as\n far as we can go and that's the location from which we should do our\n completion work.\n \"\"\"\n # get the \"globus\" command as a click.Command\n root_command = click.get_current_context().find_root().command\n # build a new context object off of it, with resilient_parsing set so that\n # no callbacks are invoked\n ctx = root_command.make_context(\"globus\", list(args), resilient_parsing=True)\n\n # walk down multicommands until we've matched on everything and are at a\n # terminal context that holds all of our completed args\n while isinstance(ctx.command, click.MultiCommand) and args:\n # trim out any params that are capturable at this level of the command\n # tree by resetting the argument list\n args = ctx.protected_args + ctx.args\n\n # if there were no remaining args, stop walking the tree\n if not args:\n break\n\n # check for a matching command, and if one isn't found stop the\n # traversal and abort the whole process -- this would mean that a\n # completed command was entered which doesn't match a known command\n # there's nothing completion can do in this case unless it implements\n # sophisticated fuzzy matching\n command = ctx.command.get_command(ctx, args[0])\n if not command:\n return None\n\n # otherwise, grab that command, and build a subcontext to continue the\n # tree walk\n else:\n ctx = command.make_context(\n args[0], args[1:], parent=ctx, resilient_parsing=True\n )\n\n # return the context we found\n return ctx"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_all_choices(completed_args, cur, quoted):\n ctx = get_completion_context(completed_args)\n if not ctx:\n return []\n\n # matching rules, so we can toggle by type and such\n def match_with_case(n, m):\n \"\"\"case matters\"\"\"\n return n.startswith(m)\n\n def match_nocase(n, m):\n \"\"\"case doesn't matter\"\"\"\n return n.lower().startswith(m.lower())\n\n match_func = match_with_case\n\n ctx_options = [\n p for p in ctx.command.get_params(ctx) if isinstance(p, click.Option)\n ]\n\n last_completed = None\n if completed_args:\n last_completed = completed_args[-1]\n\n # if the last completed argument matches a Choice option, we're going to\n # have to expand cur as a choice param\n matching_choice_opt = None\n for p in ctx_options:\n if isinstance(p.type, click.Choice) and last_completed in p.opts:\n matching_choice_opt = p\n\n choices = []\n # if we ended on a choice, complete with all of the available values\n if matching_choice_opt:\n # catch the case where it's case insensitive, and we need to change our\n # comparisons / matching later on\n if isinstance(matching_choice_opt.type, CaseInsensitiveChoice):\n match_func = match_nocase\n choices = [\n (x, matching_choice_opt.help) for x in matching_choice_opt.type.choices\n ]\n # if cur looks like an option, just look for options\n # but skip if it's quoted text\n elif cur and cur.startswith(\"-\") and not quoted:\n for param in ctx_options:\n # skip hidden options\n if isinstance(param, HiddenOption):\n continue\n for optset in (param.opts, param.secondary_opts):\n for opt in optset:\n # only add long-opts, never short opts to completion,\n # unless the cur appears to be a short opt already\n if opt.startswith(\"--\") or (len(cur) > 1 and cur[1] != \"-\"):\n choices.append((opt, param.help))\n # and if it's a multicommand we see, get the list of subcommands\n elif isinstance(ctx.command, click.MultiCommand) and not quoted:\n choices = [\n (cmdname, ctx.command.get_command(ctx, cmdname).short_help)\n for cmdname in ctx.command.list_commands(ctx)\n ]\n else:\n pass\n\n # now, do final filtering\n if cur:\n choices = [(n, h) for (n, h) in choices if match_func(n, cur)]\n\n return choices", "response": "This function returns all available choices for the current word in progress."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef endpoint_is_activated(endpoint_id, until, absolute_time):\n client = get_client()\n res = client.endpoint_get_activation_requirements(endpoint_id)\n\n def fail(deadline=None):\n exp_string = \"\"\n if deadline is not None:\n exp_string = \" or will expire within {} seconds\".format(deadline)\n\n message = \"The endpoint is not activated{}.\\n\\n\".format(\n exp_string\n ) + activation_requirements_help_text(res, endpoint_id)\n formatted_print(res, simple_text=message)\n click.get_current_context().exit(1)\n\n def success(msg, *format_params):\n formatted_print(res, simple_text=(msg.format(endpoint_id, *format_params)))\n click.get_current_context().exit(0)\n\n # eternally active endpoints have a special expires_in value\n if res[\"expires_in\"] == -1:\n success(\"{} does not require activation\")\n\n # autoactivation is not supported and --until was not passed\n if until is None:\n # and we are active right now (0s in the future)...\n if res.active_until(0):\n success(\"{} is activated\")\n # or we are not active\n fail()\n\n # autoactivation is not supported and --until was passed\n if res.active_until(until, relative_time=not absolute_time):\n success(\"{} will be active for at least {} seconds\", until)\n else:\n fail(deadline=until)", "response": "Executor for globus endpoint is - activated"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef endpoint_create(**kwargs):\n client = get_client()\n\n # get endpoint type, ensure unambiguous.\n personal = kwargs.pop(\"personal\")\n server = kwargs.pop(\"server\")\n shared = kwargs.pop(\"shared\")\n\n if personal and (not server) and (not shared):\n endpoint_type = \"personal\"\n elif server and (not personal) and (not shared):\n endpoint_type = \"server\"\n elif shared and (not personal) and (not server):\n endpoint_type = \"shared\"\n else:\n raise click.UsageError(\n \"Exactly one of --personal, --server, or --shared is required.\"\n )\n\n # validate options\n kwargs[\"is_globus_connect\"] = personal or None\n validate_endpoint_create_and_update_params(endpoint_type, False, kwargs)\n\n # shared endpoint creation\n if shared:\n endpoint_id, host_path = shared\n kwargs[\"host_endpoint\"] = endpoint_id\n kwargs[\"host_path\"] = host_path\n\n ep_doc = assemble_generic_doc(\"shared_endpoint\", **kwargs)\n autoactivate(client, endpoint_id, if_expires_in=60)\n res = client.create_shared_endpoint(ep_doc)\n\n # non shared endpoint creation\n else:\n # omit `is_globus_connect` key if not GCP, otherwise include as `True`\n ep_doc = assemble_generic_doc(\"endpoint\", **kwargs)\n res = client.create_endpoint(ep_doc)\n\n # output\n formatted_print(\n res,\n fields=(COMMON_FIELDS + GCP_FIELDS if personal else COMMON_FIELDS),\n text_format=FORMAT_TEXT_RECORD,\n )", "response": "Executor for globus endpoint create"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef server_update(\n endpoint_id,\n server_id,\n subject,\n port,\n scheme,\n hostname,\n incoming_data_ports,\n outgoing_data_ports,\n):\n \"\"\"\n Executor for `globus endpoint server update`\n \"\"\"\n client = get_client()\n\n server_doc = assemble_generic_doc(\n \"server\", subject=subject, port=port, scheme=scheme, hostname=hostname\n )\n\n # n.b. must be done after assemble_generic_doc(), as that function filters\n # out `None`s, which we need to be able to set for `'unspecified'`\n if incoming_data_ports:\n server_doc.update(\n incoming_data_port_start=incoming_data_ports[0],\n incoming_data_port_end=incoming_data_ports[1],\n )\n if outgoing_data_ports:\n server_doc.update(\n outgoing_data_port_start=outgoing_data_ports[0],\n outgoing_data_port_end=outgoing_data_ports[1],\n )\n\n res = client.update_endpoint_server(endpoint_id, server_id, server_doc)\n formatted_print(res, text_format=FORMAT_TEXT_RAW, response_key=\"message\")", "response": "Executor for globus endpoint server update"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntry to decode a b32 - encoded username.", "response": "def _try_b32_decode(v):\n \"\"\"\n Attempt to decode a b32-encoded username which is sometimes generated by\n internal Globus components.\n\n The expectation is that the string is a valid ID, username, or b32-encoded\n name. Therefore, we can do some simple checking on it.\n\n If it does not appear to be formatted correctly, return None.\n \"\"\"\n # should start with \"u_\"\n if not v.startswith(\"u_\"):\n return None\n # usernames have @ , we want to allow `u_foo@example.com`\n # b32 names never have @\n if \"@\" in v:\n return None\n # trim \"u_\"\n v = v[2:]\n # wrong length\n if len(v) != 26:\n return None\n\n # append padding and uppercase so that b32decode will work\n v = v.upper() + (6 * \"=\")\n\n # try to decode\n try:\n return str(uuid.UUID(bytes=base64.b32decode(v)))\n # if it fails, I guess it's a username? Not much left to do\n except ValueError:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef supported_activation_methods(res):\n supported = [\"web\"] # web activation is always supported.\n\n # oauth\n if res[\"oauth_server\"]:\n supported.append(\"oauth\")\n\n for req in res[\"DATA\"]:\n # myproxy\n if (\n req[\"type\"] == \"myproxy\"\n and req[\"name\"] == \"hostname\"\n and req[\"value\"] != \"myproxy.globusonline.org\"\n ):\n supported.append(\"myproxy\")\n\n # delegate_proxy\n if req[\"type\"] == \"delegate_proxy\" and req[\"name\"] == \"public_key\":\n supported.append(\"delegate_proxy\")\n\n return supported", "response": "Given an activation_requirements document returns a list of activation methods supported by this endpoint."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngive an activation requirements document and an endpoint_id returns a string of help text for how to activate the endpoint", "response": "def activation_requirements_help_text(res, ep_id):\n \"\"\"\n Given an activation requirements document and an endpoint_id\n returns a string of help text for how to activate the endpoint\n \"\"\"\n methods = supported_activation_methods(res)\n\n lines = [\n \"This endpoint supports the following activation methods: \",\n \", \".join(methods).replace(\"_\", \" \"),\n \"\\n\",\n (\n \"For web activation use:\\n\"\n \"'globus endpoint activate --web {}'\\n\".format(ep_id)\n if \"web\" in methods\n else \"\"\n ),\n (\n \"For myproxy activation use:\\n\"\n \"'globus endpoint activate --myproxy {}'\\n\".format(ep_id)\n if \"myproxy\" in methods\n else \"\"\n ),\n (\n \"For oauth activation use web activation:\\n\"\n \"'globus endpoint activate --web {}'\\n\".format(ep_id)\n if \"oauth\" in methods\n else \"\"\n ),\n (\n \"For delegate proxy activation use:\\n\"\n \"'globus endpoint activate --delegate-proxy \"\n \"X.509_PEM_FILE {}'\\n\".format(ep_id)\n if \"delegate_proxy\" in methods\n else \"\"\n ),\n (\n \"Delegate proxy activation requires an additional dependency on \"\n \"cryptography. See the docs for details:\\n\"\n \"https://docs.globus.org/cli/reference/endpoint_activate/\\n\"\n if \"delegate_proxy\" in methods\n else \"\"\n ),\n ]\n\n return \"\".join(lines)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nattempting to auto - activate the given endpoint with the given client", "response": "def autoactivate(client, endpoint_id, if_expires_in=None):\n \"\"\"\n Attempts to auto-activate the given endpoint with the given client\n If auto-activation fails, parses the returned activation requirements\n to determine which methods of activation are supported, then tells\n the user to use 'globus endpoint activate' with the correct options(s)\n \"\"\"\n kwargs = {}\n if if_expires_in is not None:\n kwargs[\"if_expires_in\"] = if_expires_in\n\n res = client.endpoint_autoactivate(endpoint_id, **kwargs)\n if res[\"code\"] == \"AutoActivationFailed\":\n\n message = (\n \"The endpoint could not be auto-activated and must be \"\n \"activated before it can be used.\\n\\n\"\n + activation_requirements_help_text(res, endpoint_id)\n )\n\n safeprint(message, write_to_stderr=True)\n click.get_current_context().exit(1)\n\n else:\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of server list for the given endpoint.", "response": "def get_endpoint_w_server_list(endpoint_id):\n \"\"\"\n A helper for handling endpoint server list lookups correctly accounting\n for various endpoint types.\n\n - Raises click.UsageError when used on Shares\n - Returns (, \"S3\") for S3 endpoints\n - Returns (, ) for all other\n Endpoints\n \"\"\"\n client = get_client()\n\n endpoint = client.get_endpoint(endpoint_id)\n\n if endpoint[\"host_endpoint_id\"]: # not GCS -- this is a share endpoint\n raise click.UsageError(\n dedent(\n u\"\"\"\\\n {id} ({0}) is a share and does not have servers.\n\n To see details of the share, use\n globus endpoint show {id}\n\n To list the servers on the share's host endpoint, use\n globus endpoint server list {host_endpoint_id}\n \"\"\"\n ).format(display_name_or_cname(endpoint), **endpoint.data)\n )\n\n if endpoint[\"s3_url\"]: # not GCS -- legacy S3 endpoint type\n return (endpoint, \"S3\")\n\n else:\n return (endpoint, client.endpoint_server_list(endpoint_id))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\noptions are the core \"task wait\" options, including the `--meow` easter egg. This does the core \"task wait\" loop, including all of the IO. It *does exit* on behalf of the caller. (We can enhance with a `noabort=True` param or somesuch in the future if necessary.)", "response": "def task_wait_with_io(\n meow, heartbeat, polling_interval, timeout, task_id, timeout_exit_code, client=None\n):\n \"\"\"\n Options are the core \"task wait\" options, including the `--meow` easter\n egg.\n\n This does the core \"task wait\" loop, including all of the IO.\n It *does exit* on behalf of the caller. (We can enhance with a\n `noabort=True` param or somesuch in the future if necessary.)\n \"\"\"\n client = client or get_client()\n\n def timed_out(waited_time):\n if timeout is None:\n return False\n else:\n return waited_time >= timeout\n\n def check_completed():\n completed = client.task_wait(\n task_id, timeout=polling_interval, polling_interval=polling_interval\n )\n if completed:\n if heartbeat:\n safeprint(\"\", write_to_stderr=True)\n # meowing tasks wake up!\n if meow:\n safeprint(\n r\"\"\"\n _..\n /}_{\\ /.-'\n ( a a )-.___...-'/\n ==._.== ;\n \\ i _..._ /,\n {_;/ {_//\"\"\",\n write_to_stderr=True,\n )\n\n # TODO: possibly update TransferClient.task_wait so that we don't\n # need to do an extra fetch to get the task status after completion\n res = client.get_task(task_id)\n formatted_print(res, text_format=FORMAT_SILENT)\n\n status = res[\"status\"]\n if status == \"SUCCEEDED\":\n click.get_current_context().exit(0)\n else:\n click.get_current_context().exit(1)\n\n return completed\n\n # Tasks start out sleepy\n if meow:\n safeprint(\n r\"\"\"\n |\\ _,,,---,,_\n /,`.-'`' -. ;-;;,_\n |,4- ) )-,_..;\\ ( `'-'\n '---''(_/--' `-'\\_)\"\"\",\n write_to_stderr=True,\n )\n\n waited_time = 0\n while not timed_out(waited_time) and not check_completed():\n if heartbeat:\n safeprint(\".\", write_to_stderr=True, newline=False)\n sys.stderr.flush()\n\n waited_time += polling_interval\n\n # add a trailing newline to heartbeats if we fail\n if heartbeat:\n safeprint(\"\", write_to_stderr=True)\n\n exit_code = 1\n if timed_out(waited_time):\n safeprint(\n \"Task has yet to complete after {} seconds\".format(timeout),\n write_to_stderr=True,\n )\n exit_code = timeout_exit_code\n\n # output json if requested, but nothing for text mode\n res = client.get_task(task_id)\n formatted_print(res, text_format=FORMAT_SILENT)\n\n click.get_current_context().exit(exit_code)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntry the given function f with the given arguments and kwargs.", "response": "def retry(self, f, *args, **kwargs):\n \"\"\"\n Retries the given function self.tries times on NetworkErros\n \"\"\"\n backoff = random.random() / 100 # 5ms on average\n for _ in range(self.tries - 1):\n try:\n return f(*args, **kwargs)\n except NetworkError:\n time.sleep(backoff)\n backoff *= 2\n return f(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef recursive_operation_ls(\n self, endpoint_id, depth=3, filter_after_first=True, **params\n ):\n \"\"\"\n Makes recursive calls to ``GET /operation/endpoint//ls``\n Does not preserve access to top level operation_ls fields, but\n adds a \"path\" field for every item that represents the full\n path to that item.\n :rtype: iterable of :class:`GlobusResponse\n `\n **Parameters**\n ``endpoint_id`` (*string*)\n The endpoint being recursively ls'ed. If no \"path\" is given in\n params, the start path is determined by this endpoint.\n ``depth`` (*int*)\n The maximum file depth the recursive ls will go to.\n ``filter_after_first`` (*bool*)\n If False, any \"filter\" in params will only be applied to the\n first, top level ls, all results beyond that will be unfiltered.\n ``params``\n Parameters that will be passed through as query params.\n **Examples**\n >>> tc = globus_sdk.TransferClient(...)\n >>> for entry in tc.recursive_operation_ls(ep_id, path=\"/~/project1/\"):\n >>> print(entry[\"path\"], entry[\"type\"])\n **External Documentation**\n See\n `List Directory Contents \\\n `_\n in the REST documentation for details, but note that top level data\n fields are no longer available and an additional per item\n \"path\" field is added.\n \"\"\"\n endpoint_id = safe_stringify(endpoint_id)\n self.logger.info(\n \"TransferClient.recursive_operation_ls({}, {}, {})\".format(\n endpoint_id, depth, params\n )\n )\n return RecursiveLsResponse(self, endpoint_id, depth, filter_after_first, params)", "response": "This method is used to recursively ls the directory contents of a file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef convert(self, value, param, ctx):\n # passthrough conditions: None or already processed\n if value is None or isinstance(value, tuple):\n return value\n\n # split the value on the first colon, leave the rest intact\n splitval = value.split(\":\", 1)\n # first element is the endpoint_id\n endpoint_id = click.UUID(splitval[0])\n\n # get the second element, defaulting to `None` if there was no colon in\n # the original value\n try:\n path = splitval[1]\n except IndexError:\n path = None\n # coerce path=\"\" to path=None\n # means that we treat \"enpdoint_id\" and \"endpoint_id:\" equivalently\n path = path or None\n\n if path is None and self.path_required:\n self.fail(\"The path component is required\", param=param)\n\n return (endpoint_id, path)", "response": "Convert a value to a tuple."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef one_use_option(*args, **kwargs):\n # cannot force a multiple or count option to be single use\n if \"multiple\" in kwargs or \"count\" in kwargs:\n raise ValueError(\n \"Internal error, one_use_option cannot be used \" \"with multiple or count.\"\n )\n\n # cannot force a non Option Paramater (argument) to be a OneUseOption\n if kwargs.get(\"cls\"):\n raise TypeError(\n \"Internal error, one_use_option cannot overwrite \"\n \"cls {}.\".format(kwargs.get(\"cls\"))\n )\n\n # use our OneUseOption class instead of a normal Option\n kwargs[\"cls\"] = OneUseOption\n\n # if dealing with a flag, switch to a counting option,\n # and then assert if the count is not greater than 1 and cast to a bool\n if kwargs.get(\"is_flag\"):\n kwargs[\"is_flag\"] = False # mutually exclusive with count\n kwargs[\"count\"] = True\n\n # if not a flag, this option takes an argument(s), switch to a multiple\n # option, assert the len is 1, and treat the first element as the value\n else:\n kwargs[\"multiple\"] = True\n\n # decorate with the click.option decorator, but with our custom kwargs\n def decorator(f):\n return click.option(*args, **kwargs)(f)\n\n return decorator", "response": "Decorator that replaces any instances of the option class with the custom OneUseOption class."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting a server specification to a list of matching server docs.", "response": "def _spec_to_matches(server_list, server_spec, mode):\n \"\"\"\n mode is in {uri, hostname, hostname_port}\n\n A list of matching server docs.\n Should usually be 0 or 1 matches. Multiple matches are possible though.\n \"\"\"\n assert mode in (\"uri\", \"hostname\", \"hostname_port\")\n\n def match(server_doc):\n if mode == \"hostname\":\n return server_spec == server_doc[\"hostname\"]\n elif mode == \"hostname_port\":\n return server_spec == \"{}:{}\".format(\n server_doc[\"hostname\"], server_doc[\"port\"]\n )\n elif mode == \"uri\":\n return server_spec == \"{}://{}:{}\".format(\n server_doc[\"scheme\"], server_doc[\"hostname\"], server_doc[\"port\"]\n )\n else:\n raise NotImplementedError(\"Unreachable error! Something is very wrong.\")\n\n return [server_doc for server_doc in server_list if match(server_doc)]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef server_delete(endpoint_id, server):\n client = get_client()\n\n mode = _detect_mode(server)\n\n # list (even if not necessary) in order to make errors more consistent when\n # mode='id'\n endpoint, server_list = get_endpoint_w_server_list(endpoint_id)\n\n if server_list == \"S3\":\n raise click.UsageError(\"You cannot delete servers from S3 endpoints.\")\n\n # we don't *have to* raise an error in the GCP case, since the API would\n # deny it too, but doing so makes our errors a little bit more consistent\n # with deletes against S3 endpoints and shares\n if endpoint[\"is_globus_connect\"]:\n raise click.UsageError(\n \"You cannot delete servers from Globus Connect Personal endpoints\"\n )\n\n if mode != \"id\":\n matches = _spec_to_matches(server_list, server, mode)\n if not matches:\n raise click.UsageError('No server was found matching \"{}\"'.format(server))\n elif len(matches) > 1:\n raise click.UsageError(\n dedent(\n \"\"\"\\\n Multiple servers matched \"{}\":\n {}\n \"\"\"\n ).format(server, [x[\"id\"] for x in matches])\n )\n else:\n server = matches[0][\"id\"]\n\n response = client.delete_endpoint_server(endpoint_id, server)\n\n formatted_print(response, text_format=FORMAT_TEXT_RAW, response_key=\"message\")", "response": "Executor for globus endpoint server delete"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef show_command(endpoint_id, rule_id):\n client = get_client()\n\n rule = client.get_endpoint_acl_rule(endpoint_id, rule_id)\n formatted_print(\n rule,\n text_format=FORMAT_TEXT_RECORD,\n fields=(\n (\"Rule ID\", \"id\"),\n (\"Permissions\", \"permissions\"),\n (\"Shared With\", _shared_with_keyfunc),\n (\"Path\", \"path\"),\n ),\n )", "response": "Executor for globus endpoint permission show"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef show_command(parameter):\n section = \"cli\"\n if \".\" in parameter:\n section, parameter = parameter.split(\".\", 1)\n\n value = lookup_option(parameter, section=section)\n\n if value is None:\n safeprint(\"{} not set\".format(parameter))\n else:\n safeprint(\"{} = {}\".format(parameter, value))", "response": "Executor for globus config show"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rename_command(source, destination):\n source_ep, source_path = source\n dest_ep, dest_path = destination\n\n if source_ep != dest_ep:\n raise click.UsageError(\n (\n \"rename requires that the source and dest \"\n \"endpoints are the same, {} != {}\"\n ).format(source_ep, dest_ep)\n )\n endpoint_id = source_ep\n\n client = get_client()\n autoactivate(client, endpoint_id, if_expires_in=60)\n\n res = client.operation_rename(endpoint_id, oldpath=source_path, newpath=dest_path)\n formatted_print(res, text_format=FORMAT_TEXT_RAW, response_key=\"message\")", "response": "Executor for globus rename"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _format_value(self, val):\n name = self.name + \":\"\n if not self.multiline or \"\\n\" not in val:\n val = u\"{0} {1}\".format(name.ljust(self._text_prefix_len), val)\n else:\n spacer = \"\\n\" + \" \" * (self._text_prefix_len + 1)\n val = u\"{0}{1}{2}\".format(name, spacer, spacer.join(val.split(\"\\n\")))\n\n return val", "response": "Formats a value to be good for textmode printing\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_command(endpoint_id, rule_id):\n client = get_client()\n\n res = client.delete_endpoint_acl_rule(endpoint_id, rule_id)\n formatted_print(res, text_format=FORMAT_TEXT_RAW, response_key=\"message\")", "response": "Executor for globus endpoint permission delete"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef transfer_command(\n batch,\n sync_level,\n recursive,\n destination,\n source,\n label,\n preserve_mtime,\n verify_checksum,\n encrypt,\n submission_id,\n dry_run,\n delete,\n deadline,\n skip_activation_check,\n notify,\n perf_cc,\n perf_p,\n perf_pp,\n perf_udt,\n):\n \"\"\"\n Executor for `globus transfer`\n \"\"\"\n source_endpoint, cmd_source_path = source\n dest_endpoint, cmd_dest_path = destination\n\n if recursive and batch:\n raise click.UsageError(\n (\n \"You cannot use --recursive in addition to --batch. \"\n \"Instead, use --recursive on lines of --batch input \"\n \"which need it\"\n )\n )\n\n if (cmd_source_path is None or cmd_dest_path is None) and (not batch):\n raise click.UsageError(\n (\"transfer requires either SOURCE_PATH and DEST_PATH or \" \"--batch\")\n )\n\n # because python can't handle multiple **kwargs expansions in a single\n # call, we need to get a little bit clever\n # both the performance options (of which there are a few), and the\n # notification options (also there are a few) have elements which should be\n # omitted in some cases\n # notify comes to us clean, perf opts need more care\n # put them together into a dict before passing to TransferData\n kwargs = {}\n perf_opts = dict(\n (k, v)\n for (k, v) in dict(\n perf_cc=perf_cc, perf_p=perf_p, perf_pp=perf_pp, perf_udt=perf_udt\n ).items()\n if v is not None\n )\n kwargs.update(perf_opts)\n kwargs.update(notify)\n\n client = get_client()\n transfer_data = TransferData(\n client,\n source_endpoint,\n dest_endpoint,\n label=label,\n sync_level=sync_level,\n verify_checksum=verify_checksum,\n preserve_timestamp=preserve_mtime,\n encrypt_data=encrypt,\n submission_id=submission_id,\n delete_destination_extra=delete,\n deadline=deadline,\n skip_activation_check=skip_activation_check,\n **kwargs\n )\n\n if batch:\n\n @click.command()\n @click.option(\"--recursive\", \"-r\", is_flag=True)\n @click.argument(\"source_path\", type=TaskPath(base_dir=cmd_source_path))\n @click.argument(\"dest_path\", type=TaskPath(base_dir=cmd_dest_path))\n def process_batch_line(dest_path, source_path, recursive):\n \"\"\"\n Parse a line of batch input and turn it into a transfer submission\n item.\n \"\"\"\n transfer_data.add_item(\n str(source_path), str(dest_path), recursive=recursive\n )\n\n shlex_process_stdin(\n process_batch_line,\n (\n \"Enter transfers, line by line, as\\n\\n\"\n \" [--recursive] SOURCE_PATH DEST_PATH\\n\"\n ),\n )\n else:\n transfer_data.add_item(cmd_source_path, cmd_dest_path, recursive=recursive)\n\n if dry_run:\n formatted_print(\n transfer_data,\n response_key=\"DATA\",\n fields=(\n (\"Source Path\", \"source_path\"),\n (\"Dest Path\", \"destination_path\"),\n (\"Recursive\", \"recursive\"),\n ),\n )\n # exit safely\n return\n\n # autoactivate after parsing all args and putting things together\n # skip this if skip-activation-check is given\n if not skip_activation_check:\n autoactivate(client, source_endpoint, if_expires_in=60)\n autoactivate(client, dest_endpoint, if_expires_in=60)\n\n res = client.submit_transfer(transfer_data)\n formatted_print(\n res,\n text_format=FORMAT_TEXT_RECORD,\n fields=((\"Message\", \"message\"), (\"Task ID\", \"task_id\")),\n )", "response": "Executor for globus transfer"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef endpoint_delete(endpoint_id):\n client = get_client()\n res = client.delete_endpoint(endpoint_id)\n formatted_print(res, text_format=FORMAT_TEXT_RAW, response_key=\"message\")", "response": "Executor for globus endpoint delete"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init_command(default_output_format, default_myproxy_username):\n # now handle the output format, requires a little bit more care\n # first, prompt if it isn't given, but be clear that we have a sensible\n # default if they don't set it\n # then, make sure that if it is given, it's a valid format (discard\n # otherwise)\n # finally, set it only if given and valid\n if not default_output_format:\n safeprint(\n textwrap.fill(\n 'This must be one of \"json\" or \"text\". Other values will be '\n \"ignored. ENTER to skip.\"\n )\n )\n default_output_format = (\n click.prompt(\n \"Default CLI output format (cli.output_format)\", default=\"text\"\n )\n .strip()\n .lower()\n )\n if default_output_format not in (\"json\", \"text\"):\n default_output_format = None\n\n if not default_myproxy_username:\n safeprint(textwrap.fill(\"ENTER to skip.\"))\n default_myproxy_username = click.prompt(\n \"Default myproxy username (cli.default_myproxy_username)\",\n default=\"\",\n show_default=False,\n ).strip()\n\n # write to disk\n safeprint(\n \"\\n\\nWriting updated config to {0}\".format(os.path.expanduser(\"~/.globus.cfg\"))\n )\n write_option(OUTPUT_FORMAT_OPTNAME, default_output_format)\n write_option(MYPROXY_USERNAME_OPTNAME, default_myproxy_username)", "response": "Executor for globus config init"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef detect_and_decorate(decorator, args, kwargs):\n # special behavior when invoked with only one non-keyword argument: act as\n # a normal decorator, decorating and returning that argument with\n # click.option\n if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):\n return decorator(args[0])\n\n # if we're not doing that, we should see no positional args\n # the alternative behavior is to fall through and discard *args, but this\n # will probably confuse someone in the future when their arguments are\n # silently discarded\n elif len(args) != 0:\n raise ValueError(\"this decorator cannot take positional args\")\n\n # final case: got 0 or more kwargs, no positionals\n # do the function-which-returns-a-decorator dance to produce a\n # new decorator based on the arguments given\n else:\n\n def inner_decorator(f):\n return decorator(f, **kwargs)\n\n return inner_decorator", "response": "Decorator for detecting and decorating a function based on the given arguments and kwargs."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_command(endpoint_id):\n client = get_client()\n\n rules = client.endpoint_acl_list(endpoint_id)\n\n resolved_ids = LazyIdentityMap(\n x[\"principal\"] for x in rules if x[\"principal_type\"] == \"identity\"\n )\n\n def principal_str(rule):\n principal = rule[\"principal\"]\n if rule[\"principal_type\"] == \"identity\":\n username = resolved_ids.get(principal)\n return username or principal\n elif rule[\"principal_type\"] == \"group\":\n return (u\"https://app.globus.org/groups/{}\").format(principal)\n else:\n principal = rule[\"principal_type\"]\n\n return principal\n\n formatted_print(\n rules,\n fields=[\n (\"Rule ID\", \"id\"),\n (\"Permissions\", \"permissions\"),\n (\"Shared With\", principal_str),\n (\"Path\", \"path\"),\n ],\n )", "response": "Executor for globus endpoint permission list"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef bookmark_list():\n client = get_client()\n\n bookmark_iterator = client.bookmark_list()\n\n def get_ep_name(item):\n ep_id = item[\"endpoint_id\"]\n try:\n ep_doc = client.get_endpoint(ep_id)\n return display_name_or_cname(ep_doc)\n except TransferAPIError as err:\n if err.code == \"EndpointDeleted\":\n return \"[DELETED ENDPOINT]\"\n else:\n raise err\n\n formatted_print(\n bookmark_iterator,\n fields=[\n (\"Name\", \"name\"),\n (\"Bookmark ID\", \"id\"),\n (\"Endpoint ID\", \"endpoint_id\"),\n (\"Endpoint Name\", get_ep_name),\n (\"Path\", \"path\"),\n ],\n response_key=\"DATA\",\n json_converter=iterable_response_to_dict,\n )", "response": "Executor for globus bookmark list"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rm_command(\n ignore_missing,\n star_silent,\n recursive,\n enable_globs,\n endpoint_plus_path,\n label,\n submission_id,\n dry_run,\n deadline,\n skip_activation_check,\n notify,\n meow,\n heartbeat,\n polling_interval,\n timeout,\n timeout_exit_code,\n):\n \"\"\"\n Executor for `globus rm`\n \"\"\"\n endpoint_id, path = endpoint_plus_path\n\n client = get_client()\n\n # attempt to activate unless --skip-activation-check is given\n if not skip_activation_check:\n autoactivate(client, endpoint_id, if_expires_in=60)\n\n delete_data = DeleteData(\n client,\n endpoint_id,\n label=label,\n recursive=recursive,\n ignore_missing=ignore_missing,\n submission_id=submission_id,\n deadline=deadline,\n skip_activation_check=skip_activation_check,\n interpret_globs=enable_globs,\n **notify\n )\n\n if not star_silent and enable_globs and path.endswith(\"*\"):\n # not intuitive, but `click.confirm(abort=True)` prints to stdout\n # unnecessarily, which we don't really want...\n # only do this check if stderr is a pty\n if (\n err_is_terminal()\n and term_is_interactive()\n and not click.confirm(\n 'Are you sure you want to delete all files matching \"{}\"?'.format(path),\n err=True,\n )\n ):\n safeprint(\"Aborted.\", write_to_stderr=True)\n click.get_current_context().exit(1)\n delete_data.add_item(path)\n\n if dry_run:\n formatted_print(delete_data, response_key=\"DATA\", fields=[(\"Path\", \"path\")])\n # exit safely\n return\n\n # Print task submission to stderr so that `-Fjson` is still correctly\n # respected, as it will be by `task wait`\n res = client.submit_delete(delete_data)\n task_id = res[\"task_id\"]\n safeprint(\n 'Delete task submitted under ID \"{}\"'.format(task_id), write_to_stderr=True\n )\n\n # do a `task wait` equivalent, including printing and correct exit status\n task_wait_with_io(\n meow,\n heartbeat,\n polling_interval,\n timeout,\n task_id,\n timeout_exit_code,\n client=client,\n )", "response": "Executor for globus rm"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprompt the user with a link to authenticate with globus auth and authorize the CLI to act on their behalf.", "response": "def do_link_auth_flow(session_params=None, force_new_client=False):\n \"\"\"\n Prompts the user with a link to authenticate with globus auth\n and authorize the CLI to act on their behalf.\n \"\"\"\n session_params = session_params or {}\n\n # get the ConfidentialApp client object\n auth_client = internal_auth_client(\n requires_instance=True, force_new_client=force_new_client\n )\n\n # start the Confidential App Grant flow\n auth_client.oauth2_start_flow(\n redirect_uri=auth_client.base_url + \"v2/web/auth-code\",\n refresh_tokens=True,\n requested_scopes=SCOPES,\n )\n\n # prompt\n additional_params = {\"prompt\": \"login\"}\n additional_params.update(session_params)\n linkprompt = \"Please authenticate with Globus here\"\n safeprint(\n \"{0}:\\n{1}\\n{2}\\n{1}\\n\".format(\n linkprompt,\n \"-\" * len(linkprompt),\n auth_client.oauth2_get_authorize_url(additional_params=additional_params),\n )\n )\n\n # come back with auth code\n auth_code = click.prompt(\"Enter the resulting Authorization Code here\").strip()\n\n # finish auth flow\n exchange_code_and_store_config(auth_client, auth_code)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef do_local_server_auth_flow(session_params=None, force_new_client=False):\n session_params = session_params or {}\n\n # start local server and create matching redirect_uri\n with start_local_server(listen=(\"127.0.0.1\", 0)) as server:\n _, port = server.socket.getsockname()\n redirect_uri = \"http://localhost:{}\".format(port)\n\n # get the ConfidentialApp client object and start a flow\n auth_client = internal_auth_client(\n requires_instance=True, force_new_client=force_new_client\n )\n auth_client.oauth2_start_flow(\n refresh_tokens=True, redirect_uri=redirect_uri, requested_scopes=SCOPES\n )\n additional_params = {\"prompt\": \"login\"}\n additional_params.update(session_params)\n url = auth_client.oauth2_get_authorize_url(additional_params=additional_params)\n\n # open web-browser for user to log in, get auth code\n webbrowser.open(url, new=1)\n auth_code = server.wait_for_code()\n\n if isinstance(auth_code, LocalServerError):\n safeprint(\"Authorization failed: {}\".format(auth_code), write_to_stderr=True)\n click.get_current_context().exit(1)\n elif isinstance(auth_code, Exception):\n safeprint(\n \"Authorization failed with unexpected error:\\n{}\".format(auth_code),\n write_to_stderr=True,\n )\n click.get_current_context().exit(1)\n\n # finish auth flow and return true\n exchange_code_and_store_config(auth_client, auth_code)\n return True", "response": "Starts a local server and opens a browser to authenticate the user and gets the code redirected to the server"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nexchanging code for tokens and gets user info from auth.", "response": "def exchange_code_and_store_config(auth_client, auth_code):\n \"\"\"\n Finishes auth flow after code is gotten from command line or local server.\n Exchanges code for tokens and gets user info from auth.\n Stores tokens and user info in config.\n \"\"\"\n # do a token exchange with the given code\n tkn = auth_client.oauth2_exchange_code_for_tokens(auth_code)\n tkn = tkn.by_resource_server\n\n # extract access tokens from final response\n transfer_at = tkn[\"transfer.api.globus.org\"][\"access_token\"]\n transfer_at_expires = tkn[\"transfer.api.globus.org\"][\"expires_at_seconds\"]\n transfer_rt = tkn[\"transfer.api.globus.org\"][\"refresh_token\"]\n auth_at = tkn[\"auth.globus.org\"][\"access_token\"]\n auth_at_expires = tkn[\"auth.globus.org\"][\"expires_at_seconds\"]\n auth_rt = tkn[\"auth.globus.org\"][\"refresh_token\"]\n\n # revoke any existing tokens\n for token_opt in (\n TRANSFER_RT_OPTNAME,\n TRANSFER_AT_OPTNAME,\n AUTH_RT_OPTNAME,\n AUTH_AT_OPTNAME,\n ):\n token = lookup_option(token_opt)\n if token:\n auth_client.oauth2_revoke_token(token)\n\n # write new tokens to config\n write_option(TRANSFER_RT_OPTNAME, transfer_rt)\n write_option(TRANSFER_AT_OPTNAME, transfer_at)\n write_option(TRANSFER_AT_EXPIRES_OPTNAME, transfer_at_expires)\n write_option(AUTH_RT_OPTNAME, auth_rt)\n write_option(AUTH_AT_OPTNAME, auth_at)\n write_option(AUTH_AT_EXPIRES_OPTNAME, auth_at_expires)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef endpoint_search(filter_fulltext, filter_owner_id, filter_scope):\n if filter_scope == \"all\" and not filter_fulltext:\n raise click.UsageError(\n \"When searching all endpoints (--filter-scope=all, the default), \"\n \"a full-text search filter is required. Other scopes (e.g. \"\n \"--filter-scope=recently-used) may be used without specifying \"\n \"an additional filter.\"\n )\n\n client = get_client()\n\n owner_id = filter_owner_id\n if owner_id:\n owner_id = maybe_lookup_identity_id(owner_id)\n\n search_iterator = client.endpoint_search(\n filter_fulltext=filter_fulltext,\n filter_scope=filter_scope,\n filter_owner_id=owner_id,\n )\n\n formatted_print(\n search_iterator,\n fields=ENDPOINT_LIST_FIELDS,\n json_converter=iterable_response_to_dict,\n )", "response": "Executor for globus endpoint search"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwrapping around click. echo that does not raise IOError.", "response": "def safeprint(message, write_to_stderr=False, newline=True):\n \"\"\"\n Wrapper around click.echo used to encapsulate its functionality.\n Also protects against EPIPE during click.echo calls, as this can happen\n normally in piped commands when the consumer closes before the producer.\n \"\"\"\n try:\n click.echo(message, nl=newline, err=write_to_stderr)\n except IOError as err:\n if err.errno is errno.EPIPE:\n pass\n else:\n raise"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _key_to_keyfunc(k):\n # if the key is a string, then the \"keyfunc\" is just a basic lookup\n # operation -- return that\n if isinstance(k, six.string_types):\n\n def lookup(x):\n return x[k]\n\n return lookup\n # otherwise, the key must be a function which is executed on the item\n # to produce a value -- return it verbatim\n return k", "response": "Converts a key to a function that returns a value for the item\n ."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef endpoint_id_arg(*args, **kwargs):\n\n def decorate(f, **kwargs):\n \"\"\"\n Work of actually decorating a function -- wrapped in here because we\n want to dispatch depending on how this is invoked\n \"\"\"\n metavar = kwargs.get(\"metavar\", \"ENDPOINT_ID\")\n f = click.argument(\"endpoint_id\", metavar=metavar, type=click.UUID)(f)\n return f\n\n return detect_and_decorate(decorate, args, kwargs)", "response": "Decorator for handling the endpoint_id argument."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nvalidate the parameters for an endpoint create and update.", "response": "def validate_endpoint_create_and_update_params(endpoint_type, managed, params):\n \"\"\"\n Given an endpoint type of \"shared\" \"server\" or \"personal\" and option values\n Confirms the option values are valid for the given endpoint\n \"\"\"\n # options only allowed for GCS endpoints\n if endpoint_type != \"server\":\n # catch params with two option flags\n if params[\"public\"] is False:\n raise click.UsageError(\n \"Option --private only allowed \" \"for Globus Connect Server endpoints\"\n )\n # catch any params only usable with GCS\n for option in [\n \"public\",\n \"myproxy_dn\",\n \"myproxy_server\",\n \"oauth_server\",\n \"location\",\n \"network_use\",\n \"max_concurrency\",\n \"preferred_concurrency\",\n \"max_parallelism\",\n \"preferred_parallelism\",\n ]:\n if params[option] is not None:\n raise click.UsageError(\n (\n \"Option --{} can only be used with Globus Connect Server \"\n \"endpoints\".format(option.replace(\"_\", \"-\"))\n )\n )\n\n # if the endpoint was not previously managed, and is not being passed\n # a subscription id, it cannot use managed endpoint only fields\n if (not managed) and not (params[\"subscription_id\"] or params[\"managed\"]):\n for option in [\n \"network_use\",\n \"max_concurrency\",\n \"preferred_concurrency\",\n \"max_parallelism\",\n \"preferred_parallelism\",\n ]:\n if params[option] is not None:\n raise click.UsageError(\n (\n \"Option --{} can only be used with managed \"\n \"endpoints\".format(option.replace(\"_\", \"-\"))\n )\n )\n\n # because the Transfer service doesn't do network use level updates in a\n # patchy way, *both* endpoint `POST`s *and* `PUT`s must either use\n # - `network_use='custom'` with *every* other parameter specified (which\n # is validated by the service), or\n # - a preset/absent `network_use` with *no* other parameter specified\n # (which is *not* validated by the service; in this case, Transfer will\n # accept but ignore the others parameters if given, leading to user\n # confusion if we don't do this validation check)\n custom_network_use_params = (\n \"max_concurrency\",\n \"preferred_concurrency\",\n \"max_parallelism\",\n \"preferred_parallelism\",\n )\n if params[\"network_use\"] != \"custom\":\n for option in custom_network_use_params:\n if params[option] is not None:\n raise click.UsageError(\n \"The {} options require you use --network-use=custom.\".format(\n \"/\".join(\n \"--\" + option.replace(\"_\", \"-\")\n for option in custom_network_use_params\n )\n )\n )\n\n # make sure --(no-)managed and --subscription-id are mutually exclusive\n # if --managed given pass DEFAULT as the subscription_id\n # if --no-managed given, pass None\n managed_flag = params.get(\"managed\")\n if managed_flag is not None:\n params.pop(\"managed\")\n if managed_flag:\n params[\"subscription_id\"] = params.get(\"subscription_id\") or \"DEFAULT\"\n else:\n if params.get(\"subscription_id\"):\n raise click.UsageError(\n \"Cannot specify --subscription-id and \"\n \"use the --no-managed option.\"\n )\n params[\"subscription_id\"] = EXPLICIT_NULL\n\n # make sure --no-default-directory are mutually exclusive\n # if --no-managed given, pass an EXPLICIT_NULL as the default directory\n if params.get(\"no_default_directory\"):\n\n if params.get(\"default_directory\"):\n raise click.UsageError(\n \"--no-default-directory and --default-directory are mutually \"\n \"exclusive.\"\n )\n else:\n params[\"default_directory\"] = EXPLICIT_NULL\n params.pop(\"no_default_directory\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef task_id_arg(*args, **kwargs):\n\n def inner_decorator(f, required=True):\n f = click.argument(\"TASK_ID\", required=required)(f)\n return f\n\n return detect_and_decorate(inner_decorator, args, kwargs)", "response": "Decorator to specify the TASK_ID argument for a Transfer Task."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef task_submission_options(f):\n\n def notify_opt_callback(ctx, param, value):\n \"\"\"\n Parse --notify\n - \"\" is the same as \"off\"\n - parse by lowercase, comma-split, strip spaces\n - \"off,x\" is invalid for any x\n - \"on,x\" is valid for any valid x (other than \"off\")\n - \"failed\", \"succeeded\", \"inactive\" are normal vals\n\n In code, produces True, False, or a set\n \"\"\"\n # if no value was set, don't set any explicit options\n # the API default is \"everything on\"\n if value is None:\n return {}\n\n value = value.lower()\n value = [x.strip() for x in value.split(\",\")]\n # [\"\"] is what you'll get if value is \"\" to start with\n # special-case it into \"off\", which helps avoid surprising scripts\n # which take a notification settings as inputs and build --notify\n if value == [\"\"]:\n value = [\"off\"]\n\n off = \"off\" in value\n on = \"on\" in value\n # set-ize it -- duplicates are fine\n vals = set([x for x in value if x not in (\"off\", \"on\")])\n\n if (vals or on) and off:\n raise click.UsageError('--notify cannot accept \"off\" and another value')\n\n allowed_vals = set((\"on\", \"succeeded\", \"failed\", \"inactive\"))\n if not vals <= allowed_vals:\n raise click.UsageError(\n \"--notify received at least one invalid value among {}\".format(\n list(vals)\n )\n )\n\n # return the notification options to send!\n # on means don't set anything (default)\n if on:\n return {}\n # off means turn off everything\n if off:\n return {\n \"notify_on_succeeded\": False,\n \"notify_on_failed\": False,\n \"notify_on_inactive\": False,\n }\n # otherwise, return the exact set of values seen\n else:\n return {\n \"notify_on_succeeded\": \"succeeded\" in vals,\n \"notify_on_failed\": \"failed\" in vals,\n \"notify_on_inactive\": \"inactive\" in vals,\n }\n\n f = click.option(\n \"--dry-run\",\n is_flag=True,\n help=(\"Don't actually submit the task, print submission \" \"data instead\"),\n )(f)\n f = click.option(\n \"--notify\",\n callback=notify_opt_callback,\n help=(\n \"Comma separated list of task events which notify by email. \"\n \"'on' and 'off' may be used to enable or disable notifications \"\n \"for all event types. Otherwise, use 'succeeded', 'failed', or \"\n \"'inactive'\"\n ),\n )(f)\n f = click.option(\n \"--submission-id\",\n help=(\n \"Task submission ID, as generated by `globus task \"\n \"generate-submission-id`. Used for safe resubmission in the \"\n \"presence of network failures.\"\n ),\n )(f)\n f = click.option(\"--label\", default=None, help=\"Set a label for this task.\")(f)\n f = click.option(\n \"--deadline\",\n default=None,\n type=ISOTimeType(),\n help=\"Set a deadline for this to be canceled if not completed by.\",\n )(f)\n f = click.option(\n \"--skip-activation-check\",\n is_flag=True,\n help=(\"Submit the task even if the endpoint(s) \" \"aren't currently activated.\"),\n )(f)\n\n return f", "response": "This function returns a list of options shared by both transfer and delete tasks."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delete_and_rm_options(*args, **kwargs):\n\n def inner_decorator(f, supports_batch=True, default_enable_globs=False):\n f = click.option(\n \"--recursive\", \"-r\", is_flag=True, help=\"Recursively delete dirs\"\n )(f)\n f = click.option(\n \"--ignore-missing\",\n \"-f\",\n is_flag=True,\n help=\"Don't throw errors if the file or dir is absent\",\n )(f)\n f = click.option(\n \"--star-silent\",\n \"--unsafe\",\n \"star_silent\",\n is_flag=True,\n help=(\n 'Don\\'t prompt when the trailing character is a \"*\".'\n + (\" Implicit in --batch\" if supports_batch else \"\")\n ),\n )(f)\n f = click.option(\n \"--enable-globs/--no-enable-globs\",\n is_flag=True,\n default=default_enable_globs,\n show_default=True,\n help=(\n \"Enable expansion of *, ?, and [ ] characters in the last \"\n \"component of file paths, unless they are escaped with \"\n \"a preceeding backslash, \\\\\"\n ),\n )(f)\n if supports_batch:\n f = click.option(\n \"--batch\",\n is_flag=True,\n help=(\n \"Accept a batch of paths on stdin (i.e. run in \"\n \"batchmode). Uses ENDPOINT_ID as passed on the \"\n \"commandline. Any commandline PATH given will be used \"\n \"as a prefix to all paths given\"\n ),\n )(f)\n return f\n\n return detect_and_decorate(inner_decorator, args, kwargs)", "response": "Decorator to add globus delete and globus rm options to a single globus archive."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsharing collection of options for `globus transfer endpoint server add` and `globus transfer endpoint server update`. Accepts a toggle to know if it's being used as `add` or `update`. usage: >>> @server_add_and_update_opts >>> def command_func(subject, port, scheme, hostname): >>> ... or >>> @server_add_and_update_opts(add=True) >>> def command_func(subject, port, scheme, hostname): >>> ...", "response": "def server_add_and_update_opts(*args, **kwargs):\n \"\"\"\n shared collection of options for `globus transfer endpoint server add` and\n `globus transfer endpoint server update`.\n Accepts a toggle to know if it's being used as `add` or `update`.\n\n usage:\n\n >>> @server_add_and_update_opts\n >>> def command_func(subject, port, scheme, hostname):\n >>> ...\n\n or\n\n >>> @server_add_and_update_opts(add=True)\n >>> def command_func(subject, port, scheme, hostname):\n >>> ...\n \"\"\"\n\n def port_range_callback(ctx, param, value):\n if not value:\n return None\n\n value = value.lower().strip()\n if value == \"unspecified\":\n return None, None\n if value == \"unrestricted\":\n return 1024, 65535\n\n try:\n lower, upper = map(int, value.split(\"-\"))\n except ValueError: # too many/few values from split or non-integer(s)\n raise click.BadParameter(\n \"must specify as 'unspecified', \"\n \"'unrestricted', or as range separated \"\n \"by a hyphen (e.g. '50000-51000')\"\n )\n if not 1024 <= lower <= 65535 or not 1024 <= upper <= 65535:\n raise click.BadParameter(\"must be within the 1024-65535 range\")\n\n return (lower, upper) if lower <= upper else (upper, lower)\n\n def inner_decorator(f, add=False):\n f = click.option(\"--hostname\", required=add, help=\"Server Hostname.\")(f)\n\n default_scheme = \"gsiftp\" if add else None\n f = click.option(\n \"--scheme\",\n help=\"Scheme for the Server.\",\n type=CaseInsensitiveChoice((\"gsiftp\", \"ftp\")),\n default=default_scheme,\n show_default=add,\n )(f)\n\n default_port = 2811 if add else None\n f = click.option(\n \"--port\",\n help=\"Port for Globus control channel connections.\",\n type=int,\n default=default_port,\n show_default=add,\n )(f)\n\n f = click.option(\n \"--subject\",\n help=(\n \"Subject of the X509 Certificate of the server. When \"\n \"unspecified, the CN must match the server hostname.\"\n ),\n )(f)\n\n for adjective, our_preposition, their_preposition in [\n (\"incoming\", \"to\", \"from\"),\n (\"outgoing\", \"from\", \"to\"),\n ]:\n f = click.option(\n \"--{}-data-ports\".format(adjective),\n callback=port_range_callback,\n help=\"Indicate to firewall administrators at other sites how to \"\n \"allow {} traffic {} this server {} their own. Specify as \"\n \"either 'unspecified', 'unrestricted', or as range of \"\n \"ports separated by a hyphen (e.g. '50000-51000') within \"\n \"the 1024-65535 range.\".format(\n adjective, our_preposition, their_preposition\n ),\n )(f)\n\n return f\n\n return detect_and_decorate(inner_decorator, args, kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_versions():\n # import in the func (rather than top-level scope) so that at setup time,\n # `requests` isn't required -- otherwise, setuptools will fail to run\n # because requests isn't installed yet.\n import requests\n\n try:\n version_data = requests.get(\n \"https://pypi.python.org/pypi/globus-cli/json\"\n ).json()\n latest = max(LooseVersion(v) for v in version_data[\"releases\"])\n return latest, LooseVersion(__version__)\n # if the fetch from pypi fails\n except requests.RequestException:\n return None, LooseVersion(__version__)", "response": "Get the latest version of the current node"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _lookup_identity_names(self):\n id_batch_size = 100\n\n # fetch in batches of 100, store in a dict\n ac = get_auth_client()\n self._resolved_map = {}\n for i in range(0, len(self.identity_ids), id_batch_size):\n chunk = self.identity_ids[i : i + id_batch_size]\n resolved_result = ac.get_identities(ids=chunk)\n for x in resolved_result[\"identities\"]:\n self._resolved_map[x[\"id\"]] = x[\"username\"]", "response": "Resolve identities to usernames."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef write_option(option, value, section=\"cli\", system=False):\n # deny rwx to Group and World -- don't bother storing the returned old mask\n # value, since we'll never restore it in the CLI anyway\n # do this on every call to ensure that we're always consistent about it\n os.umask(0o077)\n\n # FIXME: DRY violation with config_commands.helpers\n conf = get_config_obj(system=system)\n\n # add the section if absent\n if section not in conf:\n conf[section] = {}\n\n conf[section][option] = value\n conf.write()", "response": "Write an option to disk"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef internal_auth_client(requires_instance=False, force_new_client=False):\n client_id = lookup_option(CLIENT_ID_OPTNAME)\n client_secret = lookup_option(CLIENT_SECRET_OPTNAME)\n template_id = lookup_option(TEMPLATE_ID_OPTNAME) or DEFAULT_TEMPLATE_ID\n template_client = internal_native_client()\n existing = client_id and client_secret\n\n # if we are forcing a new client, delete any existing client\n if force_new_client and existing:\n existing_client = globus_sdk.ConfidentialAppAuthClient(client_id, client_secret)\n try:\n existing_client.delete(\"/v2/api/clients/{}\".format(client_id))\n\n # if the client secret has been invalidated or the client has\n # already been removed, we continue on\n except globus_sdk.exc.AuthAPIError:\n pass\n\n # if we require a new client to be made\n if force_new_client or (requires_instance and not existing):\n # register a new instance client with auth\n body = {\"client\": {\"template_id\": template_id, \"name\": \"Globus CLI\"}}\n res = template_client.post(\"/v2/api/clients\", json_body=body)\n\n # get values and write to config\n credential_data = res[\"included\"][\"client_credential\"]\n client_id = credential_data[\"client\"]\n client_secret = credential_data[\"secret\"]\n write_option(CLIENT_ID_OPTNAME, client_id)\n write_option(CLIENT_SECRET_OPTNAME, client_secret)\n\n return globus_sdk.ConfidentialAppAuthClient(\n client_id, client_secret, app_name=\"Globus CLI\"\n )\n\n # if we already have a client, just return it\n elif existing:\n return globus_sdk.ConfidentialAppAuthClient(\n client_id, client_secret, app_name=\"Globus CLI\"\n )\n\n # fall-back to a native client to not break old logins\n # TOOD: eventually remove this behavior\n else:\n return template_client", "response": "Returns a ConfidentialAppAuthClient object for the current CLI s Instance Client."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexits with the status mapped by what we were given.", "response": "def exit_with_mapped_status(http_status):\n \"\"\"\n Given an HTTP Status, exit with either an error status of 1 or the\n status mapped by what we were given.\n \"\"\"\n # get the mapping by looking up the state and getting the mapping attr\n mapping = click.get_current_context().ensure_object(CommandState).http_status_map\n\n # if there is a mapped exit code, exit with that. Otherwise, exit 1\n if http_status in mapping:\n sys.exit(mapping[http_status])\n else:\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef session_hook(exception):\n safeprint(\n \"The resource you are trying to access requires you to \"\n \"re-authenticate with specific identities.\"\n )\n\n params = exception.raw_json[\"authorization_parameters\"]\n message = params.get(\"session_message\")\n if message:\n safeprint(\"message: {}\".format(message))\n\n identities = params.get(\"session_required_identities\")\n if identities:\n id_str = \" \".join(identities)\n safeprint(\n \"Please run\\n\\n\"\n \" globus session update {}\\n\\n\"\n \"to re-authenticate with the required identities\".format(id_str)\n )\n else:\n safeprint(\n 'Please use \"globus session update\" to re-authenticate '\n \"with specific identities\".format(id_str)\n )\n\n exit_with_mapped_status(exception.http_status)", "response": "Handles globus session update"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef custom_except_hook(exc_info):\n exception_type, exception, traceback = exc_info\n\n # check if we're in debug mode, and run the real excepthook if we are\n ctx = click.get_current_context()\n state = ctx.ensure_object(CommandState)\n if state.debug:\n sys.excepthook(exception_type, exception, traceback)\n\n # we're not in debug mode, do custom handling\n else:\n\n # if it's a click exception, re-raise as original -- Click's main\n # execution context will handle pretty-printing\n if isinstance(exception, click.ClickException):\n reraise(exception_type, exception, traceback)\n\n # catch any session errors to give helpful instructions\n # on how to use globus session update\n elif (\n isinstance(exception, exc.GlobusAPIError)\n and exception.raw_json\n and \"authorization_parameters\" in exception.raw_json\n ):\n session_hook(exception)\n\n # handle the Globus-raised errors with our special hooks\n # these will present the output (on stderr) as JSON\n elif isinstance(exception, exc.TransferAPIError):\n if exception.code == \"ClientError.AuthenticationFailed\":\n authentication_hook(exception)\n else:\n transferapi_hook(exception)\n\n elif isinstance(exception, exc.AuthAPIError):\n if exception.code == \"UNAUTHORIZED\":\n authentication_hook(exception)\n # invalid_grant occurs when the users refresh tokens are not valid\n elif exception.message == \"invalid_grant\":\n invalidrefresh_hook(exception)\n else:\n authapi_hook(exception)\n\n elif isinstance(exception, exc.GlobusAPIError):\n globusapi_hook(exception)\n\n # specific checks fell through -- now check if it's any kind of\n # GlobusError\n elif isinstance(exception, exc.GlobusError):\n globus_generic_hook(exception)\n\n # not a GlobusError, not a ClickException -- something like ValueError\n # or NotImplementedError bubbled all the way up here: just print it\n # out, basically\n else:\n safeprint(u\"{}: {}\".format(exception_type.__name__, exception))\n sys.exit(1)", "response": "Custom excepthook to present python errors produced by the CLI."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ls_command(\n endpoint_plus_path,\n recursive_depth_limit,\n recursive,\n long_output,\n show_hidden,\n filter_val,\n):\n \"\"\"\n Executor for `globus ls`\n \"\"\"\n endpoint_id, path = endpoint_plus_path\n\n # do autoactivation before the `ls` call so that recursive invocations\n # won't do this repeatedly, and won't have to instantiate new clients\n client = get_client()\n autoactivate(client, endpoint_id, if_expires_in=60)\n\n # create the query paramaters to send to operation_ls\n ls_params = {\"show_hidden\": int(show_hidden)}\n if path:\n ls_params[\"path\"] = path\n if filter_val:\n # this char has special meaning in the LS API's filter clause\n # can't be part of the pattern (but we don't support globbing across\n # dir structures anyway)\n if \"/\" in filter_val:\n raise click.UsageError('--filter cannot contain \"/\"')\n # format into a simple filter clause which operates on filenames\n ls_params[\"filter\"] = \"name:{}\".format(filter_val)\n\n # get the `ls` result\n if recursive:\n # NOTE:\n # --recursive and --filter have an interplay that some users may find\n # surprising\n # if we're asked to change or \"improve\" the behavior in the future, we\n # could do so with \"type:dir\" or \"type:file\" filters added in, and\n # potentially work out some viable behavior based on what people want\n res = client.recursive_operation_ls(\n endpoint_id, depth=recursive_depth_limit, **ls_params\n )\n else:\n res = client.operation_ls(endpoint_id, **ls_params)\n\n def cleaned_item_name(item):\n return item[\"name\"] + (\"/\" if item[\"type\"] == \"dir\" else \"\")\n\n # and then print it, per formatting rules\n formatted_print(\n res,\n fields=[\n (\"Permissions\", \"permissions\"),\n (\"User\", \"user\"),\n (\"Group\", \"group\"),\n (\"Size\", \"size\"),\n (\"Last Modified\", \"last_modified\"),\n (\"File Type\", \"type\"),\n (\"Filename\", cleaned_item_name),\n ],\n simple_text=(\n None\n if long_output or is_verbose() or not outformat_is_text()\n else \"\\n\".join(cleaned_item_name(x) for x in res)\n ),\n json_converter=iterable_response_to_dict,\n )", "response": "Executor for globus ls"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef iterable_func(self):\n # BFS is not done until the queue is empty\n while self.queue:\n logger.debug(\n (\n \"recursive_operation_ls BFS queue not empty, \"\n \"getting next path now.\"\n )\n )\n\n # rate limit based on number of ls calls we have made\n self.ls_count += 1\n if self.ls_count % SLEEP_FREQUENCY == 0:\n logger.debug(\n (\n \"recursive_operation_ls sleeping {} seconds to \"\n \"rate limit itself.\".format(SLEEP_LEN)\n )\n )\n time.sleep(SLEEP_LEN)\n\n # get path and current depth from the queue\n abs_path, rel_path, depth = self.queue.pop()\n\n # set the target path to the popped absolute path if it exists\n if abs_path:\n self.ls_params[\"path\"] = abs_path\n\n # if filter_after_first is False, stop filtering after the first\n # ls call has been made\n if not self.filter_after_first:\n if self.filtering:\n self.filtering = False\n else:\n try:\n self.ls_params.pop(\"filter\")\n except KeyError:\n pass\n\n # do the operation_ls with the updated params\n res = self.client.operation_ls(self.endpoint_id, **self.ls_params)\n res_data = res[\"DATA\"]\n\n # if we aren't at the depth limit, add dir entries to the queue.\n # including the dir's name in the absolute and relative paths\n # and increase the depth by one.\n # data is reversed to maintain any \"orderby\" ordering\n if depth < self.max_depth:\n self.queue.extend(\n [\n (\n res[\"path\"] + item[\"name\"],\n (rel_path + \"/\" if rel_path else \"\") + item[\"name\"],\n depth + 1,\n )\n for item in reversed(res_data)\n if item[\"type\"] == \"dir\"\n ]\n )\n\n # for each item in the response data update the item's name with\n # the relative path popped from the queue, and yield the item\n for item in res_data:\n item[\"name\"] = (rel_path + \"/\" if rel_path else \"\") + item[\"name\"]\n yield GlobusResponse(item)", "response": "This is the internal function which is used by the recursive_operation_ls method. It is called by the recursive_operation_ls method in order to get the next item from the queue."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngive the activation requirements for an endpoint and a filename for a file for a Delegate Proxy activation requirements returns the activation requirements data with the proxy chain filled in.", "response": "def fill_delegate_proxy_activation_requirements(\n requirements_data, cred_file, lifetime_hours=12\n):\n \"\"\"\n Given the activation requirements for an endpoint and a filename for\n X.509 credentials, extracts the public key from the activation\n requirements, uses the key and the credentials to make a proxy credential,\n and returns the requirements data with the proxy chain filled in.\n \"\"\"\n # get the public key from the activation requirements\n for data in requirements_data[\"DATA\"]:\n if data[\"type\"] == \"delegate_proxy\" and data[\"name\"] == \"public_key\":\n public_key = data[\"value\"]\n break\n else:\n raise ValueError(\n (\n \"No public_key found in activation requirements, this endpoint \"\n \"does not support Delegate Proxy activation.\"\n )\n )\n\n # get user credentials from user credential file\"\n with open(cred_file) as f:\n issuer_cred = f.read()\n\n # create the proxy credentials\n proxy = create_proxy_credentials(issuer_cred, public_key, lifetime_hours)\n\n # return the activation requirements document with the proxy_chain filled\n for data in requirements_data[\"DATA\"]:\n if data[\"type\"] == \"delegate_proxy\" and data[\"name\"] == \"proxy_chain\":\n data[\"value\"] = proxy\n return requirements_data\n else:\n raise ValueError(\n (\n \"No proxy_chain found in activation requirements, this endpoint \"\n \"does not support Delegate Proxy activation.\"\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_proxy_credentials(issuer_cred, public_key, lifetime_hours):\n # parse the issuer credential\n loaded_cert, loaded_private_key, issuer_chain = parse_issuer_cred(issuer_cred)\n\n # load the public_key into a cryptography object\n loaded_public_key = serialization.load_pem_public_key(\n public_key.encode(\"ascii\"), backend=default_backend()\n )\n\n # check that the issuer certificate is not an old proxy\n # and is using the keyUsage section as required\n confirm_not_old_proxy(loaded_cert)\n validate_key_usage(loaded_cert)\n\n # create the proxy cert cryptography object\n new_cert = create_proxy_cert(\n loaded_cert, loaded_private_key, loaded_public_key, lifetime_hours\n )\n\n # extend the proxy chain as a unicode string\n extended_chain = loaded_cert.public_bytes(serialization.Encoding.PEM).decode(\n \"ascii\"\n ) + six.u(issuer_chain)\n\n # return in PEM format as a unicode string\n return (\n new_cert.public_bytes(serialization.Encoding.PEM).decode(\"ascii\")\n + extended_chain\n )", "response": "Create a proxy credentials file from an activation requirements document and a public key."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing a PEM file containing a list of issuer credentials and returns the list of cryptography objects and the proxy chain of the certificate and private key.", "response": "def parse_issuer_cred(issuer_cred):\n \"\"\"\n Given an X509 PEM file in the form of a string, parses it into sections\n by the PEM delimiters of: -----BEGIN