forked from heldersepu/GMapCatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileUtils.py
185 lines (159 loc) · 5.87 KB
/
fileUtils.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
## @package gmapcatcher.fileUtils
# A common location for all the File I/O Utilities
import os
import re
import codecs
from time import time
## Return all the locations from a given file (filePath)
def read_file(strInfo, filePath, maxLine=0):
fileData = {}
if os.path.exists(filePath):
p = re.compile(strInfo + '="([^"]+)".*lat="([^"]+)".*lng="([^"]+)".*')
q = re.compile('.*zoom="([^"]+)".*')
file = open(filePath, "r")
for line in file:
try:
# the file should be in UTF-8, so decoding is needed
line = line.decode("UTF-8")
if (line[0] != '#'):
m = p.search(line)
if m:
zoom = 10
z = q.search(line)
if z:
zoom = int(z.group(1))
if m.group(1) in fileData:
name = '%s %i' % (m.group(1), len(fileData))
else:
name = m.group(1)
fileData[name] = (float(m.group(2)),
float(m.group(3)),
zoom)
if (maxLine > 0):
if (len(fileData) > maxLine):
break
except:
pass
file.close()
return fileData
else:
write_file(strInfo, filePath, fileData)
return None
## Writes all the locations (fileData) to given file (filePath)
def write_file(strInfo, filePath, fileData):
try:
file = open(filePath, "w")
except Exception:
print 'Error! Can NOT write file:'
print ' ' + filePath
return
file.write(codecs.BOM_UTF8+"# This is the " + strInfo + "s file used by GMapCatcher.\n" +
"#\n" +
"# This file contains a list of Locations/Position.\n" +
"# Each entry should be kept on an individual line,\n" +
"# and MUST have a unique name.\n" +
"# The latitude, longitud and zoom should be TAB separated.\n" +
"#\n" +
"# Additionally, comments (such as these) may be inserted on\n" +
"# lines sarting with a '#' symbol.\n" +
"#\n" + "# For example:\n" + "#\n" +
('# ' + strInfo + '="%s"\tlat="%f"\tlng="%f"\tzoom="%i"\n' %
("Paris, France", 48.856667, 2.350987, 5)) + "#\n")
for l in sorted(fileData.keys()):
# The method 'write' takes an unicode string here and acording to python manual
# it translates it automatically to string buffer acording to system defaults.
# Probably all systems translate unicode to UTF-8
file.write(strInfo + '="%s"\tlat="%f"\tlng="%f"\tzoom="%i"\n' %
(l.encode('UTF-8'), fileData[l][0], fileData[l][1], fileData[l][2]))
file.close()
## Append the location (strData) to given file (filePath)
def append_file(strInfo, filePath, strData, strName, extraTag=False):
try:
file = open(filePath, "a")
except Exception:
print 'Error! Can NOT write file:'
print ' ' + filePath
return
if extraTag:
file.write(strInfo + '="%s"\tlat="%s"\tlng="%s"\tzoom="%i"\t%s\n' %
(strName, strData[0], strData[1], strData[2] + 2, extraTag))
else:
file.write(strInfo + '="%s"\tlat="%s"\tlng="%s"\tzoom="%i"\n' %
(strName, strData[0], strData[1], strData[2] + 2))
file.close()
## Writes a new gtkrc file with the given theme
def write_gtkrc(strTheme):
filePath = './etc/gtk-2.0/gtkrc'
try:
file = open(filePath, "w")
except Exception:
print 'Error! Can NOT write file:'
print ' ' + filePath
return
file.write('# You can change the GMapCatcher theme here!\n' +
'#\n' +
'#gtk-theme-name = "Industrial"\n' +
'#gtk-theme-name = "XFCE-4.2"\n' +
'\n' +
'gtk-theme-name = "' + strTheme + '"')
file.close()
## Returns the current theme from the gtkrc file
def read_gtkrc():
filePath = './etc/gtk-2.0/gtkrc'
if os.path.exists(filePath):
file = open(filePath, "r")
for line in file:
line = line.strip()
if line.startswith('gtk-theme-name'):
return line[17:].strip('"')
## Returns all the available themes
def get_themes():
themesPath = './share/themes/'
myThemes = []
if os.path.isdir(themesPath):
for l in os.listdir(themesPath):
if os.path.isdir(themesPath + l) and (l[0] != '.'):
myThemes += [l]
return myThemes
## Returns all the available tracks
def get_tracks():
tracksPath = './tracks'
myTracks = []
if os.path.isdir(tracksPath):
for l in os.listdir(tracksPath):
#if os.path.isdir(tracksPath + l):
myTracks += [tracksPath + '/' + l]
return myTracks
## Checks if a directory exist if not it will be created
def check_dir(strPath, strSubPath=None):
if (strSubPath is not None):
strPath = os.path.join(strPath, strSubPath)
if not os.path.isdir(strPath):
try:
os.makedirs(strPath)
except Exception:
print 'Error! Can not create directory:'
print ' ' + strPath
return strPath
## Deletes a given file
def del_file(filename):
try:
os.remove(filename)
except:
pass
## Check if the file is older than given time
# (24h * 3600s/h) = 86400s
def is_old(filename, intDays):
if os.path.isfile(filename):
if (int(time() - os.path.getmtime(filename)) > (86400 * intDays)):
return True
return False
## Remove file if is older than given time
def delete_old(filename, intDays):
if is_old(filename, intDays):
try:
os.remove(filename)
return True
except:
pass
return False