Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pybites/karmabot
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Sep 20, 2018
2 parents 50d6f79 + 0f04b1f commit e23a2d3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,4 +8,4 @@ PyBites Article: [From Script to Project part 1. - Building a Karma Bot with Pyt

![karma example](https://pybit.es/images/karma_example.png)

__Update 06.09.2018__: karmabot now supports commands, add them to the `commands/` subdirectory, then import it to `bot/slack.py` and add it to `BOT_COMMANDS`.
__Update 06.09.2018__: karmabot now supports commands, add them to the `commands/` subdirectory, then import it to `bot/slack.py` and add it to `BOT_COMMANDS`. More info: https://www.youtube.com/watch?v=Yx9qYl6lmzM&t=2s
2 changes: 2 additions & 0 deletions bot/slack.py
Expand Up @@ -9,6 +9,7 @@
# bot commands
from commands.add import add_command
from commands.age import pybites_age
from commands.doc import doc_command
from commands.help import create_commands_table
from commands.feed import get_pybites_last_entries
from commands.score import get_karma, top_karma
Expand All @@ -33,6 +34,7 @@
tip=get_random_tip,
topchannels=get_recommended_channels)
PRIVATE_BOT_COMMANDS = dict(feed=get_pybites_last_entries, # takes up space
doc=doc_command,
help=create_commands_table, # have everywhere
karma=get_karma,
)
Expand Down
82 changes: 82 additions & 0 deletions commands/doc.py
@@ -0,0 +1,82 @@
"""a karambot pydoc interface.
"""

import pydoc
import contextlib
import io

MSG_APOLOGY = '''Sorry {user}, I got nothing for "{text}".
I'll do a keyword search for "{text}" if you add -k before {text}.
Try "topics" or "modules" for more general help.
'''

MSG_FOUNDIT = '''Good news {user}, I found the following about {text}:
```
{result}
```
'''


MSG_HELP = '''
pydoc [-k keyword] [module_path_or_topic|topics|modules|help]
You can use pydoc to look up all sorts of pythonic things!
Use this to get the docstring for a module:
pydoc list
Or do a keyword search to get a list of modules that match:
pydoc -k keyword
Get a list of modules:
pydoc modules
A list of python language topics, super interesting:
pydoc topics
And information about the specific listed topics:
pydoc LOOPING
'''

def doc_command(**kwargs: dict) -> str:
'''Browse and search python documentation, "pydoc help"
'''
user, text = kwargs.get('user'), kwargs.get('text')

if len(text) == 0 or text.lower() == 'help':
return MSG_HELP

apropos = '-k' in text

if '-' in text and not apropos: # weed out switches that aren't -k
return MSG_HELP

output = io.StringIO()
with contextlib.redirect_stdout(output):
if apropos:
pydoc.apropos(text.partition('-k')[-1])
else:
help(text)
result = output.getvalue()

if result.startswith('No'):
return MSG_APOLOGY.format(user=user, text=text)

return MSG_FOUNDIT.format(user=user, text=text, result=result)

if __name__ == '__main__':
import sys

kwargs = { 'user': 'Erik',
'channel': '#unix',
'text':' '.join(sys.argv[1:])}

print(doc_command(**kwargs))

0 comments on commit e23a2d3

Please sign in to comment.