MoinMoin.util package

Submodules

MoinMoin.util.StringIOClosing module

class MoinMoin.util.StringIOClosing.StringIO(buf='')[source]

Bases: StringIO.StringIO

same as StringIO from stdlib, but enhanced with a context manager, so it can be used within a “with” statement and gets automatically closed when the with-block is left. The standard “file” object behaves that way, so a StringIO “file emulation” should behave the same.

MoinMoin.util.SubProcess module

Enhanced subprocess.Popen subclass, supporting:
  • .communicate() with timeout
Sample usage:
out, err = Popen(...).communicate(input, timeout=300)
class MoinMoin.util.SubProcess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)[source]

Bases: subprocess.Popen

communicate(input=None, timeout=None)[source]

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.

communicate() returns a tuple (stdout, stderr).

MoinMoin.util.SubProcess.exec_cmd(cmd, input=None, timeout=None)[source]

MoinMoin.util.clock module

MoinMoin - Clock

class MoinMoin.util.clock.Clock[source]

Bases: object

Helper class for measuring the time needed to run code.

Usage:
flaskg.clock.start(‘mytimer’) # do something flaskg.clock.stop(‘mytimer’) # or if you want to use its value later timerval = flaskg.clock.stop(‘mytimer’)

Starting a timer multiple times is supported but the one started last has to be stopped first.

start(timer)[source]
stop(timer)[source]
MoinMoin.util.clock.add_timing(f, name=None)[source]
MoinMoin.util.clock.timed(name=None)[source]

MoinMoin.util.crypto module

MoinMoin - Cryptographic and random functions

Features:

  • generate password recovery tokens
  • verify password recovery tokens
  • generate random strings of given length (for salting)
MoinMoin.util.crypto.cache_key(**kw)[source]

Calculate a cache key (ascii only)

Important key properties:

  • The key must be different for different <kw>.
  • Key is pure ascii
Parameters:kw – keys/values to compute cache key from
MoinMoin.util.crypto.generate_token(key=None, stamp=None)[source]

generate a pair of a secret key and a crypto token.

you can use this to implement a password recovery functionality by calling generate_token() and transmitting the returned token to the (correct) user (e.g. by email) and storing the returned (secret) key into the user’s profile on the server side.

after the user received the token, he returns to the wiki, gives his user name or email address and the token he received. read the (secret) key from the user profile and call valid_token(key, token) to verify if the token is valid. if it is, consider the user authenticated, remove the secret key from his profile and let him reset his password.

Parameters:
  • key – give it to recompute some specific token for verification
  • stamp – give it to recompute some specific token for verification
Return type:

2-tuple

Returns:

key, token (both unicode)

MoinMoin.util.crypto.make_uuid()
MoinMoin.util.crypto.random_string(length, allowed_chars=None)[source]

Generate a random string with given length consisting of the given characters.

Note: this is now just a little wrapper around passlib’s randomness code.

Parameters:
  • length – length of the string
  • allowed_chars – string with allowed characters or None to indicate all 256 byte values should be used
Returns:

random string

MoinMoin.util.crypto.valid_token(key, token, timeout=7200)[source]

check if token is valid with respect to the secret key, the token must not be older than timeout seconds.

Parameters:
  • key – give the secret key to verify the token
  • token – the token to verify
  • timeout – timeout seconds, set to None to ignore timeout
Return type:

bool

Returns:

token is valid and not timed out

MoinMoin.util.diff3 module

MoinMoin - diff3 algorithm

MoinMoin.util.diff3.find_match(list1, list2, nr1, nr2, mincount=3)[source]

searches next matching pattern with lenght mincount if no pattern is found len of the both lists is returned

MoinMoin.util.diff3.main()[source]
MoinMoin.util.diff3.match(list1, list2, nr1, nr2, maxcount=3)[source]

return the number matching items after the given positions maximum maxcount lines are are processed

MoinMoin.util.diff3.merge(old, other, new, allow_conflicts=1, *markers)[source]

do line by line diff3 merge input must be lists containing single lines

MoinMoin.util.diff3.text_merge(old, other, new, allow_conflicts=1, *markers)[source]

do line by line diff3 merge with three strings

MoinMoin.util.diff3.tripple_match(old, other, new, other_match, new_match)[source]

find next matching pattern unchanged in both other and new return the position in all three lists

MoinMoin.util.diff_datastruct module

class MoinMoin.util.diff_datastruct.UndefinedType[source]

Bases: object

Represents a non-existing value

MoinMoin.util.diff_datastruct.diff(d1, d2, basekeys=None)[source]

Get the diff of 2 datastructures (usually 2 meta dicts)

Parameters:
  • d1 – old datastructure
  • d2 – new datastructure
  • basekeys – list of data keys’ basenames (default: None, meaning [])
Returns:

a list of tuples of the format (<change type>, <basekeys>, <value>) that can be used to format a diff

MoinMoin.util.diff_datastruct.make_text_diff(changes)[source]

Transform change tuples into text diffs

Parameters:changes – a list of tuples of the format (<change type>, <basekeys>, <value>) that represent a diff
Returns:a generator of text diffs

MoinMoin.util.diff_html module

MoinMoin - Side by side diffs

MoinMoin.util.diff_html.diff(old, new)[source]

Find changes between old and new and return HTML markup visualising them.

Parameters:
  • old – old text [unicode]
  • new – new text [unicode]
MoinMoin.util.diff_html.indent(line)[source]

MoinMoin.util.diff_text module

MoinMoin - simple text diff (uses difflib)

MoinMoin.util.diff_text.diff(oldlines, newlines, **kw)[source]

Find changes between oldlines and newlines.

Parameters:
  • oldlines – list of old text lines
  • newlines – list of new text lines
  • ignorews – if 1: ignore whitespace
Return type:

list

Returns:

lines like diff tool does output.

MoinMoin.util.filesys module

MoinMoin - File System Utilities

MoinMoin.util.filesys.access_denied_decorator(fn)[source]

Due to unknown reasons, some os.* functions on Win32 sometimes fail with Access Denied (although access should be possible). Just retrying it a bit later works and this is what we do.

MoinMoin.util.filesys.chmod(name, mode, catchexception=True)[source]

change mode of some file/dir on platforms that support it.

MoinMoin.util.filesys.copystat(src, dst)[source]

Copy stat bits from src to dst

This should be used when shutil.copystat would be used on directories on win32 because win32 does not support utime() for directories.

According to the official docs written by Microsoft, it returns ENOACCES if the supplied filename is a directory. Looks like a trainee implemented the function.

MoinMoin.util.filesys.copytree(src, dst, symlinks=False)[source]

Recursively copy a directory tree using copy2().

The destination directory must not already exist. If exception(s) occur, an Error is raised with a list of reasons.

If the optional symlinks flag is true, symbolic links in the source tree result in symbolic links in the destination tree; if it is false, the contents of the files pointed to by symbolic links are copied.

In contrary to shutil.copytree, this version also copies directory stats, not only file stats.

MoinMoin.util.filesys.fuid(filename, max_staleness=3600)[source]

return a unique id for a file

Using just the file’s mtime to determine if the file has changed is not reliable - if file updates happen faster than the file system’s mtime granularity, then the modification is not detectable because the mtime is still the same.

This function tries to improve by using not only the mtime, but also other metadata values like file size and inode to improve reliability.

For the calculation of this value, we of course only want to use data that we can get rather fast, thus we use file metadata, not file data (file content).

Note: depending on the operating system capabilities and the way the
file update is done, this function might return the same value even if the file has changed. It should be better than just using file’s mtime though. max_staleness tries to avoid the worst for these cases.
Parameters:
  • filename – file name of the file to look at
  • max_staleness – if a file is older than that, we may consider it stale and return a different uid - this is a dirty trick to work around changes never being detected. Default is 3600 seconds, use None to disable this trickery. See below for more details.
Returns:

an object that changes value if the file changed, None is returned if there were problems accessing the file

MoinMoin.util.filesys.rename_no_overwrite(oldname, newname, delete_old=False)[source]

Multiplatform rename

This kind of rename is doing things differently: it fails if newname already exists. This is the usual thing on win32, but not on posix.

If delete_old is True, oldname is removed in any case (even if the rename did not succeed).

MoinMoin.util.filesys.touch(name)[source]

MoinMoin.util.forms module

MoinMoin - form helpers for flatland / jinja2

class MoinMoin.util.forms.FileStorage(value=Unspecified, **kw)[source]

Bases: flatland.schema.scalars.Scalar

Schema element for Werkzeug FileStorage instances.

adapt(value)[source]
MoinMoin.util.forms.autofocus_filter(tagname, attributes, contents, context, bind)[source]
MoinMoin.util.forms.button_filter(tagname, attributes, contents, context, bind)[source]

Show translated text in clickable buttons and submits.

MoinMoin.util.forms.error_filter(tagname, attributes, contents, context, bind)
MoinMoin.util.forms.error_filter_factory(class_='moin-error')[source]

Returns an HTML generation filter annotating field CSS class on error.

Parameters:class – The css class to apply in case of validation error on a field. Default: ‘error’
MoinMoin.util.forms.label_filter(tagname, attributes, contents, context, bind)[source]

Provide a translated, generated fallback for field labels.

MoinMoin.util.forms.make_generator()[source]

make an html generator

MoinMoin.util.forms.placeholder_filter(tagname, attributes, contents, context, bind)[source]
MoinMoin.util.forms.required_filter(tagname, attributes, contents, context, bind)[source]

MoinMoin.util.interwiki module

MoinMoin - interwiki support code

class MoinMoin.util.interwiki.CompositeName[source]

Bases: MoinMoin.util.interwiki.CompositeName

namedtuple to hold the compositename

fullname
get_root_fqname()[source]

Set value to the item_root of that namespace, and return the new CompisteName.

query

returns a dict that can be used as a whoosh query to lookup index documents matching this CompositeName

split

returns a dict of field_names/field_values

class MoinMoin.util.interwiki.InterWikiMap(s)[source]

Bases: object

Parse a valid interwiki map file/string, transforming into a simple python dict object. Provides a set of utilities for parsing and checking a interwiki maps.

SKIP = '#'
static from_file(filename)[source]

Load and parse a valid interwiki map file.

static from_string(ustring)[source]

Load and parse a valid interwiki map “unicode” object.

MoinMoin.util.interwiki.getInterwikiHome(username)[source]

Get a user’s homepage.

cfg.user_homewiki influences behaviour of this: ‘Self’ does mean we store user homepage in THIS wiki. When set to our own interwikiname, it behaves like with ‘Self’.

‘SomeOtherWiki’ means we store user homepages in another wiki.

Parameters:username – the user’s name
Return type:tuple
Returns:(wikiname, itemname)
MoinMoin.util.interwiki.getInterwikiName(item_name)[source]

Get the (fully qualified) interwiki name of a local item name.

Parameters:item_name – item name (unicode)
Return type:unicode
Returns:wiki_name:item_name
MoinMoin.util.interwiki.get_download_file_name(fqname)[source]

returns the filename that is used for downloading items

MoinMoin.util.interwiki.get_fqname(item_name, field, namespace)[source]

Compute composite name from item_name, field, namespace composite name == [NAMESPACE/][@FIELD/]NAME

MoinMoin.util.interwiki.is_known_wiki(wiki_name)[source]

check if <wiki_name> is a known wiki name

Note: interwiki_map should have entries for the special wikinames denoting THIS wiki, so we do not need to check these names separately.

MoinMoin.util.interwiki.is_local_wiki(wiki_name)[source]

check if <wiki_name> is THIS wiki

MoinMoin.util.interwiki.join_wiki(wikiurl, wikitail, field, namespace)[source]

Add a (url_quoted) page name to an interwiki url.

Note: We can’t know what kind of URL quoting a remote wiki expects.
We just use a utf-8 encoded string with standard URL quoting.
Parameters:
  • wikiurl – wiki url, maybe including a $PAGE placeholder
  • wikitail – page name
  • namespace – namespace
Return type:

string

Returns:

generated URL of the page in the other wiki

MoinMoin.util.interwiki.split_fqname(url)[source]

Split a fully qualified url into namespace, field and pagename url -> [NAMESPACE/][@FIELD/]NAME

Parameters:url – the url to split
Returns:a namedtuple CompositeName(namespace, field, itemname)

Examples:

url: u'ns1/ns2/@itemid/Page' return u'ns1/ns2', u'itemid', u'Page'
url: u'@revid/OtherPage' return u'', u'revid', u'OtherPage'
url: u'ns1/Page' return u'ns1', u'', u'Page'
url: u'ns1/ns2/@notfield' return u'ns1/ns2', u'', u'@notfield'
MoinMoin.util.interwiki.split_interwiki(wikiurl)[source]

Split a interwiki name, into wikiname and pagename, e.g:

'MoinMoin/FrontPage' -> "MoinMoin", "", "", "FrontPage"
'FrontPage' -> "Self", "", "", "FrontPage"
'MoinMoin/Page with blanks' -> "MoinMoin", "", "", "Page with blanks"
'MoinMoin/' -> "MoinMoin", "", "", ""
'MoinMoin/@Someid/SomeValue' -> "MoinMoin", "", "Someid", "SomeValue" if Someid field exists or "MoinMoin", "", "", "Someid/SomePage" if not
'MoinMoin/interwikins/AnyPage' -> "MoinMoin", "interwikins", "", "AnyPage"
'ns/AnyPage' -> "Self", "ns", "", "AnyPage" if ns namespace exists or "Self", "", "", "ns:AnyPage" if not.
'ns1/ns2/AnyPage' -> "Self", "ns1/ns2", "", "AnyPage" if ns1/ns2 namespace exists OR
                     "Self", "ns1", "", "ns2/AnyPage" if ns1 namespace exists OR
                     "Self", "", "", "ns1/ns2/AnyPage" else.
'MoinMoin/ns/@Somefield/AnyPage' -> "MoinMoin", "ns", "", "@Somefield/AnyPage" if ns namespace exists and field Somefield does not OR
                                 "MoinMoin", "ns", "Somefield", "AnyPage" if ns namespace and field Somefield exist OR
                                 "MoinMoin", "", "", "ns/@Somefield/AnyPage" else.
:param wikiurl: the url to split
:rtype: tuple
:returns: (wikiname, namespace, field, pagename)
MoinMoin.util.interwiki.url_for_item(item_name, wiki_name=u'', field=u'', namespace=u'', rev=u'current', endpoint=u'frontend.show_item', _external=False)[source]

Compute URL for some local or remote/interwiki item.

For local items: give <rev> to get the url of some specific revision. give the <endpoint> to get the url of some specific view, give _external=True to compute fully specified URLs.

For remote/interwiki items: If you just give <item_name> and <wiki_name>, a generic interwiki URL will be built. If you also give <rev> and/or <endpoint>, it is assumed that remote wiki URLs are built in the same way as local URLs. Computed URLs are always fully specified.

MoinMoin.util.iri module

MoinMoin - Generic? IRI implementation

Implements the generic IRI form as defined in RFC 3987.

class MoinMoin.util.iri.Iri(_iri=None, _quoted=True, scheme=None, authority=None, path=None, query=None, fragment=None)[source]

Bases: MoinMoin.util.pysupport.AutoNe

authority

Authority part of the IRI.

fragment

Fragment part of the IRI.

overall_rules = '\n ^\n (\n (?P<scheme>\n [^:/?\\#]+\n )\n :\n )?\n (\n //\n (?P<authority>\n [^/?\\#]*\n )\n )?\n (?P<path>\n [^?\\#]+\n )?\n (\n \\?\n (?P<query>\n [^\\#]*\n )\n )?\n (\n \\#\n (?P<fragment>\n .*\n )\n )?\n '
path

Path part of the IRI.

query

Query part of the IRI.

scheme

Scheme part of the IRI.

class MoinMoin.util.iri.IriAuthority(iri_authority=None, _quoted=True, userinfo=None, host=None, port=None)[source]

Bases: MoinMoin.util.pysupport.AutoNe

authority_rules = '\n ^\n (\n (?P<userinfo>\n [^@]*\n )\n @\n )?\n (?P<host>\n .*?\n )\n (\n :\n (?P<port>\n \\d*\n )\n )?\n $\n '
fullquoted

Full quoted form of the authority part of the IRI.

All characters which are illegal in the authority part are encoded. Used to generate the full IRI.

host
quoted

Minimal quoted form of the authority part of the IRI.

Only ‘%’ and illegal UTF-8 sequences are encoded. Primarily used to have a one-to-one mapping with non-UTF-8 URIs.

urlquoted

URI quoted form of the authority part of the IRI.

All characters which are illegal in the authority part are encoded. Used to generate the full URI.

userinfo
class MoinMoin.util.iri.IriAuthorityHost[source]

Bases: MoinMoin.util.iri._Value

class MoinMoin.util.iri.IriAuthorityUserinfo[source]

Bases: MoinMoin.util.iri._Value

class MoinMoin.util.iri.IriFragment[source]

Bases: MoinMoin.util.iri._Value

quote_filter = frozenset(['@', ':', '?', '/'])
class MoinMoin.util.iri.IriPath(iri_path=None, _quoted=True)[source]

Bases: MoinMoin.util.pysupport.AutoNe

extend(value)[source]
fullquoted

Full quoted form of the path part of the IRI.

All characters which are illegal in the path part are encoded. Used to generate the full IRI.

quoted

Minimal quoted form of the path part of the IRI.

Only ‘%’ and illegal UTF-8 sequences are encoded. Primarily used to have a one-to-one mapping with non-UTF-8 URIs.

urlquoted

URI quoted form of the path part of the IRI.

All characters which are illegal in the path part are encoded. Used to generate the full URI.

class MoinMoin.util.iri.IriPathSegment[source]

Bases: MoinMoin.util.iri._Value

quote_filter = frozenset(['@', ':', '/'])
class MoinMoin.util.iri.IriQuery[source]

Bases: MoinMoin.util.iri._Value

quote_filter = frozenset(['@', ':', '?', '/'])

MoinMoin.util.mime module

MoinMoin - MIME helpers

class MoinMoin.util.mime.Type[source]

Bases: MoinMoin.util.mime.Type, MoinMoin.util.pysupport.AutoNe

Variables:
  • type – Type part
  • subtype – Subtype part
  • parameters – Parameters part
issupertype(other)[source]

Check if this object is a super type of the other

A super type is defined as - the other type matches this (possibly wildcard) type, - the other subtype matches this (possibly wildcard) subtype and - the other parameters are a supperset of this one.

s = frozenset(['!', '#', '%', '$', "'", '&', '+', '*', '-', '.', '1', '0', '3', '2', '5', '4', '7', '6', '9', '8', 'A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'J', 'M', 'L', 'O', 'N', 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X', 'Z', '_', '^', 'a', '`', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', 's', 'r', 'u', 't', 'w', 'v', 'y', 'x', '{', 'z', '}', '|', '~'])

MoinMoin.util.mimetype module

MoinMoin - mimetype support

class MoinMoin.util.mimetype.MimeType(mimestr=None, filename=None)[source]

Bases: object

represents a mimetype like text/plain

as_attachment(cfg)[source]
content_type(major=None, minor=None, charset=None, params=None)[source]

return a string suitable for Content-Type header

mime_type()[source]

return a string major/minor only, no params

module_name()[source]

convert this mimetype to a string useable as python module name, we yield the exact module name first and then proceed to shorter module names (useful for falling back to them, if the more special module is not found) - e.g. first “text_python”, next “text”. Finally, we yield “application_octet_stream” as the most general mimetype we have.

Hint: the fallback handler module for text/* should be implemented in module “text” (not “text_plain”)

parse_filename(filename)[source]
parse_format(format)[source]

maps from what we currently use on-page in a #format xxx processing instruction to a sanitized mimetype major, minor tuple. can also be user later for easier entry by the user, so he can just type “wiki” instead of “text/x.moin.wiki”.

parse_mimetype(mimestr)[source]

take a string like used in content-type and parse it into components, alternatively it also can process some abbreviated string like “wiki”

sanitize()[source]

convert to some representation that makes sense - this is not necessarily conformant to /etc/mime.types or IANA listing, but if something is readable text, we will return some text/* mimetype, not application/*, because we need text/plain as fallback and not application/octet-stream.

spoil()[source]

this returns something conformant to /etc/mime.type or IANA as a string, kind of inverse operation of sanitize(), but doesn’t change self

MoinMoin.util.monkeypatch module

This module contains some monkeypatching for 3rd party code we use.

We hope that any 3rd party might find this code useful and will adopt it, so we don’t need to patch it any more. If you adopt some code from here, please notify us, so we can remove it from here.

class MoinMoin.util.monkeypatch.BaseRequestHandler(request, client_address, server)[source]

Bases: werkzeug.serving.WSGIRequestHandler

log(type, message, *args)[source]

MoinMoin.util.notifications module

MoinMoin - Notifications

class MoinMoin.util.notifications.Notification(app, fqname, revs, **kwargs)[source]

Bases: object

Represents a mail notification about an item change

generate_diff_url(domain)[source]

Generate the URL that leads to diff page of the last 2 revisions

Parameters:domain – domain name
Returns:the absolute URL to the diff page
get_content_diff()[source]

Create a content diff for the last item change

Returns:list of diff lines
get_meta_diff()[source]

Create a meta diff for the last item change

Returns:a list of tuples of the format (<change type>, <basekeys>, <value>) that can be used to format a diff
html_template = 'mail/notification_main.html'
render_templates(content_diff, meta_diff)[source]

Render both plain text and HTML templates by providing all the necessary arguments

Returns:tuple consisting of plain text and HTML notification message
txt_template = 'mail/notification.txt'
MoinMoin.util.notifications.get_item_last_revisions(app, fqname)[source]

Get 2 or less most recent item revisions from the index

Parameters:
  • app – local proxy app
  • fqname – the fqname of the item
Returns:

a list of revisions

MoinMoin.util.notifications.msgs()[source]

Encapsulates the main notification messages

Returns:a dictionary of notification messages
MoinMoin.util.notifications.send_notifications(app, fqname, **kwargs)[source]

Send mail notifications to subscribers on item change

Parameters:
  • app – local proxy app
  • fqname – fqname of the changed item
  • kwargs – key/value pairs that contain extra information about the item required in order to create a notification

MoinMoin.util.paramparser module

MoinMoin - parameter parsing and invoking of extension functions

exception MoinMoin.util.paramparser.BracketError[source]

Bases: exceptions.Exception

exception MoinMoin.util.paramparser.BracketMissingCloseError(bracket)[source]

Bases: MoinMoin.util.paramparser.BracketError

exception MoinMoin.util.paramparser.BracketUnexpectedCloseError(bracket)[source]

Bases: MoinMoin.util.paramparser.BracketError

class MoinMoin.util.paramparser.IEFArgument[source]

Base class for new argument parsers for invoke_extension_function.

get_default()[source]

Return the default for this argument.

parse_argument(s)[source]

Parse the argument given in s (a string) and return the argument for the extension function.

class MoinMoin.util.paramparser.ParserPrefix(prefix)[source]

Bases: object

Trivial container-class holding a single character for the possible prefixes for parse_quoted_separated_ext and implementing rich equal comparison.

class MoinMoin.util.paramparser.UnitArgument(default, argtype, units=['mm'], defaultunit=None)[source]

Bases: MoinMoin.util.paramparser.IEFArgument

Argument class for invoke_extension_function that forces having any of the specified units given for a value.

Note that the default unit is “mm”.

Use, for example, “UnitArgument(‘7mm’, float, [‘%’, ‘mm’])”.

If the defaultunit parameter is given, any argument that can be converted into the given argtype is assumed to have the default unit. NOTE: This doesn’t work with a choice (tuple or list) argtype.

get_default()[source]
parse_argument(s)[source]
MoinMoin.util.paramparser.get_bool(arg, name=None, default=None)[source]

For use with values returned from parse_quoted_separated or given as macro parameters, return a boolean from a unicode string. Valid input is ‘true’/’false’, ‘yes’/’no’ and ‘1’/‘0’ or None for the default value.

Parameters:
  • arg – The argument, may be None or a unicode string
  • name – Name of the argument, for error messages
  • default – default value if arg is None
Return type:

boolean or None

Returns:

the boolean value of the string according to above rules (or default value)

MoinMoin.util.paramparser.get_choice(arg, name=None, choices=[None], default_none=False)[source]

For use with values returned from parse_quoted_separated or given as macro parameters, return a unicode string that must be in the choices given. None is a valid input and yields first of the valid choices.

Parameters:
  • arg – The argument, may be None or a unicode string
  • name – Name of the argument, for error messages
  • choices – the possible choices
  • default_none – If False (default), get_choice returns first available choice if arg is None. If True, get_choice returns None if arg is None. This is useful if some arg value is required (no default choice).
Return type:

unicode or None

Returns:

the unicode string (or default value)

MoinMoin.util.paramparser.get_complex(arg, name=None, default=None)[source]

For use with values returned from parse_quoted_separated or given as macro parameters, return a complex from a unicode string. None is a valid input and yields the default value.

Parameters:
  • arg – The argument, may be None or a unicode string
  • name – Name of the argument, for error messages
  • default – default return value if arg is None
Return type:

complex or None

Returns:

the complex value of the string (or default value)

MoinMoin.util.paramparser.get_float(arg, name=None, default=None)[source]

For use with values returned from parse_quoted_separated or given as macro parameters, return a float from a unicode string. None is a valid input and yields the default value.

Parameters:
  • arg – The argument, may be None or a unicode string
  • name – Name of the argument, for error messages
  • default – default return value if arg is None
Return type:

float or None

Returns:

the float value of the string (or default value)

MoinMoin.util.paramparser.get_int(arg, name=None, default=None)[source]

For use with values returned from parse_quoted_separated or given as macro parameters, return an integer from a unicode string containing the decimal representation of a number. None is a valid input and yields the default value.

Parameters:
  • arg – The argument, may be None or a unicode string
  • name – Name of the argument, for error messages
  • default – default value if arg is None
Return type:

int or None

Returns:

the integer value of the string (or default value)

MoinMoin.util.paramparser.get_unicode(arg, name=None, default=None)[source]

For use with values returned from parse_quoted_separated or given as macro parameters, return a unicode string from a unicode string. None is a valid input and yields the default value.

Parameters:
  • arg – The argument, may be None or a unicode string
  • name – Name of the argument, for error messages
  • default – default return value if arg is None;
Return type:

unicode or None

Returns:

the unicode string (or default value)

MoinMoin.util.paramparser.invoke_extension_function(function, args, fixed_args=[])[source]

Parses arguments for an extension call and calls the extension function with the arguments.

If the macro function has a default value that is a bool, int, long, float or unicode object, then the given value is converted to the type of that default value before passing it to the macro function. That way, macros need not call the get_* functions for any arguments that have a default.

Parameters:
  • function – the function to invoke
  • args – unicode string with arguments (or evaluating to False)
  • fixed_args – fixed arguments to pass as the first arguments
Returns:

the return value from the function called

MoinMoin.util.paramparser.parse_quoted_separated(args, separator=', ', name_value=True, seplimit=0)[source]
MoinMoin.util.paramparser.parse_quoted_separated_ext(args, separator=None, name_value_separator=None, brackets=None, seplimit=0, multikey=False, prefixes=None, quotes='"')[source]

Parses the given string according to the other parameters.

Items can be quoted with any character from the quotes parameter and each quote can be escaped by doubling it, the separator and name_value_separator can both be quoted, when name_value_separator is set then the name can also be quoted.

Values that are not given are returned as None, while the empty string as a value can be achieved by quoting it.

If a name or value does not start with a quote, then the quote looses its special meaning for that name or value, unless it starts with one of the given prefixes (the parameter is unicode containing all allowed prefixes.) The prefixes will be returned as ParserPrefix() instances in the first element of the tuple for that particular argument.

If multiple separators follow each other, this is treated as having None arguments inbetween, that is also true for when space is used as separators (when separator is None), filter them out afterwards.

The function can also do bracketing, i.e. parse expressions that contain things like:

"(a (a b))" to ['(', 'a', ['(', 'a', 'b']],

in this case, as in this example, the returned list will contain sub-lists and the brackets parameter must be a list of opening and closing brackets, e.g.:

brackets = ['()', '<>']

Each sub-list’s first item is the opening bracket used for grouping. Nesting will be observed between the different types of brackets given. If bracketing doesn’t match, a BracketError instance is raised with a ‘bracket’ property indicating the type of missing or unexpected bracket, the instance will be either of the class BracketMissingCloseError or of the class BracketUnexpectedCloseError.

If multikey is True (along with setting name_value_separator), then the returned tuples for (key, value) pairs can also have multiple keys, e.g.:

"a=b=c" -> ('a', 'b', 'c')
Parameters:
  • args – arguments to parse
  • separator – the argument separator, defaults to None, meaning any space separates arguments
  • name_value_separator – separator for name=value, default ‘=’, name=value keywords not parsed if evaluates to False
  • brackets – a list of two-character strings giving opening and closing brackets
  • seplimit – limits the number of parsed arguments
  • multikey – multiple keys allowed for a single value
Return type:

list

Returns:

list of unicode strings and tuples containing unicode strings, or lists containing the same for bracketing support

class MoinMoin.util.paramparser.required_arg(argtype)[source]

Wrap a type in this class and give it as default argument for a function passed to invoke_extension_function() in order to get generic checking that the argument is given.

MoinMoin.util.plugins module

MoinMoin - plugin loader

exception MoinMoin.util.plugins.PluginAttributeError[source]

Bases: MoinMoin.util.plugins.PluginError

Raised when plugin does not contain an attribtue

exception MoinMoin.util.plugins.PluginError[source]

Bases: exceptions.Exception

Base class for plugin errors

exception MoinMoin.util.plugins.PluginMissingError[source]

Bases: MoinMoin.util.plugins.PluginError

Raised when a plugin is not found

MoinMoin.util.plugins.builtinPlugins(kind)[source]

Gets a list of modules in MoinMoin.’kind’

Parameters:kind – what kind of modules we look for
Return type:list
Returns:module names
MoinMoin.util.plugins.getPlugins(kind, cfg)[source]

Gets a list of plugin names of kind

Parameters:kind – what kind of modules we look for
Return type:list
Returns:module names
MoinMoin.util.plugins.importBuiltinPlugin(kind, name, function='execute')[source]

Import builtin plugin from MoinMoin package

See importPlugin docstring.

MoinMoin.util.plugins.importNameFromPlugin(moduleName, name)[source]

Return <name> attr from <moduleName> module, raise PluginAttributeError if name does not exist.

If name is None, return the <moduleName> module object.

MoinMoin.util.plugins.importPlugin(cfg, kind, name, function='execute')[source]

Import wiki or builtin plugin

Returns <function> attr from a plugin module <name>. If <function> attr is missing, raise PluginAttributeError. If <function> is None, return the whole module object.

If <name> plugin can not be imported, raise PluginMissingError.

kind may be one of ‘action’, ‘macro’ or any other directory that exist in MoinMoin or data/plugin.

Wiki plugins will always override builtin plugins. If you want specific plugin, use either importWikiPlugin or importBuiltinPlugin directly.

Parameters:
  • cfg – wiki config instance
  • kind – what kind of module we want to import
  • name – the name of the module
  • function – the function name
Return type:

any object

Returns:

“function” of module “name” of kind “kind”, or None

MoinMoin.util.plugins.importWikiPlugin(cfg, kind, name, function='execute')[source]

Import plugin from the wiki data directory

See importPlugin docstring.

MoinMoin.util.plugins.searchAndImportPlugin(cfg, type, name, what=None)[source]
MoinMoin.util.plugins.wikiPlugins(kind, cfg)[source]

Gets a dict containing the names of all plugins of <kind> as the key and the containing module name as the value.

Parameters:kind – what kind of modules we look for
Return type:dict
Returns:plugin name to containing module name mapping

MoinMoin.util.profile module

profile - moin profiling utilities

This module provides profilers used to profile the memory usage of a long running process.

Typical usage:

  1. Create a profiler:

    from MoinMoin.util.profile import Profiler profiler = Profiler(‘my log’)

  2. In the request handler, add each request to the profiler:

    profiler.addRequest()

  3. If you like, you can add extra samples:

    profiler.sample()

You can customize the profiler when you create it:

  • requestsPerSample (default 100):

    How many requests to run between samples. Set higher for live wiki or lower for more accurate results.

  • collect (default 0):

    Use gc.collect to force a memory cleanup each sample. Keeps the memory usage much lower, but your profile data will not reflect the real world memory usage of the application.

Based on code by Oliver Graf

class MoinMoin.util.profile.Profiler(name, requestsPerSample=100, collect=0)[source]

Profile memory usage

Profiler count requests and sample memory usage.

FIXME: We might want to save the profiler log in the profiled wiki data dir, but the data dir is available only later in request. This should be fixed by loading the config earlier.

addRequest()[source]

Add a request to the profile

Call this for each page request.

WARNING: This is the most important call. if you don’t call this for each request - you will not have any profile data.

Invoke sample when self.count reach self.requestsPerSample.

sample()[source]

Make a sample of memory usage and log it

You can call this to make samples between the samples done each requestsPerSample, for example, at startup.

Invoke common methods for all profilers. Some profilers like TwistedProfiler override this method.

class MoinMoin.util.profile.TwistedProfiler(name, requestsPerSample=100, collect=0)[source]

Bases: MoinMoin.util.profile.Profiler

Twisted specific memory profiler

Customize the way we call ps, to overcome blocking problems on twisted.

sample()[source]

Make a sample of memory usage and log it

On twisted we can’t just call ps - we have to use deferred, which will call us using a callback when its finished, and then we log.

Since twisted continue to serve while the deferred fetch the memory, the reading may be late in few requests.

MoinMoin.util.pysupport module

MoinMoin - Supporting functions for Python magic

class MoinMoin.util.pysupport.AutoNe[source]

Bases: object

Implement __ne__ in terms of __eq__. This is a mixin class.

MoinMoin.util.pysupport.getPackageModules(packagefile)[source]

Return a list of modules for a package, omitting any modules starting with an underscore.

MoinMoin.util.pysupport.getPluginModules(packagedir)[source]

Return a list of plugin modules for a given plugin package dir, omitting any that start with an underscore.

MoinMoin.util.pysupport.importName(modulename, name)[source]

Import name dynamically from module

Used to do dynamic import of modules and names that you know their names only in runtime.

Any error raised here must be handled by the caller.

Parameters:
  • modulename – full qualified mudule name, e.g. x.y.z
  • name – name to import from modulename
Return type:

any object

Returns:

name from module

MoinMoin.util.pysupport.isImportable(module)[source]

Check whether a certain module is available.

MoinMoin.util.pysupport.load_package_modules(package_name, package_pathes)[source]

Load (import) all modules from some package (except those starting with _).

This is useful if there is some code in the module that runs at import time and registers some code of that module somewhere.

Call this from __init__.py of the same package like this:

load_package_modules(__name__, __path__)
MoinMoin.util.pysupport.makeThreadSafe(function, lock=None)[source]

Call with a function you want to make thread safe

Call without lock to make the function thread safe using one lock per function. Call with existing lock object if you want to make several functions use same lock, e.g. all functions that change same data structure.

Parameters:
  • function – function to make thread safe
  • lock – threading.Lock instance or None
Return type:

function

Returns:

function decorated with locking

MoinMoin.util.registry module

MoinMoin - Module registry

Every module registers a factory for itself at the registry with a given priority. During the lookup each factory is called with the given arguments and can return a callable to consider itself as a match.

class MoinMoin.util.registry.Registry[source]

Bases: MoinMoin.util.registry.RegistryBase

register(factory, priority=0)[source]

Register a factory

Parameters:factory – Factory to register. Callable, have to return a class
class MoinMoin.util.registry.RegistryBase[source]

Bases: object

class Entry[source]

Bases: MoinMoin.util.registry.Entry

RegistryBase.PRIORITY_FIRST = -10
RegistryBase.PRIORITY_LAST = 10
RegistryBase.PRIORITY_MIDDLE = 0
RegistryBase.PRIORITY_REALLY_FIRST = -20
RegistryBase.PRIORITY_REALLY_LAST = 20
RegistryBase.get(*args, **kw)[source]

Lookup a matching module

Each registered factory is called with the given arguments and the first matching wins.

RegistryBase.unregister(factory)[source]

Unregister a factory

Parameters:factory – Factory to unregister

MoinMoin.util.rev_navigation module

Helper function to get prior, current, next revisions and mod-times.

MoinMoin.util.rev_navigation.prior_next_revs(revid, fqname)[source]

Return prior, current, and next revids and time stamps.

MoinMoin.util.send_file module

A better send_file

Initially, this was a modified implementation of flask 0.6.0’s send_file(), trying to be as compatible as possible.

For details see: https://github.com/mitsuhiko/flask/issues/issue/104 and the history of this file in our repository. This code fixes all the issues described in the bug report.

As we forked send_file, we later modified it (without trying to stay compatible), because we can easily adapt anyway and the code can be much simpler without compatibility code.

MoinMoin.util.send_file.encode_rfc2231(value, coding='UTF-8', lang='')[source]

Encode a value according to RFC2231/5987.

Parameters:
  • value – the value to encode. must be either unicode or encoded in <coding>.
  • coding – the coding (charset) to use. it is a good idea to use ‘UTF-8’.
  • lang – the language to use. defaults to empty string (no language given).
MoinMoin.util.send_file.send_file(filename=None, file=None, mimetype=None, as_attachment=False, attachment_filename=None, mtime=None, cache_timeout=43200, add_etags=True, etag=None, conditional=False)[source]

Sends the contents of a file to the client.

A file can be either a filesystem file or a file-like object (this code is careful about not assuming that every file is a filesystem file).

This will use the most efficient method available, configured and possible (for filesystem files some more optimizations may be possible that for file-like objects not having a filesystem filename). By default it will try to use the WSGI server’s file_wrapper support. Alternatively you can set the application’s use_x_sendfile attribute to True to directly emit an X-Sendfile header. This however requires support of the underlying webserver for X-Sendfile.

send_file will try to guess some stuff for you if you do not provide them:

  • mimetype (based on filename / attachment_filename)
  • mtime (based on filesystem file’s metadata)
  • etag (based on filename, mtime, filesystem file size)

If you do not provide enough information, send_file might raise a TypeError.

For extra security you probably want to sent certain files as attachment (HTML for instance).

Please never pass filenames to this function from user sources without checking them first. Something like this is usually sufficient to avoid security problems:

if '..' in filename or filename.startswith('/'):
    abort(404)
Parameters:
  • filename – the filesystem filename of the file to send (relative to the root_path if a relative path is specified). If you just have an open filesystem file object f, give f.name here. If you don’t have a filesystem file nor a filesystem file name, but just a file-like obj, don’t use this argument.
  • file – a file (or file-like) object, you may give it if you either do not have a filesystem filename or if you already have an open file anyway.
  • mimetype – the mimetype of the file if provided, otherwise auto detection happens based on the filename or attachment_filename.
  • as_attachment – set to True if you want to send this file with a Content-Disposition: attachment header.
  • attachment_filename – the filename for the attachment if it differs from the filename argument.
  • mtime – the modification time of the file if provided, otherwise it will be determined automatically for filesystem files
  • cache_timeout – the timeout in seconds for the headers.
  • conditional – set to True to enable conditional responses.
  • add_etags – set to False to disable attaching of etags.
  • etag – you can give an etag here, None means to try to compute the etag from the file’s filesystem metadata (the latter of course only works for filesystem files). If you do not give a filename, but you use add_etags, you must explicitely provide the etag as it can’t compute it for that case.

MoinMoin.util.subscriptions module

MoinMoin - Subscriptions

class MoinMoin.util.subscriptions.Subscriber(itemid, name, email, locale)

Bases: tuple

email

Alias for field number 2

itemid

Alias for field number 0

locale

Alias for field number 3

name

Alias for field number 1

MoinMoin.util.subscriptions.get_matched_subscription_patterns(subscription_patterns, **meta)[source]

Get all the subscriptions with patterns that match at least one of item names

Parameters:
  • subscription_patterns – a list of subscription patterns (the ones that start with NAMERE or NAMEPREFIX)
  • meta – key/value pairs from item metadata - name and namespace keys
Returns:

a list of matched subscription patterns

MoinMoin.util.subscriptions.get_subscribers(**meta)[source]

Get all users that are subscribed to the item

Parameters:meta – key/value pairs from item metadata - itemid, name, namespace, tags keys
Returns:a set of Subscriber objects

MoinMoin.util.thread_monitor module

Thread monitor - Check the state of all threads.

Just call activate_hook() as early as possible in program execution. Then you can trigger the output of tracebacks of all threads by calling trigger_dump().

MoinMoin.util.thread_monitor.dump_regularly(seconds)[source]

Dumps the tracebacks every ‘seconds’ seconds.

MoinMoin.util.tree module

MoinMoin - Tree name and element generator

class MoinMoin.util.tree.Name[source]

Bases: emeraldtree.tree.QName

Represents a QName and factory for elements with this QName

class MoinMoin.util.tree.Namespace[source]

Bases: unicode

Represents a namespace and factory for Names within this namespace

namespace

MoinMoin.util.version module

MoinMoin - dealing with version numbers

class MoinMoin.util.version.Version[source]

Bases: tuple

Version objects store versions like 1.2.3a4 in a structured way and support version comparisons and direct version component access. 1: major version (digits only) 2: minor version (digits only) 3: (maintenance) release version (digits only) a4: optional additional version specification (str)

See PEP386 for more details. TODO: use 3rd party code for PEP386 version numbers later.

You can create a Version instance either by giving the components, like:
Version(1,2,3,’a4’)
or by giving the composite version string, like:
Version(version=”1.2.3a4”).

Version subclasses tuple, so comparisons to tuples should work. Also, we inherit all the comparison logic from tuple base class.

VERSION_RE = <_sre.SRE_Pattern object>
additional
major
minor
classmethod parse_version(version)[source]
release

Module contents

MoinMoin - Utility Functions General helper functions that are not directly wiki related.

MoinMoin.util.TranslateCDATA(text)[source]

Convert a string to a CDATA-encoded one Copyright (c) 1999-2000 FourThought, http://4suite.com/4DOM

MoinMoin.util.TranslateText(text)[source]

Convert a string to a PCDATA-encoded one (do minimal encoding) Copyright (c) 1999-2000 FourThought, http://4suite.com/4DOM

MoinMoin.util.getPageContent(results, offset, results_per_page)[source]

Selects the content to show on a single page

Parameters:
  • results – the whole result, from which results for one page will be selected (generally a generator but could be a list also),
  • offset – after skipping how many results, the selection of results for that page will be done (int),
  • results_per_page – number of results to be shown on a single page (int)
Return type:

tuple

Returns:

selected_result (list), offset for next page (If 0 then no next page), offset for previous page (If less than 0, then no previous page)

MoinMoin.util.rangelist(numbers)[source]

Convert a list of integers to a range string in the form ‘1,2-5,7’.