-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored iranzo/stampython #29
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,10 +46,8 @@ def run_migrations_offline(): | |
|
||
""" | ||
|
||
# if a database path was provided, override the one in alembic.ini | ||
database = context.get_x_argument(as_dictionary=True).get('database') | ||
if database: | ||
url = "sqlite:///%s" % database | ||
if database := context.get_x_argument(as_dictionary=True).get('database'): | ||
url = f"sqlite:///{database}" | ||
else: | ||
url = config.get_main_option("sqlalchemy.url") | ||
|
||
|
@@ -71,10 +69,8 @@ def run_migrations_online(): | |
# get the alembic section of the config file | ||
ini_section = config.get_section(config.config_ini_section) | ||
|
||
# if a database path was provided, override the one in alembic.ini | ||
database = context.get_x_argument(as_dictionary=True).get('database') | ||
if database: | ||
ini_section['sqlalchemy.url'] = "sqlite:///%s" % database | ||
if database := context.get_x_argument(as_dictionary=True).get('database'): | ||
ini_section['sqlalchemy.url'] = f"sqlite:///{database}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
else: | ||
ini_section = config.get_section(config.config_ini_section) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,18 @@ def init(): | |
Initializes module | ||
:return: List of triggers for plugin | ||
""" | ||
triggers = ["^/admin"] | ||
return triggers | ||
return ["^/admin"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def run(message): # do not edit this line | ||
def run(message): # do not edit this line | ||
Comment on lines
-28
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
""" | ||
Executes plugin | ||
:param message: message to run against | ||
:return: | ||
""" | ||
text = stampy.stampy.getmsgdetail(message)["text"] | ||
if text and stampy.stampy.is_owner_or_admin(message): | ||
if text.split()[0].lower()[0:6] == "/admin": | ||
if text.split()[0].lower()[:6] == "/admin": | ||
admincommands(message) | ||
return | ||
|
||
|
@@ -89,8 +88,7 @@ def admincommands(message): | |
changenmastertoken(message=message) | ||
elif word == "slave": | ||
try: | ||
token = texto.split(' ')[3] | ||
if token: | ||
if token := texto.split(' ')[3]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
chanlinkslave(message=message, token=token) | ||
except: | ||
pass | ||
|
@@ -120,8 +118,8 @@ def changenmastertoken(message): | |
if not stampy.plugin.config.config(key='link-master', default=False, gid=chat_id): | ||
charset = string.letters + string.digits | ||
size = 20 | ||
token = ''.join((random.choice(charset)) for x in range(size)) | ||
generatedtoken = "%s:%s" % (chat_id, token) | ||
token = ''.join((random.choice(charset)) for _ in range(size)) | ||
generatedtoken = f"{chat_id}:{token}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
stampy.plugin.config.setconfig(key='link-master', gid=chat_id, | ||
value=generatedtoken) | ||
logger.debug(msg=_L("Generated token %s for channel %s") % (token, chat_id)) | ||
|
@@ -205,9 +203,9 @@ def chanunlink(message): | |
|
||
chat_id = msgdetail["chat_id"] | ||
message_id = msgdetail["message_id"] | ||
masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id) | ||
|
||
if masterid: | ||
if masterid := stampy.plugin.config.config( | ||
key='link', default=False, gid=chat_id | ||
): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# Delete link from slave | ||
stampy.plugin.config.deleteconfig(key='link', gid=chat_id) | ||
|
||
|
@@ -242,14 +240,14 @@ def chanshowslave(message): | |
|
||
chat_id = msgdetail["chat_id"] | ||
message_id = msgdetail["message_id"] | ||
masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id) | ||
|
||
if masterid: | ||
if masterid := stampy.plugin.config.config( | ||
key='link', default=False, gid=chat_id | ||
): | ||
# We've a master channel, report it | ||
text = _("This channel %s is slave of channel %s") % (chat_id, masterid) | ||
else: | ||
# We nee to check database to see if we've any slave | ||
sql = "SELECT id from config where key='link' and value='%s'" % chat_id | ||
sql = f"SELECT id from config where key='link' and value='{chat_id}'" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
cur = stampy.stampy.dbsql(sql=sql) | ||
|
||
text = _("Defined slaves:\n") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,21 +20,19 @@ def init(): | |
Initializes module | ||
:return: List of triggers for plugin | ||
""" | ||
triggers = ["^/alias"] | ||
return triggers | ||
return ["^/alias"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def run(message): # do not edit this line | ||
def run(message): # do not edit this line | ||
Comment on lines
-27
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
""" | ||
Executes plugin | ||
:param message: message to run against | ||
:return: | ||
""" | ||
logger = logging.getLogger(__name__) | ||
logger.debug(msg=_L("Processing plugin: Code: %s") % __file__) | ||
text = stampy.stampy.getmsgdetail(message)["text"] | ||
if text: | ||
if text.split()[0].lower()[0:6] == "/alias": | ||
if text := stampy.stampy.getmsgdetail(message)["text"]: | ||
if text.split()[0].lower()[:6] == "/alias": | ||
aliascommands(message) | ||
return | ||
|
||
|
@@ -125,8 +123,8 @@ def deletealias(word, gid=0): | |
""" | ||
|
||
logger = logging.getLogger(__name__) | ||
sql = "DELETE FROM alias WHERE key='%s' AND gid='%s';" % (word, gid) | ||
logger.debug(msg="rmalias: %s for group %s" % (word, gid)) | ||
sql = f"DELETE FROM alias WHERE key='{word}' AND gid='{gid}';" | ||
logger.debug(msg=f"rmalias: {word} for group {gid}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
stampy.stampy.dbsql(sql) | ||
return | ||
|
||
|
@@ -157,7 +155,7 @@ def listalias(word=False, gid=0): | |
text = _("%s has an alias %s") % (word, value) | ||
|
||
else: | ||
sql = "select key,value from alias WHERE gid='%s' ORDER BY key ASC;" % gid | ||
sql = f"select key,value from alias WHERE gid='{gid}' ORDER BY key ASC;" | ||
Comment on lines
-160
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
cur = stampy.stampy.dbsql(sql) | ||
text = _("Defined aliases:\n") | ||
table = from_db_cursor(cur) | ||
|
@@ -178,17 +176,16 @@ def createalias(word, value, gid=0): | |
logger = logging.getLogger(__name__) | ||
if getalias(value, gid=gid) == word or word.lower() == value.lower(): | ||
logger.error(msg=_L("createalias: circular reference %s=%s for gid %s") % (word, value, gid)) | ||
else: | ||
if not getalias(word, gid) or getalias(word, gid) == word: | ||
# Removing duplicates on karma DB and add | ||
# the previous values | ||
old = stampy.plugin.karma.getkarma(word=word, gid=gid) | ||
stampy.plugin.karma.updatekarma(word=word, change=-old, gid=gid) | ||
stampy.plugin.karma.updatekarma(word=value, change=old, gid=gid) | ||
sql = "INSERT INTO alias(key, value, gid) VALUES('%s','%s', '%s');" % (word, value, gid) | ||
logger.debug(msg="createalias: %s=%s for gid %s" % (word, value, gid)) | ||
stampy.stampy.dbsql(sql) | ||
return | ||
elif not getalias(word, gid) or getalias(word, gid) == word: | ||
# Removing duplicates on karma DB and add | ||
# the previous values | ||
old = stampy.plugin.karma.getkarma(word=word, gid=gid) | ||
stampy.plugin.karma.updatekarma(word=word, change=-old, gid=gid) | ||
stampy.plugin.karma.updatekarma(word=value, change=old, gid=gid) | ||
sql = f"INSERT INTO alias(key, value, gid) VALUES('{word}','{value}', '{gid}');" | ||
logger.debug(msg=f"createalias: {word}={value} for gid {gid}") | ||
stampy.stampy.dbsql(sql) | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return False | ||
|
||
|
||
|
@@ -205,7 +202,7 @@ def getalias(word, gid=0): | |
sql = "SELECT key,value FROM alias WHERE key='%s' AND gid='%s';" % string | ||
cur = stampy.stampy.dbsql(sql) | ||
value = cur.fetchone() | ||
logger.debug(msg="getalias: %s for gid %s" % (word, gid)) | ||
logger.debug(msg=f"getalias: {word} for gid {gid}") | ||
Comment on lines
-208
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
try: | ||
# Get value from SQL query | ||
|
@@ -218,6 +215,4 @@ def getalias(word, gid=0): | |
# We can define recursive aliases, so this will return the ultimate one | ||
if value: | ||
return getalias(word=value, gid=gid) | ||
if word: | ||
return word.lower() | ||
return False | ||
return word.lower() if word else False |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,16 +28,15 @@ def init(): | |
return triggers | ||
|
||
|
||
def run(message): # do not edit this line | ||
def run(message): # do not edit this line | ||
""" | ||
Executes plugin | ||
:param message: message to run against | ||
:return: | ||
""" | ||
code = None | ||
text = stampy.stampy.getmsgdetail(message)["text"] | ||
if text: | ||
if text.split()[0].lower()[0:6] == "/autok": | ||
if text := stampy.stampy.getmsgdetail(message)["text"]: | ||
if text.split()[0].lower()[:6] == "/autok": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
code = True | ||
autokcommands(message) | ||
autokarmawords(message) | ||
|
@@ -135,15 +134,11 @@ def getautok(key, gid=0): | |
""" | ||
|
||
logger = logging.getLogger(__name__) | ||
sql = "SELECT key,value FROM autokarma WHERE key='%s' AND gid='%s';" % (key, gid) | ||
sql = f"SELECT key,value FROM autokarma WHERE key='{key}' AND gid='{gid}';" | ||
cur = stampy.stampy.dbsql(sql) | ||
data = cur.fetchall() | ||
value = [] | ||
for row in data: | ||
# Fill valid values | ||
value.append(row[1]) | ||
|
||
logger.debug(msg="getautok: %s - %s for gid %s" % (key, value, gid)) | ||
value = [row[1] for row in data] | ||
logger.debug(msg=f"getautok: {key} - {value} for gid {gid}") | ||
Comment on lines
-138
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
return value | ||
|
||
|
@@ -158,15 +153,11 @@ def getautokeywords(gid=0): | |
if gid is False: | ||
sql = "SELECT distinct key FROM autokarma;" | ||
else: | ||
sql = "SELECT distinct key FROM autokarma WHERE gid='%s';" % gid | ||
sql = f"SELECT distinct key FROM autokarma WHERE gid='{gid}';" | ||
cur = stampy.stampy.dbsql(sql) | ||
data = cur.fetchall() | ||
value = [] | ||
for row in data: | ||
# Fill valid values | ||
value.append(row[0]) | ||
|
||
logger.debug(msg="getautokeywords: %s for gid %s" % (value, gid)) | ||
value = [row[0] for row in data] | ||
logger.debug(msg=f"getautokeywords: {value} for gid {gid}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
return value | ||
|
||
|
@@ -184,8 +175,8 @@ def createautok(word, value, gid=0): | |
if value in getautok(word): | ||
logger.error(msg=_L("createautok: autok pair %s - %s for gid %s already exists") % (word, value, gid)) | ||
else: | ||
sql = "INSERT INTO autokarma(key, value, gid) VALUES('%s','%s', '%s');" % (word, value, gid) | ||
logger.debug(msg="createautok: %s=%s for gid %s" % (word, value, gid)) | ||
sql = f"INSERT INTO autokarma(key, value, gid) VALUES('{word}','{value}', '{gid}');" | ||
logger.debug(msg=f"createautok: {word}={value} for gid {gid}") | ||
Comment on lines
-187
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
stampy.stampy.dbsql(sql) | ||
return True | ||
return False | ||
|
@@ -201,8 +192,8 @@ def deleteautok(key, value, gid=0): | |
""" | ||
|
||
logger = logging.getLogger(__name__) | ||
sql = "DELETE FROM autokarma WHERE key='%s' and value='%s' and gid='%s';" % (key, value, gid) | ||
logger.debug(msg="rmautok: %s=%s for gid %s" % (key, value, gid)) | ||
sql = f"DELETE FROM autokarma WHERE key='{key}' and value='{value}' and gid='{gid}';" | ||
logger.debug(msg=f"rmautok: {key}={value} for gid {gid}") | ||
Comment on lines
-204
to
+197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
stampy.stampy.dbsql(sql) | ||
return True | ||
|
||
|
@@ -261,9 +252,7 @@ def autokarmawords(message): | |
for autok in keywords: | ||
if autok in text_to_process: | ||
# If trigger word is there, add the triggered action | ||
for word in getautok(key=autok, gid=gid): | ||
wordadd.append(word + "++") | ||
|
||
wordadd.extend(f"{word}++" for word in getautok(key=autok, gid=gid)) | ||
Comment on lines
-264
to
+256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if wordadd: | ||
# Reduce text in message to just the words we encountered to optimize | ||
msgdetail["text"] = " ".join(wordadd) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,19 +17,18 @@ def init(): | |
Initializes module | ||
:return: List of triggers for plugin | ||
""" | ||
triggers = ["^/quit"] | ||
return triggers | ||
return ["^/quit"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def run(message): # do not edit this line | ||
def run(message): # do not edit this line | ||
Comment on lines
-24
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
""" | ||
Executes plugin | ||
:param message: message to run against | ||
:return: | ||
""" | ||
text = stampy.stampy.getmsgdetail(message)["text"] | ||
if text and stampy.stampy.is_owner_or_admin(message): | ||
if text.split()[0].lower()[0:6] == "/quit": | ||
if text.split()[0].lower()[:6] == "/quit": | ||
basecommands(message) | ||
return | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,31 +20,30 @@ def init(): | |
Initializes module | ||
:return: List of triggers for plugin | ||
""" | ||
triggers = ["^/cn"] | ||
return triggers | ||
return ["^/cn"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def run(message): # do not edit this line | ||
def run(message): # do not edit this line | ||
""" | ||
Executes plugin | ||
:param message: message to run against | ||
:return: | ||
""" | ||
text = stampy.stampy.getmsgdetail(message)["text"] | ||
if text: | ||
if text := stampy.stampy.getmsgdetail(message)["text"]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if text.split()[0].lower() == "/cn": | ||
cn(message=message) | ||
return | ||
|
||
|
||
def help(message): # do not edit this line | ||
def help(message): # do not edit this line | ||
""" | ||
Returns help for plugin | ||
:param message: message to process | ||
:return: help text | ||
""" | ||
commandtext = _("Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n") | ||
return commandtext | ||
return _( | ||
"Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n" | ||
) | ||
Comment on lines
-40
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def cn(message): | ||
|
@@ -63,7 +62,7 @@ def cn(message): | |
message_id = msgdetail["message_id"] | ||
who_un = msgdetail["who_un"] | ||
|
||
logger.debug(msg=_L("Command: %s by %s" % (texto, who_un))) | ||
logger.debug(msg=_L(f"Command: {texto} by {who_un}")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
# We might be have been given no command, just stock | ||
try: | ||
|
@@ -74,7 +73,7 @@ def cn(message): | |
if not command: | ||
url = "https://api.chucknorris.io/jokes/random" | ||
else: | ||
url = "https://api.chucknorris.io/jokes/search?query=%s" % command | ||
url = f"https://api.chucknorris.io/jokes/search?query={command}" | ||
|
||
text = "``` " | ||
# we might get more than one result | ||
|
@@ -90,10 +89,7 @@ def cn(message): | |
totalelem = len(result['result']) | ||
except: | ||
totalelem = 0 | ||
if totalelem > 1: | ||
elem = random.randint(0, totalelem - 1) | ||
else: | ||
elem = 0 | ||
elem = random.randint(0, totalelem - 1) if totalelem > 1 else 0 | ||
text += result['result'][elem]['value'] | ||
else: | ||
text += "Chuck Norris didn't said a word about it." | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
run_migrations_offline
refactored with the following changes:use-named-expression
)replace-interpolation-with-fstring
)This removes the following comments ( why? ):