This repository has been archived by the owner on Nov 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared.py
76 lines (56 loc) · 2.34 KB
/
shared.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
'''
Methods shared accross WrightData.
'''
### import ####################################################################
import os
import sys
import configparser
import WrightTools as wt
### define ####################################################################
package_folder = os.path.dirname(__file__)
### open configs ##############################################################
def get_config(filename):
ini = configparser.ConfigParser()
path = os.path.join(package_folder, filename)
ini.read(path)
return dict(ini.items('id')) # return a dictionary
google_drive = get_config('google drive.ini')
google_drive_private = get_config('google drive private.ini')
### google drive download #####################################################
def download(key, directory):
try:
# get folder_id
key = key.lower()
if key in google_drive.keys():
folder_id = google_drive[key]
elif key in google_drive_private.keys():
folder_id = google_drive_private[key]
else:
raise Exception('key invalid')
# get files
drive = wt.google_drive.Drive()
ids = drive.list_folder(folder_id)
for fileid in ids:
drive.download(fileid, directory=directory)
except Exception as inst:
print(inst)
### process ###################################################################
def process(key, workup_method, raw_dictionary, processed_dictionary,
raw_pickle_path, processed_pickle_path):
# get from pickle or create
is_raw_pickle = os.path.isfile(raw_pickle_path)
is_processed_pickle = os.path.isfile(processed_pickle_path)
if is_raw_pickle and is_processed_pickle:
try:
raw = wt.data.from_pickle(raw_pickle_path, verbose=False)
processed = wt.data.from_pickle(processed_pickle_path, verbose=False)
except UnicodeDecodeError: # pickles written in wrong version of python, probably
raw, processed = workup_method()
else:
raw, processed = workup_method()
# check version (should be identical, check only processed)
if not processed.__version__.split('.')[0] == wt.__version__.split('.')[0]:
raw_movie, processed_movie = workup_method()
# add to dictionaries
raw_dictionary[key] = raw
processed_dictionary[key] = processed