Stylistic changes

This commit is contained in:
petr.kalis 2020-07-17 12:51:32 +02:00
parent c3219554c0
commit 88b6bfa840
2 changed files with 31 additions and 53 deletions

View file

@ -10,10 +10,7 @@ import random
from googleapiclient.http import MediaFileUpload
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.file'] # for write|delete
files = [
'c:\\projects\\Test\\Assets\\Cylinder\\publish\\look\\lookMain\\v001\\test_Cylinder_lookMain_v001.ma']
'https://www.googleapis.com/auth/drive.file'] # for write|delete
class GDriveHandler():
@ -32,9 +29,9 @@ class GDriveHandler():
:return:
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
# The file token.pickle stores the user's access and refresh tokens,
# and is created automatically when the authorization flow completes
# for the first time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
@ -61,10 +58,8 @@ class GDriveHandler():
:param folders: list of dictionaries with folder metadata
:return: <dictionary> - path as a key, folder id as a value
"""
tree = {}
tree["/"] = {"id": self.root["id"]}
ending_by = {}
ending_by[self.root["id"]] = "/" + self.root["name"]
tree = {"/": {"id": self.root["id"]}}
ending_by = {self.root["id"]: "/" + self.root["name"]}
not_changed_times = 0
folders_cnt = len(folders) * 5
# exit loop for weird unresolved folders, raise ValueError, safety
@ -132,7 +127,7 @@ class GDriveHandler():
'parents': [folder_id]
}
folder = self.service.files().create(body=folder_metadata,
fields='id').execute()
fields='id').execute()
folder_id = folder["id"]
new_path_key = path + '/' + new_folder_name
@ -153,7 +148,8 @@ class GDriveHandler():
:return: <string> file_id of created/modified file
"""
if not os.path.isfile(source_path):
raise ValueError("Source file {} doesn't exist.".format(source_path))
raise ValueError("Source file {} doesn't exist.".
format(source_path))
root, ext = os.path.splitext(path)
@ -231,12 +227,12 @@ class GDriveHandler():
def _get_folder_metadata(self, path):
"""
Get info about folder with 'path'
:param id: <string>
:param path: <string>
:return: <dictionary> with metadata or raises ValueError
"""
try:
return self.tree[path]
except:
except Exception:
raise ValueError("Uknown folder id {}".format(id))
def list_folders(self):
@ -271,11 +267,10 @@ class GDriveHandler():
fields = 'nextPageToken, files(id, name, parents)'
while True:
q = self._handle_q("")
response = self.service.files().\
list(q=q,
spaces='drive',
fields=fields,
pageToken=page_token).execute()
response = self.service.files().list(q=q,
spaces='drive',
fields=fields,
pageToken=page_token).execute()
files.extend(response.get('files', []))
page_token = response.get('nextPageToken', None)
if page_token is None:
@ -323,8 +318,7 @@ class GDriveHandler():
:return: file metadata, False if not found
"""
q = self._handle_q("name = '{}' and '{}' in parents"
.format(file_name, folder_id)
)
.format(file_name, folder_id))
response = self.service.files().list(
q=q,
spaces='drive',
@ -338,19 +332,16 @@ class GDriveHandler():
return False
return file[0]
def _handle_q(self, q, trashed=False, hidden=False):
def _handle_q(self, q, trashed=False):
""" API list call contain trashed and hidden files/folder by default.
Usually we dont want those, must be included in query explicitly.
:param q: <string> query portion
:param trashed: False|True
:param hidden: False|True
:return: <string>
"""
parts = [q]
if not trashed:
parts.append(" trashed = false ")
# if not hidden:
# parts.append(" hidden = false ")
return " and ".join(parts)
@ -369,7 +360,7 @@ class GDriveHandler():
q.append("name = '%s'" % name.replace("'", "\\'"))
if is_folder is not None:
q.append("mimeType %s '%s'" % (
'=' if is_folder else '!=', self.FOLDER_STR))
'=' if is_folder else '!=', self.FOLDER_STR))
if parent is not None:
q.append("'%s' in parents" % parent.replace("'", "\\'"))
params = {'pageToken': None, 'orderBy': order_by}
@ -392,7 +383,7 @@ class GDriveHandler():
:return: <generator>
"""
if by_name:
top, = self.iterfiles(name=top, is_folder=True)
top, = self._iterfiles(name=top, is_folder=True)
else:
top = self.service.files().get(fileId=top).execute()
if top['mimeType'] != self.FOLDER_STR:
@ -401,7 +392,7 @@ class GDriveHandler():
while stack:
path, top = stack.pop()
dirs, files = is_file = [], []
for f in self.iterfiles(parent=top['id']):
for f in self._iterfiles(parent=top['id']):
is_file[f['mimeType'] != self.FOLDER_STR].append(f)
yield path, top, dirs, files
if dirs:
@ -410,19 +401,5 @@ class GDriveHandler():
if __name__ == '__main__':
gd = GDriveHandler()
# print(gd.list_folders())
# print(gd.walk())
# print(len(gd.list_folders()))
# print((gd.list_folders()[0]))
print(gd.get_folder('d'))
print(gd.root)
#print(gd.get_subfolders('Test'))
# print(gd.get_folder('2017454654645'))
print(gd.tree)
# print(gd.folder_path_exists('/My Drive/Test'))
# print(gd.file_path_exists('/My Drive/Clover/clouser.txt'))
#print(gd.create_folder('/My Drive/Test/new/new/new/new'))
print(gd.upload_file(files[0], '/My Drive/Test/new/new/new/new_file.ma', overwrite=True))
print(gd.delete_file('/My Drive/Test/new/new/new/new_file.ma'))
print(gd.delete_folder('/My Drive/Test/new/new/new/'))

View file

@ -2,7 +2,6 @@ from pype.api import config, Logger
from avalon import io
import threading
from aiohttp import web
import asyncio
from enum import Enum
@ -54,8 +53,6 @@ class SyncServer():
classes and registered in 'providers.py'.
"""
def __init__(self):
self.qaction = None
self.failed_icon = None
@ -135,18 +132,17 @@ class SyncServer():
target_file = file.get("path", "").replace('{root}', target_root)
new_file_id = handler.upload_file(source_file,
target_file,
overwrite=True)
target_file,
overwrite=True)
if new_file_id:
representation_id = representation.get("_id")
file_id = file.get("_id")
filter = {
query = {
"_id": representation_id,
"files._id": file_id
}
io.update_many(
filter
query
,
{"$set": {"files.$.sites.gdrive.id": new_file_id,
"files.$.sites.gdrive.created_dt":
@ -184,12 +180,17 @@ class SyncServer():
def thread_stopped(self):
self._is_running = False
class SynchServerThread(threading.Thread):
class SynchServerThread(threading.Thread):
"""
Separate thread running synchronization server with asyncio loop.
Stopped when tray is closed.
"""
def __init__(self, module):
super(SynchServerThread, self).__init__()
self.module = module
self.loop = None
self.is_running = False
def run(self):
self.is_running = True
@ -248,4 +249,4 @@ class SynchServerThread(threading.Thread):
await self.loop.shutdown_asyncgens()
# to really make sure everything else has time to stop
await asyncio.sleep(0.07)
self.loop.stop()
self.loop.stop()