def change_expression(id, new_expression): connection = database.get_current_gallery("connection") c = connection.cursor() # get folder c.execute( ( "SELECT location, name, use_softlink " "FROM folder " "WHERE pk_id = ?" ), (id,) ) output = c.fetchone() if output is None: raise ValueError("Invalid folder id", id) # parse expression where = expression.parse(new_expression) # remake folder folder = os.path.join(output[0], output[1]) if os.path.isdir(folder): shutil.rmtree(folder) os.makedirs(folder) # relink files c.execute( ( "SELECT uuid, file_name " "FROM file " "WHERE {}" ).format(where) ) files = c.fetchall() if files is not None: gallery = database.get_current_gallery("directory") for file in files: source = os.path.join(gallery, "files", file[0]) target = os.path.join(folder, file[1]) create_folders.symlink(source, target, output[2]) # update db c.execute( ( "UPDATE folder " "SET expression = ? " "WHERE pk_id = ?" ), (new_expression, id), ) connection.commit()
def _check_gallery(id): """checks the integrity of a gallery""" connection = database.get_gallery(id, "connection") c = connection.cursor() gallery = os.path.join(database.get_gallery(id, "directory"), "files") result = { "untracked": [], "missing": [], "occupied": [], } # check database files_fs = set(os.listdir(gallery)) files_db = {x[0] for x in c.execute("SELECT uuid FROM file")} for untracked_file in (files_fs - files_db): if untracked_file == "temp_links": continue result["untracked"].append(os.path.join( gallery, untracked_file, )) for missing_file in (files_db - files_fs): c.execute("SELECT file_name FROM file WHERE uuid=:uuid", { "uuid": missing_file, }) result["missing"].append(( os.path.join(gallery, missing_file), c.fetchone()[0], )) # check advanced output folders c.execute("SELECT location, name, use_softlink, expression, pk_id " "FROM folder") for folder in c.fetchall(): path = os.path.join(folder[0], folder[1]) # ensure folder exists if not os.path.exists(path): os.makedirs(path) elif not os.path.isdir(path): result['occupied'].append((True, folder[4])) continue # create missing links files_fs = set(os.listdir(path)) c.execute(("SELECT uuid, file_name " "FROM file " "WHERE {}").format(folder[3])) files = [f for f in c.fetchall() if f not in files_fs] for file in files: create_folders.symlink( os.path.join(gallery, file[0]), os.path.join(path, file[1]), folder[2], ) # check gallery output folders c.execute("SELECT location, name, use_softlink, pk_id " "FROM gallery_folder") for folder in c.fetchall(): c.execute( ("SELECT t.name, t.pk_id " "FROM gallery_folder g " "JOIN gallery_folder_has_tag gt " "ON g.pk_id = gt.pk_fk_gallery_folder_id " "JOIN tag t ON gt.pk_fk_tag_id = t.pk_id " "WHERE g.pk_id = ?"), (folder[3], ), ) for tag in c.fetchall(): path = os.path.join(folder[0], folder[1], tag[0]) # ensure folder exists if not os.path.exists(path): os.makedirs(path) elif not os.path.isdir(path): result['occupied'].append((False, folder[3])) continue # create missing links files_fs = set(os.listdir(path)) c.execute( ("SELECT uuid, file_name " "FROM file f " "JOIN file_has_tag ft ON t.pk_id = ft.pk_fk_file_id " "JOIN tag t ON ft.pk_fk_tag_id = t.pk_id " "WHERE t.pk_id = ?"), (tag[1], ), ) files = [f for f in c.fetchall() if f not in files_fs] for file in files: create_folders.symlink( os.path.join(gallery, file[0]), os.path.join(path, file[1]), folder[2], ) return result
def change(item, tag, create): connection = database.get_current_gallery("connection") c = connection.cursor() c.execute( ( "SELECT uuid, file_name " "FROM file " "WHERE pk_id=:id" ), { "id": item } ) # TODO better var name g_file = c.fetchone() if g_file is None: raise ValueError("No file with this id", item) target = os.path.join( database.get_current_gallery("directory"), "files", g_file[0] ) # gallery output folders c.execute( "SELECT pk_id, location, name, use_softlink " "FROM gallery_folder" ) folders = c.fetchall() for folder in folders: c.execute( ( "SELECT name " "FROM gallery_folder_has_tag " "JOIN tag ON pk_fk_tag_id=pk_id " "WHERE pk_fk_gallery_folder_id=:folder AND pk_id=:tag" ), { "folder": folder[0], "tag": tag } ) tag_name = c.fetchone() if tag_name is None: # gallery output does not use this tag continue else: tag_name = tag_name[0] path = os.path.join(folder[1], folder[2], tag_name.replace("_", " ")) if not os.path.isdir(path): os.makedirs(path) link_path = os.path.join(path, g_file[1]) if create: # create if os.path.exists(link_path): os.remove(link_path) create_folders.symlink(target, link_path, folder[3]) else: # delete if os.path.lexists(link_path): os.remove(link_path) # advanced output folders c.execute( "SELECT location, name, expression, use_softlink " "FROM folder" ) folders = c.fetchall() for folder in folders: path = os.path.join(folder[0], folder[1]) link = os.path.join(path, g_file[1]) query_file = ( "SELECT pk_id " "FROM file " "WHERE %s AND pk_id = %d" % (expression.parse(folder[2]), item) ) c.execute(query_file) matches = c.fetchone() if matches is None and os.path.exists(link): os.remove(link) elif matches is not None: if os.path.exists(link): os.remove(link) if not os.path.isdir(path): os.makedirs(path) create_folders.symlink(target, link, folder[3])
def change_link_type(id, advanced, type): """Changes the link type of an output folder. Softlinks if type is True and hardlinks if type is False, like the use_softlink field""" connection = database.get_current_gallery("connection") c = connection.cursor() # get output folder information if advanced: query = ( "SELECT location, name, use_softlink, expression " "FROM folder WHERE " "pk_id = ?" ) else: query = ( "SELECT location, name, use_softlink " "FROM gallery_folder " "WHERE pk_id = ?" ) c.execute(query, (id,)) output = c.fetchone() if output is None: raise ValueError("Invalid folder id", id) if output[2] == type: # nothing to do return gallery = database.get_current_gallery("directory") if advanced: # remake folders folder = os.path.join(output[0], output[1]) if os.path.isdir(folder): shutil.rmtree(folder) os.makedirs(folder) # relink files expression = output[3] c.execute( ( "SELECT uuid, file_name " "FROM file " "WHERE ?" ), (expression,), ) files = c.fetchall() if files is None: # no files, nothing to do return for file in files: source = os.path.join(gallery, "files", file[0]) target = os.path.join(folder, file[1]) create_folders.symlink(source, target, type) # update db c.execute( ( "UPDATE folder " "SET use_softlink = ? " "WHERE pk_id = ?" ), (type, id), ) connection.commit() else: # get tags c.execute( ( "SELECT pk_id, name " "FROM gallery_folder_has_tag " "JOIN tag ON pk_fk_tag_id = pk_id " "WHERE pk_fk_gallery_folder_id = ?" ), (id,), ) tags = c.fetchall() if tags is None: # no folders, nothing to do return for tag in tags: # remake folder folder = os.path.normpath(os.path.join( output[0], output[1], tag[1].replace("_", " "), )) if os.path.isdir(folder): shutil.rmtree(folder) # Necessary because Windows removes folders with delay def createfolder(path): try: os.makedirs(folder) except: createfolder(path) createfolder(folder) # relink files c.execute( ( "SELECT uuid, file_name " "FROM file f " "JOIN file_has_tag ft ON f.pk_id = ft.pk_fk_file_id " "JOIN tag t ON ft.pk_fk_tag_id = t.pk_id " "WHERE t.pk_id = ?" ), (tag[0],), ) files = c.fetchall() if files is None: # no files, nothing to do continue for file in files: source = os.path.join(gallery, "files", file[0]) target = os.path.join(folder, file[1]) create_folders.symlink(source, target, type) # update dn c.execute( ( "UPDATE gallery_folder " "SET use_softlink = ? " "WHERE pk_id = ?" ), (type, id), ) connection.commit()
def change_gallery(id, tag, add): connection = database.get_current_gallery("connection") c = connection.cursor() # get folder information c.execute( ( "SELECT location, name, use_softlink " "FROM gallery_folder WHERE pk_id=?" ), (id,), ) folder = c.fetchone() if folder is None: raise ValueError("No gallery folder with this id", id) # check tag c.execute( ( "SELECT name FROM gallery_folder_has_tag " "JOIN tag ON pk_fk_tag_id = pk_id " "WHERE pk_fk_gallery_folder_id=? AND pk_id=?" ), (id, tag), ) tag_name = c.fetchone() if tag_name is not None and add: raise ValueError("Tag already added") elif tag_name is None and not add: raise ValueError("Tag already removed") if add: # Get tag name c.execute("SELECT name FROM tag WHERE pk_id = ?", (tag,)) result = c.fetchone() tag_name = result[0] # get files c.execute( ( "SELECT uuid, file_name " "FROM file " "JOIN file_has_tag ON pk_id = pk_fk_file_id " "WHERE pk_fk_tag_id=?" ), (tag,), ) files = c.fetchall() gallery = database.get_current_gallery("directory") # link files os.makedirs( os.path.join(folder[0], folder[1], tag_name.replace("_", " ")) ) if files is not None: for file in files: source = os.path.join(gallery, "files", file[0]) target = os.path.join( folder[0], folder[1], tag_name.replace("_", " "), file[1], ) create_folders.symlink(source, target, folder[2]) # update db c.execute( ( "INSERT INTO gallery_folder_has_tag " "VALUES (?, ?)" ), (id, tag), ) connection.commit() else: # delete files # Get tag name c.execute("SELECT name FROM tag WHERE pk_id = ?", (tag,)) result = c.fetchone() tag_name = result[0] # delete files path = os.path.join(folder[0], folder[1], tag_name.replace("_", " ")) if os.path.isdir(path): shutil.rmtree(path) # update db c.execute( ( "DELETE FROM gallery_folder_has_tag " "WHERE pk_fk_gallery_folder_id=? AND " "pk_fk_tag_id=?" ), (id, tag), ) connection.commit()