annotate vdb_common.py @ 1:5c5027485f7d draft

Uploaded correct file
author damion
date Sun, 09 Aug 2015 16:07:50 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
1 #!/usr/bin/python
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
2 import re
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
3 import os
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
4 import time
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
5 import dateutil
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
6 import dateutil.parser as parser2
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
7 import datetime
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
8 import calendar
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
9
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
10 def parse_date(adate):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
11 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
12 Convert human-entered time into linux integer timestamp
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
13
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
14 @param adate string Human entered date to parse into linux time
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
15
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
16 @return integer Linux time equivalent or 0 if no date supplied
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
17 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
18 adate = adate.strip()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
19 if adate > '':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
20 adateP = parser2.parse(adate, fuzzy=True)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
21 #dateP2 = time.mktime(adateP.timetuple())
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
22 # This handles UTC & daylight savings exactly
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
23 return calendar.timegm(adateP.timetuple())
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
24 return 0
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
25
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
26
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
27 def get_unix_time(vtime, voffset=0):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
28 return float(vtime) - int(voffset)/100*60*60
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
29
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
30
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
31 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
32 return [int(text) if text.isdigit() else text.lower()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
33 for text in re.split(_nsre, s)]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
34
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
35
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
36 class date_matcher(object):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
37 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
38 Enables cycling through a list of versions and picking the one that matches
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
39 or immediately preceeds a given date. As soon as an item is found, subsequent
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
40 calls to date_matcher return false (because of the self.found flag)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
41 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
42 def __init__(self, unix_time):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
43 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
44 @param adate linux date/time
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
45 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
46 self.found = False
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
47 self.unix_time = unix_time
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
48
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
49 def __iter__(self):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
50 return self
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
51
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
52 def next(self, unix_datetime):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
53 select = False
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
54 if (self.found == False) and (self.unix_time > 0) and (unix_datetime <= self.unix_time):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
55 self.found = True
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
56 select = True
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
57 return select
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
58
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
59
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
60 def dateISOFormat(atimestamp):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
61 return datetime.datetime.isoformat(datetime.datetime.fromtimestamp(atimestamp))
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
62
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
63 def lightDate(unixtime):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
64 return datetime.datetime.utcfromtimestamp(float(unixtime)).strftime('%Y-%m-%d %H:%M')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
65
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
66 def move_files(source_path, destination_path, file_paths):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
67 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
68 MOVE FILES TO CACHE FOLDER (RATHER THAN COPYING THEM) FOR GREATER EFFICIENCY.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
69 Since a number of data source systems have hidden / temporary files in their
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
70 data folder structure, a list of file_paths is required to select only that
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
71 content that should be copied over. Note: this will leave skeleton of folders; only files are moved.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
72
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
73 Note: Tried using os.renames() but it errors when attempting to remove folders
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
74 from git archive that aren't empty due to files that are not to be copied.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
75
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
76
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
77 @param source_path string Absolute folder path to move data files from
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
78 @param destination_path string Absolute folder path to move data files to
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
79 @param file_paths string List of files and their relative paths from source_path root
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
80 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
81 for file_name in file_paths:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
82 if len(file_name):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
83 print "(" + file_name + ")"
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
84 v_path = os.path.dirname(os.path.join(destination_path, file_name))
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
85 if not os.path.isdir(v_path):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
86 os.makedirs(v_path)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
87 os.rename(os.path.join(source_path, file_name), os.path.join(destination_path, file_name) )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
88