Example #1
0
    def _read_version1_media(self, media):
        data = self.__data_default()
        tag_data = self.__tag_data_default()
        relpath2index = {}
        keymap = dict.fromkeys(MediaData._fields)
        for k in keymap:
            keymap[k] = k
        keymap['_ctime'] = 'ctime_'
        keymap['_mtime'] = 'mtime_'

        for index, (key, m) in enumerate(media):
            relpath2index[key] = index
            tags = m.pop('tags')
            for tname, v in tags.items():
                tag_data[tname].append(v)
            for k, v in m.items():
                data[keymap[k]].append(v)
            if 'file_name' not in m:
                data['file_name'].append(basename(key))

        data['mtime_'] = [datetime_to_long(x) for x in data['mtime_']]
        data['ctime_'] = [datetime_to_long(x) for x in data['ctime_']]
        self._data = data
        self._tag_data = tag_data
        self._relpath2index = relpath2index
Example #2
0
def test_datetime():
    dtf = fields.DATETIME(stored=True)
    schema = fields.Schema(id=fields.ID(stored=True), date=dtf)
    st = RamStorage()
    ix = st.create_index(schema)

    w = ix.writer()
    for month in xrange(1, 12):
        for day in xrange(1, 28):
            w.add_document(id=u("%s-%s") % (month, day),
                           date=datetime(2010, month, day, 14, 0, 0))
    w.commit()

    with ix.searcher() as s:
        qp = qparser.QueryParser("id", schema)

        r = s.search(qp.parse("date:20100523"))
        assert len(r) == 1
        assert r[0]["id"] == "5-23"
        assert r[0]["date"].__class__ is datetime
        assert r[0]["date"].month == 5
        assert r[0]["date"].day == 23

        r = s.search(qp.parse("date:'2010 02'"))
        assert len(r) == 27

        q = qp.parse(u("date:[2010-05 to 2010-08]"))
        startdt = datetime(2010, 5, 1, 0, 0, 0, 0)
        enddt = datetime(2010, 8, 31, 23, 59, 59, 999999)
        assert q.__class__ is query.NumericRange
        assert q.start == times.datetime_to_long(startdt)
        assert q.end == times.datetime_to_long(enddt)
Example #3
0
def test_datetime():
    dtf = fields.DATETIME(stored=True)
    schema = fields.Schema(id=fields.ID(stored=True), date=dtf)
    st = RamStorage()
    ix = st.create_index(schema)

    w = ix.writer()
    for month in xrange(1, 12):
        for day in xrange(1, 28):
            w.add_document(id=u("%s-%s") % (month, day),
                           date=datetime(2010, month, day, 14, 0, 0))
    w.commit()

    with ix.searcher() as s:
        qp = qparser.QueryParser("id", schema)

        r = s.search(qp.parse("date:20100523"))
        assert len(r) == 1
        assert r[0]["id"] == "5-23"
        assert r[0]["date"].__class__ is datetime
        assert r[0]["date"].month == 5
        assert r[0]["date"].day == 23

        r = s.search(qp.parse("date:'2010 02'"))
        assert len(r) == 27

        q = qp.parse(u("date:[2010-05 to 2010-08]"))
        startdt = datetime(2010, 5, 1, 0, 0, 0, 0)
        enddt = datetime(2010, 8, 31, 23, 59, 59, 999999)
        assert q.__class__ is query.NumericRange
        assert q.start == times.datetime_to_long(startdt)
        assert q.end == times.datetime_to_long(enddt)
Example #4
0
 def __init__(
     self,
     fieldname,
     start,
     end,
     startexcl=False,
     endexcl=False,
     boost=1.0,
     constantscore=True,
 ):
     self.startdate = start
     self.enddate = end
     if start:
         start = datetime_to_long(start)
     if end:
         end = datetime_to_long(end)
     super(DateRange, self).__init__(
         fieldname,
         start,
         end,
         startexcl=startexcl,
         endexcl=endexcl,
         boost=boost,
         constantscore=constantscore,
     )
Example #5
0
 def __init__(self, fieldname, start, end, startexcl=False, endexcl=False,
              boost=1.0, constantscore=True):
     self.startdate = start
     self.enddate = end
     if start:
         start = datetime_to_long(start)
     if end:
         end = datetime_to_long(end)
     super(DateRange, self).__init__(fieldname, start, end,
                                     startexcl=startexcl, endexcl=endexcl,
                                     boost=boost,
                                     constantscore=constantscore)
Example #6
0
    def parse_range(self, fieldname, start, end, startexcl, endexcl, boost=1.0):
        from whoosh import query

        if start is None and end is None:
            return query.Every(fieldname, boost=boost)

        if start is not None:
            startdt = self._parse_datestring(start).floor()
            start = datetime_to_long(startdt)

        if end is not None:
            enddt = self._parse_datestring(end).ceil()
            end = datetime_to_long(enddt)

        return query.NumericRange(fieldname, start, end, boost=boost)
Example #7
0
    def parse_query(self, fieldname, qstring, boost=1.0):
        from whoosh import query
        from whoosh.util.times import is_ambiguous

        try:
            at = self._parse_datestring(qstring)
        except:
            e = sys.exc_info()[1]
            return query.error_query(e)

        if is_ambiguous(at):
            startnum = datetime_to_long(at.floor())
            endnum = datetime_to_long(at.ceil())
            return query.NumericRange(fieldname, startnum, endnum)
        else:
            return query.Term(fieldname, at, boost=boost)
Example #8
0
def get_media_data(path, relpath):
    if os.path.exists(path):
        stat = os.stat(path)
        _mtime = datetime.datetime.fromtimestamp(stat.st_mtime)
        _ctime = datetime.datetime.fromtimestamp(stat.st_ctime)
        size = stat.st_size
        mtime = _mtime.strftime('%d %b %Y %H:%M:%S')
        ctime = _ctime.strftime('%d %b %Y %H:%M:%S')
        fname = os.path.basename(path)
        _ctime = datetime_to_long(_ctime)
        _mtime = datetime_to_long(_mtime)
        type = find_type(path)
        return MediaData(path, relpath, fname, size, ctime, _ctime, mtime,
                         _mtime, type)
    else:
        return None
Example #9
0
    def parse_range(self, fieldname, start, end, startexcl, endexcl,
                    boost=1.0):
        from whoosh import query

        if start is None and end is None:
            return query.Every(fieldname, boost=boost)

        if start is not None:
            startdt = self._parse_datestring(start).floor()
            start = datetime_to_long(startdt)

        if end is not None:
            enddt = self._parse_datestring(end).ceil()
            end = datetime_to_long(enddt)

        return query.NumericRange(fieldname, start, end, boost=boost)
Example #10
0
    def parse_query(self, fieldname, qstring, boost=1.0):
        from whoosh import query
        from whoosh.util.times import is_ambiguous

        try:
            at = self._parse_datestring(qstring)
        except:
            e = sys.exc_info()[1]
            return query.error_query(e)

        if is_ambiguous(at):
            startnum = datetime_to_long(at.floor())
            endnum = datetime_to_long(at.ceil())
            return query.NumericRange(fieldname, startnum, endnum)
        else:
            return query.Term(fieldname, at, boost=boost)
Example #11
0
    def prepare_datetime(self, x):
        from whoosh.util.times import floor

        if isinstance(x, text_type):
            # For indexing, support same strings as for query parsing --
            # convert unicode to datetime object
            x = self._parse_datestring(x)
            x = floor(x)  # this makes most sense (unspecified = lowest)

        if isinstance(x, datetime.datetime):
            return datetime_to_long(x)
        elif isinstance(x, bytes_type):
            return x
        else:
            raise Exception("%r is not a datetime" % (x,))
Example #12
0
    def prepare_datetime(self, x):
        from whoosh.util.times import floor

        if isinstance(x, text_type):
            # For indexing, support same strings as for query parsing --
            # convert unicode to datetime object
            x = self._parse_datestring(x)
            x = floor(x)  # this makes most sense (unspecified = lowest)

        if isinstance(x, datetime.datetime):
            return datetime_to_long(x)
        elif isinstance(x, bytes_type):
            return x
        else:
            raise Exception("%r is not a datetime" % (x,))