<commit_before>"""
Upload images to the cloud in a hierarchical time structure.
"""
# coding: utf-8

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os.path
import sys, time, mimetypes
import socket
import cv2
from collections import deque
import threading
import time

def save(img, img_time, uploadqueue=None):
    """Save images to disc or a Google Drive account.

        Save an image in a hierarchical structure inside the detected/
        folder -> year/month/day/image Additionally saves the image
        using a similar structure inside a Google Drive account, if set.

        Args:
            img: a cv2 image.
            img_time: the time of capture.
            google: a Drive object.

        Returns:

        Raises:

    """


    if os.path.exists("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day)))):
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:19] + ".png")), img);
    elif os.path.exists("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B')))):
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:19] + ".png")), img);
    elif os.path.exists("/".join(("detected", str(img_time.year)))):
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:19] + ".png")), img);
    elif os.path.exists("detected"):
        os.mkdir("/".join(("detected", str(img_time.year))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:19] + ".png")), img);
    else:
        os.mkdir("detected")
        os.mkdir("/".join(("detected", str(img_time.year))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:19] + ".png")), img);

    if uploadqueue:
        uploadqueue.append(img_time);

class UploadQueue(object):
    """Implements a queue of images for upload.
    """

    def __init__(self, drive):
        """UploadQueue constructor.

            Args:
                drive: a Drive object.

            Returns:

            Raises:

        """
        self.uploadqueue = deque();
        self.drive = drive;
        self.running = True;

    def append(self, img_time):
        """Append a new image for upload.

            Args:
                img_time: a datetime object representing the time the frame was taken.

            Returns:

            Raises:

        """
        if len(self.uploadqueue) == 0:
            print(str(img_time)[:19] , "[ADDED TO UPLOAD LIST]")
            self.uploadqueue.append(img_time)
        elif (not str(img_time)[:19] == str(self.uploadqueue[-1])[:19]):
            print(str(img_time)[:19] , "[ADDED TO UPLOAD LIST]")
            self.uploadqueue.append(img_time)


    def start(self):
        threading.Thread(target=self.uploadloop, args=()).start()

    def uploadloop(self):
        """UploadQueue main method. Continuously uploads the first element of the queue.

            Args:

            Returns:

            Raises:

        """
        print("Starting upload loop")
        while self.running and self.drive:
            if self.drive and len(self.uploadqueue) > 0:
                img_time = self.uploadqueue[0];
                self.uploadqueue.popleft();
                upload_path = None;
                while not upload_path:
                    upload_path = self.drive.get_link(img_time);
                    time.sleep(1);
                if self.drive:
                    disk_path = "/".join(("detected", str(img_time.year), str(img_time.month) + ". "
                                         + img_time.strftime('%B'), str(img_time.day), str(img_time)[:19] + ".png"))
                    print(str(img_time)[:19], "[UPLOAD STARTED]")
                    self.drive.save_img(disk_path, upload_path);
                    print(str(img_time)[:19], "[UPLOAD FINISHED]")
            time.sleep(1)

    def quit(self):
        self.drive = None;
        self.running = False;


class Drive(object):
    """Upload images to a Google Drive account in a time structure.

        Drive uses google login data to manage a Google Docs
        account. This same API can be used to manage the new Google
        Drive. This class saves images according to date and time of
        capture in a hierarchical time structure year/month/day/image.

        >>> import save;
        >>> drive = save.Drive();
        >>> current_time = datetime.datetime.now();
        >>> folder_link = drive.get_link(current_time);
        >>> drive.save_img("path/to/image.jpg", folder_link);

        Attributes:
            No public attributes.

    """<commit_msg>Removing ":" from image timestamps<commit_after>"""
Upload images to the cloud in a hierarchical time structure.
"""
# coding: utf-8

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os.path
import sys, time, mimetypes
import socket
import cv2
from collections import deque
import threading
import time

def save(img, img_time, uploadqueue=None):
    """Save images to disc or a Google Drive account.

        Save an image in a hierarchical structure inside the detected/
        folder -> year/month/day/image Additionally saves the image
        using a similar structure inside a Google Drive account, if set.

        Args:
            img: a cv2 image.
            img_time: the time of capture.
            google: a Drive object.

        Returns:

        Raises:

    """


    if os.path.exists("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day)))):
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] \
                                                                                                                                                                                            + "m" + str(img_time)[17:19] + "s" + ".png")), img);
    elif os.path.exists("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B')))):
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] \
                                                                                                                                                                                            + "m" + str(img_time)[17:19] + "s" + ".png")), img);
    elif os.path.exists("/".join(("detected", str(img_time.year)))):
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] \
                                                                                                                                                                                            + "m" + str(img_time)[17:19] + "s" + ".png")), img);
    elif os.path.exists("detected"):
        os.mkdir("/".join(("detected", str(img_time.year))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] \
                                                                                                                                                                                            + "m" + str(img_time)[17:19] + "s" + ".png")), img);
    else:
        os.mkdir("detected")
        os.mkdir("/".join(("detected", str(img_time.year))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'))))
        os.mkdir("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day))))
        cv2.imwrite("/".join(("detected", str(img_time.year), str(img_time.month) + ". " + img_time.strftime('%B'), str(img_time.day), str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] \
                                                                                                                                                                                            + "m" + str(img_time)[17:19] + "s" + ".png")), img);

    if uploadqueue:
        uploadqueue.append(img_time);

class UploadQueue(object):
    """Implements a queue of images for upload.
    """

    def __init__(self, drive):
        """UploadQueue constructor.

            Args:
                drive: a Drive object.

            Returns:

            Raises:

        """
        self.uploadqueue = deque();
        self.drive = drive;
        self.running = True;

    def append(self, img_time):
        """Append a new image for upload.

            Args:
                img_time: a datetime object representing the time the frame was taken.

            Returns:

            Raises:

        """
        if len(self.uploadqueue) == 0:
            print(str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] + "m" + str(img_time)[17:19] + "s", "[ADDED TO UPLOAD LIST]")
            self.uploadqueue.append(img_time)
        elif (not str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] + "m" + str(img_time)[17:19] + "s"
                    == str(self.uploadqueue[-1])[:10] + " " + str(self.uploadqueue[-1])[11:13] + "h" + str(self.uploadqueue[-1])[14:16] + "m" + str(self.uploadqueue[-1])[17:19] + "s"):
            print(str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] + "m" + str(img_time)[17:19] + "s" , "[ADDED TO UPLOAD LIST]")
            self.uploadqueue.append(img_time)


    def start(self):
        threading.Thread(target=self.uploadloop, args=()).start()

    def uploadloop(self):
        """UploadQueue main method. Continuously uploads the first element of the queue.

            Args:

            Returns:

            Raises:

        """
        print("Starting upload loop")
        while self.running and self.drive:
            if self.drive and len(self.uploadqueue) > 0:
                img_time = self.uploadqueue[0];
                self.uploadqueue.popleft();
                upload_path = None;
                while not upload_path and self.running:
                    upload_path = self.drive.get_link(img_time);
                    time.sleep(0.1);
                if self.drive:
                    disk_path = "/".join(("detected", str(img_time.year), str(img_time.month) + ". "
                                         + img_time.strftime('%B'), str(img_time.day), str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] + "m" + str(img_time)[17:19] + "s" + ".png"))
                    print(str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] + "m" + str(img_time)[17:19] + "s", "[UPLOAD STARTED]")
                    self.drive.save_img(disk_path, upload_path);
                    print(str(img_time)[:10] + " " + str(img_time)[11:13] + "h" + str(img_time)[14:16] + "m" + str(img_time)[17:19] + "s", "[UPLOAD FINISHED]")
            time.sleep(0.1)

    def quit(self):
        self.drive = None;
        self.running = False;


class Drive(object):
    """Upload images to a Google Drive account in a time structure.

        Drive uses google login data to manage a Google Docs
        account. This same API can be used to manage the new Google
        Drive. This class saves images according to date and time of
        capture in a hierarchical time structure year/month/day/image.

        >>> import save;
        >>> drive = save.Drive();
        >>> current_time = datetime.datetime.now();
        >>> folder_link = drive.get_link(current_time);
        >>> drive.save_img("path/to/image.jpg", folder_link);

        Attributes:
            No public attributes.

    """
{'fraction_non_alphanumeric': 0.16871842311250268, 'fraction_numerical': 0.01601081619583007, 'mean_word_length': 2.096276712932364, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 49, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '865238', 'n_tokens_mistral': 4572, 'n_tokens_neox': 4318, 'n_words': 869}