pyrocore.util package

Utility Modules.

Copyright (c) 2010 The PyroScope Project <pyroscope.project@gmail.com>

Submodules

pyrocore.util.algo module

Helper Algorithms.

Copyright (c) 2009, 2010 The PyroScope Project <pyroscope.project@gmail.com>

class pyrocore.util.algo.AttributeMapping(obj, defaults=None)[source]

Bases: object

Wrap an object’s dict so that it can be accessed by the mapping protocol.

pyrocore.util.algo.flatten(nested, containers=(<type 'list'>, <type 'tuple'>))[source]

Flatten a nested list in-place and return it.

pyrocore.util.load_config module

Configuration Loader.

For details, see https://pyrocore.readthedocs.io/en/latest/setup.html

Copyright (c) 2009, 2010, 2011 The PyroScope Project <pyroscope.project@gmail.com>

class pyrocore.util.load_config.ConfigLoader(config_dir=None)[source]

Bases: object

Populates this module’s dictionary with the user-defined configuration values.

CONFIG_INI = 'config.ini'
CONFIG_PY = 'config.py'
INTERPOLATION_ESCAPE = <_sre.SRE_Pattern object>
create(remove_all_rc_files=False)[source]

Create default configuration files at either the default location or the given directory.

load(optional_cfg_files=None)[source]

Actually load the configuation from either the default location or the given directory.

pyrocore.util.load_config.validate(key, val)[source]

Validate a configuration value.

pyrocore.util.load_config.walk_resources(package_or_requirement, resource_name, recurse=True, base='')[source]

Yield paths of files in the given resource directory, all paths start with ‘/’.

pyrocore.util.matching module

Torrent Item Filters.

Copyright (c) 2009, 2010, 2011 The PyroScope Project <pyroscope.project@gmail.com>

class pyrocore.util.matching.BoolFilter(name, value)[source]

Bases: pyrocore.util.matching.FieldFilter

Filter boolean values.

match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.ByteSizeFilter(name, value)[source]

Bases: pyrocore.util.matching.NumericFilterBase

Filter size and bandwidth values.

UNITS = {'k': 1024, 'b': 1, 'm': 1048576, 'g': 1073741824}
pre_filter()[source]

Return rTorrent condition to speed up data transfer.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.CompoundFilterAll[source]

Bases: pyrocore.util.matching.CompoundFilterBase

List of filters that must all match (AND).

match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

class pyrocore.util.matching.CompoundFilterAny[source]

Bases: pyrocore.util.matching.CompoundFilterBase

List of filters where at least one must match (OR).

match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

class pyrocore.util.matching.CompoundFilterBase[source]

Bases: pyrocore.util.matching.Filter, list

List of filters.

class pyrocore.util.matching.ConditionParser(lookup, default_field=None, ident_re='[_A-Za-z][_A-Za-z0-9]*')[source]

Bases: object

Filter condition parser.

classmethod AMENABLE(_)[source]

Prefined lookup mode for typeless access to any field name.

COMPARISON_OPS = {'>=': '!-%s', '<>': '!%s', '<=': '!+%s', '~': '/%s/', '!=': '!%s', '<': '-%s', '>': '+%s'}
parse(conditions)[source]

Parse filter conditions.

@param conditions: multiple conditions. @type conditions: list or str

class pyrocore.util.matching.DurationFilter(name, value)[source]

Bases: pyrocore.util.matching.TimeFilter

Filter durations in seconds.

match(item)[source]

Return True if filter matches item.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.EqualsFilter(name, value)[source]

Bases: pyrocore.util.matching.FieldFilter

Filter fields equal to the given value.

match(item)[source]

Return True if filter matches item.

class pyrocore.util.matching.FieldFilter(name, value)[source]

Bases: pyrocore.util.matching.Filter

Base class for all field filters.

PRE_FILTER_FIELDS = {'uploaded': 'd.up.total=', 'custom_tm_completed': 'd.custom=tm_completed', 'tagged': 'd.custom=tags', 'hash': 'd.hash=', 'prio': 'd.priority=', 'started': 'd.custom=tm_started', 'completed': 'd.custom=tm_completed', 'is_active': 'd.is_active=', 'custom_tm_started': 'd.custom=tm_started', 'up': 'd.up.rate=', 'down': 'd.down.rate=', 'is_open': 'd.is_open=', 'path': 'd.base_path=', 'loaded': 'd.custom=tm_loaded', 'message': 'd.message=', 'size': 'd.size_bytes=', 'ratio': 'd.ratio=', 'name': 'd.name=', 'metafile': 'd.tied_to_file=', 'is_ignored': 'd.ignore_commands=', 'throttle': 'd.throttle_name=', 'custom_tm_loaded': 'd.custom=tm_loaded', 'is_complete': 'd.complete=', 'is_multi_file': 'd.is_multi_file='}
validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.FilesFilter(name, value)[source]

Bases: pyrocore.util.matching.PatternFilter

Case-insensitive pattern filter on filenames in a torrent.

match(item)[source]

Return True if filter matches item.

class pyrocore.util.matching.Filter[source]

Bases: object

Base class for all filters.

match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

exception pyrocore.util.matching.FilterError[source]

Bases: pyrocore.error.UserError

(Syntax) error in filter.

class pyrocore.util.matching.FloatFilter(name, value)[source]

Bases: pyrocore.util.matching.NumericFilterBase

Filter float values.

FIELD_SCALE = {'ratio': 1000}
pre_filter()[source]

Return rTorrent condition to speed up data transfer.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.MagicFilter(name, value)[source]

Bases: pyrocore.util.matching.FieldFilter

Filter that looks at the comparison value and automatically decides what type of filter to use.

match(item)[source]

Return True if filter matches item.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.NegateFilter(inner)[source]

Bases: pyrocore.util.matching.Filter

Negate result of another filter (NOT).

match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

class pyrocore.util.matching.NumericFilterBase(name, value)[source]

Bases: pyrocore.util.matching.FieldFilter

Base class for numerical value filters.

match(item)[source]

Return True if filter matches item.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.PatternFilter(name, value)[source]

Bases: pyrocore.util.matching.FieldFilter

Case-insensitive pattern filter, either a glob or a /regex/ pattern.

CLEAN_PRE_VAL_RE = <_sre.SRE_Pattern object>
SPLIT_PRE_GLOB_RE = <_sre.SRE_Pattern object>
SPLIT_PRE_VAL_RE = <_sre.SRE_Pattern object>
match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.TaggedAsFilter(name, value)[source]

Bases: pyrocore.util.matching.FieldFilter

Case-insensitive tags filter. Tag fields are white-space separated lists of tags.

match(item)[source]

Return True if filter matches item.

pre_filter()[source]

Return rTorrent condition to speed up data transfer.

validate()[source]

Validate filter condition (template method).

class pyrocore.util.matching.TimeFilter(name, value)[source]

Bases: pyrocore.util.matching.NumericFilterBase

Filter UNIX timestamp values.

TIMEDELTA_RE = <_sre.SRE_Pattern object at 0x2fda870>
TIMEDELTA_UNITS = {'y': <function <lambda>>, 's': <function <lambda>>, 'd': <function <lambda>>, 'w': <function <lambda>>, 'i': <function <lambda>>, 'h': <function <lambda>>, 'm': <function <lambda>>}
pre_filter()[source]

Return rTorrent condition to speed up data transfer.

validate()[source]

Validate filter condition (template method).

validate_time(duration=False)[source]

Validate filter condition (template method) for timestamps and durations.

class pyrocore.util.matching.TimeFilterNotNull(name, value)[source]

Bases: pyrocore.util.matching.TimeFilter

Filter UNIX timestamp values, ignore unset values unless compared to 0.

validate()[source]

Validate filter condition (template method).

pyrocore.util.matching.truth(val, context)[source]

Convert truth value in “val” to a boolean.

pyrocore.util.matching.unquote_pre_filter(pre_filter, _regex=<_sre.SRE_Pattern object>)[source]

Unquote a pre-filter condition.

pyrocore.util.metafile module

Metafile Support.

Copyright (c) 2009, 2010, 2011 The PyroScope Project <pyroscope.project@gmail.com>

class pyrocore.util.metafile.MaskingPrettyPrinter(indent=1, width=80, depth=None, stream=None)[source]

Bases: pprint.PrettyPrinter

A PrettyPrinter that masks strings in the object tree.

format(obj, context, maxlevels, level)[source]

Mask obj if it looks like an URL, then pass it to the super class.

class pyrocore.util.metafile.Metafile(filename, datapath=None)[source]

Bases: object

A torrent metafile.

IGNORE_GLOB = [u'core', u'CVS', u'.*', u'*~', u'*.swp', u'*.tmp', u'*.bak', u'[Tt]humbs.db', u'[Dd]esktop.ini', u'ehthumbs_vista.db']
check(metainfo, datapath, progress=None)[source]

Check piece hashes of a metafile against the given datapath.

create(datapath, tracker_urls, comment=None, root_name=None, created_by=None, private=False, no_date=False, progress=None, callback=None)[source]

Create a metafile with the path given on object creation. Returns the last metafile dict that was written (as an object, not bencoded).

datapath

Get a valid datapath, else raise an exception.

listing(masked=True)[source]

List torrent info & contents. Returns a list of formatted lines.

walk()[source]

Generate paths in “self.datapath”.

pyrocore.util.metafile.add_fast_resume(meta, datapath)[source]

Add fast resume data to a metafile dict.

pyrocore.util.metafile.assign_fields(meta, assignments)[source]

Takes a list of C{key=value} strings and assigns them to the given metafile. If you want to set nested keys (e.g. “info.source”), you have to use a dot as a separator. For exotic keys containing a dot, double that dot (“dotted..key”).

Numeric values starting with “+” or “-” are converted to integers.

If just a key name is given (no ‘=’), the field is removed.

pyrocore.util.metafile.check_info(info)[source]

Validate info dict.

Raise ValueError if validation fails.

pyrocore.util.metafile.check_meta(meta)[source]

Validate meta dict.

Raise ValueError if validation fails.

pyrocore.util.metafile.checked_open(filename, log=None, quiet=False)[source]

Open and validate the given metafile. Optionally provide diagnostics on the passed logger, for invalid metafiles, which then just cause a warning but no exception. “quiet” can supress that warning.

pyrocore.util.metafile.clean_meta(meta, including_info=False, logger=None)[source]

Clean meta dict. Optionally log changes using the given logger.

@param logger: If given, a callable accepting a string message. @return: Set of keys removed from C{meta}.

pyrocore.util.metafile.console_progress()[source]

Return a progress indicator for consoles if stdout is a tty.

pyrocore.util.metafile.data_size(metadata)[source]

Calculate the size of a torrent based on parsed metadata.

pyrocore.util.metafile.info_hash(metadata)[source]

Return info hash as a string.

pyrocore.util.metafile.mask_keys(announce_url)[source]

Mask any passkeys (hex sequences) in an announce URL.

pyrocore.util.metafile.sanitize(meta)[source]

Try to fix common problems, especially transcode non-standard string encodings.

pyrocore.util.osmagic module

Platform Specific Incantations.

Copyright (c) 2011 The PyroScope Project <pyroscope.project@gmail.com>

pyrocore.util.osmagic.check_process(pidfile)[source]

Read pid file and check process status. Return (running, pid).

pyrocore.util.osmagic.daemonize(pidfile=None, logfile=None, sync=True)[source]

Fork the process into the background.

@param pidfile: Optional PID file path. @param sync: Wait for parent process to disappear? @param logfile: Optional name of stdin/stderr log file or stream.

pyrocore.util.osmagic.guard(pidfile, guardfile=None)[source]

Raise an EnvironmentError when the “guardfile” doesn’t exist, or the process with the ID found in “pidfile” is still active.

pyrocore.util.pymagic module

Python Utility Functions.

Copyright (c) 2009, 2010 The PyroScope Project <pyroscope.project@gmail.com>

class pyrocore.util.pymagic.JSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)[source]

Bases: json.encoder.JSONEncoder

Custon JSON encoder.

default(o)[source]

Support more object types.

pyrocore.util.pymagic.get_class_logger(obj)[source]

Get a logger specific for the given object’s class.

pyrocore.util.pymagic.get_lazy_logger(name)[source]

Return a logger proxy that is lazily initialized.

This avoids the problems associated with module-level loggers being created early (on import), before the logging system is properly initialized.

pyrocore.util.pymagic.import_name(module_spec, name=None)[source]

Import identifier C{name} from module C{module_spec}.

If name is omitted, C{module_spec} must contain the name after the module path, delimited by a colon (like a setuptools entry-point).

@param module_spec: Fully qualified module name, e.g. C{x.y.z}. @param name: Name to import from C{module_spec}. @return: Requested object. @rtype: object

pyrocore.util.stats module

Statistics data.

Copyright (c) 2014 The PyroScope Project <pyroscope.project@gmail.com>

pyrocore.util.stats.engine_data(engine)[source]

Get important performance data and metadata from rTorrent.

pyrocore.util.traits module

Classification.

Copyright (c) 2010, 2011 The PyroScope Project <pyroscope.project@gmail.com>

pyrocore.util.traits.detect_traits(name=None, alias=None, filetype=None)[source]

Build traits list from passed attributes.

The result is a list of hierarchical classifiers, the top-level consisting of “audio”, “movie”, “tv”, “video”, “document”, etc. It can be used as a part of completion paths to build directory structures.

pyrocore.util.traits.get_filetypes(filelist, path=None, size=<function <lambda>>)[source]

Get a sorted list of file types and their weight in percent from an iterable of file names.

@return: List of weighted file extensions (no ‘.’), sorted in descending order @rtype: list of (weight, filetype)

pyrocore.util.traits.name_trait(name, add_info=False)[source]

Determine content type from name.

pyrocore.util.xmlrpc module

RTorrent client proxy.

Copyright (c) 2011 The PyroScope Project <pyroscope.project@gmail.com>

exception pyrocore.util.xmlrpc.HashNotFound(msg, *args)[source]

Bases: pyrocore.util.xmlrpc.XmlRpcError

Non-existing or disappeared hash.

class pyrocore.util.xmlrpc.RTorrentMethod(proxy, method_name)[source]

Bases: object

Collect attribute accesses to build the final method name.

NEEDS_FAKE_TARGET = set(['ui.current_view.set', 'view_filter'])
class pyrocore.util.xmlrpc.RTorrentProxy(url, mapping=None)[source]

Bases: object

Proxy to rTorrent’s XMLRPC interface.

Method calls are built from attribute accesses, i.e. you can do something like C{proxy.system.client_version()}.

exception pyrocore.util.xmlrpc.XmlRpcError(msg, *args)[source]

Bases: exceptions.Exception

Base class for XMLRPC protocol errors.