<commit_before>"""Handles imports from shotwell databases."""

import os

from collections import defaultdict
from logging import getLogger

from myarchive.db.tag_db.tables import TrackedFile, Tag
from myarchive.db.shotwell.shotwell_db import ShotwellDB
from myarchive.db.shotwell.tables import PhotoTable, VideoTable, TagTable


LOGGER = getLogger("myarchive")


def import_from_shotwell_db(
        tag_db, media_path, sw_database_path=None,
        sw_storage_folder_override=None):
    """Imports images from the shotwell DB into ours."""
    sw_db = ShotwellDB(
        drivername='sqlite',
        # db_name=sw_database_path,
    )

    # if sw_storage_folder_override:
    #     photo_paths = []
    #     for photo_path, in sw_db.session.query(PhotoTable.filename):
    #         photo_paths.append(photo_path)
    #     original_storage_path = os.path.commonprefix(photo_paths)
    #     del photo_paths

    # Grab all the photos and add them.
    files_by_id = dict()
    for table in (PhotoTable, VideoTable):
        for photo_row in sw_db.session.query(table):
            media_filepath = str(photo_row.filename)
            LOGGER.critical(media_filepath)
            # if sw_storage_folder_override:
            #     filepath = original_storage_path.replace(
            #         original_storage_path, sw_storage_folder_override)
            tracked_file = TrackedFile.add_file(
                db_session=tag_db.session, media_path=media_path,
                copy_from_filepath=media_filepath)
            files_by_id[int(photo_row.id)] = tracked_file
            tracked_file.tags.append(Tag(name="shotwell"))
            tag_db.session.add(tracked_file)
    tag_db.session.commit()

    # Grab all the tags and apply them to the photos.
    tags_by_id = defaultdict(list)
    tags_by_tag_name = dict()
    for tag_tuple in sw_db.session.query(
            TagTable.name, TagTable.photo_id_list).all():
        tag_name = tag_tuple[0]
        tag_ids = list()
        photo_id_str = tag_tuple[1]
        if not photo_id_str:
            continue
        for photo_id in photo_id_str.split(","):
            if not photo_id:
                continue
            tag_ids.append(int(photo_id[5:], 16))
        for tag_id in tag_ids:
            tags_by_id[tag_id].append(tag_name)
        tags_by_tag_name[tag_name] = \<commit_msg>More logging and a shotwell tag<commit_after>"""Handles imports from shotwell databases."""

import os

from collections import defaultdict
from logging import getLogger

from myarchive.db.tag_db.tables import TrackedFile, Tag
from myarchive.db.shotwell.shotwell_db import ShotwellDB
from myarchive.db.shotwell.tables import PhotoTable, VideoTable, TagTable


LOGGER = getLogger("myarchive")


def import_from_shotwell_db(
        tag_db, media_path, sw_database_path=None,
        sw_storage_folder_override=None):
    """Imports images from the shotwell DB into ours."""
    sw_db = ShotwellDB(
        drivername='sqlite',
        # db_name=sw_database_path,
    )

    # if sw_storage_folder_override:
    #     photo_paths = []
    #     for photo_path, in sw_db.session.query(PhotoTable.filename):
    #         photo_paths.append(photo_path)
    #     original_storage_path = os.path.commonprefix(photo_paths)
    #     del photo_paths

    shotwell_tag = Tag.get_tag(db_session=tag_db.session, tag_name="shotwell")
    tag_db.session.commit()

    LOGGER.info("Importing images... [Part 1 of 2]")
    # Grab all the photos and add them.
    files_by_id = dict()
    for table in (PhotoTable, VideoTable):
        for photo_row in sw_db.session.query(table):
            media_filepath = str(photo_row.filename)
            # if sw_storage_folder_override:
            #     filepath = original_storage_path.replace(
            #         original_storage_path, sw_storage_folder_override)
            tracked_file = TrackedFile.add_file(
                db_session=tag_db.session, media_path=media_path,
                copy_from_filepath=media_filepath)
            files_by_id[int(photo_row.id)] = tracked_file
            tracked_file.tags.append(shotwell_tag)
            tag_db.session.add(tracked_file)
    tag_db.session.commit()

    LOGGER.info("Attaching tags... [Part 2 of 2]")
    # Grab all the tags and apply them to the photos.
    tags_by_id = defaultdict(list)
    tags_by_tag_name = dict()
    for tag_tuple in sw_db.session.query(
            TagTable.name, TagTable.photo_id_list).all():
        tag_name = tag_tuple[0]
        tag_ids = list()
        photo_id_str = tag_tuple[1]
        if not photo_id_str:
            continue
        for photo_id in photo_id_str.split(","):
            if not photo_id:
                continue
            tag_ids.append(int(photo_id[5:], 16))
        for tag_id in tag_ids:
            tags_by_id[tag_id].append(tag_name)
        tags_by_tag_name[tag_name] = \
{'fraction_non_alphanumeric': 0.08565573770491804, 'fraction_numerical': 0.0028688524590163933, 'mean_word_length': 2.6921331316187596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4424887', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1520, 'n_words': 310}