示例#1
0
 def index_new_dir(self, path):
     if self._check_if_was_scanned(path) is True:
         self._date = datetime.datetime.now()
         date = datetime.datetime.fromtimestamp(time.time()).\
                 strftime("%Y.%m.%d-%H.%M.%S")
         file_str = CATALOGDIR + os.path.basename(path) + ".-." + date \
                     + ".db"
         self.create_connection(file_str, path)
         self._factory = Factory(self)
         self._indexer = Indexer(path, self._tvhandler.pbar, self._tvhandler,
                                 self._factory)
         return True
     else:
         return False
示例#2
0
class DBManager(object):

    def __init__(self, mainhandler, tvhandler):
        self._mainhandler = mainhandler
        self._tvhandler = tvhandler

    #################################
    #Propeties
    #################################
    def get_stop(self):
        if hasattr(self, "indexer") and self._indexer is not None:
            return self._indexer.stop

    def set_stop(self, stop):
        if hasattr(self, "indexer") and self._indexer is not None:
            self._indexer.stop = stop
            os.remove(self._scanningcatalog)

    def get_indexer(self):
        return self._indexer

    def set_indexer(self, indexer):
        if indexer is None:
            self._indexer = None
            gc.collect()

    def get_conn(self):
        return self._conn

    def get_tvhandler(self):
        return self._tvhandler

    def set_tvhandler(self, value):
        self._tvhandler = value

    def get_path(self):
        return self._path

    def get_root(self):
        return self._root

    def get_scanningcatalog(self):
        return self._scanningcatalog

    stop = property(get_stop, set_stop)
    indexer = property(get_indexer, set_indexer)
    conn = property(get_conn)
    tvhandler = property(get_tvhandler, set_tvhandler)
    path = property(get_path)
    root = property(get_root)
    scanningcatalog = property(get_scanningcatalog)

    #################################
    #Methods
    #################################

    def create_connection(self, file_str, path):
        con_str = "sqlite://" + file_str
        self._scanningcatalog = file_str
        self._conn = connectionForURI(con_str)
        self._path = path

    def index_new_dir(self, path):
        if self._check_if_was_scanned(path) is True:
            self._date = datetime.datetime.now()
            date = datetime.datetime.fromtimestamp(time.time()).\
                    strftime("%Y.%m.%d-%H.%M.%S")
            file_str = CATALOGDIR + os.path.basename(path) + ".-." + date \
                        + ".db"
            self.create_connection(file_str, path)
            self._factory = Factory(self)
            self._indexer = Indexer(path, self._tvhandler.pbar, self._tvhandler,
                                    self._factory)
            return True
        else:
            return False

    def _check_if_was_scanned(self, path):
        for entry in os.listdir(CATALOGDIR):
            if entry.startswith(os.path.basename(path)):
                if self._check_if_is_same_dir(entry, path) is True:
                    label = gtk.Label()
                    label.set_markup("Indexor has already registered " + \
                                      "this directory. Do you wish\n" + 
                                      "to overwrite the catalog with the " + \
                                      "updated one?")
                    hbox = gtk.HBox(spacing = 8)
                    dialog = gtk.Dialog("Overwrite previous catalog?",
                                        self._mainhandler.window,
                                        gtk.DIALOG_MODAL,
                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                                         gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
                    icon = gtk.icon_theme_get_default().\
                            load_icon("emblem-important", 64,
                                      gtk.ICON_LOOKUP_NO_SVG)
                    img = gtk.image_new_from_pixbuf(icon)
                    img.show()
                    hbox.pack_start(img, True, True, 0)
                    hbox.pack_start(label, True, True, 0)
                    hbox.show()
                    dialog.vbox.pack_start(hbox, True, True, 0)
                    label.show()
                    response = dialog.run()
                    dialog.destroy()
                    if response == gtk.RESPONSE_ACCEPT:
                        os.remove(CATALOGDIR + entry)
                        return True
                    else:
                        return False
        return True

    def _check_if_is_same_dir(self, entry, path):
        con_str = "sqlite://" + CATALOGDIR + entry
        conn = connectionForURI(con_str)
        metadircount = MetaDir.select(MetaDir.q.target == path,
                                 connection = conn).count()
        if metadircount > 0:
            return MetaDir.select(MetaDir.q.target == path,
                                 connection = conn)[0].target == path
        #return metadir.target == path

    def start_counting(self):
        return self._indexer.start_counting()

    def start_indexing(self):
        return self._indexer.start_indexing()

    def create_metadir(self):
        metadircount = MetaDir.select(connection = self._conn).count()
        if not metadircount == 1:
            root = Directory.select(Directory.q.strabs == self._indexer.path, connection = self._conn).getOne()
            self._factory.new_metadir(self._date, self._indexer.path,
                                      self._indexer.countfiles,
                                      self._indexer.countdirs, root.size,
                                      root.strsize, root.name, self._indexer.timer)
            return 0
        else:
            return 1

    def get_time_consumed(self):
        return self._indexer.timer

    def reload_connection(self):
        self._conn.close()
        self._conn = connectionForURI("sqlite://" + self._scanningcatalog)

    def set_root_node(self):
        rootselect = Directory.select(Directory.q.relpath == "/",
                                      connection = self._conn)
        self._root = rootselect[0]