forked from GELOG/optithera-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
144 lines (109 loc) · 3.63 KB
/
Copy pathutils.py
File metadata and controls
144 lines (109 loc) · 3.63 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import re
#Begin Diego Alvarez
import logging
import sys
import os
import datetime
import config
#End Diego Alvarez
NEWLINE_REPLACEMENT = " " # Default
SUBARRAY_SEPARATOR = ";" # Default
FIELD_SEPARATOR = "," # Default
newline_regex = re.compile(r"[\n\r]")
# Sanitizes all the elements in a list
def prepare_fields(li):
for i, x in enumerate(li):
updated = str(x) if x is not None else ""
updated = newline_regex.sub(NEWLINE_REPLACEMENT, updated)
li[i] = updated
return li
# Turns a cursor into a list ready to write
def to_prepared_list(li):
return prepare_fields(list(li))
# Turns a cursor into a csv string
def to_csv(li):
return FIELD_SEPARATOR.join(to_prepared_list(li))
# Completely drains a cursor to return a csv string
def drain_cursor_to_csv(cursor):
result = []
f = cursor.fetchone()
while f:
result.append(to_csv(f))
f = cursor.fetchone()
return "\n".join(result)
# Returns the length of the longest element at a position.
# Rewinds the cursor before returning
def size_of_subarray(cursor, pos):
size = 0
cursor.scroll(0, "absolute")
f = cursor.fetchone()
while f:
size = max(size, len(f[pos].split(SUBARRAY_SEPARATOR)))
f = cursor.fetchone()
cursor.scroll(0, "absolute")
return size
# Turns the subarray created by array_to_string(array_agg(...)) into a python list
def break_subarray(li, pos, size):
sub = li[pos].split(SUBARRAY_SEPARATOR)
padding = ["" for i in xrange(0, size - len(sub))]
return li[:pos] + sub + li[pos+1:] + padding
# Prepares headers dynamically based on the number of columns
def make_headers(name, pos, nb):
return FIELD_SEPARATOR.join([name+str(i) for i in xrange(pos, nb)])
# Prepares placeholders (ex: {0}) dynamically to fill in with format()
def make_placeholders(pos, nb):
return FIELD_SEPARATOR.join(["{"+str(i)+"}" for i in xrange(pos, pos+nb)])
#Begin Diego Alvarez
def create_logger(p_source):
# Create a logger
try:
os.makedirs(config.log_file_destination)
except OSError as err:
pass
#utils.log(logger,str(err))
now = datetime.datetime.now()
LOG_FILENAME=config.log_file_destination+"log_"+p_source+"_"+now.strftime("%Y%m%d_%H%M%S")+".out"
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
# Create file handler
fhandler = logging.FileHandler(LOG_FILENAME)
fhandler.setLevel(logging.DEBUG)
# create a logging format
#formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('%(asctime)s - %(message)s')
fhandler.setFormatter(formatter)
# add the handlers to the logger
my_logger.addHandler(fhandler)
return my_logger;
def log(p_logger, p_message):
p_logger.debug(p_message)
#def find_file_in_list(p_filelist, p_filename):
#
# filename = None
# for item in p_filelist:
# if p_filename in item:
# filename=item
# break
# return filename
def find_file_in_list(p_filelist, p_regex):
filename = None
for item in p_filelist:
if re.match(p_regex, item):
filename=item
break
return filename
def get_file_name(p_filename):
#Get filename without the extension
filename, file_extension = os.path.splitext(p_filename)
short_filename=filename.split()
return short_filename[0]
def get_individual_info(p_filename, p_info):
info= None
with open(p_filename,'rb') as f:
for line in f:
columns=line.split()
if columns[0] == p_info:
info = columns[2]
break
return info
#End Diego Alvarez