galaxy.util package

Submodules

galaxy.util.aliaspickler module

class galaxy.util.aliaspickler.AliasPickleModule(aliases)[source]

Bases: object

dump(obj, fileobj, protocol=0)[source]
dumps(obj, protocol=0)[source]
load(fileobj)[source]
loads(string)[source]
class galaxy.util.aliaspickler.AliasUnpickler(aliases, *args, **kw)[source]

Bases: pickle.Unpickler

find_class(module, name)[source]

galaxy.util.bunch module

class galaxy.util.bunch.Bunch(**kwds)[source]

Bases: object

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308

Often we want to just collect a bunch of stuff together, naming each item of the bunch; a dictionary’s OK for that, but a small do-nothing class is even handier, and prettier to use.

dict()[source]
get(key, default=None)[source]
items()[source]
keys()[source]
values()[source]

galaxy.util.checkers module

galaxy.util.checkers.check_binary(name, file_path=True)[source]
galaxy.util.checkers.check_bz2(file_path, check_content=True)[source]
galaxy.util.checkers.check_gzip(file_path, check_content=True)[source]
galaxy.util.checkers.check_html(file_path, chunk=None)[source]
galaxy.util.checkers.check_image(file_path)[source]

Simple wrapper around image_type to yield a True/False verdict

galaxy.util.checkers.check_zip(file_path, check_content=True, files=1)[source]
galaxy.util.checkers.is_gzip(file_path)[source]
galaxy.util.checkers.is_bz2(file_path)[source]
galaxy.util.checkers.is_zip(file_path)[source]

galaxy.util.compression_utils module

class galaxy.util.compression_utils.CompressedFile(file_path, mode='r')[source]

Bases: object

extract(path)[source]

Determine the path to which the archive should be extracted.

getmember(name)[source]
getmembers()[source]
getmembers_tar()[source]
getmembers_zip()[source]
getname(member)[source]
getname_tar(item)[source]
getname_zip(item)[source]
isdir(member)[source]
isdir_tar(member)[source]
isdir_zip(member)[source]
isfile(member)[source]
open_tar(filepath, mode)[source]
open_zip(filepath, mode)[source]
safemembers()[source]
zipfile_ok(path_to_archive)[source]

This function is a bit pedantic and not functionally necessary. It checks whether there is no file pointing outside of the extraction, because ZipFile.extractall() has some potential security holes. See python zipfile documentation for more details.

galaxy.util.compression_utils.get_fileobj(filename, mode='r', compressed_formats=None)[source]

Returns a fileobj. If the file is compressed, return an appropriate file reader. In text mode, always use ‘utf-8’ encoding.

Parameters:
  • filename – path to file that should be opened
  • mode – mode to pass to opener
  • compressed_formats – list of allowed compressed file formats among ‘bz2’, ‘gzip’ and ‘zip’. If left to None, all 3 formats are allowed
galaxy.util.compression_utils.get_fileobj_raw(filename, mode='r', compressed_formats=None)[source]

galaxy.util.dictifiable module

class galaxy.util.dictifiable.Dictifiable[source]

Bases: object

Mixin that enables objects to be converted to dictionaries. This is useful when for sharing objects across boundaries, such as the API, tool scripts, and JavaScript code.

to_dict(view='collection', value_mapper=None)[source]

Return item dictionary.

galaxy.util.expressions module

Expression evaluation support.

For the moment this depends on python’s eval. In the future it should be replaced with a “safe” parser.

class galaxy.util.expressions.ExpressionContext(dict, parent=None)[source]

Bases: _abcoll.MutableMapping

galaxy.util.filelock module

Code obtained from https://github.com/dmfrey/FileLock.

See full license at:

https://github.com/dmfrey/FileLock/blob/master/LICENSE.txt

class galaxy.util.filelock.FileLock(file_name, timeout=10, delay=0.05)[source]

Bases: object

A file locking mechanism that has context-manager support so you can use it in a with statement. This should be relatively cross compatible as it doesn’t rely on msvcrt or fcntl for the locking.

acquire()[source]

Acquire the lock, if possible. If the lock is in use, it check again every wait seconds. It does this until it either gets the lock or exceeds timeout number of seconds, in which case it throws an exception.

release()[source]

Get rid of the lock by deleting the lockfile. When working in a with statement, this gets automatically called at the end.

exception galaxy.util.filelock.FileLockException[source]

Bases: exceptions.Exception

galaxy.util.hash_util module

Utility functions for bi-directional Python version compatibility. Python 2.5 introduced hashlib which replaced sha in Python 2.4 and previous versions.

galaxy.util.hash_util.md5()

Returns a md5 hash object; optionally initialized with a string

galaxy.util.hash_util.sha1()

Returns a sha1 hash object; optionally initialized with a string

galaxy.util.hash_util.sha()

Returns a sha1 hash object; optionally initialized with a string

galaxy.util.hash_util.new_secure_hash(text_type=None)[source]

Returns either a sha1 hash object (if called with no arguments), or a hexdigest of the sha1 hash of the argument text_type.

galaxy.util.hash_util.hmac_new(key, value)[source]
galaxy.util.hash_util.is_hashable(value)[source]

galaxy.util.heartbeat module

class galaxy.util.heartbeat.Heartbeat(config, name='Heartbeat Thread', period=20, fname='heartbeat.log')[source]

Bases: threading.Thread

Thread that periodically dumps the state of all threads to a file

close_logs()[source]
dump()[source]
dump_signal_handler(signum, frame)[source]
get_interesting_stack_frame(stack_frames)[source]

Scans a given backtrace stack frames, returns a single quadraple of [filename, line, function-name, text] of the single, deepest, most interesting frame.

Interesting being:

inside the galaxy source code ("/lib/galaxy"),
prefreably not an egg.
open_logs()[source]
print_nonsleeping(threads_object_dict)[source]
run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

shutdown()[source]
thread_is_sleeping(last_stack_frame)[source]

Returns True if the given stack-frame represents a known sleeper function (at least in python 2.5)

galaxy.util.heartbeat.get_current_thread_object_dict()[source]

Get a dictionary of all ‘Thread’ objects created via the threading module keyed by thread_id. Note that not all interpreter threads have a thread objects, only the main thread and any created via the ‘threading’ module. Threads created via the low level ‘thread’ module will not be in the returned dictionary.

HACK: This mucks with the internals of the threading module since that
module does not expose any way to match ‘Thread’ objects with intepreter thread identifiers (though it should).

galaxy.util.image_util module

Provides utilities for working with image files.

galaxy.util.image_util.check_image_type(filename, types)[source]
galaxy.util.image_util.image_type(filename)[source]

galaxy.util.inflection module

class galaxy.util.inflection.Base[source]

Bases: object

Locale inflectors must inherit from this base class inorder to provide the basic Inflector functionality

camelize(word)[source]

Returns given word as CamelCased Converts a word like “send_email” to “SendEmail”. It will remove non alphanumeric character from the word, so “who’s online” will be converted to “WhoSOnline”

classify(table_name)[source]

Converts a table name to its class name according to rails naming conventions. Example: Converts “people” to “Person”

cond_plural(number_of_records, word)[source]

Returns the plural form of a word if first parameter is greater than 1

demodulize(module_name)[source]
foreignKey(class_name, separate_class_name_and_id_with_underscore=1)[source]

Returns class_name in underscored form, with “_id” tacked on at the end. This is for use in dealing with the database.

humanize(word, uppercase='')[source]

Returns a human-readable string from word Returns a human-readable string from word, by replacing underscores with a space, and by upper-casing the initial character by default. If you need to uppercase all the words you just have to pass ‘all’ as a second parameter.

modulize(module_description)[source]
ordinalize(number)[source]

Converts number to its ordinal English form. This method converts 13 to 13th, 2 to 2nd …

string_replace(word, find, replace)[source]

This function returns a copy of word, translating all occurrences of each character in find to the corresponding character in replace

tableize(class_name)[source]

Converts a class name to its table name according to rails naming conventions. Example. Converts “Person” to “people”

titleize(word, uppercase='')[source]

Converts an underscored or CamelCase word into a English sentence. The titleize function converts text like “WelcomePage”, “welcome_page” or “welcome page” to this “Welcome Page”. If second parameter is set to ‘first’ it will only capitalize the first character of the title.

unaccent(text)[source]

Transforms a string to its unaccented version. This might be useful for generating “friendly” URLs

underscore(word)[source]

Converts a word “into_it_s_underscored_version” Convert any “CamelCased” or “ordinary Word” into an “underscored_word”. This can be really useful for creating friendly URLs.

urlize(text)[source]

Transform a string its unaccented and underscored version ready to be inserted in friendly URLs

variablize(word)[source]

Same as camelize but first char is lowercased Converts a word like “send_email” to “sendEmail”. It will remove non alphanumeric character from the word, so “who’s online” will be converted to “whoSOnline”

class galaxy.util.inflection.English[source]

Bases: galaxy.util.inflection.Base

Inflector for pluralize and singularize English nouns.

This is the default Inflector for the Inflector obj

pluralize(word)[source]

Pluralizes English nouns.

singularize(word)[source]

Singularizes English nouns.

class galaxy.util.inflection.Inflector(Inflector=<class 'galaxy.util.inflection.English'>)[source]

Bases: object

Inflector for pluralizing and singularizing nouns.

It provides methods for helping on creating programs based on naming conventions like on Ruby on Rails.

camelize(word)[source]

Returns given word as CamelCased Converts a word like “send_email” to “SendEmail”. It will remove non alphanumeric character from the word, so “who’s online” will be converted to “WhoSOnline”

classify(table_name)[source]

Converts a table name to its class name according to rails naming conventions. Example: Converts “people” to “Person”

cond_plural(number_of_records, word)[source]

Returns the plural form of a word if first parameter is greater than 1

demodulize(module_name)[source]
foreignKey(class_name, separate_class_name_and_id_with_underscore=1)[source]

Returns class_name in underscored form, with “_id” tacked on at the end. This is for use in dealing with the database.

humanize(word, uppercase='')[source]

Returns a human-readable string from word Returns a human-readable string from word, by replacing underscores with a space, and by upper-casing the initial character by default. If you need to uppercase all the words you just have to pass ‘all’ as a second parameter.

modulize(module_description)[source]
ordinalize(number)[source]

Converts number to its ordinal form. This method converts 13 to 13th, 2 to 2nd …

pluralize(word)[source]

Pluralizes nouns.

singularize(word)[source]

Singularizes nouns.

tableize(class_name)[source]

Converts a class name to its table name according to rails naming conventions. Example. Converts “Person” to “people”

titleize(word, uppercase='')[source]

Converts an underscored or CamelCase word into a sentence. The titleize function converts text like “WelcomePage”, “welcome_page” or “welcome page” to this “Welcome Page”. If the “uppercase” parameter is set to ‘first’ it will only capitalize the first character of the title.

unaccent(text)[source]

Transforms a string to its unaccented version. This might be useful for generating “friendly” URLs

underscore(word)[source]

Converts a word “into_it_s_underscored_version” Convert any “CamelCased” or “ordinary Word” into an “underscored_word”. This can be really useful for creating friendly URLs.

urlize(text)[source]

Transform a string to its unaccented and underscored version ready to be inserted in friendly URLs

variablize(word)[source]

Same as camelize but first char is lowercased Converts a word like “send_email” to “sendEmail”. It will remove non alphanumeric character from the word, so “who’s online” will be converted to “whoSOnline”

galaxy.util.json module

galaxy.util.json.safe_dumps(*args, **kwargs)[source]

This is a wrapper around dumps that encodes Infinity and NaN values. It’s a fairly rare case (which will be low in request volume). Basically, we tell json.dumps to blow up if it encounters Infinity/NaN, and we ‘fix’ it before re-encoding.

galaxy.util.json.validate_jsonrpc_request(request, regular_methods, notification_methods)[source]
galaxy.util.json.validate_jsonrpc_response(response, id=None)[source]
galaxy.util.json.jsonrpc_request(method, params=None, id=None, jsonrpc='2.0')[source]
galaxy.util.json.jsonrpc_response(request=None, id=None, result=None, error=None, jsonrpc='2.0')[source]

galaxy.util.lazy_process module

class galaxy.util.lazy_process.LazyProcess(command_and_args)[source]

Bases: object

Abstraction describing a command line launching a service - probably as needed as functionality is accessed in Galaxy.

running
shutdown()[source]
start_process()[source]
class galaxy.util.lazy_process.NoOpLazyProcess[source]

Bases: object

LazyProcess abstraction meant to describe potentially optional services, in those cases where one is not configured or valid, this class can be used in place of LazyProcess.

running
shutdown()[source]
start_process()[source]

galaxy.util.object_wrapper module

Classes for wrapping Objects and Sanitizing string output.

class galaxy.util.object_wrapper.CallableSafeStringWrapper(value, safe_string_wrapper_function=<function wrap_with_safe_string>)[source]

Bases: galaxy.util.object_wrapper.SafeStringWrapper

class galaxy.util.object_wrapper.SafeStringWrapper(value, safe_string_wrapper_function=<function wrap_with_safe_string>)[source]

Bases: object

Class that wraps and sanitizes any provided value’s attributes that will attempt to be cast into a string.

Attempts to mimic behavior of original class, including operands.

To ensure proper handling of e.g. subclass checks, the wrap_with_safe_string() method should be used.

This wrapping occurs in a recursive/parasitic fashion, as all called attributes of the originally wrapped object will also be wrapped and sanitized, unless the attribute is of a type found in __DONT_SANITIZE_TYPES__ + __DONT_WRAP_TYPES__, where e.g. ~(strings will still be sanitized, but not wrapped), and e.g. integers will have neither.

galaxy.util.object_wrapper.cmp(x, y)[source]
galaxy.util.object_wrapper.pickle_SafeStringWrapper(safe_object)[source]
galaxy.util.object_wrapper.sanitize_lists_to_string(values, valid_characters=set(['!', ' ', ')', '(', '+', '*', '-', ', ', '/', '.', '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']), character_map={'\t': '__tc__', '\n': '__cn__', '\r': '__cr__', '"': '__dq__', '#': '__pd__', "'": '__sq__', '<': '__lt__', '>': '__gt__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__'}, invalid_character='X')[source]
galaxy.util.object_wrapper.wrap_with_safe_string(value, no_wrap_classes=None)[source]

Recursively wrap values that should be wrapped.

galaxy.util.odict module

Ordered dictionary implementation.

class galaxy.util.odict.odict(dict=None)[source]

Bases: UserDict.UserDict

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747

This dictionary class extends UserDict to record the order in which items are added. Calling keys(), values(), items(), etc. will return results in this order.

clear()[source]
copy()[source]
insert(index, key, item)[source]
items()[source]
iteritems()[source]
iterkeys()[source]
itervalues()[source]
keys()[source]
popitem()[source]
reverse()[source]
setdefault(key, failobj=None)[source]
update(dict)[source]
values()[source]

galaxy.util.oset module

Ordered set implementation from https://code.activestate.com/recipes/576694/

class galaxy.util.oset.OrderedSet(iterable=None)[source]

Bases: _abcoll.MutableSet

add(key)[source]

Add an element.

discard(key)[source]

Remove an element. Do not raise an exception if absent.

pop(last=True)[source]

Return the popped value. Raise KeyError if empty.

galaxy.util.plugin_config module

galaxy.util.plugin_config.load_plugins(plugins_dict, plugin_source, extra_kwds={})[source]
galaxy.util.plugin_config.plugin_source_from_path(path)[source]
galaxy.util.plugin_config.plugins_dict(module, plugin_type_identifier)[source]

Walk through all classes in submodules of module and find ones labelled with specified plugin_type_identifier and throw in a dictionary to allow constructions from plugins by these types later on.

galaxy.util.properties module

Module used to blend ini, environment, and explicit dictionary properties to determine application configuration. Some hard coded defaults for Galaxy but this should be reusable by tool shed and pulsar as well.

galaxy.util.properties.find_config_file(names, exts=None, dirs=None, include_samples=False)[source]

Locate a config file in multiple directories, with multiple extensions.

>>> from shutil import rmtree
>>> from tempfile import mkdtemp
>>> def touch(d, f):
...     open(os.path.join(d, f), 'w').close()
>>> def _find_config_file(*args, **kwargs):
...     return find_config_file(*args, **kwargs).replace(d, '')
>>> d = mkdtemp()
>>> d1 = os.path.join(d, 'd1')
>>> d2 = os.path.join(d, 'd2')
>>> os.makedirs(d1)
>>> os.makedirs(d2)
>>> touch(d1, 'foo.ini')
>>> touch(d1, 'foo.bar')
>>> touch(d1, 'baz.ini.sample')
>>> touch(d2, 'foo.yaml')
>>> touch(d2, 'baz.yml')
>>> _find_config_file('foo', dirs=(d1, d2))
'/d1/foo.ini'
>>> _find_config_file('baz', dirs=(d1, d2))
'/d2/baz.yml'
>>> _find_config_file('baz', dirs=(d1, d2), include_samples=True)
'/d2/baz.yml'
>>> _find_config_file('baz', dirs=(d1,), include_samples=True)
'/d1/baz.ini.sample'
>>> _find_config_file('foo', dirs=(d2, d1))
'/d2/foo.yaml'
>>> find_config_file('quux', dirs=(d,))
>>> _find_config_file('foo', exts=('bar', 'ini'), dirs=(d1,))
'/d1/foo.bar'
>>> rmtree(d)
galaxy.util.properties.load_app_properties(kwds={}, ini_file=None, ini_section=None, config_file=None, config_section=None, config_prefix='GALAXY_CONFIG_')[source]
class galaxy.util.properties.NicerConfigParser(filename, *args, **kw)[source]

Bases: ConfigParser.ConfigParser

class InterpolateWrapper(original)[source]

Bases: object

before_get(parser, section, option, value, defaults)[source]
defaults()[source]

Return the defaults, with their values interpolated (with the defaults dict itself)

Mainly to support defaults using values such as %(here)s

read_file(fp, filename=None)

Like read() but the argument must be a file-like object.

The `fp’ argument must have a `readline’ method. Optional second argument is the `filename’, which if not given, is taken from fp.name. If fp has no `name’ attribute, `<???>’ is used.

galaxy.util.simplegraph module

Fencepost-simple graph structure implementation.

class galaxy.util.simplegraph.SimpleGraph(nodes=None, edges=None)[source]

Bases: object

Each node is unique (by id) and stores its own index in the node list/odict. Each edge is represented as two indeces into the node list/odict. Both nodes and edges allow storing extra information if needed.

Allows:
multiple edges between two nodes self referential edges (an edge from a node to itself)

These graphs are not specifically directed but since source and targets on the edges are listed - it could easily be used that way.

add_edge(source_id, target_id, **data)[source]

Adds a new node only if it doesn’t already exist. :param source_id: the id of the source node :type source_id: (hashable) :param target_id: the id of the target node :type target_id: (hashable) :param data: any extra data that needs to be saved for the edge :type data: (variadic dictionary) :returns: the new node

..note: that, although this will create new nodes if necessary, there’s no way to pass data to them - so if you need to assoc. more data with the nodes, use add_node first.

add_node(node_id, **data)[source]

Adds a new node only if it doesn’t already exist. :param node_id: some unique identifier :type node_id: (hashable) :param data: any extra data that needs to be saved :type data: (variadic dictionary) :returns: the new node

as_dict()[source]

Returns a dictionary of the form:

{ 'nodes': <a list of node dictionaries>, 'edges': <a list of node dictionaries> }
gen_edge_dicts()[source]

Returns a generator that yields node dictionaries in the form:

{
    'source': <the index of the source node in the graph's node list>,
    'target': <the index of the target node in the graph's node list>,
    'data'  : <any additional edge data>
}
gen_node_dicts()[source]
Returns a generator that yields node dictionaries in the form:
{ ‘id’: <the nodes unique id>, ‘data’: <any additional node data> }
class galaxy.util.simplegraph.SimpleGraphEdge(source_index, target_index, **data)[source]

Bases: object

Edge representation.

class galaxy.util.simplegraph.SimpleGraphNode(index, **data)[source]

Bases: object

Node representation.

galaxy.util.sleeper module

class galaxy.util.sleeper.Sleeper[source]

Bases: object

Provides a ‘sleep’ method that sleeps for a number of seconds unless the notify method is called (from a different thread).

sleep(seconds)[source]
wake()[source]

galaxy.util.sockets module

galaxy.util.sockets.unused_port(range=None)[source]

galaxy.util.specs module

galaxy.util.specs.is_in(*args)[source]
galaxy.util.specs.to_bool(value)[source]
galaxy.util.specs.to_bool_or_none(value)[source]
galaxy.util.specs.to_float_or_none(value)[source]
galaxy.util.specs.to_str_or_none(value)[source]

galaxy.util.sqlite module

galaxy.util.sqlite.connect(path)[source]
galaxy.util.sqlite.is_read_only_query(query)[source]

galaxy.util.submodules module

galaxy.util.submodules.submodules(module)[source]

galaxy.util.tool_version module

galaxy.util.tool_version.remove_version_from_guid(guid)[source]

Removes version from toolshed-derived tool_id(=guid).

galaxy.util.topsort module

Topological sort.

From Tim Peters, see:
http://mail.python.org/pipermail/python-list/1999-July/006660.html

topsort takes a list of pairs, where each pair (x, y) is taken to mean that x <= y wrt some abstract partial ordering. The return value is a list, representing a total ordering that respects all the input constraints. E.g.,

topsort( [(1,2), (3,3)] )

Valid topological sorts would be any of (but nothing other than)

[3, 1, 2] [1, 3, 2] [1, 2, 3]

… however this variant ensures that ‘key’ order (first element of tuple) is preserved so the following will be result returned:

[1, 3, 2]

because those are the permutations of the input elements that respect the “1 precedes 2” and “3 precedes 3” input constraints. Note that a constraint of the form (x, x) is really just a trick to make sure x appears somewhere in the output list.

If there’s a cycle in the constraints, say

topsort( [(1,2), (2,1)] )

then CycleError is raised, and the exception object supports many methods to help analyze and break the cycles. This requires a good deal more code than topsort itself!

exception galaxy.util.topsort.CycleError(sofar, numpreds, succs)[source]

Bases: exceptions.Exception

get_elements()[source]
get_pairlist()[source]
get_partial()[source]
get_pred_counts()[source]
get_preds()[source]
get_succs()[source]
pick_a_cycle()[source]
galaxy.util.topsort.topsort(pairlist)[source]
galaxy.util.topsort.topsort_levels(pairlist)[source]

galaxy.util.xml_macros module

galaxy.util.xml_macros.imported_macro_paths(root)[source]
galaxy.util.xml_macros.load(path)[source]
galaxy.util.xml_macros.load_with_references(path)[source]

Load XML documentation from file system and preprocesses XML macros.

Return the XML representation of the expanded tree and paths to referenced files that were imported (macros).

galaxy.util.xml_macros.raw_xml_tree(path)[source]

Load raw (no macro expansion) tree representation of XML represented at the specified path.

galaxy.util.xml_macros.template_macro_params(root)[source]

Look for template macros and populate param_dict (for cheetah) with these.

Module contents

Utility functions used systemwide.

class galaxy.util.ExecutionTimer[source]

Bases: object

elapsed
class galaxy.util.Params(params, sanitize=True)[source]

Bases: object

Stores and ‘sanitizes’ parameters. Alphanumeric characters and the non-alphanumeric ones that are deemed safe are let to pass through (see L{valid_chars}). Some non-safe characters are escaped to safe forms for example C{>} becomes C{__lt__} (see L{mapped_chars}). All other characters are replaced with C{X}.

Operates on string or list values only (HTTP parameters).

>>> values = { 'status':'on', 'symbols':[  'alpha', '<>', '$rm&#!' ]  }
>>> par = Params(values)
>>> par.status
'on'
>>> par.value == None      # missing attributes return None
True
>>> par.get('price', 0)
0
>>> par.symbols            # replaces unknown symbols with X
['alpha', '__lt____gt__', 'XrmX__pd__!']
>>> sorted(par.flatten())  # flattening to a list
[('status', 'on'), ('symbols', 'XrmX__pd__!'), ('symbols', '__lt____gt__'), ('symbols', 'alpha')]
NEVER_SANITIZE = ['file_data', 'url_paste', 'URL', 'filesystem_paths']
flatten()[source]

Creates a tuple list from a dict with a tuple/value pair for every value that is a list

get(key, default)[source]
update(values)[source]
class galaxy.util.ParamsWithSpecs(specs=None, params=None)[source]

Bases: collections.defaultdict

galaxy.util.asbool(obj)[source]
galaxy.util.build_url(base_url, port=80, scheme='http', pathspec=None, params=None, doseq=False)[source]
galaxy.util.commaify(amount)[source]
galaxy.util.compare_urls(url1, url2, compare_scheme=True, compare_hostname=True, compare_path=True)[source]
galaxy.util.config_directories_from_setting(directories_setting, galaxy_root='/home/docs/checkouts/readthedocs.org/user_builds/galaxy-lib/checkouts/latest/galaxy/util/../../..')[source]

Parse the directories_setting into a list of relative or absolute filesystem paths that will be searched to discover plugins.

Parameters:
  • galaxy_root (string) – the root path of this galaxy installation
  • directories_setting (string (default: None)) – the filesystem path (or paths) to search for plugins. Can be CSV string of paths. Will be treated as absolute if a path starts with ‘/’, relative otherwise.
Return type:

list of strings

Returns:

list of filesystem paths

galaxy.util.directory_hash_id(id)[source]
>>> directory_hash_id( 100 )
['000']
>>> directory_hash_id( "90000" )
['090']
>>> directory_hash_id("777777777")
['000', '777', '777']
>>> directory_hash_id("135ee48a-4f51-470c-ae2f-ce8bd78799e6")
['1', '3', '5']
galaxy.util.docstring_trim(docstring)[source]

Trimming python doc strings. Taken from: http://www.python.org/dev/peps/pep-0257/

galaxy.util.download_to_file(url, dest_file_path, timeout=30, chunk_size=1048576)[source]

Download a URL to a file in chunks.

galaxy.util.file_iter(fname, sep=None)[source]

This generator iterates over a file and yields its lines splitted via the C{sep} parameter. Skips empty lines and lines starting with the C{#} character.

>>> lines = [ line for line in file_iter(__file__) ]
>>> len(lines) !=  0
True
galaxy.util.file_reader(fp, chunk_size=65536)[source]

This generator yields the open fileobject in chunks (default 64k). Closes the file at the end

galaxy.util.find_instance_nested(item, instances, match_key=None)[source]

Recursively find instances from lists, dicts, tuples.

instances should be a tuple of valid instances If match_key is given the key must match for an instance to be added to the list of found instances.

galaxy.util.galaxy_directory()[source]
galaxy.util.get_charset_from_http_headers(headers, default=None)[source]
galaxy.util.get_file_size(value, default=None)[source]
galaxy.util.in_directory(file, directory, local_path_module=<module 'posixpath' from '/home/docs/checkouts/readthedocs.org/user_builds/galaxy-lib/envs/latest/lib/python2.7/posixpath.pyc'>)[source]

Return true, if the common prefix of both is equal to directory e.g. /a/b/c/d.rst and directory is /a/b, the common prefix is /a/b. This function isn’t used exclusively for security checks, but if it is used for such checks it is assumed that directory is a “trusted” path - supplied by Galaxy or by the admin and file is something generated by a tool, configuration, external web server, or user supplied input.

local_path_module is used by Pulsar to check Windows paths while running on a POSIX-like system.

>>> base_dir = tempfile.mkdtemp()
>>> safe_dir = os.path.join(base_dir, "user")
>>> os.mkdir(safe_dir)
>>> good_file = os.path.join(safe_dir, "1")
>>> with open(good_file, "w") as f: _ = f.write("hello")
>>> in_directory(good_file, safe_dir)
True
>>> in_directory("/other/file/is/here.txt", safe_dir)
False
>>> unsafe_link = os.path.join(safe_dir, "2")
>>> os.symlink("/other/file/bad.fasta", unsafe_link)
>>> in_directory(unsafe_link, safe_dir)
False
galaxy.util.is_binary(value)[source]

File is binary if it contains a null-byte by default (e.g. behavior of grep, etc.). This may fail for utf-16 files, but so would ASCII encoding. >>> is_binary( string.printable ) False >>> is_binary( b’xcex94’ ) False >>> is_binary( b’x00’ ) True

galaxy.util.is_uuid(value)[source]

This method returns True if value is a UUID, otherwise False. >>> is_uuid( “123e4567-e89b-12d3-a456-426655440000” ) True >>> is_uuid( “0x3242340298902834” ) False

galaxy.util.listify(item, do_strip=False)[source]

Make a single item a single item list.

If item is a string, it is split on comma (,) characters to produce the list. Optionally, if do_strip is true, any extra whitespace around the split items is stripped.

If item is a list it is returned unchanged. If item is a tuple, it is converted to a list and returned. If item evaluates to False, an empty list is returned.

Parameters:
  • item (object) – object to make a list from
  • do_strip (bool) – strip whitespaces from around split items, if set to True
Return type:

list

Returns:

The input as a list

galaxy.util.mask_password_from_url(url)[source]

Masks out passwords from connection urls like the database connection in galaxy.ini

>>> mask_password_from_url( 'sqlite+postgresql://user:password@localhost/' )
'sqlite+postgresql://user:********@localhost/'
>>> mask_password_from_url( 'amqp://user:amqp@localhost' )
'amqp://user:********@localhost'
>>> mask_password_from_url( 'amqp://localhost')
'amqp://localhost'
galaxy.util.merge_sorted_iterables(operator, *iterables)[source]
>>> operator = lambda x: x
>>> list( merge_sorted_iterables( operator, [1,2,3], [4,5] ) )
[1, 2, 3, 4, 5]
>>> list( merge_sorted_iterables( operator, [4, 5], [1,2,3] ) )
[1, 2, 3, 4, 5]
>>> list( merge_sorted_iterables( operator, [1, 4, 5], [2], [3] ) )
[1, 2, 3, 4, 5]
galaxy.util.mkstemp_ln(src, prefix='mkstemp_ln_')[source]

From tempfile._mkstemp_inner, generate a hard link in the same dir with a random name. Created so we can persist the underlying file of a NamedTemporaryFile upon its closure.

galaxy.util.move_merge(source, target)[source]
galaxy.util.nice_size(size)[source]

Returns a readably formatted string with the size

>>> nice_size(100)
'100 bytes'
>>> nice_size(10000)
'9.8 KB'
>>> nice_size(1000000)
'976.6 KB'
>>> nice_size(100000000)
'95.4 MB'
galaxy.util.object_to_string(obj)[source]
galaxy.util.parse_int(value, min_val=None, max_val=None, default=None, allow_none=False)[source]
galaxy.util.parse_non_hex_float(s)[source]

Parse string s into a float but throw a ValueError if the string is in the otherwise acceptable format d+ed+ (e.g. 40000000000000e5.)

This can be passed into json.loads to prevent a hex string in the above format from being incorrectly parsed as a float in scientific notation.

>>> parse_non_hex_float( '123.4' )
123.4
>>> parse_non_hex_float( '2.45e+3' )
2450.0
>>> parse_non_hex_float( '2.45e-3' )
0.00245
>>> parse_non_hex_float( '40000000000000e5' )
Traceback (most recent call last):
    ...
ValueError: could not convert string to float: 40000000000000e5
galaxy.util.parse_resource_parameters(resource_param_file)[source]

Code shared between jobs and workflows for reading resource parameter configuration files.

TODO: Allow YAML in addition to XML.

galaxy.util.parse_xml(fname)[source]

Returns a parsed xml tree

galaxy.util.parse_xml_string(xml_string)[source]
galaxy.util.pretty_print_json(json_data, is_json_string=False)[source]
galaxy.util.pretty_print_time_interval(time=False, precise=False, utc=False)[source]

Get a datetime object or a int() Epoch timestamp and return a pretty string like ‘an hour ago’, ‘Yesterday’, ‘3 months ago’, ‘just now’, etc credit: http://stackoverflow.com/questions/1551382/user-friendly-time-format-in-python

galaxy.util.pretty_print_xml(elem, level=0)[source]
galaxy.util.read_build_sites(filename, check_builds=True)[source]

read db names to ucsc mappings from file, this file should probably be merged with the one above

galaxy.util.read_dbnames(filename)[source]

Read build names from file

galaxy.util.ready_name_for_url(raw_name)[source]

General method to convert a string (i.e. object name) to a URL-ready slug.

>>> ready_name_for_url( "My Cool Object" )
'My-Cool-Object'
>>> ready_name_for_url( "!My Cool Object!" )
'My-Cool-Object'
>>> ready_name_for_url( "Hello₩◎ґʟⅾ" )
'Hello'
galaxy.util.remove_protocol_from_url(url)[source]

Supplied URL may be null, if not ensure http:// or https:// etc… is stripped off.

galaxy.util.restore_text(text, character_map={'\t': '__tc__', '\n': '__cn__', '\r': '__cr__', '"': '__dq__', '#': '__pd__', "'": '__sq__', '<': '__lt__', '>': '__gt__', '@': '__at__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__'})[source]

Restores sanitized text

galaxy.util.roundify(amount, sfs=2)[source]

Take a number in string form and truncate to ‘sfs’ significant figures.

galaxy.util.rst_to_html(s, error=False)[source]

Convert a blob of reStructuredText to HTML

galaxy.util.safe_str_cmp(a, b)[source]

safely compare two strings in a timing-attack-resistant manner

galaxy.util.sanitize_for_filename(text, default=None)[source]

Restricts the characters that are allowed in a filename portion; Returns default value or a unique id string if result is not a valid name. Method is overly aggressive to minimize possible complications, but a maximum length is not considered.

galaxy.util.sanitize_lists_to_string(values, valid_characters=set(['!', ' ', ')', '(', '+', '*', '-', ', ', '/', '.', '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']), character_map={'\t': '__tc__', '\n': '__cn__', '\r': '__cr__', '"': '__dq__', '#': '__pd__', "'": '__sq__', '<': '__lt__', '>': '__gt__', '@': '__at__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__'}, invalid_character='X')[source]
galaxy.util.sanitize_param(value, valid_characters=set(['!', ' ', ')', '(', '+', '*', '-', ', ', '/', '.', '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']), character_map={'\t': '__tc__', '\n': '__cn__', '\r': '__cr__', '"': '__dq__', '#': '__pd__', "'": '__sq__', '<': '__lt__', '>': '__gt__', '@': '__at__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__'}, invalid_character='X')[source]

Clean incoming parameters (strings or lists)

galaxy.util.sanitize_text(text, valid_characters=set(['!', ' ', ')', '(', '+', '*', '-', ', ', '/', '.', '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']), character_map={'\t': '__tc__', '\n': '__cn__', '\r': '__cr__', '"': '__dq__', '#': '__pd__', "'": '__sq__', '<': '__lt__', '>': '__gt__', '@': '__at__', '[': '__ob__', ']': '__cb__', '{': '__oc__', '}': '__cc__'}, invalid_character='X')[source]

Restricts the characters that are allowed in text; accepts both strings and lists of strings; non-string entities will be cast to strings.

galaxy.util.send_mail(frm, to, subject, body, config, html=None)[source]

Sends an email.

Parameters:
  • frm (str) – from address
  • to (str) – to address
  • subject (str) – Subject line
  • body (str) – Body text (should be plain text)
  • config (object) – Galaxy configuration object
  • html (str) – Alternative HTML representation of the body content. If provided will convert the message to a MIMEMultipart. (Default ‘None’)
galaxy.util.shrink_stream_by_size(value, size, join_by='..', left_larger=True, beginning_on_size_error=False, end_on_size_error=False)[source]

Shrinks bytes read from value to size.

value needs to implement tell/seek, so files need to be opened in binary mode. Returns unicode text with invalid characters replaced.

galaxy.util.shrink_string_by_size(value, size, join_by='..', left_larger=True, beginning_on_size_error=False, end_on_size_error=False)[source]
galaxy.util.size_to_bytes(size)[source]

Returns a number of bytes if given a reasonably formatted string with the size

>>> size_to_bytes('1024')
1024
>>> size_to_bytes('10 bytes')
10
>>> size_to_bytes('4k')
4096
>>> size_to_bytes('2.2 TB')
2418925581107
galaxy.util.smart_str(s, encoding='utf-8', strings_only=False, errors='strict')[source]

Returns a bytestring version of ‘s’, encoded as specified in ‘encoding’.

If strings_only is True, don’t convert (some) non-string-like objects.

Adapted from an older, simpler version of django.utils.encoding.smart_str.

>>> assert smart_str(None) == b'None'
>>> assert smart_str(None, strings_only=True) is None
>>> assert smart_str(3) == b'3'
>>> assert smart_str(3, strings_only=True) == 3
>>> s = b'a bytes string'; assert smart_str(s) == s
>>> s = bytearray(b'a bytes string'); assert smart_str(s) == s
>>> assert smart_str(u'a simple unicode string') == b'a simple unicode string'
>>> assert smart_str(u'à strange ünicode ڃtring') == b'\xc3\xa0 strange \xc3\xbcnicode \xda\x83tring'
>>> assert smart_str(b'\xc3\xa0n \xc3\xabncoded utf-8 string', encoding='latin-1') == b'\xe0n \xebncoded utf-8 string'
>>> assert smart_str(bytearray(b'\xc3\xa0n \xc3\xabncoded utf-8 string'), encoding='latin-1') == b'\xe0n \xebncoded utf-8 string'
galaxy.util.string_as_bool(string)[source]
galaxy.util.string_as_bool_or_none(string)[source]
Returns True, None or False based on the argument:
True if passed True, ‘True’, ‘Yes’, or ‘On’ None if passed None or ‘None’ False otherwise

Note: string comparison is case-insensitive so lowecase versions of those function equivalently.

galaxy.util.string_to_object(s)[source]
galaxy.util.stringify_dictionary_keys(in_dict)[source]
galaxy.util.strip_control_characters(s)[source]

Strip unicode control characters from a string.

galaxy.util.strip_control_characters_nested(item)[source]

Recursively strips control characters from lists, dicts, tuples.

galaxy.util.synchronized(func)[source]

This wrapper will serialize access to ‘func’ to a single thread. Use it as a decorator.

galaxy.util.umask_fix_perms(path, umask, unmasked_perms, gid=None)[source]

umask-friendly permissions fixing

galaxy.util.unicodify(value, encoding='utf-8', error='replace')[source]

Returns a Unicode string or None.

>>> assert unicodify(None) is None
>>> assert unicodify('simple string') == u'simple string'
>>> assert unicodify(3) == u'3'
>>> assert unicodify(bytearray([115, 116, 114, 196, 169, 195, 177, 103])) == u'strĩñg'
>>> assert unicodify(Exception('message')) == u'message'
>>> assert unicodify('cómplǐcḁtëd strĩñg') == u'cómplǐcḁtëd strĩñg'
>>> s = u'cómplǐcḁtëd strĩñg'; assert unicodify(s) == s
>>> s = u'lâtín strìñg'; assert unicodify(s.encode('latin-1'), 'latin-1') == s
>>> s = u'lâtín strìñg'; assert unicodify(s.encode('latin-1')) == u'l�t�n str��g'
>>> s = u'lâtín strìñg'; assert unicodify(s.encode('latin-1'), error='ignore') == u'ltn strg'
galaxy.util.unique_id(KEY_SIZE=128)[source]

Generates an unique id

>>> ids = [ unique_id() for i in range(1000) ]
>>> len(set(ids))
1000
galaxy.util.url_get(base_url, password_mgr=None, pathspec=None, params=None)[source]

Make contact with the uri provided and return any contents.

galaxy.util.which(file)[source]
galaxy.util.xml_element_compare(elem1, elem2)[source]
galaxy.util.xml_element_list_compare(elem_list1, elem_list2)[source]
galaxy.util.xml_element_to_dict(elem)[source]
galaxy.util.xml_text(root, name=None)[source]

Returns the text inside an element

galaxy.util.xml_to_string(elem, pretty=False)[source]

Returns a string from an xml tree