Improve readability and fix minor typos.

This commit is contained in:
Sébastien Lavoie 2018-12-30 20:49:38 -06:00
parent d439b1c90a
commit 094f44b369
11 changed files with 705 additions and 474 deletions

View File

@ -1,5 +1,5 @@
#
# Bash completion definition for buku.
# Bash completion definition for Buku.
#
# Author:
# Arun Prakash Jana <engineerarun@gmail.com>

View File

@ -1,5 +1,5 @@
#
# Fish completion definition for buku.
# Fish completion definition for Buku.
#
# Author:
# Arun Prakash Jana <engineerarun@gmail.com>

View File

@ -1,6 +1,6 @@
#compdef buku
#
# Completion definition for buku.
# Completion definition for Buku.
#
# Author:
# Arun Prakash Jana <engineerarun@gmail.com>

908
buku

File diff suppressed because it is too large Load Diff

View File

@ -70,7 +70,14 @@ class BaseFilter(filters.BaseFilter):
class TagBaseFilter(BaseFilter):
def __init__(self, name, operation_text=None, apply_func=None, filter_type=None, options=None, data_type=None):
def __init__(
self,
name,
operation_text=None,
apply_func=None,
filter_type=None,
options=None,
data_type=None):
if operation_text in ('in list', 'not in list'):
super().__init__(name, options, data_type='select2-tags')
else:
@ -91,7 +98,9 @@ class TagBaseFilter(BaseFilter):
self.operation_text = operation_text
def clean(self, value):
if self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST) and self.name == 'usage_count':
if (
self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST) and
self.name == 'usage_count'):
value = [int(v.strip()) for v in value.split(',') if v.strip()]
elif self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST):
value = [v.strip() for v in value.split(',') if v.strip()]
@ -135,7 +144,14 @@ class BookmarkBukuFilter(BaseFilter):
class BookmarkBaseFilter(BaseFilter):
def __init__(self, name, operation_text=None, apply_func=None, filter_type=None, options=None, data_type=None):
def __init__(
self,
name,
operation_text=None,
apply_func=None,
filter_type=None,
options=None,
data_type=None):
if operation_text in ('in list', 'not in list'):
super().__init__(name, options, data_type='select2-tags')
else:
@ -154,7 +170,9 @@ class BookmarkBaseFilter(BaseFilter):
self.operation_text = operation_text
def clean(self, value):
if self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST) and self.name == BookmarkField.ID.name.lower():
if (
self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST) and
self.name == BookmarkField.ID.name.lower()):
value = [int(v.strip()) for v in value.split(',') if v.strip()]
elif self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST):
value = [v.strip() for v in value.split(',') if v.strip()]

View File

@ -41,7 +41,7 @@ def get_bukudb():
def get_tags():
"""get tags."""
tags = getattr(flask.g,'bukudb', get_bukudb()).get_tag_all()
tags = getattr(flask.g, 'bukudb', get_bukudb()).get_tag_all()
result = {
'tags': tags[0]
}
@ -99,9 +99,13 @@ def update_tag(tag):
result_flag = getattr(flask.g, 'bukudb', get_bukudb()).replace_tag(tag, new_tags)
op_text = 'replace tag [{}] with [{}]'.format(tag, ', '.join(new_tags))
if request.method == 'PUT' and result_flag and request.path.startswith('/api/'):
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
elif request.method == 'PUT' and request.path.startswith('/api/'):
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
elif request.method == 'POST' and result_flag:
flash(Markup('Success {}'.format(op_text)), 'success')
res = redirect(url_for('get_tags-html'))
@ -212,12 +216,18 @@ def refresh_bookmarks():
if request.method == 'POST':
print(request.form['index'])
print(request.form['threads'])
result_flag = getattr(flask.g, 'bukudb',
get_bukudb()).refreshdb(request.form['index'], request.form['threads'])
result_flag = getattr(
flask.g,
'bukudb',
get_bukudb()).refreshdb(request.form['index'], request.form['threads'])
if result_flag:
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
else:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
@ -259,14 +269,26 @@ def bookmark_api(id):
elif request.method == 'PUT' or is_html_post_request:
if request.method == 'PUT':
result_flag = bukudb.update_rec(
id, request.form['url'], request.form.get('title'), request.form['tags'], request.form['description'])
id,
request.form['url'],
request.form.get('title'),
request.form['tags'],
request.form['description'])
if result_flag and not is_html_post_request:
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
elif not result_flag and not is_html_post_request:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
elif is_html_post_request:
result_flag = bukudb.update_rec(
id, bookmark_form.url.data, bookmark_form.title.data, bookmark_form.tags.data, bookmark_form.description.data)
id,
bookmark_form.url.data,
bookmark_form.title.data,
bookmark_form.tags.data,
bookmark_form.description.data)
if result_flag:
flash(Markup('Success edit bookmark, id:{}'.format(id)), 'success')
else:
@ -277,9 +299,13 @@ def bookmark_api(id):
else:
result_flag = bukudb.delete_rec(id)
if result_flag:
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
else:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
@ -294,9 +320,13 @@ def refresh_bookmark(id):
result_flag = getattr(flask.g, 'bukudb',
get_bukudb()).refreshdb(id, request.form['threads'])
if result_flag:
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
else:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
@ -315,7 +345,9 @@ def get_tiny_url(id):
}
res = jsonify(result)
else:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
@ -335,7 +367,9 @@ def get_long_url(id):
}
res = jsonify(result)
else:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
@ -371,18 +405,26 @@ def bookmark_range_operations(starting_id, ending_id):
for i in range(starting_id, ending_id + 1, 1):
result_flag = bukudb.delete_rec(i)
if result_flag is False:
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
{'ContentType': 'application/json'}
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
return (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
elif request.method == 'PUT':
for i in range(starting_id, ending_id + 1, 1):
updated_bookmark = request.form[str(i)]
result_flag = bukudb.update_rec(
i, updated_bookmark['url'], updated_bookmark['title'], updated_bookmark['tags'], updated_bookmark['description'])
i,
updated_bookmark['url'],
updated_bookmark['title'],
updated_bookmark['tags'],
updated_bookmark['description'])
if result_flag is False:
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
{'ContentType': 'application/json'}
return (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
res = jsonify(response.response_template['success']), status.HTTP_200_OK, \
{'ContentType': 'application/json'}
return res
@ -401,12 +443,12 @@ def search_bookmarks():
all_keywords = False if all_keywords is None else all_keywords
deep = False if deep is None else deep
regex = False if regex is None else regex
all_keywords = \
all_keywords if type(all_keywords) == bool else all_keywords.lower() == 'true'
deep = \
deep if type(deep) == bool else deep.lower() == 'true'
regex = \
regex if type(regex) == bool else regex.lower() == 'true'
all_keywords = (
all_keywords if isinstance(all_keywords, bool) else
all_keywords.lower() == 'true'
)
deep = deep if isinstance(deep, bool) else deep.lower() == 'true'
regex = regex if isinstance(regex, bool) else regex.lower() == 'true'
else:
keywords = search_bookmarks_form.keywords.data
all_keywords = search_bookmarks_form.all_keywords.data
@ -464,9 +506,13 @@ def search_bookmarks():
for bookmark in found_bookmarks:
result_flag = bukudb.delete_rec(bookmark[0])
if result_flag is False:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
if res is None:
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
res = (jsonify(response.response_template['success']),
status.HTTP_200_OK,
{'ContentType': 'application/json'})
return res
@ -504,17 +550,37 @@ def create_app(config_filename=None):
# api
app.add_url_rule('/api/tags', 'get_tags', tag_list, methods=['GET'])
app.add_url_rule('/api/tags/<tag>', 'update_tag', tag_detail, methods=['GET', 'PUT'])
app.add_url_rule('/api/network_handle', 'networkk_handle', network_handle_detail, methods=['POST'])
app.add_url_rule(
'/api/network_handle',
'networkk_handle',
network_handle_detail,
methods=['POST'])
app.add_url_rule('/api/bookmarks', 'bookmarks', bookmarks, methods=['GET', 'POST', 'DELETE'])
app.add_url_rule('/api/bookmarks/refresh', 'refresh_bookmarks', refresh_bookmarks, methods=['POST'])
app.add_url_rule('/api/bookmarks/<id>', 'bookmark_api', bookmark_api, methods=['GET', 'PUT', 'DELETE'])
app.add_url_rule('/api/bookmarks/<id>/refresh', 'refresh_bookmark', refresh_bookmark, methods=['POST'])
app.add_url_rule(
'/api/bookmarks/refresh',
'refresh_bookmarks',
refresh_bookmarks,
methods=['POST'])
app.add_url_rule(
'/api/bookmarks/<id>',
'bookmark_api',
bookmark_api,
methods=['GET', 'PUT', 'DELETE'])
app.add_url_rule(
'/api/bookmarks/<id>/refresh',
'refresh_bookmark',
refresh_bookmark,
methods=['POST'])
app.add_url_rule('/api/bookmarks/<id>/tiny', 'get_tiny_url', get_tiny_url, methods=['GET'])
app.add_url_rule('/api/bookmarks/<id>/long', 'get_long_url', get_long_url, methods=['GET'])
app.add_url_rule(
'/api/bookmarks/<starting_id>/<ending_id>',
'bookmark_range_operations', bookmark_range_operations, methods=['GET', 'PUT', 'DELETE'])
app.add_url_rule('/api/bookmarks/search', 'search_bookmarks', search_bookmarks, methods=['GET', 'DELETE'])
app.add_url_rule(
'/api/bookmarks/search',
'search_bookmarks',
search_bookmarks,
methods=['GET', 'DELETE'])
# non api
admin.add_view(views.BookmarkModelView(
bukudb, 'Bookmarks', page_size=per_page, url_render_mode=url_render_mode))

View File

@ -25,7 +25,7 @@ except ImportError:
STATISTIC_DATA = None
DEFAULT_URL_RENDER_MODE = 'full'
DEFAULT_PER_PAGE = 10
log = logging.getLogger("bukuserver.views")
LOG = logging.getLogger("bukuserver.views")
class CustomAdminIndexView(AdminIndexView):
@ -160,7 +160,7 @@ class BookmarkModelView(BaseModelView):
except Exception as ex:
if not self.handle_view_exception(ex):
flash(gettext('Failed to create record. %(error)s', error=str(ex)), 'error')
log.exception('Failed to create record.')
LOG.exception('Failed to create record.')
return False
else:
self.after_model_change(form, model, True)
@ -173,7 +173,7 @@ class BookmarkModelView(BaseModelView):
except Exception as ex:
if not self.handle_view_exception(ex):
flash(gettext('Failed to delete record. %(error)s', error=str(ex)), 'error')
log.exception('Failed to delete record.')
LOG.exception('Failed to delete record.')
return False
else:
self.after_model_delete(model)
@ -198,7 +198,7 @@ class BookmarkModelView(BaseModelView):
bookmarks = self._apply_filters(bookmarks, filters)
if sort_field:
key_idx = [x.value for x in BookmarkField if x.name.lower() == sort_field][0]
bookmarks = sorted(bookmarks, key=lambda x:x[key_idx], reverse=sort_desc)
bookmarks = sorted(bookmarks, key=lambda x: x[key_idx], reverse=sort_desc)
count = len(bookmarks)
if page_size and bookmarks:
bookmarks = list(chunks(bookmarks, page_size))[page]
@ -331,7 +331,7 @@ class BookmarkModelView(BaseModelView):
except Exception as ex:
if not self.handle_view_exception(ex):
flash(gettext('Failed to update record. %(error)s', error=str(ex)), 'error')
log.exception('Failed to update record.')
LOG.exception('Failed to update record.')
return False
else:
self.after_model_change(form, model, False)
@ -453,7 +453,7 @@ class TagModelView(BaseModelView):
except Exception as ex:
if not self.handle_view_exception(ex):
flash(gettext('Failed to delete record. %(error)s', error=str(ex)), 'error')
log.exception('Failed to delete record.')
LOG.exception('Failed to delete record.')
return False
else:
self.after_model_delete(model)
@ -469,7 +469,7 @@ class TagModelView(BaseModelView):
except Exception as ex:
if not self.handle_view_exception(ex):
flash(gettext('Failed to update record. %(error)s', error=str(ex)), 'error')
log.exception('Failed to update record.')
LOG.exception('Failed to update record.')
return False
else:
self.after_model_change(form, model, False)

View File

@ -1,9 +1,5 @@
[MESSAGES CONTROL]
disable=
anomalous-backslash-in-string,
bad-continuation,
bad-whitespace,
bare-except,
broad-except,
dangerous-default-value,
expression-not-assigned,
@ -14,20 +10,14 @@ disable=
len-as-condition,
logging-format-interpolation,
lost-exception,
misplaced-comparison-constant,
missing-docstring,
missing-final-newline,
no-else-return,
#no-member,
no-self-use,
pointless-statement,
protected-access,
redefined-argument-from-local,
redefined-builtin,
redefined-outer-name,
superfluous-parens,
too-many-arguments,
too-many-boolean-expressions,
too-many-branches,
too-many-instance-attributes,
too-many-lines,
@ -43,6 +33,5 @@ disable=
unsupported-assignment-operation,
unused-argument,
unused-variable,
wrong-import-order,
[FORMAT]
max-line-length=139

View File

@ -60,8 +60,8 @@ def test_gen_headers():
'DNT': '1'
}
buku.gen_headers()
assert buku.myproxy is None
assert buku.myheaders == exp_myheaders
assert buku.MYPROXY is None
assert buku.MYHEADERS == exp_myheaders
@pytest.mark.parametrize('m_myproxy', [None, mock.Mock()])
@ -199,7 +199,7 @@ def test_edit_at_prompt(nav, is_editor_valid_retval, edit_rec_retval):
# test
if nav == 'w' and not is_editor_valid_retval:
return
elif nav == 'w':
if nav == 'w':
m_edit_rec.assert_called_once_with(editor, '', None, buku.DELIM, None)
elif buku.is_int(nav[2:]):
obj.edit_update_rec.assert_called_once_with(int(nav[2:]))
@ -599,7 +599,7 @@ def test_is_nongeneric_url(url, exp_res):
'newtag, exp_res',
[
(None, ('http://example.com', 'text1', None, None, 0, True)),
('tag1',('http://example.com', 'text1', ',tag1,', None, 0, True)),
('tag1', ('http://example.com', 'text1', ',tag1,', None, 0, True)),
]
)
def test_import_md(tmpdir, newtag, exp_res):

View File

@ -15,13 +15,13 @@ from genericpath import exists
from itertools import product
from tempfile import TemporaryDirectory
from unittest import mock
import unittest
import pytest
import yaml
from hypothesis import given, example, settings
from hypothesis import strategies as st
from unittest import mock
import pytest
import unittest
import vcr
import yaml
from buku import BukuDb, parse_tags, prompt
@ -829,20 +829,20 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input
# teardown
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
return
elif (low == 0 or high == 0) and input_retval != 'y':
if (low == 0 or high == 0) and input_retval != 'y':
assert not res
assert len(bdb_dc.get_rec_all()) == db_len
# teardown
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
return
elif (low == 0 or high == 0) and input_retval == 'y':
if (low == 0 or high == 0) and input_retval == 'y':
assert res == exp_res
with pytest.raises(sqlite3.OperationalError):
bdb.get_rec_all()
# teardown
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
return
elif n_low > db_len and n_low > 0:
if n_low > db_len and n_low > 0:
assert not res
assert len(bdb_dc.get_rec_all()) == db_len
# teardown
@ -976,7 +976,7 @@ def test_delete_rec_on_non_interger(index, low, high, is_range):
with pytest.raises(TypeError):
bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
return
elif not is_range and not isinstance(index, int):
if not is_range and not isinstance(index, int):
res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
assert not res
assert len(bdb.get_rec_all()) == db_len
@ -1260,7 +1260,7 @@ def test_browse_by_index(low, high, index, is_range, empty_database):
res = bdb.browse_by_index(index=index, low=low, high=high, is_range=is_range)
if is_range and (low < 0 or high < 0):
assert not res
elif is_range and 0 < n_low and 0 < n_high:
elif is_range and n_low > 0 and n_high > 0:
assert res
elif is_range:
assert not res

View File

@ -12,7 +12,7 @@ def test_load_from_empty():
# Assert
count = sum(1 for _ in items)
assert 0 == count
assert count == 0
def test_load_full_entry():
"""test method."""
@ -48,11 +48,11 @@ def test_load_full_entry():
for item in items:
result.append(item)
assert 1 == len(result)
assert 'http://uri.com/abc?234&536' == result[0][0]
assert 'title' == result[0][1]
assert ',x,y,' == result[0][2]
assert 'desc' == result[0][3]
assert len(result) == 1
assert result[0][0] == 'http://uri.com/abc?234&536'
assert result[0][1] == 'title'
assert result[0][2] == ',x,y,'
assert result[0][3] == 'desc'
def test_load_no_typecode():
@ -80,7 +80,7 @@ def test_load_no_typecode():
for item in items:
result.append(item)
assert 0 == len(result)
assert len(result) == 0
def test_load_invalid_typecode():
@ -108,7 +108,7 @@ def test_load_invalid_typecode():
for item in items:
result.append(item)
assert 0 == len(result)
assert len(result) == 0
def test_load_one_child():
@ -140,11 +140,11 @@ def test_load_one_child():
for item in items:
result.append(item)
assert 1 == len(result)
assert 'http://uri1' == result[0][0]
assert 'title1' == result[0][1]
assert ',' == result[0][2]
assert 'desc' == result[0][3]
assert len(result) == 1
assert result[0][0] == 'http://uri1'
assert result[0][1] == 'title1'
assert result[0][2] == ','
assert result[0][3] == 'desc'
def test_load_one_container_child():
"""test method."""
@ -169,7 +169,7 @@ def test_load_one_container_child():
for item in items:
result.append(item)
assert 0 == len(result)
assert len(result) == 0
def test_load_many_children():
"""test method."""
@ -194,7 +194,7 @@ def test_load_many_children():
for item in items:
result.append(item)
assert 3 == len(result)
assert len(result) == 3
def test_load_hierarchical_container():
"""test method."""
@ -229,13 +229,13 @@ def test_load_hierarchical_container():
for item in items:
result.append(item)
assert 6 == len(result)
assert 'http://uri1.com/#more-74' == result[0][0]
assert 'http://uri2.com/xyz' == result[1][0]
assert 'http://uri3.com' == result[2][0]
assert 'http://uri4.com/#more-74' == result[3][0]
assert 'http://uri5.com/xyz' == result[4][0]
assert 'http://uri6.com' == result[5][0]
assert len(result) == 6
assert result[0][0] == 'http://uri1.com/#more-74'
assert result[1][0] == 'http://uri2.com/xyz'
assert result[2][0] == 'http://uri3.com'
assert result[3][0] == 'http://uri4.com/#more-74'
assert result[4][0] == 'http://uri5.com/xyz'
assert result[5][0] == 'http://uri6.com'
def test_load_separator():
"""test method."""
@ -261,7 +261,7 @@ def test_load_separator():
for item in items:
result.append(item)
assert 0 == len(result)
assert len(result) == 0
def test_load_multiple_tags():
"""test method."""
@ -290,5 +290,5 @@ def test_load_multiple_tags():
for item in items:
result.append(item)
assert 1 == len(result)
assert ",tag1,tag2," == result[0][2]
assert len(result) == 1
assert result[0][2] == ",tag1,tag2,"