forked from anishathalye/neural-style
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.py
103 lines (88 loc) · 3.58 KB
/
drive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from collections import defaultdict, namedtuple
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import matplotlib.pyplot as plt
from util import *
ROOT_DIR_GDID = '13kelXjrToB3IpbHNj0WmRtP0yRNHcNUg'
CONTENT_DIR_GDID = '11_SKmzv0UqQhxb-rgcLYVsF5sdfUlu6A'
STYLE_DIR_GDID = '1ExIrZyGT2u4brgBIqVzQQk6-j63XP1xu'
OUTPUT_DIR_GDID = '190RRdQlpmipXHztkyK1tF_6WTeA5Ulmo'
IGNORED_TYPES = ["application/octet-stream"]
FOLDER_TYPE = "application/vnd.google-apps.folder"
IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif"]
tree = lambda: defaultdict(tree)
fdata = namedtuple("fdata","title id mimeType")
class Drive:
def __init__(self) -> None:
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
self.drive = GoogleDrive(gauth)
self.image_index = defaultdict(lambda:None)
self.dir_index = defaultdict(list)
self.local_files = defaultdict(lambda:None)
# self.path_tree = tree()
def Create_index(self):
queue = [CONTENT_DIR_GDID, STYLE_DIR_GDID]
while queue:
dir_id = queue.pop()
listed = self.drive.ListFile({'q': f"'{dir_id}' in parents"}).GetList()
for file in listed:
title, mimeType, id = file['title'], file['mimeType'], file['id']
if mimeType in IGNORED_TYPES:
continue
elif mimeType == FOLDER_TYPE:
queue.append(id)
elif mimeType in IMAGE_TYPES:
im_name = title.split('.')[0]
self.image_index[im_name] = fdata(title, id, mimeType)
else:
print("Unrecognized:", title, mimeType)
# print(file['title'],file['mimeType'],file['id'])
output_dirs = [(OUTPUT_DIR_GDID,"")]
while output_dirs:
dir_id, path = output_dirs.pop()
listed = self.drive.ListFile({'q': f"'{dir_id}' in parents"}).GetList()
for file in listed:
title, mimeType, id = file['title'], file['mimeType'], file['id']
if mimeType in IGNORED_TYPES:
continue
elif mimeType == FOLDER_TYPE:
path += f"{title}/"
self.dir_index[path] = id
output_dirs.append((id,path))
def List(self):
for im in sorted(self.image_index):
print(im)
def contains(self, img_name):
return self.image_index[img_name] is not None
def img_data(self, img_name):
return self.image_index[img_name]
def Retrieve(self, img_name):
local_path = self.local_files[img_name]
if local_path is None:
img_data = self.image_index[img_name]
if img_data is not None:
img_file = self.drive.CreateFile({'id': img_data.id})
img_file.GetContentFile(img_data.title, img_data.mimeType)
local_path = img_data.title
self.local_files[img_name] = local_path
return local_path
# def Store_image(self, image_file, paths=["common/"], create=True):
# parents = []
# # Get parent ids for each of the paths passed in
# for path in paths:
# parent_id = self.dir_index[path]
# if parent_id is None:
# if create:
# path_dirs = path.split('/')
# path, dir = path_dirs[:-1], path_dirs[-1]
# parent_id = self.create_dir(path, dir) # TODO: implement create_dir
# else:
# raise AttributeError(f"Path {path} does not exist and the create flag is {create}")
# parents.append[parent_id]
# upload_file = self.drive.CreateFile({'title': image_file,'parents': [{'id': pid} for pid in parents]})
# upload_file.SetContentFile(image_file)
# upload_file.Upload()