Fix for #481: Enhance output value

This commit is contained in:
Rato
2017-07-20 22:23:09 +02:00
parent d8f1c89ef0
commit 0bda2a73bd
5 changed files with 63 additions and 8 deletions
+1
View File
@@ -97,6 +97,7 @@ History
- Added default and configurable list of allowed languages and countries
- Added `VC-1` as new `video_codec` possible value
- Enhanced dash-separated `release_group` detection.
- Changed `size` output to return `guessit.Quantity` object.
2.1.4 (2017-06-01)
------------------
+3 -1
View File
@@ -265,7 +265,9 @@ Other properties
- **size**
Size (MB, GB, TB).
Size (MB, GB, TB). Examples: ``1.2GB`` (``<Quantity [1.2GB]>``), ``430MB`` (``<Quantity [430MB]>``).
- ``[<guessit.Quantity>]`` (object has ``magnitude`` and ``units``)
- **edition**
+1
View File
@@ -5,5 +5,6 @@ Extracts as much information as possible from a video file.
"""
from .api import guessit, GuessItApi
from .options import ConfigurationException
from .rules.common.quantity import Quantity
from .__version__ import __version__
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Quantity
"""
import re
import six
class Quantity(object):
"""
Represent a quantity object with magnitude and units.
"""
parser_re = re.compile(r'(?P<magnitude>\d+(?:[.]\d+)?)(?P<units>[^\d]+)')
def __init__(self, magnitude, units):
self.magnitude = magnitude
self.units = units
@classmethod
def fromstring(cls, string):
"""
Parse the string into a quantity object.
:param string:
:return:
"""
values = cls.parser_re.match(string).groupdict()
try:
magnitude = int(values['magnitude'])
except ValueError:
magnitude = float(values['magnitude'])
units = values['units'].upper()
return Quantity(magnitude, units)
def __hash__(self):
return hash(str(self))
def __eq__(self, other):
if isinstance(other, six.string_types):
return str(self) == other
if not isinstance(other, Quantity):
return NotImplemented
return self.magnitude == other.magnitude and self.units == other.units
def __ne__(self, other):
return not self == other
def __repr__(self):
return '<Quantity [{0}]>'.format(self)
def __str__(self):
return '{0}{1}'.format(self.magnitude, self.units)
+3 -7
View File
@@ -7,8 +7,9 @@ import re
from rebulk import Rebulk
from ..common.validators import seps_surround
from ..common import dash
from ..common.quantity import Quantity
from ..common.validators import seps_surround
def size():
@@ -17,13 +18,8 @@ def size():
:return: Created Rebulk object
:rtype: Rebulk
"""
def format_size(value):
"""Format size using uppercase and no space."""
return re.sub(r'(?<=\d)[.](?=[^\d])', '', value.upper())
rebulk = Rebulk().regex_defaults(flags=re.IGNORECASE, abbreviations=[dash])
rebulk.defaults(name='size', validator=seps_surround)
rebulk.regex(r'\d+\.?[mgt]b', r'\d+\.\d+[mgt]b', formatter=format_size, tags=['release-group-prefix'])
rebulk.regex(r'\d+\.?[mgt]b', r'\d+\.\d+[mgt]b', formatter=Quantity.fromstring, tags=['release-group-prefix'])
return rebulk