Ejemplo n.º 1
0
    def _save_as_v1(self, fp):
        """Save copy to specified path.

        This mainly exists for testing and making sure we still read the old
        saved files.
        """
        def _rewrite_dir(state):
            "Rewrite directories in the old format."
            state['files'] = [x[0] for x in state['files']]
            state['directories'] = [_rewrite_dir(d)
                                    for d in state['directories']]
            state.pop('relpath')
            state.pop('name')
            return state

        fp = open_file(fp, 'wb')
        media = [(key, self.get(key).to_dict()) for key in self._relpath2index]
        tags = [(t.name, t.type) for t in self.tags]
        root = _rewrite_dir(self.root.__getstate__())
        processors = [processor.dump(x) for x in self.processors]
        for k, m in media:
            m['_ctime'] = long_to_datetime(m['_ctime'])
            m['_mtime'] = long_to_datetime(m['_mtime'])
        data = dict(
            version=1, path=self.path, name=self.name,
            description=self.description, tags=tags, media=media,
            root=root, processors=processors
        )
        json_tricks.dump(data, fp, compression=True)
        fp.close()
        logger.info('Saved project: %s', self.name)
Ejemplo n.º 2
0
 def from_bytes(self, bs):
     x = NUMERIC.from_bytes(self, bs)
     return long_to_datetime(x)
Ejemplo n.º 3
0
 def from_column_value(self, x):
     return long_to_datetime(x)
Ejemplo n.º 4
0
def query_sql(obj):
    if isinstance(obj, whoosh.query.And):
        return reduce(
            lambda x, y: x & y,
            (query_sql(q)
             for q in obj.subqueries if q != whoosh.query.NullQuery),
        )
    if isinstance(obj, whoosh.query.Or):
        return reduce(
            lambda x, y: x | y,
            (query_sql(q)
             for q in obj.subqueries if q != whoosh.query.NullQuery),
        )
    if isinstance(obj, whoosh.query.Not):
        return ~query_sql(obj.query)
    if isinstance(obj, Exact):
        return exact_sql(obj.fieldname, obj.text)
    if isinstance(obj, whoosh.query.Term):
        if obj.fieldname == "has":
            return has_sql(obj.text)
        if obj.fieldname == "is":
            return is_sql(obj.text)
        return field_extra(obj.fieldname,
                           Q(**{field_name(obj.fieldname): obj.text}))
    if isinstance(obj, whoosh.query.DateRange):
        return field_extra(
            obj.fieldname,
            range_sql(
                obj.fieldname,
                obj.startdate,
                obj.enddate,
                obj.startexcl,
                obj.endexcl,
                timezone.make_aware,
            ),
        )
    if isinstance(obj, whoosh.query.NumericRange):
        if obj.fieldname in {"added", "changed"}:
            return field_extra(
                obj.fieldname,
                range_sql(
                    obj.fieldname,
                    long_to_datetime(obj.start),
                    long_to_datetime(obj.end),
                    obj.startexcl,
                    obj.endexcl,
                    timezone.make_aware,
                ),
            )
        return range_sql(obj.fieldname, obj.start, obj.end, obj.startexcl,
                         obj.endexcl)
    if isinstance(obj, whoosh.query.Regex):
        try:
            re.compile(obj.text)
            return Q(**{field_name(obj.fieldname, "regex"): obj.text})
        except re.error as error:
            raise ValueError(_("Invalid regular expression: {}").format(error))

    if obj == whoosh.query.NullQuery:
        return Q()
    raise ValueError("Unsupported: {!r}".format(obj))
Ejemplo n.º 5
0
 def from_bytes(self, bs):
     x = NUMERIC.from_bytes(self, bs)
     return long_to_datetime(x)
Ejemplo n.º 6
0
 def from_column_value(self, x):
     return long_to_datetime(x)