This repository was archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunctions.py
More file actions
242 lines (163 loc) · 5.79 KB
/
Copy pathfunctions.py
File metadata and controls
242 lines (163 loc) · 5.79 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python
# coding: utf-8
import sys
import os
import arrow
import re
import constants
def prettyDate(date):
"""Prettify a date. Ex: 3 days ago"""
now = arrow.now()
date = arrow.get(date)
if now.timestamp - date.timestamp < 86400:
return "Today"
else:
return date.humanize(now.naive)
def simpleChar(string, wildcards=True):
"""Sluggify the string.
If wildcards is False, don't sluggify them"""
# http://www.siteduzero.com/forum-83-810635-p1-sqlite-recherche-avec-like-insensible-a-la-casse.html#r7767300
# http://stackoverflow.com/questions/5574042/string-slugification-in-python
# string = unidecodePerso(string).lower()
resource_dir, _ = getRightDirs()
with open(os.path.join(resource_dir, 'config/data.bin'), 'rb') as f:
_replaces = f.read().decode('utf8').split('\x00')
string = string.lower()
chars = []
for ch in string:
if ch == '*' and not wildcards:
chars.append('*')
continue
codepoint = ord(ch)
if not codepoint:
chars.append('\x00')
continue
try:
chars.append(_replaces[codepoint - 1])
except IndexError:
pass
string = "".join(chars)
# http://stackoverflow.com/questions/35382793/regex-match-all-special-characters-but-not
return re.sub(r'_|[^\w\s*]+', ' ', string)
def queryString(word):
"""
Function to return a string formatted to be
included in a LIKE query
Ex:
querySting("sper*m") -> % sper%mine %
querySting("*sperm*") -> %sperm%
querySting("spermine") -> % spermine %
"""
word = str(word)
if word[0] != '*' and word[-1] != '*' and '*' in word:
word = word.replace('*', '%')
res = word.replace('*', '')
if word[0] == '*':
res = '%' + res
else:
res = '% ' + res
if word[-1] == '*':
res = res + '%'
else:
res = res + ' %'
return res
def buildSearch(topic_entries, author_entries, radio_states):
"""Build the query"""
base = "SELECT * FROM papers WHERE "
str_topic = ['', '']
str_author = ['', '']
# Include line for topic, radio "Any" is not checked
# -> AND query
if topic_entries[0]:
words = [simpleChar(word.strip(), False) for word
in topic_entries[0].split(",")]
words = [queryString(word) for word in words]
if radio_states[0]:
operator = 'OR'
else:
operator = 'AND'
for word in words:
if word == words[0]:
str_topic[0] = "topic_simple LIKE '{}'".format(word)
else:
str_topic[0] += " {} topic_simple LIKE '{}'".format(operator, word)
# TOPIC, NOT condition
if topic_entries[1]:
words = [simpleChar(word.strip(), False) for word
in topic_entries[1].split(",")]
words = [queryString(word) for word in words]
for word in words:
if word == words[0]:
str_topic[1] = "topic_simple NOT LIKE '{}'".format(word)
else:
str_topic[1] += " AND topic_simple NOT LIKE '{}'".format(word)
# AUTHOR, AND/OR condition
if author_entries[0]:
words = [simpleChar(word.strip(), False) for word
in author_entries[0].split(",")]
words = [queryString(word) for word in words]
if radio_states[2]:
operator = 'OR'
else:
operator = 'AND'
for word in words:
if word == words[0]:
str_author[0] = " author_simple LIKE '{}'".format(word)
else:
str_author[0] += " {} author_simple LIKE '{}'".format(operator, word)
# AUTHOR, NOT condition
if author_entries[1]:
words = [simpleChar(word.strip(), False) for word
in author_entries[1].split(",")]
words = [queryString(word) for word in words]
for word in words:
if word == words[0]:
str_author[1] = " author_simple NOT LIKE '{}'".format(word)
else:
str_author[1] += " AND author_simple NOT LIKE '{}'".format(word)
# Build the query from the parts. Concatenate them with AND
concatenate = [element for element in str_topic + str_author if element]
for element in concatenate:
if element is concatenate[0]:
base += "(" + element + ")"
else:
base += " AND (" + element + ")"
return base
def removeHtml(data):
"""Simple function to remove html tags.
Not very robust, but does the job.
Used in gui.shareByEmail"""
p = re.compile(r'<.*?>')
return p.sub('', data)
def getRightDirs():
"""Get the DATA_PATH and the resource_dir pathes.
DATA_PATH is on the user side if CB is frozen"""
if getattr(sys, "frozen", False):
# resource_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
resource_dir = sys._MEIPASS
DATA_PATH = constants.DATA_PATH
else:
resource_dir = '.'
DATA_PATH = '.'
return resource_dir, DATA_PATH
def getVersion():
"""Get the ChemBrows' version"""
resource_dir, DATA_PATH = getRightDirs()
with open(os.path.join(resource_dir, 'config/version.txt'),
'r', encoding='utf-8') as version_file:
version = version_file.read().strip()
return version
if __name__ == "__main__":
# like(10)
# _, dois = listDoi()
# print(dois)
# queryString("sper**mine")
# queryString("*sperm*")
# queryString("spermine")
# checkData()
# match(['jean-patrick francoia', 'robert pascal', 'laurent vial'], "r* pascal")
# unidecodePerso('test')
print(simpleChar("Her_%%%v*é Cottet", False))
# queryString("Hervé Cottet")
# simpleChar("C* N. hunter", False)
pass