From 0bda2a73bd83d1e0b4358acdef4449e3edb41d59 Mon Sep 17 00:00:00 2001 From: Rato Date: Thu, 20 Jul 2017 22:23:09 +0200 Subject: [PATCH] Fix for #481: Enhance output value --- HISTORY.rst | 1 + docs/properties.rst | 4 ++- guessit/__init__.py | 1 + guessit/rules/common/quantity.py | 55 ++++++++++++++++++++++++++++++++ guessit/rules/properties/size.py | 10 ++---- 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 guessit/rules/common/quantity.py diff --git a/HISTORY.rst b/HISTORY.rst index ebbec41..e30aa9e 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) ------------------ diff --git a/docs/properties.rst b/docs/properties.rst index a5c07ea..2c58dae 100644 --- a/docs/properties.rst +++ b/docs/properties.rst @@ -265,7 +265,9 @@ Other properties - **size** - Size (MB, GB, TB). + Size (MB, GB, TB). Examples: ``1.2GB`` (````), ``430MB`` (````). + + - ``[]`` (object has ``magnitude`` and ``units``) - **edition** diff --git a/guessit/__init__.py b/guessit/__init__.py index 22e9dbb..4b519c9 100644 --- a/guessit/__init__.py +++ b/guessit/__init__.py @@ -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__ diff --git a/guessit/rules/common/quantity.py b/guessit/rules/common/quantity.py new file mode 100644 index 0000000..57614af --- /dev/null +++ b/guessit/rules/common/quantity.py @@ -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\d+(?:[.]\d+)?)(?P[^\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 ''.format(self) + + def __str__(self): + return '{0}{1}'.format(self.magnitude, self.units) diff --git a/guessit/rules/properties/size.py b/guessit/rules/properties/size.py index 84f0303..2657dd2 100644 --- a/guessit/rules/properties/size.py +++ b/guessit/rules/properties/size.py @@ -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