Skip to content

Instantly share code, notes, and snippets.

@shiumachi
Last active August 7, 2019 06:00
Show Gist options
  • Save shiumachi/5cbc27ac8452195bd290ccf3ba73bc46 to your computer and use it in GitHub Desktop.
Save shiumachi/5cbc27ac8452195bd290ccf3ba73bc46 to your computer and use it in GitHub Desktop.
Bot Saying Migrate CLI

saying migrate CLI

Usage

  • Copy the output of saying list from the source saying command.
  • Run the following command with pasting the copied outputs
$ python saying_migrate.py --from-saying foo --to-saying bar - <<EOM
(paste the copied text here)
EOM

License

Apache License 2.0

Copyright

2019 Sho Shimauchi

import re
import click
def parse_one_row(text):
try:
return re.match("(.*): (.*)", text).groups()[1]
except AttributeError as ae:
if len(text) != 0:
raise AttributeError(f"Invalid Text: {text}")
else:
raise AttributeError(f"Empty Text")
def parse_saying_list(texts):
parsed_texts = list()
for text in texts:
if len(text) == 0:
continue
parsed_texts.append(parse_one_row(text))
return parsed_texts
def generate_register_text_command(to_saying, text):
return f"!{to_saying} {text}"
def generate_remove_text_command(from_saying, text):
return f"!{from_saying}_delete {text}"
def generate_migrate_commands(from_saying, to_saying, parsed_texts):
for parse_text in parsed_texts:
yield generate_register_text_command(to_saying=to_saying, text=parse_text)
yield generate_remove_text_command(from_saying=from_saying, text=parse_text)
@click.command()
@click.option("--from-saying")
@click.option("--to-saying")
@click.argument("input_file", type=click.File("r"))
def main(from_saying, to_saying, input_file):
parsed_texts = parse_saying_list([text.strip() for text in input_file])
commands = generate_migrate_commands(from_saying, to_saying, parsed_texts)
print("\n".join(commands))
if __name__ == "__main__":
main()
import pytest
class Test_parse_one_row:
@pytest.fixture
def target(self):
from saying_migrate import parse_one_row
return parse_one_row
def test_ok(self, target):
data = 'user001: "text001"'
expected = '"text001"'
result = target(data)
assert result == expected
def test_empty_data(self, target):
data = ""
with pytest.raises(AttributeError) as excinfo:
target(data)
assert excinfo.value == "Empty Text"
def test_invalid_data(self, target):
data = "invalid_data"
with pytest.raises(AttributeError) as excinfo:
target(data)
assert excinfo.value == "Invalid Text: invalid_data"
class Test_parse_saying_list:
@pytest.fixture
def target(self):
from saying_migrate import parse_saying_list
return parse_saying_list
@pytest.mark.parametrize(
"data, expected", [(["text001", "text002"], ["", ""]), (["text001", ""], [""])]
)
def test_ok(self, target, mocker, data, expected):
mocker.patch("saying_migrate.parse_one_row", return_value="")
result = target(data)
assert result == expected
class Test_generate_register_text_command:
@pytest.fixture
def target(self):
from saying_migrate import generate_register_text_command
return generate_register_text_command
def test_ok(self, target):
to_saying = "user001"
text = "text001"
expected = "!user001 text001"
result = target(to_saying, text)
assert result == expected
class Test_generate_remove_text_command:
@pytest.fixture
def target(self):
from saying_migrate import generate_remove_text_command
return generate_remove_text_command
def test_ok(self, target):
from_saying = "user001"
text = "text001"
expected = "!user001_delete text001"
result = target(from_saying, text)
assert result == expected
class Test_generate_migrate_commands:
@pytest.fixture
def target(self):
from saying_migrate import generate_migrate_commands
return generate_migrate_commands
def test_ok(self, target, mocker):
from_saying = "user001"
to_saying = "user002"
texts = ["text001", "text002"]
expected = ["register", "remove", "register", "remove"]
mocker.patch(
"saying_migrate.parse_saying_list", return_value=["parsed001", "parsed002"]
)
mocker.patch(
"saying_migrate.generate_register_text_command", return_value="register"
)
mocker.patch(
"saying_migrate.generate_remove_text_command", return_value="remove"
)
result = list(target(from_saying, to_saying, texts))
assert result == expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment