code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
'''Read input file as HTML and return the links.'''
scrape_result = session.factory['HTMLScraper'].scrape_file(
session.args.input_file,
encoding=session.args.local_encoding or 'utf-8'
)
for context in scrape_result.link_contexts:
yield context.link | def _input_file_as_html_links(cls, session: AppSession) | Read input file as HTML and return the links. | 7.4694 | 5.871157 | 1.272219 |
'''Return a new Request to be passed to the Web Client.'''
url_record = self._item_session.url_record
url_info = url_record.url_info
request = self._item_session.app_session.factory['WebClient'].request_factory(url_info.url)
self._populate_common_request(request)
if wi... | def _new_initial_request(self, with_body: bool=True) | Return a new Request to be passed to the Web Client. | 5.696903 | 4.833519 | 1.178624 |
'''Populate the Request with common fields.'''
url_record = self._item_session.url_record
# Note that referrer may have already been set by the --referer option
if url_record.parent_url and not request.fields.get('Referer'):
self._add_referrer(request, url_record)
i... | def _populate_common_request(self, request) | Populate the Request with common fields. | 8.235021 | 7.86419 | 1.047154 |
'''Add referrer URL to request.'''
# Prohibit leak of referrer from HTTPS to HTTP
# rfc7231 section 5.5.2.
if url_record.parent_url.startswith('https://') and \
url_record.url_info.scheme == 'http':
return
request.fields['Referer'] = url_record.parent... | def _add_referrer(cls, request: Request, url_record: URLRecord) | Add referrer URL to request. | 6.152467 | 5.329999 | 1.154309 |
'''Process robots.txt.
Coroutine.
'''
try:
self._item_session.request = request = self._new_initial_request(with_body=False)
verdict, reason = (yield from self._should_fetch_reason_with_robots(
request))
except REMOTE_ERRORS as error:
... | def _process_robots(self) | Process robots.txt.
Coroutine. | 5.697531 | 5.305345 | 1.073923 |
'''Fetch URL including redirects.
Coroutine.
'''
while not self._web_client_session.done():
self._item_session.request = self._web_client_session.next_request()
verdict, reason = self._should_fetch_reason()
_logger.debug('Filter verdict {} reason {}... | def _process_loop(self) | Fetch URL including redirects.
Coroutine. | 6.063748 | 4.858045 | 1.248187 |
'''Process one of the loop iteration.
Coroutine.
Returns:
If True, stop processing any future requests.
'''
_logger.info(_('Fetching ‘{url}’.'), url=request.url)
response = None
try:
response = yield from self._web_client_session.start(... | def _fetch_one(self, request: Request) -> Tuple[bool, float] | Process one of the loop iteration.
Coroutine.
Returns:
If True, stop processing any future requests. | 5.112914 | 4.417592 | 1.157398 |
'''Return the next URLInfo to be processed.
This returns either the original URLInfo or the next URLinfo
containing the redirect link.
'''
if not self._web_client_session:
return self._item_session.url_record.url_info
return self._web_client_session.next_req... | def _next_url_info(self) -> URLInfo | Return the next URLInfo to be processed.
This returns either the original URLInfo or the next URLinfo
containing the redirect link. | 8.678754 | 4.205795 | 2.063523 |
'''Return info about whether the URL should be fetched.
Returns:
tuple: A two item tuple:
1. bool: If True, the URL should be fetched.
2. str: A short reason string explaining the verdict.
'''
is_redirect = False
if self._strong_redirects:
... | def _should_fetch_reason(self) -> Tuple[bool, str] | Return info about whether the URL should be fetched.
Returns:
tuple: A two item tuple:
1. bool: If True, the URL should be fetched.
2. str: A short reason string explaining the verdict. | 7.021034 | 3.792976 | 1.851062 |
'''Return info whether the URL should be fetched including checking
robots.txt.
Coroutine.
'''
result = yield from \
self._fetch_rule.check_initial_web_request(self._item_session, request)
return result | def _should_fetch_reason_with_robots(self, request: Request) -> Tuple[bool, str] | Return info whether the URL should be fetched including checking
robots.txt.
Coroutine. | 18.759775 | 7.703486 | 2.435232 |
'''Add data to the payload.'''
if self._item_session.url_record.post_data:
data = wpull.string.to_bytes(self._item_session.url_record.post_data)
else:
data = wpull.string.to_bytes(
self._processor.fetch_params.post_data
)
request.metho... | def _add_post_data(self, request: Request) | Add data to the payload. | 4.619382 | 4.217897 | 1.095186 |
'''Log response.'''
_logger.info(
_('Fetched ‘{url}’: {status_code} {reason}. '
'Length: {content_length} [{content_type}].'),
url=request.url,
status_code=response.status_code,
reason=wpull.string.printable_str(response.reason),
... | def _log_response(self, request: Request, response: Response) | Log response. | 3.812608 | 3.601463 | 1.058628 |
'''Process the response.
Returns:
A value from :class:`.hook.Actions`.
'''
self._item_session.update_record_value(status_code=response.status_code)
if self._web_client_session.redirect_tracker.is_redirect() or \
self._web_client_session.loop_type() =... | def _handle_response(self, request: Request, response: Response) -> Actions | Process the response.
Returns:
A value from :class:`.hook.Actions`. | 4.293138 | 3.777161 | 1.136604 |
'''Convert string to int.
If ``inf`` is supplied, it returns ``0``.
'''
if string == 'inf':
return 0
try:
value = int(string)
except ValueError as error:
raise argparse.ArgumentTypeError(error)
if value < 0:
raise... | def int_0_inf(cls, string) | Convert string to int.
If ``inf`` is supplied, it returns ``0``. | 3.640235 | 2.628599 | 1.384858 |
'''Convert string describing size to int.'''
if string[-1] in ('k', 'm'):
value = cls.int_0_inf(string[:-1])
unit = string[-1]
if unit == 'k':
value *= 2 ** 10
else:
value *= 2 ** 20
return value
else:
... | def int_bytes(cls, string) | Convert string describing size to int. | 3.42973 | 2.899446 | 1.182891 |
'''Convert a comma separated string to list.'''
items = string.split(',')
items = list([item.strip() for item in items])
return items | def comma_list(cls, string) | Convert a comma separated string to list. | 5.095642 | 4.453029 | 1.144309 |
'''Convert a comma separated string to `CommaChoiceListArgs`.'''
items = string.split(',')
items = CommaChoiceListArgs([item.strip() for item in items])
return items | def comma_choice_list(cls, string) | Convert a comma separated string to `CommaChoiceListArgs`. | 7.712875 | 4.365656 | 1.766716 |
'''Return an iterator of links found in the document.
Args:
file: A file object containing the document.
encoding (str): The encoding of the document.
Returns:
iterable: str
'''
return [item[0] for item in self.iter_text(file, encoding) if it... | def read_links(self, file, encoding=None) | Return an iterator of links found in the document.
Args:
file: A file object containing the document.
encoding (str): The encoding of the document.
Returns:
iterable: str | 5.568027 | 2.908546 | 1.914368 |
'''Create the File Writer.
Returns:
FileWriter: An instance of :class:`.writer.BaseFileWriter`.
'''
args = session.args
if args.delete_after:
return session.factory.new('FileWriter') # is a NullWriter
elif args.output_document:
sess... | def _build_file_writer(cls, session: AppSession) | Create the File Writer.
Returns:
FileWriter: An instance of :class:`.writer.BaseFileWriter`. | 4.029624 | 3.729072 | 1.080597 |
'''Setup Ctrl+C and SIGTERM handlers.'''
if platform.system() == 'Windows':
_logger.warning(_(
'Graceful stopping with Unix signals is not supported '
'on this OS.'
))
return
event_loop = asyncio.get_event_loop()
gracef... | def setup_signal_handlers(self) | Setup Ctrl+C and SIGTERM handlers. | 3.560772 | 3.353818 | 1.061707 |
'''Run the application.
This function is blocking.
Returns:
int: The exit status.
'''
exit_status = asyncio.get_event_loop().run_until_complete(self.run())
# The following loop close procedure should avoid deadlock while
# allowing all callbacks to ... | def run_sync(self) -> int | Run the application.
This function is blocking.
Returns:
int: The exit status. | 5.289728 | 4.223352 | 1.252495 |
'''Set the exit code based on the error type.
Args:
error (:class:`Exception`): An exception instance.
'''
for error_type, exit_code in self.ERROR_CODE_MAP.items():
if isinstance(error, error_type):
self.update_exit_code(exit_code)
... | def _update_exit_code_from_error(self, error) | Set the exit code based on the error type.
Args:
error (:class:`Exception`): An exception instance. | 3.072769 | 2.449517 | 1.254438 |
'''Set the exit code if it is serious than before.
Args:
code: The exit code.
'''
if code:
if self._exit_code:
self._exit_code = min(self._exit_code, code)
else:
self._exit_code = code | def update_exit_code(self, code: int) | Set the exit code if it is serious than before.
Args:
code: The exit code. | 4.581692 | 2.431791 | 1.884082 |
'''Consult by fetching robots.txt as needed.
Args:
request: The request to be made
to get the file.
Returns:
True if can fetch
Coroutine
'''
if not self._robots_txt_checker:
return True
result = yield from se... | def consult_robots_txt(self, request: HTTPRequest) -> bool | Consult by fetching robots.txt as needed.
Args:
request: The request to be made
to get the file.
Returns:
True if can fetch
Coroutine | 9.218379 | 2.912479 | 3.165132 |
'''Consult the URL filter.
Args:
url_record: The URL record.
is_redirect: Whether the request is a redirect and it is
desired that it spans hosts.
Returns
tuple:
1. bool: The verdict
2. str: A short reason string: nof... | def consult_filters(self, url_info: URLInfo, url_record: URLRecord, is_redirect: bool=False) \
-> Tuple[bool, str, dict] | Consult the URL filter.
Args:
url_record: The URL record.
is_redirect: Whether the request is a redirect and it is
desired that it spans hosts.
Returns
tuple:
1. bool: The verdict
2. str: A short reason string: nofilters, fil... | 5.338534 | 1.996012 | 2.674601 |
'''Consult the scripting hook.
Returns:
tuple: (bool, str)
'''
try:
reasons = {
'filters': test_info['map'],
'reason': reason,
}
verdict = self.hook_dispatcher.call(
PluginFunctions.accept_u... | def consult_hook(self, item_session: ItemSession, verdict: bool,
reason: str, test_info: dict) | Consult the scripting hook.
Returns:
tuple: (bool, str) | 11.582754 | 8.905802 | 1.300585 |
'''Check robots.txt, URL filters, and scripting hook.
Returns:
tuple: (bool, str)
Coroutine.
'''
verdict, reason, test_info = self.consult_filters(item_session.request.url_info, item_session.url_record)
if verdict and self._robots_txt_checker:
c... | def check_initial_web_request(self, item_session: ItemSession, request: HTTPRequest) -> Tuple[bool, str] | Check robots.txt, URL filters, and scripting hook.
Returns:
tuple: (bool, str)
Coroutine. | 6.809271 | 4.300473 | 1.583377 |
'''Check URL filters and scripting hook.
Returns:
tuple: (bool, str)
'''
verdict, reason, test_info = self.consult_filters(
item_session.request.url_info,
item_session.url_record, is_redirect=is_redirect)
# TODO: provide an option to change t... | def check_subsequent_web_request(self, item_session: ItemSession,
is_redirect: bool=False) -> Tuple[bool, str] | Check URL filters and scripting hook.
Returns:
tuple: (bool, str) | 6.794128 | 4.941014 | 1.375047 |
'''Check URL filters and scripting hook.
Returns:
tuple: (bool, str)
'''
verdict, reason, test_info = self.consult_filters(
item_session.request.url_info,
item_session.url_record)
verdict, reason = self.consult_hook(item_session, verdict,
... | def check_generic_request(self, item_session: ItemSession) -> Tuple[bool, str] | Check URL filters and scripting hook.
Returns:
tuple: (bool, str) | 7.773744 | 4.814968 | 1.614495 |
'''Process a response that is starting.'''
action = self.consult_pre_response_hook(item_session)
if action == Actions.RETRY:
item_session.set_status(Status.skipped)
elif action == Actions.FINISH:
item_session.set_status(Status.done)
elif action == Actions... | def handle_pre_response(self, item_session: ItemSession) -> Actions | Process a response that is starting. | 6.782657 | 5.714981 | 1.186821 |
'''Process a successful document response.
Returns:
A value from :class:`.hook.Actions`.
'''
self._waiter.reset()
action = self.handle_response(item_session)
if action == Actions.NORMAL:
self._statistics.increment(item_session.response.body.size... | def handle_document(self, item_session: ItemSession, filename: str) -> Actions | Process a successful document response.
Returns:
A value from :class:`.hook.Actions`. | 9.312389 | 5.599063 | 1.663205 |
'''Callback for successful responses containing no useful document.
Returns:
A value from :class:`.hook.Actions`.
'''
self._waiter.reset()
action = self.handle_response(item_session)
if action == Actions.NORMAL:
item_session.set_status(Status.sk... | def handle_no_document(self, item_session: ItemSession) -> Actions | Callback for successful responses containing no useful document.
Returns:
A value from :class:`.hook.Actions`. | 11.863239 | 4.779058 | 2.482339 |
'''Callback for successful intermediate responses.
Returns:
A value from :class:`.hook.Actions`.
'''
self._waiter.reset()
action = self.handle_response(item_session)
return action | def handle_intermediate_response(self, item_session: ItemSession) -> Actions | Callback for successful intermediate responses.
Returns:
A value from :class:`.hook.Actions`. | 12.848272 | 5.289739 | 2.428905 |
'''Callback for when the document only describes an server error.
Returns:
A value from :class:`.hook.Actions`.
'''
self._waiter.increment()
self._statistics.errors[ServerError] += 1
action = self.handle_response(item_session)
if action == Actions.... | def handle_document_error(self, item_session: ItemSession) -> Actions | Callback for when the document only describes an server error.
Returns:
A value from :class:`.hook.Actions`. | 12.459411 | 5.594485 | 2.227088 |
'''Generic handler for a response.
Returns:
A value from :class:`.hook.Actions`.
'''
action = self.consult_response_hook(item_session)
if action == Actions.RETRY:
item_session.set_status(Status.error)
elif action == Actions.FINISH:
it... | def handle_response(self, item_session: ItemSession) -> Actions | Generic handler for a response.
Returns:
A value from :class:`.hook.Actions`. | 6.065252 | 4.17787 | 1.451757 |
'''Process an error.
Returns:
A value from :class:`.hook.Actions`.
'''
if not self._ssl_verification and \
isinstance(error, SSLVerificationError):
# Change it into a different error since the user doesn't care
# about verifying certif... | def handle_error(self, item_session: ItemSession, error: BaseException) -> Actions | Process an error.
Returns:
A value from :class:`.hook.Actions`. | 4.604653 | 4.102708 | 1.122345 |
'''Return the wait time in seconds between requests.'''
seconds = self._waiter.get()
try:
return self.hook_dispatcher.call(PluginFunctions.wait_time, seconds,
item_session, error)
except HookDisconnected:
return seconds | def get_wait_time(self, item_session: ItemSession, error=None) | Return the wait time in seconds between requests. | 12.507126 | 9.316849 | 1.34242 |
'''Return the wait time between requests.
Args:
seconds: The original time in seconds.
item_session:
error:
Returns:
The time in seconds.
'''
return seconds | def plugin_wait_time(seconds: float, item_session: ItemSession, error: Optional[Exception]=None) -> float | Return the wait time between requests.
Args:
seconds: The original time in seconds.
item_session:
error:
Returns:
The time in seconds. | 6.630971 | 2.315163 | 2.864148 |
'''Return scripting action when a response begins.'''
try:
return self.hook_dispatcher.call(
PluginFunctions.handle_pre_response,
item_session
)
except HookDisconnected:
return Actions.NORMAL | def consult_pre_response_hook(self, item_session: ItemSession) -> Actions | Return scripting action when a response begins. | 19.845675 | 11.709604 | 1.69482 |
'''Return scripting action when a response ends.'''
try:
return self.hook_dispatcher.call(
PluginFunctions.handle_response, item_session
)
except HookDisconnected:
return Actions.NORMAL | def consult_response_hook(self, item_session: ItemSession) -> Actions | Return scripting action when a response ends. | 22.475983 | 13.138756 | 1.710663 |
'''Return scripting action when an error occured.'''
try:
return self.hook_dispatcher.call(
PluginFunctions.handle_error, item_session, error)
except HookDisconnected:
return Actions.NORMAL | def consult_error_hook(self, item_session: ItemSession, error: BaseException) | Return scripting action when an error occured. | 26.619019 | 11.891483 | 2.238494 |
'''Add additional URLs such as robots.txt, favicon.ico.'''
if item_session.url_record.level == 0 and self._sitemaps:
extra_url_infos = (
self.parse_url(
'{0}://{1}/robots.txt'.format(
item_session.url_record.url_info.scheme,
... | def add_extra_urls(self, item_session: ItemSession) | Add additional URLs such as robots.txt, favicon.ico. | 3.022271 | 2.584629 | 1.169325 |
'''Process document for links.'''
self.event_dispatcher.notify(
PluginFunctions.get_urls, item_session
)
if not self._document_scraper:
return
demux_info = self._document_scraper.scrape_info(
item_session.request, item_session.response,
... | def scrape_document(self, item_session: ItemSession) | Process document for links. | 5.270863 | 4.814843 | 1.094711 |
'''Collect the URLs from the scrape info dict.'''
if not scrape_result:
return 0, 0
num_inline = 0
num_linked = 0
for link_context in scrape_result.link_contexts:
url_info = self.parse_url(link_context.link)
if not url_info:
... | def _process_scrape_info(self, scraper: BaseScraper,
scrape_result: ScrapeResult,
item_session: ItemSession) | Collect the URLs from the scrape info dict. | 3.767226 | 3.264922 | 1.153848 |
'''Return a rewritten URL such as escaped fragment.'''
if self._url_rewriter:
return self._url_rewriter.rewrite(url_info)
else:
return url_info | def rewrite_url(self, url_info: URLInfo) -> URLInfo | Return a rewritten URL such as escaped fragment. | 6.306219 | 2.90606 | 2.170023 |
'''Log exceptions during a fetch.'''
_logger.error(
_('Fetching ‘{url}’ encountered an error: {error}'),
url=request.url, error=error
) | def _log_error(self, request, error) | Log exceptions during a fetch. | 10.147425 | 6.556963 | 1.54758 |
repo = get_repo(token=token, org=org, name=name)
try:
repo.create_label(name=system.strip(), color=SYSTEM_LABEL_COLOR)
click.secho("Successfully added new system {}".format(system), fg="green")
if prompt and click.confirm("Run update to re-generate the page?"):
run_updat... | def run_add_system(name, token, org, system, prompt) | Adds a new system to the repo. | 3.141444 | 2.965998 | 1.059152 |
repo = get_repo(token=token, org=org, name=name)
try:
label = repo.get_label(name=system.strip())
label.delete()
click.secho("Successfully deleted {}".format(system), fg="green")
if prompt and click.confirm("Run update to re-generate the page?"):
run_update(name=... | def run_remove_system(name, token, org, system, prompt) | Removes a system from the repo. | 3.330037 | 3.190214 | 1.043829 |
files = get_files(repo)
config = DEFAULT_CONFIG
if "config.json" in files:
# get the config file, parse JSON and merge it with the default config
config_file = repo.get_file_contents('/config.json', ref="gh-pages")
try:
repo_config = json.loads(config_file.decoded_co... | def get_config(repo) | Get the config for the repo, merged with the default config. Returns the default config if
no config file is found. | 3.172462 | 3.020574 | 1.050284 |
if not self.registration_allowed():
return HttpResponseRedirect(force_text(self.disallowed_url))
return super(RegistrationView, self).dispatch(*args, **kwargs) | def dispatch(self, *args, **kwargs) | Check that user signup is allowed before even bothering to
dispatch or do other processing. | 4.597105 | 3.70956 | 1.239259 |
extra_context = {}
try:
activated_user = self.activate(*args, **kwargs)
except ActivationError as e:
extra_context['activation_error'] = {
'message': e.message,
'code': e.code,
'params': e.params
}
... | def get(self, *args, **kwargs) | The base activation logic; subclasses should leave this method
alone and implement activate(), which is called from this
method. | 2.300556 | 2.248507 | 1.023148 |
return signing.dumps(
obj=user.get_username(),
salt=REGISTRATION_SALT
) | def get_activation_key(self, user) | Generate the activation key which will be emailed to the user. | 8.633016 | 8.224144 | 1.049716 |
scheme = 'https' if self.request.is_secure() else 'http'
return {
'scheme': scheme,
'activation_key': activation_key,
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
'site': get_current_site(self.request)
} | def get_email_context(self, activation_key) | Build the template context used for the activation email. | 2.303771 | 2.222526 | 1.036556 |
activation_key = self.get_activation_key(user)
context = self.get_email_context(activation_key)
context['user'] = user
subject = render_to_string(
template_name=self.email_subject_template,
context=context,
request=self.request
)
... | def send_activation_email(self, user) | Send the activation email. The activation key is the username,
signed using TimestampSigner. | 1.9597 | 1.984867 | 0.987321 |
try:
username = signing.loads(
activation_key,
salt=REGISTRATION_SALT,
max_age=settings.ACCOUNT_ACTIVATION_DAYS * 86400
)
return username
except signing.SignatureExpired:
raise ActivationError(
... | def validate_key(self, activation_key) | Verify that the activation key is valid and within the
permitted activation time window, returning the username if
valid or raising ``ActivationError`` if not. | 2.410344 | 2.243006 | 1.074604 |
User = get_user_model()
try:
user = User.objects.get(**{
User.USERNAME_FIELD: username,
})
if user.is_active:
raise ActivationError(
self.ALREADY_ACTIVATED_MESSAGE,
code='already_activate... | def get_user(self, username) | Given the verified username, look up and return the
corresponding user account if it exists, or raising
``ActivationError`` if it doesn't. | 2.620408 | 2.357385 | 1.111574 |
if not isinstance(value, six.text_type):
return
if confusables.is_dangerous(value):
raise ValidationError(CONFUSABLE, code='invalid') | def validate_confusables(value) | Validator which disallows 'dangerous' usernames likely to
represent homograph attacks.
A username is 'dangerous' if it is mixed-script (as defined by
Unicode 'Script' property) and contains one or more characters
appearing in the Unicode Visually Confusable Characters file. | 5.205336 | 6.024405 | 0.864041 |
if '@' not in value:
return
local_part, domain = value.split('@')
if confusables.is_dangerous(local_part) or \
confusables.is_dangerous(domain):
raise ValidationError(CONFUSABLE_EMAIL, code='invalid') | def validate_confusables_email(value) | Validator which disallows 'dangerous' email addresses likely to
represent homograph attacks.
An email address is 'dangerous' if either the local-part or the
domain, considered on their own, are mixed-script and contain one
or more characters appearing in the Unicode Visually Confusable
Characters f... | 3.304685 | 3.298599 | 1.001845 |
for k, v in JS_FILE_MAPPING.items():
input_files = " ".join(v["input_files"])
output_file = v["output_file"]
uglifyjs_command = "uglifyjs {input_files} -o {output_file}".format(
input_files=input_files,
output_file=output_file
)
local(uglifyjs_co... | def minify_js_files() | This command minified js files with UglifyJS | 2.693926 | 2.547615 | 1.057431 |
for k, v in CSS_FILE_MAPPING.items():
input_files = " ".join(v["input_files"])
output_file = v["output_file"]
uglifyjs_command = "uglifycss {input_files} > {output_file}".format(
input_files=input_files,
output_file=output_file
)
local(uglifyjs_c... | def minify_css_files() | This command minified js files with UglifyCSS | 2.994328 | 2.894411 | 1.034521 |
dt = dt or datetime.now()
if timezone is None:
return dt.strftime('%Y-%m-%d %H:%M%z')
if not dt.tzinfo:
tz = timezone.get_current_timezone()
if not tz:
tz = timezone.utc
dt = dt.replace(tzinfo=timezone.get_current_timezone())
return dt.strftime("%Y-%m-%d ... | def timestamp_with_timezone(dt=None) | Return a timestamp with a timezone for the configured locale. If all else
fails, consider localtime to be UTC. | 2.236816 | 2.216631 | 1.009106 |
fn_path = getattr(settings, 'ROSETTA_ACCESS_CONTROL_FUNCTION', None)
if fn_path is None:
return is_superuser_staff_or_in_translators_group
# Dynamically load a permissions function
perm_module, perm_func = fn_path.rsplit('.', 1)
perm_module = importlib.import_module(perm_module)
ret... | def get_access_control_function() | Return a predicate for determining if a user can
access the Rosetta views | 4.120236 | 3.914958 | 1.052435 |
# (Formerly known as "rosetta_i18n_lang_code")
lang_id = self.kwargs['lang_id']
if lang_id not in {l[0] for l in rosetta_settings.ROSETTA_LANGUAGES}:
raise Http404
if not can_translate_language(self.request.user, lang_id):
raise Http404
return lan... | def language_id(self) | Determine/return the language id from the url kwargs, after
validating that:
1. the language is in rosetta_settings.ROSETTA_LANGUAGES, and
2. the current user is permitted to translate that language
(If either of the above fail, throw a 404.) | 4.277371 | 2.754805 | 1.552695 |
# This was formerly referred to as 'rosetta_i18n_fn'
idx = self.kwargs['idx']
idx = int(idx) # idx matched url re expression; calling int() is safe
third_party_apps = self.po_filter in ('all', 'third-party')
django_apps = self.po_filter in ('all', 'django')
pro... | def po_file_path(self) | Based on the url kwargs, infer and return the path to the .po file to
be shown/updated.
Throw a 404 if a file isn't found. | 5.532571 | 4.871686 | 1.135658 |
if self.po_file_is_writable:
# If we can write changes to file, then we pull it up fresh with
# each request.
# XXX: brittle; what if this path doesn't exist? Isn't a .po file?
po_file = pofile(self.po_file_path,
wrapwidth=ros... | def po_file(self) | Return the parsed .po file that is currently being translated/viewed.
(Note that this parsing also involves marking up each entry with a hash
of its contents.) | 3.078038 | 2.952383 | 1.042561 |
if 0 == len(in_) or 0 == len(out_):
return out_
if "\r" in out_ and "\r" not in in_:
out_ = out_.replace("\r", '')
if "\n" == in_[0] and "\n" != out_[0]:
out_ = "\n" + out_
elif "\n" != in_[0] and "\n" == out_[0]:
out_ = out_.lst... | def fix_nls(self, in_, out_) | Fixes submitted translations by filtering carriage returns and pairing
newlines at the begging and end of the translated string with the original | 1.933424 | 1.821579 | 1.0614 |
ref_lang = self._request_request('ref_lang', 'msgid')
if ref_lang != 'msgid':
allowed_languages = {l[0] for l in rosetta_settings.ROSETTA_LANGUAGES}
if ref_lang not in allowed_languages:
raise Http404
return ref_lang | def ref_lang(self) | Return the language id for the "reference language" (the language to
be translated *from*, if not English).
Throw a 404 if it's not in rosetta_settings.ROSETTA_LANGUAGES. | 5.42379 | 3.944837 | 1.374908 |
ref_pofile = None
if rosetta_settings.ENABLE_REFLANG and self.ref_lang != 'msgid':
replacement = '{separator}locale{separator}{ref_lang}'.format(
separator=os.sep,
ref_lang=self.ref_lang
)
pattern = '\{separator}locale\{separat... | def ref_lang_po_file(self) | Return a parsed .po file object for the "reference language", if one
exists, otherwise None. | 6.506901 | 6.190748 | 1.051069 |
if self.query:
msg_filter = None
else:
msg_filter = self._request_request('msg_filter', 'all')
available_msg_filters = {'untranslated', 'translated', 'fuzzy', 'all'}
if msg_filter not in available_msg_filters:
msg_filter = 'all'
... | def msg_filter(self) | Validate/return msg_filter from request (e.g. 'fuzzy', 'untranslated'),
or a default.
If a query is also specified in the request, then return None. | 5.447537 | 3.49381 | 1.559197 |
if self.query:
# Scenario #1: terms matching a search query
rx = re.compile(re.escape(self.query), re.IGNORECASE)
def concat_entry(e):
return (six.text_type(e.msgstr) +
six.text_type(e.msgid) +
six.text... | def get_entries(self) | Return a list of the entries (messages) that would be part of the
current "view"; that is, all of the ones from this .po file matching the
current query or msg_filter. | 2.797476 | 2.548099 | 1.097868 |
converted_gps_time = None
gps_timestamp = float(gps_time)
if gps_week != None:
# image date
converted_gps_time = GPS_START + datetime.timedelta(seconds=int(gps_week) *
SECS_IN_WEEK + gps_timestamp)
else:
# TAI s... | def convert_from_gps_time(gps_time, gps_week=None) | Convert gps time in ticks to standard time. | 3.753556 | 3.714213 | 1.010592 |
try:
return float(FFProbe(video_file).video[0].duration)
except Exception as e:
print("could not extract duration from video {} due to {}".format(video_file, e))
return None | def get_video_duration(video_file) | Get video duration in seconds | 3.320182 | 3.191192 | 1.040421 |
if not os.path.isfile(video_file):
print("Error, video file {} does not exist".format(video_file))
return None
try:
time_string = FFProbe(video_file).video[0].creation_time
try:
creation_time = datetime.datetime.strptime(
time_string, TIME_FORMAT)... | def get_video_end_time(video_file) | Get video end time in seconds | 2.402246 | 2.325373 | 1.033058 |
if not os.path.isfile(video_file):
print("Error, video file {} does not exist".format(video_file))
return None
video_end_time = get_video_end_time(video_file)
duration = get_video_duration(video_file)
if video_end_time == None or duration == None:
return None
else:
... | def get_video_start_time(video_file) | Get start time in seconds | 2.001729 | 2.007789 | 0.996982 |
'''
Format time string with invalid time elements in hours/minutes/seconds
Format for the timestring needs to be "%Y_%m_%d_%H_%M_%S"
e.g. 2014_03_31_24_10_11 => 2014_04_01_00_10_11
'''
subseconds = False
data = time_string.split("_")
hours, minutes, seconds = int(data[3]), int(data[4]),... | def format_time(time_string) | Format time string with invalid time elements in hours/minutes/seconds
Format for the timestring needs to be "%Y_%m_%d_%H_%M_%S"
e.g. 2014_03_31_24_10_11 => 2014_04_01_00_10_11 | 3.092477 | 1.962948 | 1.575425 |
'''
Extract a value for a list of ordered fields.
Return the value of the first existed field in the list
'''
for field in fields:
if field in self.tags:
if field_type is float:
value = eval_frac(self.tags[field].values[0])
... | def _extract_alternative_fields(self, fields, default=None, field_type=float) | Extract a value for a list of ordered fields.
Return the value of the first existed field in the list | 3.967167 | 2.622128 | 1.512957 |
'''
Name of file in the form {lat}_{lon}_{ca}_{datetime}_{filename}_{hash}
'''
mapillary_description = json.loads(self.extract_image_description())
lat = None
lon = None
ca = None
date_time = None
if "MAPLatitude" in mapillary_description:
... | def exif_name(self) | Name of file in the form {lat}_{lon}_{ca}_{datetime}_{filename}_{hash} | 2.688666 | 2.066146 | 1.301295 |
'''
Extract altitude
'''
altitude_ref = {
0: 1,
1: -1}
fields = ['GPS GPSAltitude', 'EXIF GPS GPSAltitude']
refs = ['GPS GPSAltitudeRef', 'EXIF GPS GPSAltitudeRef']
altitude, _ = self._extract_alternative_fields(fields, 0, float)
re... | def extract_altitude(self) | Extract altitude | 5.13908 | 4.822637 | 1.065616 |
'''
Extract capture time from EXIF
return a datetime object
TODO: handle GPS DateTime
'''
time_string = exif_datetime_fields()[0]
capture_time, time_field = self._extract_alternative_fields(
time_string, 0, str)
if time_field in exif_gps_date_f... | def extract_capture_time(self) | Extract capture time from EXIF
return a datetime object
TODO: handle GPS DateTime | 3.687655 | 3.25454 | 1.13308 |
'''
Extract image direction (i.e. compass, heading, bearing)
'''
fields = ['GPS GPSImgDirection',
'EXIF GPS GPSImgDirection',
'GPS GPSTrack',
'EXIF GPS GPSTrack']
direction, _ = self._extract_alternative_fields(fields)
... | def extract_direction(self) | Extract image direction (i.e. compass, heading, bearing) | 6.47623 | 4.248909 | 1.52421 |
'''
Extract geo-related information from exif
'''
altitude = self.extract_altitude()
dop = self.extract_dop()
lon, lat = self.extract_lon_lat()
d = {}
if lon is not None and lat is not None:
d['latitude'] = lat
d['longitude'] = lon
... | def extract_geo(self) | Extract geo-related information from exif | 2.943413 | 2.34335 | 1.25607 |
'''
Extract timestamp from GPS field.
'''
gps_date_field = "GPS GPSDate"
gps_time_field = "GPS GPSTimeStamp"
gps_time = 0
if gps_date_field in self.tags and gps_time_field in self.tags:
date = str(self.tags[gps_date_field].values).split(":")
... | def extract_gps_time(self) | Extract timestamp from GPS field. | 2.465314 | 2.272519 | 1.084837 |
'''
Extract a list of exif infos
'''
width, height = self.extract_image_size()
make, model = self.extract_make(), self.extract_model()
orientation = self.extract_orientation()
geo = self.extract_geo()
capture = self.extract_capture_time()
direction... | def extract_exif(self) | Extract a list of exif infos | 2.995027 | 2.659517 | 1.126155 |
'''
Extract image height and width
'''
width, _ = self._extract_alternative_fields(
['Image ImageWidth', 'EXIF ExifImageWidth'], -1, int)
height, _ = self._extract_alternative_fields(
['Image ImageLength', 'EXIF ExifImageLength'], -1, int)
return w... | def extract_image_size(self) | Extract image height and width | 4.514529 | 3.909804 | 1.154669 |
'''
Extract camera make
'''
fields = ['EXIF LensMake', 'Image Make']
make, _ = self._extract_alternative_fields(
fields, default='none', field_type=str)
return make | def extract_make(self) | Extract camera make | 12.58847 | 8.988137 | 1.400565 |
'''
Extract camera model
'''
fields = ['EXIF LensModel', 'Image Model']
model, _ = self._extract_alternative_fields(
fields, default='none', field_type=str)
return model | def extract_model(self) | Extract camera model | 13.553288 | 9.841224 | 1.377195 |
'''
Extract camera firmware (tag is called 'software' in EXIF)
'''
fields = ['Image Software']
software, _ = self._extract_alternative_fields(
fields, default="", field_type=str)
return software | def extract_firmware(self) | Extract camera firmware (tag is called 'software' in EXIF) | 21.641893 | 7.680771 | 2.817672 |
'''
Extract image orientation
'''
fields = ['Image Orientation']
orientation, _ = self._extract_alternative_fields(
fields, default=1, field_type=int)
if orientation not in range(1, 9):
return 1
return orientation | def extract_orientation(self) | Extract image orientation | 7.55509 | 6.024837 | 1.253991 |
'''
Extract microseconds
'''
fields = [
'Image SubSecTimeOriginal',
'EXIF SubSecTimeOriginal',
'Image SubSecTimeDigitized',
'EXIF SubSecTimeDigitized',
'Image SubSecTime',
'EXIF SubSecTime'
]
sub_sec,... | def extract_subsec(self) | Extract microseconds | 4.996644 | 4.452295 | 1.122262 |
'''
Check existence of a list fields in exif
'''
for rexif in fields:
vflag = False
for subrexif in rexif:
if subrexif in self.tags:
vflag = True
if not vflag:
print("Missing required EXIF tag: {0} fo... | def fields_exist(self, fields) | Check existence of a list fields in exif | 5.610631 | 3.846395 | 1.458673 |
'''
Check existence of required Mapillary tags
'''
description_tag = "Image ImageDescription"
if description_tag not in self.tags:
return False
for requirement in ["MAPSequenceUUID", "MAPSettingsUserKey", "MAPCaptureTime", "MAPLongitude", "MAPLatitude"]:
... | def mapillary_tag_exists(self) | Check existence of required Mapillary tags | 7.070605 | 5.475812 | 1.291243 |
'''
Send query to the search API and get dict with image data.
'''
# Create URL
params = urllib.urlencode(zip(
['client_id', 'bbox', 'per_page'],
[CLIENT_ID, ','.join([str(min_lon), str(min_lat), str(
max_lon), str(max_lat)]), str(max_results)]
))
print(MAPILLARY... | def query_search_api(min_lat, max_lat, min_lon, max_lon, max_results) | Send query to the search API and get dict with image data. | 4.100819 | 3.452658 | 1.187728 |
'''
Download images in query result to path.
Return list of downloaded images with lat,lon.
There are four sizes available: 320, 640, 1024 (default), or 2048.
'''
im_size = "thumb-{0}.jpg".format(size)
im_list = []
for im in query:
# Use key to create url to download from and f... | def download_images(query, path, size=1024) | Download images in query result to path.
Return list of downloaded images with lat,lon.
There are four sizes available: 320, 640, 1024 (default), or 2048. | 4.665938 | 3.385575 | 1.378182 |
'''
Read location and time stamps from a track in a GPX file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly.
'''
with open(gpx_file, 'r') as f:
gpx = gpxpy.parse(f)
points = []
... | def get_lat_lon_time_from_gpx(gpx_file, local_time=True) | Read location and time stamps from a track in a GPX file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly. | 2.487591 | 1.588807 | 1.565698 |
'''
Read location and time stamps from a track in a NMEA file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly.
'''
with open(nmea_file, "r") as f:
lines = f.readlines()
lines = ... | def get_lat_lon_time_from_nmea(nmea_file, local_time=True) | Read location and time stamps from a track in a NMEA file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly. | 3.130302 | 1.971723 | 1.587598 |
'''
Compute ECEF XYZ from latitude, longitude and altitude.
All using the WGS94 model.
Altitude is the distance to the WGS94 ellipsoid.
Check results here http://www.oc.nps.edu/oc2902w/coord/llhxyz.htm
'''
a2 = WGS84_a ** 2
b2 = WGS84_b ** 2
lat = math.radians(lat)
lon = math.r... | def ecef_from_lla(lat, lon, alt) | Compute ECEF XYZ from latitude, longitude and altitude.
All using the WGS94 model.
Altitude is the distance to the WGS94 ellipsoid.
Check results here http://www.oc.nps.edu/oc2902w/coord/llhxyz.htm | 2.425774 | 1.537908 | 1.57732 |
'''
Distance between two (lat,lon) pairs.
>>> p1 = (42.1, -11.1)
>>> p2 = (42.2, -11.3)
>>> 19000 < gps_distance(p1, p2) < 20000
True
'''
x1, y1, z1 = ecef_from_lla(latlon_1[0], latlon_1[1], 0.)
x2, y2, z2 = ecef_from_lla(latlon_2[0], latlon_2[1], 0.)
dis = math.sqrt((x1 - x2) ... | def gps_distance(latlon_1, latlon_2) | Distance between two (lat,lon) pairs.
>>> p1 = (42.1, -11.1)
>>> p2 = (42.2, -11.3)
>>> 19000 < gps_distance(p1, p2) < 20000
True | 2.004842 | 1.469445 | 1.364353 |
'''
Returns the radius of an entire GPS track. Used to calculate whether or not the entire sequence was just stationary video
Takes a sequence of points as input
'''
latlon_list = []
# Remove timestamps from list
for idx, point in enumerate(latlon_track):
lat = latlon_track[idx][1]
... | def get_max_distance_from_start(latlon_track) | Returns the radius of an entire GPS track. Used to calculate whether or not the entire sequence was just stationary video
Takes a sequence of points as input | 3.815135 | 2.043617 | 1.866854 |
'''
Returns the total distance traveled of a GPS track. Used to calculate whether or not the entire sequence was just stationary video
Takes a sequence of points as input
'''
latlon_list = []
# Remove timestamps from list
for idx, point in enumerate(latlon_track):
lat = latlon_track[... | def get_total_distance_traveled(latlon_track) | Returns the total distance traveled of a GPS track. Used to calculate whether or not the entire sequence was just stationary video
Takes a sequence of points as input | 3.614199 | 2.034278 | 1.77665 |
'''
Convert from degrees, minutes, seconds to decimal degrees.
@author: mprins
'''
dms = float(degrees) + float(minutes) / 60 + float(seconds) / 3600
if hemisphere in "WwSs":
dms = -1 * dms
return dms | def dms_to_decimal(degrees, minutes, seconds, hemisphere) | Convert from degrees, minutes, seconds to decimal degrees.
@author: mprins | 3.990396 | 2.839958 | 1.40509 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.