import logging
import os
import sys
from collections import namedtuple

from .posix import get_drive_list as get_drive_list_posix
from .windows import get_drive_list as get_drive_list_windows

logger = logging.getLogger(__name__)

# The name of the folder we export data and content to, and what we look for in drives when we want to import
EXPORT_FOLDER_NAME = "KOLIBRI_DATA"

DriveData = namedtuple(
    "DriveData",
    [
        "id",
        "name",
        "path",
        "writable",
        "datafolder",
        "freespace",
        "totalspace",
        "filesystem",
        "drivetype",
        "metadata",
    ]
)

def enumerate_mounted_disk_partitions():
    """
    Searches the local device for attached partitions/drives, and computes metadata about each one.
    Returns a dict that maps mount paths to DriveData objects containing metadata about each drive.
    Note that drives for which the current user does not have read permissions are not included.
    """

    if sys.platform == "win32":
        drive_list = get_drive_list_windows()
    else:
        drive_list = get_drive_list_posix()

    drives = {}

    for drive in drive_list:

        path = drive["path"]

        drives[path] = DriveData(
            id=drive["guid"] or path,
            path=path,
            name=drive["name"],
            writable=os.access(path, os.W_OK),
            datafolder=find_kolibri_data_folder(path),
            freespace=drive["freespace"],
            totalspace=drive["totalspace"],
            filesystem=drive["filesystem"],
            drivetype=drive["drivetype"],
            metadata={},
        )

    return drives


def find_kolibri_data_folder(folder):
    """
    Looks for a folder with name matching EXPORT_FOLDER_NAME underneath the provided folder path.
    If it is found, the full path to the data folder is returned. Otherwise, None is returned.
    """

    kolibri_data_dir = os.path.join(
        folder,
        EXPORT_FOLDER_NAME,
    )

    if os.path.exists(kolibri_data_dir):
        return kolibri_data_dir
    else:
        return None

@@ -1,12 +1,27 @@
+import hashlib%0A
 import loggi
@@ -818,18 +818,16 @@
 aps 
-mount path
+drive ID
 s to
@@ -1198,17 +1198,16 @@
 %22path%22%5D%0A
-%0A
         
@@ -1215,14 +1215,91 @@
 rive
-s%5Bpath
+_id = hashlib.sha1(drive%5B%22guid%22%5D or path).hexdigest()%5B:32%5D%0A%0A        drives%5Bdrive_id
 %5D = 
@@ -1329,32 +1329,19 @@
 id=drive
-%5B%22guid%22%5D or path
+_id
 ,%0A      
@@ -1459,20 +1459,19 @@
 afolder=
-find
+get
 _kolibri
@@ -1482,16 +1482,21 @@
 a_folder
+_path
 (path),%0A
@@ -1731,12 +1731,11 @@
 def 
-find
+get
 _kol
@@ -1750,16 +1750,21 @@
 a_folder
+_path
 (folder)
@@ -1781,18 +1781,32 @@
     
-Looks for 
+Constructs an export dat
 a fo
@@ -1814,374 +1814,171 @@
 der 
-with name matching EXPORT_FOLDER_NAME underneath the provided folder path.%0A    If it is found, the full path to the data folder is returned. Otherwise, None is returned.%0A    %22%22%22%0A%0A    kolibri_data_dir = os.path.join(%0A        folder,%0A        EXPORT_FOLDER_NAME,%0A    )%0A%0A    if os.path.exists(kolibri_data_dir):%0A        return kolibri_data_dir%0A    else:%0A        return None
+path by concatenating the parent folder%0A    to the EXPORT_FOLDER_NAME folder name.%0A    %22%22%22%0A%0A    return os.path.join(%0A        folder,%0A        EXPORT_FOLDER_NAME,%0A    )
 %0A

Use SHAs as drive IDs, and always return drive data path.