Skip to content

Commit

Permalink
Checkstyle with flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanderjmw committed Nov 18, 2020
1 parent d607df9 commit 438a000
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
11 changes: 11 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[flake8]
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist,
venv/*,

max-complexity = 10
9 changes: 3 additions & 6 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
- name: Set up Python 3.7.4
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.7.4
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -30,7 +30,4 @@ jobs:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
68 changes: 42 additions & 26 deletions laundrybot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'dryer2': 'Dryer 2'
}


# Building menu for every occasion
def build_menu(buttons, n_cols, header_buttons=None, footer_buttons=None):
menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
Expand All @@ -34,40 +35,46 @@ def build_menu(buttons, n_cols, header_buttons=None, footer_buttons=None):
menu.append(footer_buttons)
return InlineKeyboardMarkup(menu)


# Building emojis for every occasion
ebluediamond = emojize(":small_blue_diamond: ", use_aliases=True)
etick = emojize(":white_check_mark: ", use_aliases=True)
ecross = emojize(":x: ", use_aliases=True)
esoon = emojize(":soon: ", use_aliases=True)

# start command initializes:

# start command initializes:
def check_handler(bot, update, user_data):
user = update.message.from_user
# user = update.message.from_user
if 'pinned_level' in user_data:
level_status(bot, update, user_data,
from_pinned_level=True, new_message=True)
else:
ask_level(bot, update)

def ask_level(bot, update):
level_text = "Heyyo! I am RC4's Laundry Bot. <i>As I am currently in [BETA] mode, I can only show details for Ursa floor.</i>\n\n<b>Which laundry level do you wish to check?</b>"

def ask_level(bot, update):
level_text = "Heyyo! I am RC4's Laundry Bot. <i>As I am currently in [BETA] mode, "\
"I can only show details for Ursa floor.</i>\n\n<b>Which laundry level do you wish to check?</b>"
level_buttons = []
for level in LAUNDRY_LEVELS:
label = 'Level {}'.format(level)
data = 'set_L{}'.format(level)
buttons = InlineKeyboardButton(text=label, callback_data=data) # data callback to set_pinned_level
buttons = InlineKeyboardButton(text=label, callback_data=data) # data callback to set_pinned_level
level_buttons.append(buttons)
update.message.reply_text(text=level_text,
reply_markup=build_menu(level_buttons, 1),
parse_mode=ParseMode.HTML)


def set_pinned_level(bot, update, user_data):
query = update.callback_query
level = int(re.match('^set_L(5|8|11|14|17)$', query.data).group(1))
user_data['pinned_level'] = level

level_status(bot, update, user_data, from_pinned_level=True)


# Carves the status text for each level
def make_status_text(level_number):
laundry_data = ''
Expand All @@ -93,6 +100,7 @@ def make_status_text(level_number):
"{}\n" \
"Last updated: {}\n".format(level_number, laundry_data, current_time)


# Create the status menu which contains the help command, a pinned level number, and refresh button
def make_status_menu(level_number):
level_buttons = []
Expand All @@ -101,9 +109,9 @@ def make_status_menu(level_number):
label = 'L{}'.format(level)
data = 'check_L{}'.format(level)
if level == level_number:
# label = u'\u2022 ' + label + u' \u2022'
# label = u'\u2022 ' + label + u' \u2022'
label = ebluediamond + label

button = InlineKeyboardButton(text=label, callback_data=data)
level_buttons.append(button)

Expand All @@ -119,54 +127,63 @@ def make_status_menu(level_number):

return build_menu(level_buttons, 5, footer_buttons=refresh_button, header_buttons=help_button)


def level_status(bot, update, user_data, from_pinned_level=False, new_message=False):
query = update.callback_query
if from_pinned_level:
level = user_data['pinned_level']
else:
level = int(re.match('^check_L(5|8|11|14|17)$', query.data).group(1))

user_data['check_level'] = level

if new_message:
update.message.reply_text(text=make_status_text(level),
reply_markup=make_status_menu(level),
parse_mode=ParseMode.HTML)
else:
bot.edit_message_text(text=make_status_text(level),
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=make_status_menu(level),
parse_mode=ParseMode.HTML)
bot.edit_message_text(
text=make_status_text(level),
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=make_status_menu(level),
parse_mode=ParseMode.HTML
)


def help_menu(bot, update, user_data, from_pinned_level=False, new_message=False):
query = update.callback_query
help_text = "<b>Help</b>\n\n" + "Washer 1 and Dryer 2 accept coins\n" + etick + "= Available / Job done\n" + esoon + "= Job finishing soon\n" + ecross + "= In use\n"
help_text += "\nInformation not accurate or faced a problem? Please message @PakornUe or @Cpf05, thank you!"
help_text += "\n\nThis is a project by RC4Space's Laundry Bot Team. We appreciate your feedback as we are currently still beta-testing in Ursa before launching the college-wide implementation! :)"

help_text = "<b>Help</b>\n\n" + "Washer 1 and Dryer 2 accept coins\n" \
+ etick + "= Available / Job done\n" + esoon + "= Job finishing soon\n" + ecross + "= In use\n"

help_text += "\nInformation not accurate or faced a problem? "\
"Please message @PakornUe or @Cpf05, thank you!"
help_text += "\n\nThis is a project by RC4Space's Laundry Bot Team. "\
"We appreciate your feedback as we are currently still beta-testing "\
"in Ursa before launching the college-wide implementation! :)"

level = user_data['check_level']

help_menu_button = [InlineKeyboardButton(
text='Back',
callback_data='check_L{}'.format(level)
)]

bot.edit_message_text(text=help_text,
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=build_menu(help_menu_button, 1),
parse_mode=ParseMode.HTML)
bot.edit_message_text(
text=help_text,
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=build_menu(help_menu_button, 1),
parse_mode=ParseMode.HTML
)


def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"', update, error)


def main():
TOKEN = os.environ['RC4LAUNDRYBOT_TOKEN']
#NAME = 'nuscollegelaundrybot'
#PORT = os.environ.get('PORT')

updater = Updater(TOKEN)
dp = updater.dispatcher
Expand All @@ -183,10 +200,9 @@ def main():
pass_user_data=True))
dp.add_error_handler(error)

#updater.start_webhook(listen="0.0.0.0", port=int(PORT), url_path=TOKEN)
#updater.bot.setWebhook("https://{}.herokuapp.com/{}".format(NAME, TOKEN))
updater.start_polling()
updater.idle()


if __name__ == '__main__':
main()

0 comments on commit 438a000

Please sign in to comment.