apicrud.utils

utils.py

Utilities

Miscellaneous utility functions that don’t fit elsewhere

created 11-apr-2020 by richb@instantlinux.net

Functions

gen_id([length, prefix, chars])

Record IDs are random 48-bit RFC-4648 radix-64 with a fixed prefix to make them (somewhat) more human-recognizable

identity_normalize(identity)

Normalize an email address for use as an identity: downcase and in certain cases remove characters.

req_duration()

Report request duration as milliseconds

strip_tags(html)

Convert html to plain-text by stripping tags

utcnow()

For mocking: unittest.mock can’t patch out datetime.utcnow directly

Classes

HtmlStripper()

class apicrud.utils.HtmlStripper
get_data()

return the stripped html

Returns: str

handle_data(d)

add a string to the html object

Parameters

d (str) – chunk of html

apicrud.utils.gen_id(length=8, prefix='x-', chars='-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz')

Record IDs are random 48-bit RFC-4648 radix-64 with a fixed prefix to make them (somewhat) more human-recognizable

First 15 bits are generated from Unix epoch to make them sortable by date (granularity 24 hours); rolls over after year 2107

Parameters
  • length (int) – length of generated portion after prefix

  • prefix (str) – prefix to distinguish ID type

  • chars (str) – set of characters to choose from for random portion

apicrud.utils.identity_normalize(identity)

Normalize an email address for use as an identity: downcase and in certain cases remove characters. This is required to secure against a type of attack involving password-resets: an example of the vulnerability is described here:

https://jameshfisher.com/2018/04/07/the-dots-do-matter-how-to-scam-a-gmail-user/

Parameters

identity (str) – a raw email address

Returns: str

apicrud.utils.req_duration()

Report request duration as milliseconds

apicrud.utils.strip_tags(html)

Convert html to plain-text by stripping tags

Parameters

html (str) – an html document

apicrud.utils.utcnow()

For mocking: unittest.mock can’t patch out datetime.utcnow directly