Skip to content

Commit

Permalink
Merge pull request #120 from LanceMaverick/my-new-stache
Browse files Browse the repository at this point in the history
Implemented first draft of moustaches
  • Loading branch information
LanceMaverick committed Mar 16, 2017
2 parents 5c4adfa + dc52955 commit e29fcb9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 29 deletions.
11 changes: 10 additions & 1 deletion config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ beard_paths = [
# load
# beards = "all"
beards = [
"postcats",
"debugbeard",
# "pdfpreviewbeard", # From git (above)
]

stache_paths = [
"moustaches",
]

staches = [
"postcats",
]


db_url = "sqlite:///skybeard-2.db"
db_bin_path = "./db_binary_entries"

Expand Down
24 changes: 24 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ def delegator_beard_gen(beards):
per_chat_id(), create_open, beard, timeout=beard._timeout)


def find_last_child(path):
return Path(str(path).replace(str(path.parent)+"/", ""))


def load_stache(stache_name, possible_dirs):
for dir_ in possible_dirs:
path = Path(dir_).resolve()
python_path = path.parent
with PythonPathContext(str(python_path)):
module_spec = importlib.util.find_spec(
"{}.{}".format(str(find_last_child(path)), stache_name))

if module_spec:
foo = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(foo)

return

raise Exception("No stache with name {} found".format(stache_name))


def load_beard(beard_name, possible_dirs):
for beard_path in possible_dirs:
full_python_path = Path(get_literal_path(beard_path)).resolve()
Expand Down Expand Up @@ -94,6 +115,9 @@ def main(config):
else:
beards_to_load = config.beards

for stache in config.staches:
load_stache(stache, config.stache_paths)

for possible_beard in beards_to_load:
# If possible, import the beard through setup_beard.py
load_beard(possible_beard, config.beard_paths)
Expand Down
8 changes: 6 additions & 2 deletions beards/postcats/__init__.py → moustaches/postcats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# show spacecats test plugin
# Adapted from work by LanceMaverick
"""
A Skybeard moustache that shows spacecats on request
Adapted from work by LanceMaverick
"""
from os.path import basename
import random
import logging
Expand Down
28 changes: 2 additions & 26 deletions utils/newbeard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from shutil import copytree

import stringcase
from utils import make_init_text


def make_readme(dir_, name):
Expand All @@ -16,32 +16,8 @@ def make_readme(dir_, name):
def make_init(dir_, name):
python_path = dir_ / Path("python/{}".format(name))
python_path.mkdir(parents=True)
init_text = '''
from skybeard.beards import BeardChatHandler
from skybeard.decorators import onerror
from skybeard.utils import get_args

class {beardclassname}(BeardChatHandler):
__userhelp__ = """Default help message."""
__commands__ = [
# command, callback coro, help text
("echo", 'echo', 'Echos arguments. e.g. <code>/echo [arg1 [arg2 ... ]]</code>')
]
# __init__ is implicit
@onerror
async def echo(self, msg):
args = get_args(msg)
if args:
await self.sender.sendMessage("Args: {{}}".format(args))
else:
await self.sender.sendMessage("No arguments given.")
'''.strip().format(beardclassname=stringcase.pascalcase(name))
init_text = make_init_text(name)

with (python_path / Path("__init__.py")).open("w") as f:
f.write(init_text)
Expand Down
35 changes: 35 additions & 0 deletions utils/newstache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python

import argparse
from pathlib import Path

from utils import make_init_text


def make_file(name, filename=None):
if filename is not None:
path = Path(filename)
else:
path = Path(name+".py")

init_text = make_init_text(name)

with path.open("w") as f:
f.write(init_text)

print("Moustache created as: {}".format(str(path)))


def main():
parser = argparse.ArgumentParser(
description='Create new moustache.')
parser.add_argument('name', help="Name of beard.")
parser.add_argument('-f', '--filename', type=str, default=None,
help="Optionally specify filename.")

parsed = parser.parse_args()
make_file(parsed.name, parsed.filename)


if __name__ == '__main__':
main()
34 changes: 34 additions & 0 deletions utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pathlib import Path

import stringcase


def make_init_text(name):
init_text = '''
from skybeard.beards import BeardChatHandler
from skybeard.decorators import onerror
from skybeard.utils import get_args
class {beardclassname}(BeardChatHandler):
__userhelp__ = """Default help message."""
__commands__ = [
# command, callback coro, help text
("echo", 'echo', 'Echos arguments. e.g. <code>/echo [arg1 [arg2 ... ]]</code>')
]
# __init__ is implicit
@onerror
async def echo(self, msg):
args = get_args(msg)
if args:
await self.sender.sendMessage("Args: {{}}".format(args))
else:
await self.sender.sendMessage("No arguments given.")
'''.strip().format(beardclassname=stringcase.pascalcase(name))

return init_text

0 comments on commit e29fcb9

Please sign in to comment.