Skip to content

Commit

Permalink
refact(utils): move count_code_lines to bot_info.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Jan 4, 2024
1 parent 02ef758 commit e70a305
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
21 changes: 19 additions & 2 deletions fcts/bot_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import glob
import os
import sys
from typing import Literal, Optional, Union

Expand All @@ -10,7 +12,6 @@
from libs.bot_classes import Axobot, MyContext
from libs.checks import checks
from libs.formatutils import FormatUtils
from utils import count_code_lines


class BotInfo(commands.Cog):
Expand All @@ -26,9 +27,25 @@ def __init__(self, bot: Axobot):

@commands.Cog.listener()
async def on_ready(self):
self.codelines = await count_code_lines()
await self.refresh_code_lines_count()


async def refresh_code_lines_count(self):
"""Count lines of Python code in the current folder
Comments and empty lines are ignored."""
count = 0
path = os.getcwd() + '/**/*.py'
for filename in glob.iglob(path, recursive=True):
if '/env/' in filename or not filename.endswith('.py'):
continue
with open(filename, 'r', encoding='utf-8') as file:
for line in file.read().split("\n"):
cleaned_line = line.strip()
if len(cleaned_line) > 2 and not cleaned_line.startswith('#') or cleaned_line.startswith('"'):
count += 1
self.codelines = count

@commands.command(name="admins")
async def admin_list(self, ctx: MyContext):
"""Get the list of the bot administrators
Expand Down
13 changes: 8 additions & 5 deletions fcts/reloads.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import importlib

from discord.ext import commands

from libs.bot_classes import Axobot, MyContext
from libs.checks.checks import is_bot_admin
from libs.bot_classes import MyContext, Axobot
from utils import count_code_lines


class Reloads(commands.Cog):
"""Cog to manage the other cogs. Even if all are disabled, this is the last one left."""
Expand All @@ -20,9 +22,10 @@ def __init__(self, bot: Axobot):
]

async def reload_cogs(self, ctx: MyContext, cogs: list[str]):
"Reload a list of cogs and python modules"
if len(cogs)==1 and cogs[0]=='all':
cogs = sorted([x.file for x in self.bot.cogs.values()])
reloaded_cogs = list()
reloaded_cogs = []
for cog in cogs:
if not cog.startswith("fcts."):
fcog = "fcts."+cog
Expand Down Expand Up @@ -50,9 +53,9 @@ async def reload_cogs(self, ctx: MyContext, cogs: list[str]):
if cog == 'utilities':
await self.bot.get_cog('Utilities').on_ready()
if len(reloaded_cogs) > 0:
await ctx.send("These cogs has successfully reloaded: {}".format(", ".join(reloaded_cogs)))
await ctx.send(f"These cogs has successfully reloaded: {', '.join(reloaded_cogs)}")
if info_cog := self.bot.get_cog("BotInfo"):
info_cog.codelines = await count_code_lines()
await info_cog.refresh_code_lines_count()

@commands.command(name="add_cog",hidden=True)
@commands.check(is_bot_admin)
Expand Down
19 changes: 0 additions & 19 deletions utils.py

This file was deleted.

0 comments on commit e70a305

Please sign in to comment.