]> git.cworth.org Git - turbot/blob - turbot/rot.py
Add notes on how to update the Google sheets credentials
[turbot] / turbot / rot.py
1 import re
2
3 def rot_string(str, n=13):
4     """Return a rotated version of a string
5
6     Specifically, this functions returns a version of the input string
7     where each uppercase letter has been advanced 'n' positions in the
8     alphabet (wrapping around). Lowercase letters and any non-alphabetic
9     characters will be unchanged."""
10
11     result = ''
12     for letter in str:
13         if letter.isupper():
14             result += chr(ord("A") + (ord(letter) - ord("A") + n) % 26)
15         else:
16             result += letter
17     return result
18
19 def rot(args):
20     """Return one or more rotated versions of a sring
21
22     The arg string should be as follows:
23
24         [count|*] String to be rotated
25
26     That is, the first word of the string is an optional number (or
27     the character '*'). If this is a number it indicates an amount to
28     rotate each character in the string. If the count is '*' or is not
29     present, then the string will be rotated through all possible 25
30     values.
31
32     The result of the rotation is returned as a string (with Slack
33     formatting)."""
34
35     match = re.match(r'^([0-9]+|\*) (.*)$', args)
36     if (match):
37         try:
38             count = int(match.group(1))
39         except ValueError:
40             count = None
41         text = match.group(2)
42     else:
43         count = None
44         text = args
45
46     text = text.upper()
47
48     reply = "```/rot {} {}\n".format(count if count else '*', text)
49
50     if count:
51         reply += rot_string(text, count)
52     else:
53         reply += "\n".join(["{:02d} ".format(count) + rot_string(text, count)
54                             for count in range(1, 26)])
55
56     reply += "```"
57
58     return reply