diff options
author | Karel Kočí <cynerd@email.cz> | 2016-10-30 22:44:34 +0100 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2016-10-30 22:44:34 +0100 |
commit | 35d2ae9a115a17df2267cf0673d59419f4fd2a2f (patch) | |
tree | daa7484484806238ce98d6d1bed21122b8cd7b5e /mcwrapper/status.py | |
parent | d7b7cdf04c4a04eae1a44b53a8091fd1cda9d90f (diff) | |
download | mcserver-wrapper-35d2ae9a115a17df2267cf0673d59419f4fd2a2f.tar.gz mcserver-wrapper-35d2ae9a115a17df2267cf0673d59419f4fd2a2f.tar.bz2 mcserver-wrapper-35d2ae9a115a17df2267cf0673d59419f4fd2a2f.zip |
Split status and players to separate classes
Diffstat (limited to 'mcwrapper/status.py')
-rw-r--r-- | mcwrapper/status.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/mcwrapper/status.py b/mcwrapper/status.py new file mode 100644 index 0000000..42f8ed8 --- /dev/null +++ b/mcwrapper/status.py @@ -0,0 +1,58 @@ +# vim: expandtab ft=python ts=4 sw=4 sts=4: +import os + +from . import prints + +__STATUSSTRINGS__ = { + 0: "Not running", + 1: "Starting", + 2: "Running", + 3: "Stopping", + } +__STATUSFILE__ = 'status' + + +class MCStatus: + "Tracks server status" + def __init__(self, wrapper, file_export=False): + self.wrapper = wrapper + self.status = 0 + wrapper.hook_start(self.__server_start__) + wrapper.hook_stop(self.__server_stop__) + wrapper.hook_line(': Done', self.__server_started__) + wrapper.hook_line(': Stopping the server', self.__server_stopping__) + self.file_export = file_export + if file_export: + with open(__STATUSFILE__, 'w') as file: + file.write(__STATUSSTRINGS__[0] + '\n') + + def clean(self): + try: + os.remove(__STATUSFILE__) + except FileNotFoundError: + pass + + def __server_start__(self): + self.status = 1 + if self.file_export: + with open(__STATUSFILE__, 'w') as file: + file.write(__STATUSSTRINGS__[1] + '\n') + + def __server_stop__(self): + if self.file_export: + with open(__STATUSFILE__, 'w') as file: + file.write(__STATUSSTRINGS__[0] + '\n') + + def __server_started__(self, line): + prints.info("Server start.") + self.status = 2 + if self.file_export: + with open(__STATUSFILE__, 'w') as file: + file.write(__STATUSSTRINGS__[2] + '\n') + + def __server_stopping__(self, line): + prints.info("Server stop.") + self.status = 3 + if self.file_export: + with open(__STATUSFILE__, 'w') as file: + file.write(__STATUSSTRINGS__[3] + '\n') |