allura.lib.helpers

class allura.lib.helpers.DateTimeConverter(*args, **kw)

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

allura.lib.helpers.absurl(url)

Given a root-relative URL, return a full URL including protocol and host

allura.lib.helpers.ago(start_time, show_date_after=7)

Return time since starting time as a rounded, human readable string. E.g., “3 hours ago” Also works with future times E.g., “in 3 hours”

allura.lib.helpers.auditlog_user(message, *args, **kwargs)

Create an audit log entry for a user, including the IP address

Parameters:
allura.lib.helpers.config_with_prefix(d, prefix)

Return a subdictionary keys with a given prefix, with the prefix stripped

allura.lib.helpers.convert_bools(conf, prefix='')

For a given dict, automatically convert any true/false string values into bools. Only applies to keys starting with the prefix.

Parameters:
Returns:

dict

allura.lib.helpers.emojize(text)

Coverts emoji codes to unicode emojis

allura.lib.helpers.encode_keys(d)

Encodes the unicode keys of d, making the result a valid kwargs argument

class allura.lib.helpers.exceptionless(error_result, log=None)

Decorator making the decorated function return ‘error_result’ on any exceptions rather than propagating exceptions up the stack

class allura.lib.helpers.fixed_attrs_proxy(obj, **kw)

On attribute lookup, if keyword parameter matching attribute name was provided during object construction, returns it’s value. Otherwise proxies to obj.

allura.lib.helpers.get_current_reaction(react_users_dict)

Return current selected reaction for given react_users dict

allura.lib.helpers.get_first(d, key)

Return value for d[key][0] if d[key] is a list with elements, else return d[key].

Useful to retrieve values from solr index (e.g. title and text fields), which are stored as lists.

allura.lib.helpers.get_tool_packages(tool_name)

Return package for given tool (e.g. ‘forgetracker’ for ‘tickets’)

allura.lib.helpers.get_user_status(user)

Get user status based on disabled and pending attrs

Parameters:

user – a allura.model.auth.User

allura.lib.helpers.iter_entry_points(group, *a, **kw)

Yields entry points that have not been disabled in the config.

If group is “allura” (Allura tool entry points) or one of subgroups (e.g. “allura.phone”), this function also checks for multiple entry points with the same name. If there are multiple entry points with the same name, and one of them is a subclass of the other(s), it will be yielded, and the other entry points with that name will be ignored. If a subclass is not found, an ImportError will be raised.

This treatment of “allura” and “allura.*” entry points allows tool authors to subclass another tool while reusing the original entry point name.

allura.lib.helpers.log_if_changed(artifact, attr, new_val, message)

Set artifact.attr to new_val if changed. Add AuditLog record.

allura.lib.helpers.login_overlay(exceptions=None)

Override the default behavior of redirecting to the auth.login_url and instead display an overlay with content from auth.login_fragment_url.

This is to allow pages that require authentication for any actions but not for the initial view to be more apparent what you will get once logged in.

This should be wrapped around call to require_access() (presumably in the _check_security() method on a controller). The exceptions param can be given a list of exposed views to leave with the original behavior.

For example:

class MyController(BaseController);
    def _check_security(self):
        with login_overlay(exceptions=['process']):
            require_access(self.neighborhood, 'register')

    @expose
    def index(self, *args, **kw):
        return {}

    @expose
    def list(self, *args, **kw):
        return {}

    @expose
    def process(self, *args, **kw):
        return {}

This would show the overlay to unauthenticated users who visit / or /list but would perform the normal redirect when /process is visited.

allura.lib.helpers.make_safe_path_portion(ustr, relaxed=True)

Return an ascii representation of ustr that conforms to mount point naming rules.

Will return an empty string if no char in ustr is ascii-encodable.

Parameters:

relaxed – Use relaxed mount point naming rules (allows more characters. See re_relaxed_tool_mount_point_fragment.

Returns:

The converted string.

allura.lib.helpers.ming_config(**conf)

Temporarily swap in a new ming configuration, restoring the previous one when the contextmanager exits.

Parameters:

**conf – keyword arguments defining the new ming configuration

allura.lib.helpers.ming_config_from_ini(ini_path)

Temporarily swap in a new ming configuration, restoring the previous one when the contextmanager exits.

Parameters:

ini_path – Path to ini file containing the ming configuration

allura.lib.helpers.notifications_disabled(project, disabled=True)

Temporarily disable email notifications on a project.

allura.lib.helpers.null_contextmanager(returning=None, *args, **kw)

A no-op contextmanager.

allura.lib.helpers.paging_sanitizer(limit, page, total_count=9223372036854775807, zero_based_pages=True)

Return limit, page - both converted to int and constrained to valid ranges based on total_count.

Useful for sanitizing limit and page query params.

See also g.handle_paging which also checks prefs

allura.lib.helpers.push_context(project_id, mount_point=None, app_config_id=None, neighborhood=None)

A context manager to set c.project and c.app globals temporarily. To set c.user or others, use push_config(c, user=...)

Parameters:
  • project_id (ObjectId|str) – _id or shortname of a project

  • mount_point (str) – mount point to set c.app by

  • app_config_id (ObjectId|str) – alternative to mount_point parameter

  • neighborhood (str) – neighborhood full name, required if project is specified by shortname

allura.lib.helpers.querystring(request, url_params)

add/update/remove url parameters. When a value is set to None the key will be removed from the final constructed url.

Parameters:
  • request – request object

  • url_params – dict with the params that should be updated/added/deleted.

Returns:

a full url with updated url parameters.

allura.lib.helpers.rate_limit(cfg_opt, artifact_count, start_date, exception=None)

Check the various config-defined artifact creation rate limits, and if any are exceeded, raise exception.

Parameters:

artifact_count – a number or callable (for lazy evaluation)

allura.lib.helpers.render_any_markup(name, txt, code_mode=False, linenumbers_style='table')

renders markdown using allura enhancements if file is in markdown format renders any other markup format using the pypeline Returns jinja-safe text

allura.lib.helpers.set_context(project_shortname_or_id, mount_point=None, app_config_id=None, neighborhood=None)

Set c.project and c.app globals

Parameters:
  • project_id (ObjectId|str) – _id or shortname of a project

  • mount_point (str) – mount point to set c.app by

  • app_config_id (ObjectId|str) – alternative to mount_point parameter

  • neighborhood (str) – neighborhood full name, required if project is specified by shortname

allura.lib.helpers.slugify(name, allow_periods=False)

Returns a tuple with slug and lowered slug based on name

allura.lib.helpers.strip_bad_unicode(s)

xml doesn’t like some control characters: https://www.w3.org/TR/REC-xml/#charsets :param s: :return:

allura.lib.helpers.topological_sort(items, partial_order)

Perform topological sort. items is a list of items to be sorted. partial_order is a list of pairs. If pair (a,b) is in it, it means that item a should appear before item b. Returns a list of the items in one of the possible orders, or None if partial_order contains a loop.

Modified from: http://www.bitformation.com/art/python_toposort.html

allura.lib.helpers.unidiff(old, new)

Returns unified diff between one and two.

allura.lib.helpers.urlopen(url: str | Request, retries=3, codes=(408, 500, 502, 503, 504), timeout=None)

Open url, optionally retrying if an error is encountered.

Socket and other IO errors will always be retried if retries > 0. HTTP errors are retried if the error code is passed in codes.

Parameters:
  • retries – Number of time to retry.

  • codes – HTTP error codes that should be retried.

allura.lib.helpers.urlquote_path_only(url)

Given a relative url like /fö/bar/?sdf&sdf urlquote only the path portion of it, leaving any querystring or target hash unquoted :param url: :return: