Package gavo :: Package utils :: Module misctricks
[frames] | no frames]

Module misctricks

source code

Various helpers that didn't fit into any other xTricks.

Classes
  NotInstalledModuleStub
A stub that raises some more or less descriptive error on attribute access.
  RSTExtensions
a register for local RST extensions.
  Undefined
a sentinel for all kinds of undefined values.
  QuotedName
A string-like thing basically representing SQL delimited identifiers.
  StreamBuffer
a buffer that takes data in arbitrary chunks and returns them in chops of chunkSize bytes.
  CaseSemisensitiveDict
A dictionary allowing case-insensitive access to its content.
Functions
 
couldBeABibcode(s)
returns true if we think that the string s is a bibcode.
source code
 
grouped(n, seq)
yields items of seq in groups n elements.
source code
 
getfirst(args, key, default=<Undefined>)
returns the first value of key in the web argument-like object args.
source code
 
sendUIEvent(eventName, *args)
sends an eventName to the DC event dispatcher.
source code
 
logOldExc(exc)
logs the mutation of the currently handled exception to exc.
source code
 
getFortranRec(f)
reads a "fortran record" from f and returns the payload.
source code
 
iterFortranRecs(f, skip=0)
iterates over the fortran records in f.
source code
 
getWithCache(url, cacheDir, extraHeaders={})
returns the content of url, from a cache if possible.
source code
 
rstxToHTMLWithWarning(source, **userOverrides)
returns HTML and a string with warnings for a piece of ReStructured text.
source code
 
rstxToHTML(source, **userOverrides)
returns HTML for a piece of ReStructured text.
source code
 
pyparsingWhitechars(*args, **kwds)
a context manager that serializes pyparsing grammar compilation and manages its whitespace chars.
source code
 
pyparseString(grammar, string, **kwargs)
parses a string using a pyparsing grammar thread-safely.
source code
 
pyparseTransform(grammar, string, **kwargs)
calls grammar's transformString method thread-safely.
source code
 
parseKVLine(aString)
returns a dictionary for a "key-value line".
source code
 
makeKVLine(aDict)
serialized a dictionary to a key-value line.
source code
Variables
  BIBCODE_PATTERN = re.compile(r'[012]\d\d\d\w[^ ]{14}$')
  __package__ = 'gavo.utils'
Function Details

couldBeABibcode(s)

source code 

returns true if we think that the string s is a bibcode.

This is based on matching against BIBCODE_PATTERN.

grouped(n, seq)

source code 

yields items of seq in groups n elements.

If len(seq)%n!=0, the last elements are discarded.

>>> list(grouped(2, range(5)))
[(0, 1), (2, 3)]
>>> list(grouped(3, range(9)))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]

getfirst(args, key, default=<Undefined>)

source code 

returns the first value of key in the web argument-like object args.

args is a dictionary mapping keys to lists of values. If key is present, the first element of the list is returned; else, or if the list is empty, default if given. If not, a Validation error for the requested column is raised.

Finally, if args[key] is neither list nor tuple (in an ininstance sense), it is returned unchanged.

>>> getfirst({'x': [1,2,3]}, 'x')
1
>>> getfirst({'x': []}, 'x')
Traceback (most recent call last):
ValidationError: Field x: Missing mandatory parameter x
>>> getfirst({'x': []}, 'y')
Traceback (most recent call last):
ValidationError: Field y: Missing mandatory parameter y
>>> print(getfirst({'x': []}, 'y', None))
None
>>> getfirst({'x': 'abc'}, 'x')
'abc'

sendUIEvent(eventName, *args)

source code 

sends an eventName to the DC event dispatcher.

If no event dispatcher is available, do nothing.

The base.ui object that DaCHS uses for event dispatching is only available to sub-packages above base. Other code should not use or need it under normal circumstances, but if it does, it can use this.

All other code should use base.ui.notify<eventName>(*args) directly.

logOldExc(exc)

source code 

logs the mutation of the currently handled exception to exc.

This just does a notifyExceptionMutation using sendUIEvent; it should only be used by code at or below base.

getFortranRec(f)

source code 

reads a "fortran record" from f and returns the payload.

A "fortran record" comes from an unformatted file and has a 4-byte payload length before and after the payload. Native endianess is assumed here.

If the two length specs do not match, a ValueError is raised.

iterFortranRecs(f, skip=0)

source code 

iterates over the fortran records in f.

For details, see getFortranRec.

getWithCache(url, cacheDir, extraHeaders={})

source code 

returns the content of url, from a cache if possible.

Of course, you only want to use this if there's some external guarantee that the resource behing url doesn't change. No expiry mechanism is present here.

rstxToHTMLWithWarning(source, **userOverrides)

source code 

returns HTML and a string with warnings for a piece of ReStructured text.

source can be a unicode string or a byte string in utf-8.

userOverrides will be added to the overrides argument of docutils' core.publish_parts.

rstxToHTML(source, **userOverrides)

source code 

returns HTML for a piece of ReStructured text.

source can be a unicode string or a byte string in utf-8.

userOverrides will be added to the overrides argument of docutils' core.publish_parts.

pyparsingWhitechars(*args, **kwds)

source code 

a context manager that serializes pyparsing grammar compilation and manages its whitespace chars.

We need different whitespace definitions in some parts of DaCHS. (The default used to be " \t" for a while, so this is what things get reset to).

Since whitespace apparently can only be set globally for pyparsing, we provide this c.m. Since it is possible that grammars will be compiled in threads (e.g., as a side effect of getRD), this is protected by a lock. This, in turn, means that this can potentially block for a fairly long time.

Bottom line: When compiling pyparsing grammars, *always* set the whitespace chars explicitely, and do it through this c.m.

Decorators:
  • @contextlib.contextmanager

parseKVLine(aString)

source code 

returns a dictionary for a "key-value line".

key-value lines represent string-valued dictionaries following postgres libpq/dsn (see PQconnectdb docs; it's keyword=value, whitespace-separated, with whitespace allowed in values through single quoting, and backslash-escaping

makeKVLine(aDict)

source code 

serialized a dictionary to a key-value line.

See parseKVLine for details.