Skip to content

Commit

Permalink
add setvar , delvar and getvar
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Aug 23, 2020
1 parent 2bbc8ac commit 83dce0d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
47 changes: 15 additions & 32 deletions userge/core/types/raw/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,50 +116,33 @@ async def enable(self) -> List[str]:
""" enable all commands in the plugin """
if self.is_enabled:
return []
enabled: List[str] = []
for flt in self.commands + self.filters:
tmp = await flt.enable()
if tmp:
enabled.append(tmp)
if enabled:
_LOG.info(_LOG_STR, f"enabled {self}")
return enabled
return await _do_it(self, 'enable')

async def disable(self) -> List[str]:
""" disable all commands in the plugin """
if not self.is_enabled:
return []
disabled: List[str] = []
for flt in self.commands + self.filters:
tmp = await flt.disable()
if tmp:
disabled.append(tmp)
if disabled:
_LOG.info(_LOG_STR, f"disabled {self}")
return disabled
return await _do_it(self, 'disable')

async def load(self) -> List[str]:
""" load all commands in the plugin """
if self.is_loaded:
return []
loaded: List[str] = []
for flt in self.commands + self.filters:
tmp = await flt.load()
if tmp:
loaded.append(tmp)
if loaded:
_LOG.info(_LOG_STR, f"loaded {self}")
return loaded
return await _do_it(self, 'load')

async def unload(self) -> List[str]:
""" unload all commands in the plugin """
if not self.is_loaded:
return []
unloaded: List[str] = []
for flt in self.commands + self.filters:
tmp = await flt.unload()
if tmp:
unloaded.append(tmp)
if unloaded:
_LOG.info(_LOG_STR, f"unloaded {self}")
return unloaded
return await _do_it(self, 'unload')


async def _do_it(plg: Plugin, work_type: str) -> List[str]:
done: List[str] = []
for flt in plg.commands + plg.filters:
tmp = await getattr(flt, work_type)()
if tmp:
done.append(tmp)
if done:
_LOG.info(_LOG_STR, f"{work_type.rstrip('e')}ed {plg}")
return done
80 changes: 80 additions & 0 deletions userge/plugins/tools/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async def _init() -> None:
'usage': "{tr}restart [flag | flags]",
'examples': "{tr}restart -t -d"}, del_pre=True, allow_channels=False)
async def restart_(message: Message):
""" restart userge """
await message.edit("Restarting Userge Services", log=__name__)
LOG.info("USERGE Services - Restart initiated")
if 't' in message.flags:
Expand All @@ -59,6 +60,7 @@ async def restart_(message: Message):

@userge.on_cmd("shutdown", about={'header': "shutdown userge :)"}, allow_channels=False)
async def shutdown_(message: Message) -> None:
""" shutdown userge """
await message.edit("`shutting down ...`")
if Config.HEROKU_APP:
try:
Expand All @@ -78,6 +80,7 @@ async def shutdown_(message: Message) -> None:
'usage': "{tr}die [flags]",
'examples': ["{tr}die", "{tr}die -t5"]}, allow_channels=False)
async def die_(message: Message) -> None:
""" set offline timeout to die userge """
global MAX_IDLE_TIME, RUN_DYNO_SAVER # pylint: disable=global-statement
if not Config.HEROKU_APP:
await message.err("`heroku app not detected !`")
Expand All @@ -93,6 +96,9 @@ async def die_(message: Message) -> None:
del_in=5, log=__name__)
return
time_in_min = int(message.flags.get('-t', 5))
if time_in_min < 5:
await message.err(f"`please set higher value [{time_in_min}] !`")
return
MAX_IDLE_TIME = time_in_min * 60
SAVED_SETTINGS.update_one({'_id': 'DYNO_SAVER'},
{"$set": {'on': True, 'timeout': MAX_IDLE_TIME}}, upsert=True)
Expand All @@ -101,10 +107,84 @@ async def die_(message: Message) -> None:
RUN_DYNO_SAVER = asyncio.get_event_loop().create_task(_dyno_saver_worker())


@userge.on_cmd("setvar", about={
'header': "set var in heroku",
'usage': "{tr}setvar [var_name] [var_data]",
'examples': "{tr}setvar WORKERS 4"})
async def setvar_(message: Message) -> None:
""" set var (heroku) """
if not Config.HEROKU_APP:
await message.err("`heroku app not detected !`")
return
if not message.input_str:
await message.err("`input needed !`")
return
var_name, var_data = message.input_str.split(maxsplit=1)
if not var_data:
await message.err("`var data needed !`")
return
var_name = var_name.strip()
var_data = var_data.strip()
heroku_vars = Config.HEROKU_APP.config()
if var_name in heroku_vars:
await CHANNEL.log(
f"#HEROKU_VAR #SET #UPDATED\n\n`{var_name}` = `{var_data}`")
await message.edit(f"`var {var_name} updated and forwarded to log channel !`", del_in=3)
else:
await CHANNEL.log(
f"#HEROKU_VAR #SET #ADDED\n\n`{var_name}` = `{var_data}`")
await message.edit(f"`var {var_name} added and forwarded to log channel !`", del_in=3)
heroku_vars[var_name] = var_data


@userge.on_cmd("delvar", about={
'header': "del var in heroku",
'usage': "{tr}delvar [var_name]",
'examples': "{tr}delvar WORKERS"})
async def delvar_(message: Message) -> None:
""" del var (heroku) """
if not Config.HEROKU_APP:
await message.err("`heroku app not detected !`")
return
if not message.input_str:
await message.err("`var name needed !`")
return
var_name = message.input_str.strip()
heroku_vars = Config.HEROKU_APP.config()
if var_name not in heroku_vars:
await message.err(f"`var {var_name} not found !`")
return
await CHANNEL.log(f"#HEROKU_VAR #DEL\n\n`{var_name}` = `{heroku_vars[var_name]}`")
await message.edit(f"`var {var_name} deleted and forwarded to log channel !`", del_in=3)
del heroku_vars[var_name]


@userge.on_cmd("getvar", about={
'header': "get var in heroku",
'usage': "{tr}getvar [var_name]",
'examples': "{tr}getvar WORKERS 4"})
async def getvar_(message: Message) -> None:
""" get var (heroku) """
if not Config.HEROKU_APP:
await message.err("`heroku app not detected !`")
return
if not message.input_str:
await message.err("`var name needed !`")
return
var_name = message.input_str.strip()
heroku_vars = Config.HEROKU_APP.config()
if var_name not in heroku_vars:
await message.err(f"`var {var_name} not found !`")
return
await CHANNEL.log(f"#HEROKU_VAR #GET\n\n`{var_name}` = `{heroku_vars[var_name]}`")
await message.edit(f"`var {var_name} forwarded to log channel !`", del_in=3)


@userge.on_cmd("sleep (\\d+)", about={
'header': "sleep userge :P",
'usage': "{tr}sleep [timeout in seconds]"}, allow_channels=False)
async def sleep_(message: Message) -> None:
""" sleep userge """
seconds = int(message.matches[0].group(1))
await message.edit(f"`sleeping {seconds} seconds...`")
asyncio.get_event_loop().create_task(_slp_wrkr(seconds))
Expand Down

0 comments on commit 83dce0d

Please sign in to comment.