示例#1
0
    def range_to_dt(self, node):
        start = end = None
        dp = self.dateparser.get_parser()

        if node.start:
            start = dp.date_from(node.start, self.basedate)
            if start is None:
                return self.errorize(node.start, node)
        if node.end:
            end = dp.date_from(node.end, self.basedate)
            if end is None:
                return self.errorize(node.end, node)

        if start and end:
            ts = timespan(start, end).disambiguated(self.basedate)
            start, end = ts.start, ts.end
        elif start:
            start = start.disambiguated(self.basedate)
            if isinstance(start, timespan):
                start = start.start
        elif end:
            end = end.disambiguated(self.basedate)
            if isinstance(end, timespan):
                end = end.end
        drn = DateRangeNode(node.fieldname, start, end, boost=node.boost)
        drn.startchar = node.startchar
        drn.endchar = node.endchar
        return drn
示例#2
0
 def dates_to_timespan(self, dates):
     if self.fn:
         return self.fn(dates)
     elif len(dates) == 2:
         return timespan(dates[0], dates[1])
     else:
         raise DateParseError("Don't know what to do with %r" % (dates, ))
示例#3
0
    def range_to_dt(self, node):
        start = end = None
        dp = self.dateparser.get_parser()

        if node.start:
            start = dp.date_from(node.start, self.basedate)
            if start is None:
                return self.errorize(node.start, node)
        if node.end:
            end = dp.date_from(node.end, self.basedate)
            if end is None:
                return self.errorize(node.end, node)

        if start and end:
            ts = timespan(start, end).disambiguated(self.basedate)
            start, end = ts.start, ts.end
        elif start:
            start = start.disambiguated(self.basedate)
            if isinstance(start, timespan):
                start = start.start
        elif end:
            end = end.disambiguated(self.basedate)
            if isinstance(end, timespan):
                end = end.end
        drn = DateRangeNode(node.fieldname, start, end, boost=node.boost)
        drn.startchar = node.startchar
        drn.endchar = node.endchar
        return drn
示例#4
0
 def do_dates(self, parser, stream):
     schema = parser.schema
     if not schema:
         return stream
     
     from whoosh.fields import DATETIME
     datefields = frozenset(fieldname for fieldname, field
                            in parser.schema.items()
                            if isinstance(field, DATETIME))
     
     newstream = stream.empty()
     for t in stream:
         if isinstance(t, Group):
             t = self.do_dates(parser, t)
         elif t.fieldname in datefields:
             if isinstance(t, Word):
                 text = t.text
                 try:
                     dt = self.dateparser.date_from(text, self.basedate)
                     if dt is None:
                         if self.callback:
                             self.callback(text)
                         t = ErrorToken(t)
                     else:
                         t = DateToken(t.fieldname, dt, t.boost)
                 except DateParseError, e:
                     if self.callback:
                         self.callback("%s (%r)" % (str(e), text))
                     t = ErrorToken(t)
             
             elif isinstance(t, RangePlugin.Range):
                 start = end = None
                 error = None
                 
                 dp = self.dateparser.get_parser()
                 
                 if t.start:
                     start = dp.date_from(t.start, self.basedate)
                     if start is None:
                         error = t.start
                 if t.end:
                     end = dp.date_from(t.end, self.basedate)
                     if end is None and error is None:
                         error = t.end
                 
                 if error is not None:
                     if self.callback:
                         self.callback(error)
                     t = ErrorToken(t)
                 else:
                     ts = timespan(start, end).disambiguated(self.basedate)
                     t = DateToken(t.fieldname, ts, boost=t.boost)
         
         newstream.append(t)