Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions stampy/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Copy link
Author

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:

This removes the following comments ( why? ):

# if a database path was provided, override the one in alembic.ini

else:
url = config.get_main_option("sqlalchemy.url")

Expand All @@ -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}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_migrations_online refactored with the following changes:

This removes the following comments ( why? ):

# if a database path was provided, override the one in alembic.ini

else:
ini_section = config.get_section(config.config_ini_section)

Expand Down
28 changes: 13 additions & 15 deletions stampy/plugin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ def init():
Initializes module
:return: List of triggers for plugin
"""
triggers = ["^/admin"]
return triggers
return ["^/admin"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function init refactored with the following changes:



def run(message): # do not edit this line
def run(message): # do not edit this line
Comment on lines -28 to +27
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

"""
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

Expand Down Expand Up @@ -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]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function admincommands refactored with the following changes:

chanlinkslave(message=message, token=token)
except:
pass
Expand Down Expand Up @@ -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}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function changenmastertoken refactored with the following changes:

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))
Expand Down Expand Up @@ -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
):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function chanunlink refactored with the following changes:

# Delete link from slave
stampy.plugin.config.deleteconfig(key='link', gid=chat_id)

Expand Down Expand Up @@ -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}'"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function chanshowslave refactored with the following changes:

cur = stampy.stampy.dbsql(sql=sql)

text = _("Defined slaves:\n")
Expand Down
43 changes: 19 additions & 24 deletions stampy/plugin/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ def init():
Initializes module
:return: List of triggers for plugin
"""
triggers = ["^/alias"]
return triggers
return ["^/alias"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function init refactored with the following changes:



def run(message): # do not edit this line
def run(message): # do not edit this line
Comment on lines -27 to +26
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

"""
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

Expand Down Expand Up @@ -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}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function deletealias refactored with the following changes:

stampy.stampy.dbsql(sql)
return

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function listalias refactored with the following changes:

cur = stampy.stampy.dbsql(sql)
text = _("Defined aliases:\n")
table = from_db_cursor(cur)
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function createalias refactored with the following changes:

return False


Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getalias refactored with the following changes:


try:
# Get value from SQL query
Expand All @@ -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
39 changes: 14 additions & 25 deletions stampy/plugin/autokarma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

code = True
autokcommands(message)
autokarmawords(message)
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getautok refactored with the following changes:

This removes the following comments ( why? ):

# Fill valid values


return value

Expand All @@ -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}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getautokeywords refactored with the following changes:

This removes the following comments ( why? ):

# Fill valid values


return value

Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function createautok refactored with the following changes:

stampy.stampy.dbsql(sql)
return True
return False
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function deleteautok refactored with the following changes:

stampy.stampy.dbsql(sql)
return True

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function autokarmawords refactored with the following changes:

if wordadd:
# Reduce text in message to just the words we encountered to optimize
msgdetail["text"] = " ".join(wordadd)
Expand Down
7 changes: 3 additions & 4 deletions stampy/plugin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ def init():
Initializes module
:return: List of triggers for plugin
"""
triggers = ["^/quit"]
return triggers
return ["^/quit"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function init refactored with the following changes:



def run(message): # do not edit this line
def run(message): # do not edit this line
Comment on lines -24 to +23
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

"""
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

Expand Down
24 changes: 10 additions & 14 deletions stampy/plugin/chuck.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,30 @@ def init():
Initializes module
:return: List of triggers for plugin
"""
triggers = ["^/cn"]
return triggers
return ["^/cn"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function init refactored with the following changes:



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"]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function help refactored with the following changes:



def cn(message):
Expand All @@ -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}"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cn refactored with the following changes:


# We might be have been given no command, just stock
try:
Expand All @@ -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
Expand All @@ -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."
Expand Down
Loading