Skip to content

Commit

Permalink
Merge pull request #88 from timgates42/backnon
Browse files Browse the repository at this point in the history
Background nonwords
  • Loading branch information
timgates42 committed Mar 26, 2020
2 parents d2966b5 + 8f89e4e commit 4f80f74
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 42 deletions.
27 changes: 2 additions & 25 deletions app/meticulous/_multiworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from meticulous._controller import Controller
from meticulous._input import get_confirmation
from meticulous._input_queue import get_input_queue
from meticulous._processrepo import interactive_task_collect_nonwords
from meticulous._processrepo import processrepo_handlers
from meticulous._storage import get_json_value, set_json_value
from meticulous._submit import submit_handlers
from meticulous._threadpool import get_pool
Expand Down Expand Up @@ -45,40 +45,17 @@ def get_handlers():
Obtain the handler factory lookup
"""
handlers = {
"collect_nonwords": collect_nonwords,
"cleanup": cleanup,
"prompt_quit": prompt_quit,
"wait_threadpool": wait_threadpool,
"force_quit": force_quit,
}
handlers.update(addrepo_handlers())
handlers.update(processrepo_handlers())
handlers.update(submit_handlers())
return handlers


def collect_nonwords(context):
"""
Task to collect nonwords from a repository until a typo is found or the
repository is clean
"""

def handler():
target = context.controller.target
reponame = context.taskjson["reponame"]
if reponame in get_json_value("repository_map", {}):
interactive_task_collect_nonwords(reponame, target)
context.controller.add(
{
"name": "submit",
"interactive": True,
"priority": 50,
"reponame": reponame,
}
)

return handler


def cleanup(context):
"""
Task to collect nonwords from a repository until a typo is found or the
Expand Down
18 changes: 9 additions & 9 deletions app/meticulous/_nonword.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
from pathlib import Path

from plumbum import FG, local
from plumbum import local

from meticulous._github import check_forked, checkout, create_pr, fork

Expand Down Expand Up @@ -76,18 +76,18 @@ def update_nonwords(target):
git = local["git"]
pyexe = local[sys.executable]
with local.cwd(str(path.parent)):
_ = git["add", path.name] & FG
_ = git["commit", "-m", "update nonwords"] & FG
_ = git["pull", "--no-edit"] & FG
git("add", path.name)
git("commit", "-m", "update nonwords")
git("pull", "--no-edit")
with local.cwd(str(path.parent / "app")):
_ = pyexe["-m", "unanimous"] & FG
num = random.randrange(100000, 999999) # noqa: DUO102 # nosec
pyexe("-m", "unanimous")
num = random.randrange(100000, 999999) # noqa: S311,DUO102 # nosec
to_branch = "master"
from_branch = f"nonwords_{num}"
with local.cwd(str(path.parent)):
_ = git["add", "."] & FG
_ = git["commit", "-m", "update nonwords"] & FG
_ = git["push", "origin", f"{to_branch}:{from_branch}"] & FG
git("add", ".")
git("commit", "-m", "update nonwords")
git("push", "origin", f"{to_branch}:{from_branch}")
reponame = "unanimous"
title = "Add nonwords"
body = title
Expand Down
93 changes: 85 additions & 8 deletions app/meticulous/_processrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,94 @@ class NonwordState: # pylint: disable=too-few-public-methods
Store the nonword workflow state.
"""

def __init__(self, target, word, details, repopath):
def __init__( # pylint: disable=too-many-arguments
self, target, word, details, repopath, nonword_delegate
):
self.target = target
self.word = word
self.details = details
self.repopath = repopath
self.nonword_delegate = nonword_delegate
self.done = False


def processrepo_handlers():
"""
Add handlers for processing a repository
"""
return {"collect_nonwords": collect_nonwords, "nonword_update": nonword_update}


def collect_nonwords(context):
"""
Task to collect nonwords from a repository until a typo is found or the
repository is clean
"""

def handler():
target = context.controller.target
reponame = context.taskjson["reponame"]
if reponame in get_json_value("repository_map", {}):
interactive_task_collect_nonwords(
reponame,
target,
nonword_delegate=noninteractive_nonword_delegate(context),
)
context.controller.add(
{
"name": "submit",
"interactive": True,
"priority": 50,
"reponame": reponame,
}
)

return handler


def nonword_update(context):
"""
Task to push nonwords upstream
"""

def handler():
target = context.controller.target
update_nonwords(target)

return handler


def noninteractive_nonword_delegate(context):
"""
Obtain a basic interactive nonword update
"""

def handler():
context.controller.add({"name": "nonword_update", "interactive": False})

return handler


def interactive_nonword_delegate(target):
"""
Obtain a basic interactive nonword update
"""

def handler():
pullreq = update_nonwords(target)
print(f"Created PR #{pullreq.number} view at" f" {pullreq.html_url}")

return handler


def interactive_task_collect_nonwords( # pylint: disable=unused-argument
reponame, target
reponame, target, nonword_delegate=None
):
"""
Saves nonwords until a typo is found
"""
if nonword_delegate is None:
nonword_delegate = interactive_nonword_delegate(target)
key = "repository_map"
repository_map = get_json_value(key, {})
repodir = repository_map[reponame]
Expand All @@ -57,7 +131,11 @@ def interactive_task_collect_nonwords( # pylint: disable=unused-argument
my_engine.callbacks.replace([check_websearch, is_nonword, is_typo, what_now])
for word in words:
state = NonwordState(
target=target, word=word, details=jsonobj[word], repopath=repodirpath
target=target,
word=word,
details=jsonobj[word],
repopath=repodirpath,
nonword_delegate=nonword_delegate,
)
try:
my_engine.process([state])
Expand Down Expand Up @@ -95,7 +173,7 @@ def check_websearch(obj, eng):
show_word(obj.word, obj.details)
if suggestion.is_nonword:
if get_confirmation("Web search suggests it is a non-word, agree?"):
handle_nonword(obj.word, obj.target)
handle_nonword(obj.word, obj.target, obj.nonword_delegate)
eng.halt("found nonword")
if suggestion.is_typo:
if suggestion.replacement:
Expand Down Expand Up @@ -128,7 +206,7 @@ def is_nonword(obj, eng):
"""
show_word(obj.word, obj.details)
if get_confirmation("Is non-word?"):
handle_nonword(obj.word, obj.target)
handle_nonword(obj.word, obj.target, obj.nonword_delegate)
eng.halt("found nonword")


Expand Down Expand Up @@ -210,14 +288,13 @@ def perform_replacement(line, word, replacement):
return "".join(result)


def handle_nonword(word, target): # pylint: disable=unused-argument
def handle_nonword(word, target, nonword_delegate): # pylint: disable=unused-argument
"""
Handle a nonword
"""
add_non_word(word, target)
if check_nonwords(target):
pullreq = update_nonwords(target)
print(f"Created PR #{pullreq.number} view at" f" {pullreq.html_url}")
nonword_delegate()


def handle_typo(word, details, repopath): # pylint: disable=unused-argument
Expand Down

0 comments on commit 4f80f74

Please sign in to comment.