mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-07-15 01:32:53 -07:00
updated libs to fix guessit and subliminal. Fixes #1080
This commit is contained in:
parent
319d418af8
commit
0625f7f3c0
263 changed files with 28711 additions and 12615 deletions
153
libs/rebulk/utils.py
Normal file
153
libs/rebulk/utils.py
Normal file
|
@ -0,0 +1,153 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Various utilities functions
|
||||
"""
|
||||
from collections import MutableSet
|
||||
|
||||
from types import GeneratorType
|
||||
|
||||
|
||||
def find_all(string, sub, start=None, end=None, ignore_case=False):
|
||||
"""
|
||||
Return all indices in string s where substring sub is
|
||||
found, such that sub is contained in the slice s[start:end].
|
||||
|
||||
>>> list(find_all('The quick brown fox jumps over the lazy dog', 'fox'))
|
||||
[16]
|
||||
|
||||
>>> list(find_all('The quick brown fox jumps over the lazy dog', 'mountain'))
|
||||
[]
|
||||
|
||||
>>> list(find_all('The quick brown fox jumps over the lazy dog', 'The'))
|
||||
[0]
|
||||
|
||||
>>> list(find_all(
|
||||
... 'Carved symbols in a mountain hollow on the bank of an inlet irritated an eccentric person',
|
||||
... 'an'))
|
||||
[44, 51, 70]
|
||||
|
||||
>>> list(find_all(
|
||||
... 'Carved symbols in a mountain hollow on the bank of an inlet irritated an eccentric person',
|
||||
... 'an',
|
||||
... 50,
|
||||
... 60))
|
||||
[51]
|
||||
|
||||
:param string: the input string
|
||||
:type string: str
|
||||
:param sub: the substring
|
||||
:type sub: str
|
||||
:return: all indices in the input string
|
||||
:rtype: __generator[str]
|
||||
"""
|
||||
if ignore_case:
|
||||
sub = sub.lower()
|
||||
string = string.lower()
|
||||
while True:
|
||||
start = string.find(sub, start, end)
|
||||
if start == -1:
|
||||
return
|
||||
yield start
|
||||
start += len(sub)
|
||||
|
||||
|
||||
def get_first_defined(data, keys, default_value=None):
|
||||
"""
|
||||
Get the first defined key in data.
|
||||
:param data:
|
||||
:type data:
|
||||
:param keys:
|
||||
:type keys:
|
||||
:param default_value:
|
||||
:type default_value:
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
for key in keys:
|
||||
try:
|
||||
return data[key]
|
||||
except KeyError:
|
||||
pass
|
||||
return default_value
|
||||
|
||||
|
||||
def is_iterable(obj):
|
||||
"""
|
||||
Are we being asked to look up a list of things, instead of a single thing?
|
||||
We check for the `__iter__` attribute so that this can cover types that
|
||||
don't have to be known by this module, such as NumPy arrays.
|
||||
|
||||
Strings, however, should be considered as atomic values to look up, not
|
||||
iterables.
|
||||
|
||||
We don't need to check for the Python 2 `unicode` type, because it doesn't
|
||||
have an `__iter__` attribute anyway.
|
||||
"""
|
||||
return hasattr(obj, '__iter__') and not isinstance(obj, str) or isinstance(obj, GeneratorType)
|
||||
|
||||
|
||||
def extend_safe(target, source):
|
||||
"""
|
||||
Extends source list to target list only if elements doesn't exists in target list.
|
||||
:param target:
|
||||
:type target: list
|
||||
:param source:
|
||||
:type source: list
|
||||
"""
|
||||
for elt in source:
|
||||
if elt not in target:
|
||||
target.append(elt)
|
||||
|
||||
|
||||
class _Ref(object):
|
||||
"""
|
||||
Reference for IdentitySet
|
||||
"""
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.value is other.value
|
||||
|
||||
def __hash__(self):
|
||||
return id(self.value)
|
||||
|
||||
|
||||
class IdentitySet(MutableSet): # pragma: no cover
|
||||
"""
|
||||
Set based on identity
|
||||
"""
|
||||
def __init__(self, items=None):
|
||||
if items is None:
|
||||
items = []
|
||||
self.refs = set(map(_Ref, items))
|
||||
|
||||
def __contains__(self, elem):
|
||||
return _Ref(elem) in self.refs
|
||||
|
||||
def __iter__(self):
|
||||
return (ref.value for ref in self.refs)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.refs)
|
||||
|
||||
def add(self, elem):
|
||||
self.refs.add(_Ref(elem))
|
||||
|
||||
def discard(self, elem):
|
||||
self.refs.discard(_Ref(elem))
|
||||
|
||||
def update(self, iterable):
|
||||
"""
|
||||
Update set with iterable
|
||||
:param iterable:
|
||||
:type iterable:
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
for elem in iterable:
|
||||
self.add(elem)
|
||||
|
||||
def __repr__(self): # pragma: no cover
|
||||
return "%s(%s)" % (type(self).__name__, list(self))
|
Loading…
Add table
Add a link
Reference in a new issue