Skip to content

Commit

Permalink
Log stats on terminal (#3312)
Browse files Browse the repository at this point in the history
* added _log_on_terminal function

* Added logic to toggle terminal logging functionality from config file

* Added possibility to disable title changes, refactor code

* Refactor tuples

* Refactor ifs to clearer syntax
  • Loading branch information
sergiopalacio authored and douglascamata committed Aug 9, 2016
1 parent 3519c31 commit b94d369
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions pokemongo_bot/cell_workers/update_title_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ class UpdateTitleStats(BaseTask):
"type": "UpdateTitleStats",
"config": {
"min_interval": 10,
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"]
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"],
}
}
You can set a logging on terminal mode like this:
Example logging on console (and disabling title change):
{
"type": "UpdateTitleStats",
"config": {
"min_interval": 10,
"stats": ["login", "uptime", "km_walked", "level_stats", "xp_earned", "xp_per_hour"],
"terminal_log": true,
"terminal_title": false
}
}
Available stats :
- login : The account login (from the credentials).
- username : The trainer name (asked at first in-game connection).
Expand Down Expand Up @@ -53,8 +65,6 @@ class UpdateTitleStats(BaseTask):
"""
SUPPORTED_TASK_API_VERSION = 1

DEFAULT_MIN_INTERVAL = 10
DEFAULT_DISPLAYED_STATS = []

def __init__(self, bot, config):
"""
Expand All @@ -67,12 +77,14 @@ def __init__(self, bot, config):
super(UpdateTitleStats, self).__init__(bot, config)

self.next_update = None
self.min_interval = self.DEFAULT_MIN_INTERVAL
self.displayed_stats = self.DEFAULT_DISPLAYED_STATS

self.bot.event_manager.register_event('update_title', parameters=('title'))
self.min_interval = int(self.config.get('min_interval', 120))
self.displayed_stats = self.config.get('stats', [])
self.terminal_log = self.config.get('terminal_log', False)
self.terminal_title = self.config.get('terminal_title', True)

self._process_config()
self.bot.event_manager.register_event('update_title', parameters=('title',))
self.bot.event_manager.register_event('log_stats',parameters=('title',))

def initialize(self):
pass
Expand All @@ -89,7 +101,12 @@ def work(self):
# If title is empty, it couldn't be generated.
if not title:
return WorkerResult.SUCCESS
self._update_title(title, _platform)

if self.terminal_title:
self._update_title(title, _platform)

if self.terminal_log:
self._log_on_terminal(title)
return WorkerResult.SUCCESS

def _should_display(self):
Expand All @@ -100,6 +117,16 @@ def _should_display(self):
"""
return self.next_update is None or datetime.now() >= self.next_update

def _log_on_terminal(self, title):
self.emit_event(
'log_stats',
formatted="{title}",
data={
'title': title
}
)
self.next_update = datetime.now() + timedelta(seconds=self.min_interval)

def _update_title(self, title, platform):
"""
Updates the window title using different methods, according to the given platform
Expand All @@ -119,7 +146,7 @@ def _update_title(self, title, platform):
'title': title
}
)

if platform == "linux" or platform == "linux2" or platform == "cygwin":
stdout.write("\x1b]2;{}\x07".format(title))
stdout.flush()
Expand All @@ -133,14 +160,6 @@ def _update_title(self, title, platform):

self.next_update = datetime.now() + timedelta(seconds=self.min_interval)

def _process_config(self):
"""
Fetches the configuration for this worker and stores the values internally.
:return: Nothing.
:rtype: None
"""
self.min_interval = int(self.config.get('min_interval', self.DEFAULT_MIN_INTERVAL))
self.displayed_stats = self.config.get('stats', self.DEFAULT_DISPLAYED_STATS)

def _get_stats_title(self, player_stats):
"""
Expand Down

0 comments on commit b94d369

Please sign in to comment.