Skip to content

Commit

Permalink
Ignore invalid URLs #15
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 11, 2018
1 parent 8f789c6 commit 4e4fc92
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@ Edit the `ZOTERO_DIR` variable to point to Zotero 5's data directory if you aren
<string></string>
</dict>
<key>version</key>
<string>1.0</string>
<string>1.0.1</string>
<key>webaddress</key>
<string>https://github.com/deanishe/zothero</string>
</dict>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cite/cite.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import json
import logging
import os
import subprocess
import subprocess # nosec
from tempfile import NamedTemporaryFile

from .locales import LOCALE_DIR
Expand Down Expand Up @@ -46,7 +46,7 @@ def generate(csldata, cslfile, bibliography=False, locale=None):

log.debug('[cite] cmd=%r', cmd)

p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, # nosec
stderr=subprocess.PIPE)

stdout, stderr = p.communicate()
Expand Down
14 changes: 14 additions & 0 deletions src/lib/zothero/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class Store(object):
convert_in (callable): Called on input before storage.
convert_out (callable): Called on output before returning it.
name (str): Name of store (and database table).
"""

def __init__(self, name, cache, convert_in=None, convert_out=None):
"""Create new `Store`.
Expand All @@ -82,6 +84,7 @@ def __init__(self, name, cache, convert_in=None, convert_out=None):
cache (Cache): `Cache` object managing the database.
convert_in (callable, optional): Called on input before storage.
convert_out (callable, optional): Called on output before return.
"""
self.name = name
self.cache = cache
Expand All @@ -104,6 +107,7 @@ def keys(self):
Yields:
unicode: Store keys.
"""
sql = u"""
SELECT `key` FROM `{table}` WHERE 1
Expand All @@ -123,6 +127,7 @@ def get(self, key, default=None):
Returns:
obj: Object deserialised from the database.
"""
key = self._validate_key(key)
sql = u"""
Expand All @@ -142,6 +147,7 @@ def set(self, key, value):
Args:
key (str): Database key.
value (obj): Object to store in database.
"""
key = self._validate_key(key)
value = self.convert_in(value)
Expand Down Expand Up @@ -199,6 +205,7 @@ def updated(self, key=None):
Returns:
float: UNIX timestamp of last update, or ``0.0`` if key
doesn't exit.
"""
if key:
sql = u"""
Expand Down Expand Up @@ -230,6 +237,7 @@ def _validate_key(self, key):
Returns:
unicode: Unicode `key`.
"""
if isinstance(key, str):
key = unicode(key, 'utf-8')
Expand All @@ -248,6 +256,7 @@ class Cache(object):
filepath (str): Path to cache sqlite file.
invalid_names (tuple): Names not permitted for Stores
(i.e. bad table names).
"""

invalid_names = ('dbinfo', 'sqlite_sequence', 'sqlite_master')
Expand All @@ -257,6 +266,7 @@ def __init__(self, filepath):
Args:
filepath (str): Path of cache sqlite database.
"""
self.filepath = filepath
self._conn = None
Expand Down Expand Up @@ -295,6 +305,7 @@ def open(self, name, convert_in=None, convert_out=None):
Returns:
Store: `Store` object.
"""
# log.debug('self.caches=%r', self.caches)
log.debug('[cache] opening store %r...', name)
Expand All @@ -314,6 +325,7 @@ def clear(self, name=None):
Raises:
ValueError: Raised if specified Store does not exit.
"""
if name is None: # Delete whole cache
try:
Expand All @@ -340,6 +352,7 @@ def stores(self):
Returns:
list: String names of Stores.
"""
sql = u"SELECT name FROM `sqlite_master` WHERE type='table'"
rows = self.conn.execute(sql)
Expand All @@ -357,6 +370,7 @@ def _add_table(self, name):
Raises:
ValueError: Raised if `name` is not permitted.
"""
if name.lower() in self.invalid_names:
raise ValueError('name is reserved: %r' % name.lower())
Expand Down
4 changes: 4 additions & 0 deletions src/lib/zothero/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def zotero(self):
Returns:
.zotero.Zotero: Initialised `Zotero` object.
"""
from .zotero import Zotero

Expand Down Expand Up @@ -139,6 +140,7 @@ def index(self):
Returns:
.index.Index: Initialised search index.
"""
if not self._index:
from .index import Index
Expand Down Expand Up @@ -175,6 +177,7 @@ def styles(self):
Returns:
.styles.Styles: `Styles` object pointing to the styles directory
of :attr:`zotero`.
"""
if not self._styles:
from .styles import Styles
Expand All @@ -190,6 +193,7 @@ def entry(self, entry_id):
Returns:
zothero.zotero.Entry: `Entry` for `key` or `None` if not found.
"""
return self.index.entry(entry_id)

Expand Down
5 changes: 4 additions & 1 deletion src/lib/zothero/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,14 @@ def _update(self, zot, force=False):
e.date]

for k, v in e.zdata.items():
if k in zfields_ignore or 'date' in k.lower():
if k in zfields_ignore or 'date' in k.lower() or not v:
continue

if k == 'url':
hostname = urlparse(v).hostname
if not hostname:
continue

if hostname.startswith('www.'):
hostname = hostname[4:]
all_.append(hostname)
Expand Down
1 change: 1 addition & 0 deletions src/lib/zothero/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def __init__(self, datadir, dbpath=None, attachments_base_dir=None):
datadir (str): Path to Zotero's data directory.
dbpath (str, optional): Path to `zotero.sqlite` if not in
``datadir``.
"""
self.datadir = datadir
self._attachments_dir = attachments_base_dir
Expand Down

0 comments on commit 4e4fc92

Please sign in to comment.