-
Notifications
You must be signed in to change notification settings - Fork 2
/
commons.py
106 lines (78 loc) · 2.29 KB
/
commons.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
104
105
106
#! /usr/bin/python
import csv
from datetime import datetime
# http://labix.org/python-dateutil
from dateutil import parser
# utilities
def get_dict_data_from_csv_file(csv_file_path):
csv_file = open(csv_file_path, 'rb')
csv_file.seek(0)
sniffdialect = csv.Sniffer().sniff(csv_file.read(10000), delimiters='\t,;')
csv_file.seek(0)
dict_reader = csv.DictReader(csv_file, dialect=sniffdialect)
csv_file.seek(0)
dict_data = []
for record in dict_reader:
dict_data.append(record)
csv_file.close()
return dict_data
def write_dict_data_to_csv_file(csv_file_path, dict_data):
csv_file = open(csv_file_path, 'wb')
writer = csv.writer(csv_file, dialect='excel')
headers = dict_data[0].keys()
writer.writerow(headers)
# headers must be the same with dat.keys()
for dat in dict_data:
line = []
for field in headers:
line.append(dat[field])
writer.writerow(line)
csv_file.close()
def str_to_num(input_str):
return_num = 0
for ch in input_str:
return_num += ord(ch)
return return_num
def remove_no_parent_files(files):
tmp = files
for file in files:
if not file['parents']:
tmp.remove(file)
return tmp
def switch_email_domain(src_email, new_domain):
username = src_email.split('@')[0]
return '%s@%s' % (username, new_domain)
def has_dup_file(files):
tmp = []
for i in range(len(files)):
if files[i]['parents']:
file = {}
file['mimeType'] = files[i]['mimeType']
file['parentid'] = files[i]['parents'][0]['id']
file['title'] = files[i]['title']
if file in tmp:
print "Found duplicate file: %s" % file
return True
else:
tmp.append(file)
return False
def get_unique_file_name_list(files):
unique_file_name = []
for file in files:
if file['parents']:
file_item = {}
file_item['mimeType'] = file['mimeType']
file_item['parentid'] = file['parents'][0]['id']
file_item['title'] = file['title']
if file_item not in unique_file_name:
unique_file_name.append(file_item)
return unique_file_name
# check if the src_file is newer than the dest_file
def is_older(src_file, dest_file):
src_modified_date = parser.parse(src_file['modifiedDate'])
dest_modified_date = parser.parse(dest_file['modifiedDate'])
if src_modified_date < dest_modified_date:
return True
return False
def clean_query_string(title):
return title.replace(r"'", r"\'")