Ejemplo n.º 1
0
    def InitData(self):

        # Get and set values
        # Location, name, automatic, symlink
        gallery = database.get_current_gallery("connection")
        c = gallery.cursor()
        query_location = ("SELECT location, name, add_new_tag, use_softlink "
                          "FROM gallery_folder "
                          "WHERE pk_id = %d" % self.folder_id)
        c.execute(query_location)
        result = c.fetchone()
        self.name = result[1]
        self.location = result[0]

        self.tc_directory.SetValue(result[0])
        self.tc_name.SetValue(result[1])
        self.cb_automatic.SetValue(result[2] == 1)

        softlink = (result[3] == 1)
        self.rb_softlinks.SetValue(softlink)
        self.rb_hardlinks.SetValue(not softlink)

        # Tags
        query_tags = ("SELECT pk_fk_tag_id FROM gallery_folder_has_tag "
                      "WHERE pk_fk_gallery_folder_id = %d" % self.folder_id)
        c.execute(query_tags)
        tag_ids = c.fetchall()
        tag_names = []
        for tag_id in tag_ids:
            tag_names.append(tagging.tag_id_to_name(tag_id).replace("_", " "))

        self.lb.SetCheckedStrings(tag_names)
        self.checked_tags = tag_names
    def InitData(self):

        # Get and set values
        # Location, name, automatic, symlink
        gallery = database.get_current_gallery("connection")
        c = gallery.cursor()
        query_location = (
            "SELECT location, name, add_new_tag, use_softlink "
            "FROM gallery_folder "
            "WHERE pk_id = %d" % self.folder_id
        )
        c.execute(query_location)
        result = c.fetchone()
        self.name = result[1]
        self.location = result[0]

        self.tc_directory.SetValue(result[0])
        self.tc_name.SetValue(result[1])
        self.cb_automatic.SetValue(result[2] == 1)

        softlink = (result[3] == 1)
        self.rb_softlinks.SetValue(softlink)
        self.rb_hardlinks.SetValue(not softlink)

        # Tags
        query_tags = (
            "SELECT pk_fk_tag_id FROM gallery_folder_has_tag "
            "WHERE pk_fk_gallery_folder_id = %d"
            % self.folder_id
        )
        c.execute(query_tags)
        tag_ids = c.fetchall()
        tag_names = []
        for tag_id in tag_ids:
            tag_names.append(tagging.tag_id_to_name(tag_id).replace("_", " "))

        self.lb.SetCheckedStrings(tag_names)
        self.checked_tags = tag_names
Ejemplo n.º 3
0
def get_suggestions(selected_files):

    tag_ids = tagging.get_all_tag_ids()
    print("suggestion start")
    recomm_ids = []
    recomms = []
    file_ids = []
    tag_quantities = []
    i = 0
    query_items = "SELECT pk_id FROM file"
    final_ids = []

    cursor = database.get_current_gallery("connection").cursor()
    cursor.execute(query_items)
    result = cursor.fetchall()

    for item in result:
        file_ids.append(item[0])

    for tag_id in tag_ids:
        tag_quantities.append([0, tag_id])

    for tempFile in file_ids:
        counter = 0
        for tag_id in tag_ids:
            if tagging.file_has_tag_id(tempFile, tag_id):
                tag_quantities[counter][0] += 1
            counter += 1

    if len(tag_ids) > 7:
        while i < 7:
            if not tag_quantities:
                break

            length = len(selected_files)
            tagcount = 0
            for selected_file in selected_files:
                if tagging.file_has_tag_id(selected_file,
                                           max(tag_quantities)[1]):
                    tagcount += 1

            if tagcount is length:
                tag_quantities.remove(max(tag_quantities))
                i += 1

            else:
                recomm_ids.append(max(tag_quantities)[1])
                tag_quantities.remove(max(tag_quantities))
                i += 1
        i = 0
    else:
        while i < len(tag_ids):
            if not tag_quantities:
                break

            length = len(selected_files)
            tagcount = 0
            for selected_file in selected_files:
                if tagging.file_has_tag_id(selected_file,
                                           max(tag_quantities)[1]):
                    tagcount += 1

            if tagcount is length:
                tag_quantities.remove(max(tag_quantities))
                i += 1

            else:
                recomm_ids.append(max(tag_quantities)[1])
                tag_quantities.remove(max(tag_quantities))
                i += 1
        i = 0

    tag_corr = []
    corr_recomm_ids = []

    for tag_id in tag_ids:
        tag_corr.append([0, tag_id])

    for tempFile in selected_files:
        counter = 0
        for tag_id in tag_ids:
            if tagging.file_has_tag_id(tempFile, tag_id):
                tag_corr[counter][0] += 1
            counter += 1

    if len(tag_ids) > 7:
        while i < 7:
            if not tag_corr:
                break

            if max(tag_corr)[0] < len(selected_files):
                corr_recomm_ids.append(max(tag_corr)[1])
                tag_corr.remove(max(tag_corr))
                i += 1

            else:
                tag_corr.remove(max(tag_corr))
                i += 1

        i = 0
    else:
        while i < len(tag_ids):
            if not tag_corr:
                break

            if max(tag_corr)[0] < len(selected_files):
                corr_recomm_ids.append(max(tag_corr)[1])
                tag_corr.remove(max(tag_corr))
                i += 1

            else:
                tag_corr.remove(max(tag_corr))
                i += 1

        i = 0

    if not selected_files:
        corr_recomm_ids.append(1)
        recomm_ids.append(1)

    corr = True
    recomm = True

    while True:
        if not recomm_ids and not corr_recomm_ids:
            break

        if len(selected_files) > 1:
            if corr_recomm_ids[i] not in final_ids and corr is True:
                final_ids.append(corr_recomm_ids[i])
                if corr_recomm_ids[i] is corr_recomm_ids[len(corr_recomm_ids) -
                                                         1]:
                    corr = False

        if recomm_ids[i] not in final_ids and recomm is True:
            final_ids.append(recomm_ids[i])
            if corr_recomm_ids[i] is corr_recomm_ids[len(corr_recomm_ids) - 1]:
                recomm = False

        if recomm_ids[i] is recomm_ids[
                len(recomm_ids) - 1] and corr_recomm_ids[i] is corr_recomm_ids[
                    len(corr_recomm_ids) - 1]:
            break

        i += 1
    i = 0

    for tag in final_ids:
        recomms.append(tagging.tag_id_to_name(tag))

    return recomms
Ejemplo n.º 4
0
def get_suggestions(selected_files):

    tag_ids = tagging.get_all_tag_ids()
    print("suggestion start")
    recomm_ids = []
    recomms = []
    file_ids = []
    tag_quantities = []
    i = 0
    query_items = "SELECT pk_id FROM file"
    final_ids = []

    cursor = database.get_current_gallery("connection").cursor()
    cursor.execute(query_items)
    result = cursor.fetchall()

    for item in result:
        file_ids.append(item[0])

    for tag_id in tag_ids:
        tag_quantities.append([0, tag_id])

    for tempFile in file_ids:
        counter = 0
        for tag_id in tag_ids:
            if tagging.file_has_tag_id(tempFile, tag_id):
                tag_quantities[counter][0] += 1
            counter += 1

    if len(tag_ids) > 7:
        while i < 7:
            if not tag_quantities:
                break

            length = len(selected_files)
            tagcount = 0
            for selected_file in selected_files:
                if tagging.file_has_tag_id(selected_file, max(tag_quantities)[1]):
                    tagcount += 1

            if tagcount is length:
                tag_quantities.remove(max(tag_quantities))
                i += 1

            else:
                recomm_ids.append(max(tag_quantities)[1])
                tag_quantities.remove(max(tag_quantities))
                i += 1
        i = 0
    else:
        while i < len(tag_ids):
            if not tag_quantities:
                break

            length = len(selected_files)
            tagcount = 0
            for selected_file in selected_files:
                if tagging.file_has_tag_id(selected_file, max(tag_quantities)[1]):
                    tagcount += 1

            if tagcount is length:
                tag_quantities.remove(max(tag_quantities))
                i += 1

            else:
                recomm_ids.append(max(tag_quantities)[1])
                tag_quantities.remove(max(tag_quantities))
                i += 1
        i = 0

    tag_corr = []
    corr_recomm_ids = []

    for tag_id in tag_ids:
        tag_corr.append([0, tag_id])

    for tempFile in selected_files:
        counter = 0
        for tag_id in tag_ids:
            if tagging.file_has_tag_id(tempFile, tag_id):
                tag_corr[counter][0] += 1
            counter += 1

    if len(tag_ids) > 7:
        while i < 7:
            if not tag_corr:
                break

            if max(tag_corr)[0] < len(selected_files):
                corr_recomm_ids.append(max(tag_corr)[1])
                tag_corr.remove(max(tag_corr))
                i += 1

            else:
                tag_corr.remove(max(tag_corr))
                i += 1

        i = 0
    else:
        while i < len(tag_ids):
            if not tag_corr:
                break

            if max(tag_corr)[0] < len(selected_files):
                corr_recomm_ids.append(max(tag_corr)[1])
                tag_corr.remove(max(tag_corr))
                i += 1

            else:
                tag_corr.remove(max(tag_corr))
                i += 1

        i = 0

    if not selected_files:
        corr_recomm_ids.append(1)
        recomm_ids.append(1)

    corr = True
    recomm = True

    while True:
        if not recomm_ids and not corr_recomm_ids:
            break

        if len(selected_files) > 1:
            if corr_recomm_ids[i] not in final_ids and corr is True:
                final_ids.append(corr_recomm_ids[i])
                if corr_recomm_ids[i] is corr_recomm_ids[len(corr_recomm_ids)-1]:
                    corr = False

        if recomm_ids[i] not in final_ids and recomm is True:
            final_ids.append(recomm_ids[i])
            if corr_recomm_ids[i] is corr_recomm_ids[len(corr_recomm_ids)-1]:
                recomm = False

        if recomm_ids[i] is recomm_ids[len(recomm_ids)-1] and corr_recomm_ids[i] is corr_recomm_ids[len(corr_recomm_ids)-1]:
            break

        i += 1
    i = 0

    for tag in final_ids:
        recomms.append(tagging.tag_id_to_name(tag))

    return recomms
Ejemplo n.º 5
0
def import_files(files):

    # Get gallery connection
    gallery_conn = database.get_current_gallery("connection")
    cursor = gallery_conn.cursor()
    dest_dir = os.path.join(database.get_current_gallery("directory"), "files")

    # Retrieve import setting
    sys_cursor = database.get_sys_db().cursor()
    sys_cursor.execute(
        "SELECT import_copy FROM settings"
    )
    import_copy = (sys_cursor.fetchone()[0] == 1)

    # Keep track of files with the same name
    same_name_files = []

    # Progress Window
    current_file = 1
    message = "Importing file " + str(current_file) + " of " + str(len(files))
    dlg_progress = wx.ProgressDialog(
        "Importing",
        message,
        maximum=len(files)
    )

    if type(files) is list:

        for file in files:
            file = os.path.normpath(file)

            # Update progress info
            dlg_progress.Update(
                current_file,
                "Importing file " + str(current_file) + " of " + str(len(files)) + "."
            )
            current_file += 1

            # Defensive programming
            if os.path.isfile(file) and os.path.isdir(dest_dir):

                original_name = os.path.basename(file)  # for the database
                new_name = (
                    str(uuid.uuid4()) + "." + original_name.split(".")[-1]
                )

                while os.path.exists(os.path.join(dest_dir, new_name)):
                    new_name = (
                        str(uuid.uuid4()) + "." + original_name.split(".")[-1]
                    )
                dest = os.path.join(dest_dir, new_name)
                if import_copy:
                    shutil.copy(file, dest)
                else:
                    shutil.move(file, dest)

                # Check if name already exists
                cursor.execute(
                    "SELECT file_name FROM file WHERE file_name = ?",
                    (original_name,)
                )
                name = cursor.fetchone()
                if name:
                    same_name_files.append(name[0])

                # Save to database
                cursor.execute(
                    ("INSERT INTO file (file_name, uuid) "
                     "VALUES (\'%s\', \'%s\')") %
                    (original_name, new_name)
                )
                gallery_conn.commit()

            else:
                wx.MessageBox(
                    'An error has occured while importing.',
                    'Error',
                    wx.OK | wx.ICON_EXCLAMATION
                )

    elif type(files) is dict:
        for file, tags in files.iteritems():
            print file
            # Update progress info
            dlg_progress.Update(
                current_file,
                "Importing file " + str(current_file) + 
                " of " + str(len(files)) + "."
            )
            current_file += 1

            file = os.path.normpath(file)

            # Defensive programming
            if not os.path.isfile(file):
                continue

            if not os.path.isdir(dest_dir):
                print "Not a dir:", file, dest_dir

            if os.path.isfile(file) and os.path.isdir(dest_dir):
                original_name = os.path.basename(file)
                new_name = (
                    str(uuid.uuid4()) + "." + original_name.split(".")[-1]
                )

                while os.path.exists(os.path.join(dest_dir, new_name)):
                    new_name = (
                        str(uuid.uuid4()) + "." + original_name.split(".")[-1]
                    )

                dest = os.path.join(dest_dir, new_name)
                if import_copy:
                    shutil.copy(file, dest)
                else:
                    shutil.move(file, dest)

                # Check if name already exists
                cursor.execute(
                    "SELECT file_name FROM file WHERE file_name = ?",
                    (original_name,)
                )
                name = cursor.fetchone()
                if name:
                    same_name_files.append(name[0])

                # Save to database
                query_insert_file = (
                    ("INSERT INTO file (file_name, uuid) "
                     "VALUES (\'%s\', \'%s\')")
                    % (original_name, new_name)
                )

                cursor.execute(query_insert_file)
                gallery_conn.commit()

                # Get ID
                query_file_id = (
                    "SELECT pk_id FROM file WHERE uuid = \'%s\'"
                    % (new_name)
                )
                cursor.execute(query_file_id)
                file_id = cursor.fetchone()[0]

                # Connect with tags
                tag_names = []
                for tag in tags:
                    tag_names.append(tagging.tag_id_to_name(tag))

                for tag_name in tag_names:
                    tagging.tag_file(file_id, tag_name)

            else:
                wx.MessageBox(
                    'An error has occured while importing.',
                    'Error',
                    wx.OK | wx.ICON_EXCLAMATION
                )

    dlg_progress.Destroy()

    # Warn user about same name files
    if len(same_name_files) == 0:
        return

    else:
        wx.MessageBox(
            ("Some of your imported files share the "
             "same name with each other, or with files "
             "that are already imported. \n\n"
             "This can lead to some unexpected behaviour "
             "or inconsistencies. "
             "It is recommended to give each file a unique name. "
             "You can rename a file in OctoTagger "
             "by selecting it and pressing 'F2'.\n\n"
             "Below is the list of file names the occur multiple times:\n\n" +
             "\n".join(same_name_files)),
            "Warning"
        )