Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
Allow escaping quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjrw committed Jun 22, 2019
1 parent a058385 commit fd47267
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
16 changes: 8 additions & 8 deletions commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ def command(cls, irc_c, msg, cmd):
for regex in cmd.getarg('regex'):
try:
re.compile(regex)
# don't append the compiled - SQL doesn't like that
regexes.append(regex)
except re.RegexError:
raise CommandError("'{}' isn't a valid regular expression"
.format(search))
except re.RegexError as e:
raise CommandError("'{}' isn't a valid regular expression: {}"
.format(regex,e))
regexes.append(regex)
# don't append the compiled - SQL doesn't like that
searches.extend([{'term': r, 'type': 'regex'} for r in regexes])
# Set the tags
tags = {'include': [], 'exclude': []}
Expand Down Expand Up @@ -309,11 +309,11 @@ def command(cls, irc_c, msg, cmd):
"; ")
elif ratings['max'] is not None:
verbose += ("with a rating less than " +
str(ratings['max']) +
str(ratings['max']+1) +
"; ")
elif ratings['min'] is not None:
verbose += ("with a rating greater than " +
str(ratings['min']) +
str(ratings['min']-1) +
"; ")
if createds['min'] is not None and createds['max'] is not None:
verbose += ("created between " +
Expand Down Expand Up @@ -346,7 +346,7 @@ def command(cls, irc_c, msg, cmd):
return
pages = [irc_c.db._driver.get_article_info(p['id']) for p in pages]
if len(pages) > 1:
msg.reply("{} results: {}".format(len(pages), ", ".join(
msg.reply("{} results: {}".format(len(pages), " · ".join(
["\x02[{}]\x0F {}".format(i+1,p['title']) for i,p in enumerate(pages)]
)))
if len(pages) > 3:
Expand Down
37 changes: 23 additions & 14 deletions helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,41 +712,50 @@ def get_articles(self, searches, selection):
'author': 4, 'tags': 5, None: 6, 'regex': 7}
searches.sort(key=lambda x: keyorder[x['type']])
# begin query
articles = Table('articles')
q = MySQLQuery.from_(articles).select(articles.id)
art = Table('articles')
art_au = Table('articles_authors')
art_tags = Table('articles_tags')
q = MySQLQuery.from_(art).select(art.id)
for search in searches:
if search['type'] == 'rating':
if search['term']['max'] is not None:
q = q.where(articles.rating <= search['term']['max'])
q = q.where(art.rating <= search['term']['max'])
if search['term']['min'] is not None:
q = q.where(articles.rating >= search['term']['min'])
q = q.where(art.rating >= search['term']['min'])
elif search['type'] == 'parent':
q = q.where(articles.parent == search['term'])
q = q.where(art.parent == search['term'])
elif search['type'] == 'category':
if len(search['term']['exclude']) > 0:
q = q.where(articles.category.notin(search['term']['exclude']))
q = q.where(art.category.notin(search['term']['exclude']))
if len(search['term']['include']) > 0:
q = q.where(articles.category.isin(search['term']['include']))
q = q.where(art.category.isin(search['term']['include']))
elif search['type'] == 'date':
if search['term']['max'] is not None:
q = q.where(articles.date_posted <= search['term']['max'])
q = q.where(art.date_posted <= search['term']['max'])
if search['term']['min'] is not None:
q = q.where(articles.date_posted >= search['term']['min'])
q = q.where(art.date_posted >= search['term']['min'])
elif search['type'] == 'author':
# need to query articles_authors
# need to query art_authors
au_q = MySQLQuery()
# make a subquery!
'''
SELECT whateverthisisdonealready FROM art
WHERE 'authorname' IN (
SELECT author FROM art_authors
WHERE article_id=art.id
)
'''
pass
elif search['type'] == 'tags':
# need to query articles_tags
# need to query art_tags
pass
elif search['type'] == None:
q = q.where(
(articles.title.like(search['term']))
| (articles.scp_num == search['term'])
(art.title.like(search['term']))
| (art.scp_num == search['term'])
)
elif search['type'] == 'regex':
q = q.where(articles.title.regex(search['term']))
q = q.where(art.title.regex(search['term']))
# query complete
print(str(q))
# make the query sqlite-compatible
Expand Down
7 changes: 4 additions & 3 deletions helpers/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def __init__(self, message):
# What were the arguments?
if self.command:
# remove apostrophes because they'll fuck with shlex
self.message = self.message.replace("'", "<<APOSTROPHE>>")
self.message = self.message.replace("'", "<<APOS>>")
self.message = self.message.replace('\\"', "<<QUOT>>")
try:
self.message = shlex.split(self.message, posix=False)
# posix=False does not remove quotes
Expand All @@ -98,8 +99,8 @@ def __init__(self, message):
# raised if shlex detects fucked up quotemarks
self.message = self.message.split()
self.quote_error = True
for i,word in enumerate(self.message):
self.message[i] = word.replace("<<APOSTROPHE>>", "'")
self.message = [w.replace("<<APOS>>", "'") for w in self.message]
self.message = [w.replace("<<QUOT>>", '"') for w in self.message]
# arguments is now a list, quotes are preserved
# need to split it into different lists per tag, though
self.args = {'root': []}
Expand Down

0 comments on commit fd47267

Please sign in to comment.