-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enh] searxinstances.update: normalize URL, add tests
- add a Makefile - because fmoo/python-editor#29 , a forked has been added in this repo. - compliance to pylint rules
- Loading branch information
Showing
12 changed files
with
274 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
.coverage | ||
.vscode | ||
.pytest_cache/ | ||
|
||
*~ | ||
__pycache__/ | ||
|
@@ -15,4 +17,5 @@ dist/ | |
*.egg-info/ | ||
|
||
cache | ||
htmlcov | ||
ve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) | ||
|
||
install: | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
|
||
install-update: | ||
pip install -r requirements-update.txt | ||
|
||
install-all: install install-update | ||
pip install -r requirements-dev.txt | ||
|
||
check: | ||
python -m searxinstances.check | ||
|
||
qa: | ||
flake8 --max-line-length=120 searxinstances tests | ||
pylint searxinstances tests | ||
python -m pytest --cov-report html --cov=searxinstances tests -vv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pylint==2.4.4 | ||
pytest==5.2.2 | ||
pytest-asyncio==0.10.0 | ||
pytest-cov==2.8.1 | ||
flake8==3.7.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
httpx==0.11.0 | ||
GitPython==3.0.5 | ||
python-editor==1.0.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
from . import model | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Programmatically open an editor, capture the result. | ||
# | ||
# mostly copy paste from | ||
# https://github.com/fmoo/python-editor (including PR 15 and 16) | ||
# https://github.com/jpscaletti/texteditor | ||
# both use an Apache 2 license. | ||
# both use distutils.spawn.find_executable which is a problem with pytest | ||
# see https://github.com/fmoo/python-editor/issues/29 | ||
|
||
|
||
import sys | ||
import os.path | ||
import subprocess | ||
import tempfile | ||
import shlex | ||
from shutil import which | ||
|
||
|
||
__all__ = [ | ||
'edit', | ||
'get_editor', | ||
'EditorError', | ||
] | ||
|
||
|
||
MACOS_EDITORS = [ | ||
# The -t flag make MacOS open the default *editor* for the file | ||
"open -t" | ||
] | ||
|
||
COMMON_EDITORS = ["subl", "vscode", "atom"] | ||
|
||
# In some linuxes `vim` and/or `emacs` come preinstalled, but we don't want | ||
# to throw you to their unfamiliar UI unless there are other options. | ||
# If you are using them you probably have set your $EDITOR variable anyway. | ||
LINUX_EDITORS = COMMON_EDITORS + ["kate", "geany", "gedit", "nano", "editor"] | ||
|
||
WINDOWS_EDITORS = COMMON_EDITORS + ["notepad++.exe", "notepad.exe"] | ||
|
||
EDITORS = {"darwin": MACOS_EDITORS, "linux": LINUX_EDITORS, "win": WINDOWS_EDITORS} | ||
|
||
|
||
class EditorError(RuntimeError): | ||
pass | ||
|
||
|
||
def get_default_editors(): | ||
sys_platform = sys.platform | ||
|
||
for platform in EDITORS: | ||
if sys_platform.startswith(platform): | ||
return EDITORS[platform] | ||
|
||
return COMMON_EDITORS | ||
|
||
|
||
def get_editor_args(editor): | ||
if editor in ['vim', 'gvim', 'vim.basic', 'vim.tiny']: | ||
return ['-f', '-o'] | ||
|
||
elif editor == 'emacs': | ||
return ['-nw'] | ||
|
||
elif editor == 'gedit': | ||
return ['-w', '--new-window'] | ||
|
||
elif editor == 'nano': | ||
return ['-R'] | ||
|
||
elif editor == 'code': | ||
return ['-w', '-n'] | ||
|
||
else: | ||
return [] | ||
|
||
|
||
def get_editor(): | ||
# Get the editor from the environment. Prefer VISUAL to EDITOR | ||
editor = os.environ.get('VISUAL') or os.environ.get('EDITOR') | ||
if editor: | ||
return editor | ||
|
||
# None found in the environment. Fallback to platform-specific defaults. | ||
for editor in get_default_editors(): | ||
path = which(editor) | ||
if path is not None: | ||
return path | ||
|
||
raise EditorError("Unable to find a viable editor on this system." | ||
"Please consider setting your $EDITOR variable") | ||
|
||
|
||
def get_tty_filename(): | ||
if sys.platform == 'win32': | ||
return 'CON:' | ||
return '/dev/tty' | ||
|
||
|
||
def edit(filename=None, contents=None, use_tty=None, suffix=''): | ||
# editor | ||
editor = get_editor() | ||
|
||
# filename | ||
tmp = None | ||
if filename is None: | ||
tmp = tempfile.NamedTemporaryFile(suffix=suffix) | ||
filename = tmp.name | ||
|
||
try: | ||
# write contents | ||
if contents is not None: | ||
if isinstance(contents, str): | ||
contents = contents.encode('utf-8') | ||
|
||
with open(filename, mode='wb') as file_stream: | ||
file_stream.write(contents) | ||
|
||
# args | ||
args = shlex.split(editor) +\ | ||
get_editor_args(os.path.basename(os.path.realpath(editor))) +\ | ||
[filename] | ||
|
||
# stdout | ||
stdout = None | ||
if use_tty is None and sys.stdin.isatty() and not sys.stdout.isatty(): | ||
stdout = open(get_tty_filename(), 'wb') | ||
|
||
# call editor | ||
proc = subprocess.Popen(args, close_fds=True, stdout=stdout) | ||
proc.communicate() | ||
|
||
# read result | ||
result = None | ||
if os.path.isfile(filename): | ||
with open(filename, mode='rb') as file_stream: | ||
result = file_stream.read() | ||
|
||
# return result | ||
return result | ||
|
||
finally: | ||
# delete the temporary file for security reasons | ||
if tmp is not None: | ||
try: | ||
os.remove(tmp.name) | ||
except OSError: | ||
pass |
Oops, something went wrong.