[Python-modules-commits] [guessit] 03/07: Update dateutil patch

Etienne Millon emillon-guest at moszumanska.debian.org
Sun Nov 13 19:32:31 UTC 2016


This is an automated email from the git hooks/post-receive script.

emillon-guest pushed a commit to branch master
in repository guessit.

commit 74976ece23da7d7e627aa8287a2c32861bf11e78
Author: Etienne Millon <me at emillon.org>
Date:   Mon Nov 7 22:18:14 2016 +0100

    Update dateutil patch
---
 guessit/date.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/guessit/date.py b/guessit/date.py
index 385c508..a24034f 100644
--- a/guessit/date.py
+++ b/guessit/date.py
@@ -71,7 +71,48 @@ def search_year(string):
     return None, None
 
 
-def search_date(string, year_first=None, day_first=True):
+def _is_int(string):
+    """
+    Check if the input string is an integer
+
+    :param string:
+    :type string:
+    :return:
+    :rtype:
+    """
+    try:
+        int(string)
+        return True
+    except ValueError:
+        return False
+
+
+def _guess_day_first_parameter(groups):
+    """
+    If day_first is not defined, use some heuristic to fix it.
+    It helps to solve issues with python dateutils 2.5.3 parser changes.
+
+    :param groups: match groups found for the date
+    :type groups: list of match objects
+    :return: day_first option guessed value
+    :rtype: bool
+    """
+
+    # If match starts with a long year, then day_first is force to false.
+    if _is_int(groups[0]) and valid_year(int(groups[0][:4])):
+        return False
+    # If match ends with a long year, the day_first is forced to true.
+    elif _is_int(groups[-1]) and valid_year(int(groups[-1][-4:])):
+        return True
+    # If match starts with a short year, then day_first is force to false.
+    elif _is_int(groups[0]) and int(groups[0][:2]) > 31:
+        return False
+    # If match ends with a short year, then day_first is force to true.
+    elif _is_int(groups[-1]) and int(groups[-1][-2:]) > 31:
+        return True
+
+
+def search_date(string, year_first=None, day_first=None):
     """Looks for date patterns, and if found return the date and group span.
 
     Assumes there are sentinels at the beginning and end of the string that
@@ -91,20 +132,29 @@ def search_date(string, year_first=None, day_first=True):
     """
     start, end = None, None
     match = None
+    groups = None
     for date_re in date_regexps:
         s = date_re.search(string)
         if s and (match is None or s.end() - s.start() > len(match)):
             start, end = s.start(), s.end()
             if date_re.groups:
-                match = '-'.join(s.groups())
+                groups = s.groups()
+                match = '-'.join(groups)
             else:
                 match = s.group()
+                groups = [match]
 
     if match is None:
         return None, None
 
     today = datetime.date.today()
 
+    if year_first and day_first is None:
+        day_first = False
+
+    if day_first is None:
+        day_first = _guess_day_first_parameter(groups)
+
     # If day_first/year_first is undefined, parse is made using both possible values.
     yearfirst_opts = [False, True]
     if year_first is not None:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/guessit.git



More information about the Python-modules-commits mailing list