diff options
author | emkael <emkael@tlen.pl> | 2017-08-01 00:38:10 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2017-08-01 00:38:10 +0200 |
commit | b1bf9357d573fe4a03d98ea1da7e01c292233c5d (patch) | |
tree | 24beffa6f9ef9299c9517ca2e769ce1651ea0f25 | |
parent | bd5fd6a6596dd7625019c6c0f0ff043597cda837 (diff) |
File and Goniec operations moved to separate class
-rw-r--r-- | jfr_playoff/filemanager.py | 47 | ||||
-rw-r--r-- | playoff.py | 36 |
2 files changed, 52 insertions, 31 deletions
diff --git a/jfr_playoff/filemanager.py b/jfr_playoff/filemanager.py new file mode 100644 index 0000000..64ac6d9 --- /dev/null +++ b/jfr_playoff/filemanager.py @@ -0,0 +1,47 @@ +import os, shutil, socket +import __main__ + +class PlayoffFileManager(object): + + def __init__(self, settings): + self.settings = settings + self.goniec = self.settings.get('goniec') + self.output_file = self.settings.get('output') + self.output_path = os.path.dirname(self.output_file).strip(os.sep) + os.sep + self.files = set() + + def reset(self): + self.files.empty() + + def register_file(self, path): + if path.startswith(self.output_path): + self.files.add(path.replace(self.output_path, '')) + + + def write_content(self, content): + output = open(self.output_file, 'w') + output.write(content.encode('utf8')) + output.close() + self.register_file(self.output_file) + return self.output_file + + def copy_scripts(self, script_path='sklady/playoff.js'): + script_output_path = os.path.join(self.output_path, script_path) + shutil.copy( + unicode(os.path.join(os.path.dirname(__main__.__file__), 'playoff.js')), + unicode(script_output_path) + ) + self.register_file(script_output_path) + return script_output_path + + def send_files(self): + if self.goniec['enabled']: + try: + content_lines = [self.output_path] + list(self.files) + ['bye', ''] + print '\n'.join(content_lines) + goniec = socket.socket() + goniec.connect((self.goniec['host'], self.goniec['port'])) + goniec.sendall('\n'.join(content_lines)) + goniec.close() + except socket.error: + pass @@ -1,4 +1,3 @@ -import os, shutil, socket from datetime import datetime from urlparse import urljoin import jfr_playoff.sql as p_sql @@ -233,32 +232,7 @@ def generate_content(grid, phases, match_info, teams, grid_width, grid_height, p ) ) -def write_content(content, output_file): - output = open(output_file, 'w') - output.write(content.encode('utf8')) - output.close() - return os.path.dirname(output_file) - -def copy_scripts(output_path): - script_path = 'sklady/playoff.js' - script_output_path = os.path.join(output_path, script_path) - shutil.copy(unicode(os.path.join(os.path.dirname(__file__), 'playoff.js')), - unicode(script_output_path)) - return script_output_path - -def send_files(goniec_settings, path, files): - if goniec_settings['enabled']: - try: - base_path = path.strip(os.sep) + os.sep - content_files = [filename.replace(base_path, '') for filename in files if filename.startswith(base_path)] - content_lines = [base_path] + content_files + ['bye', ''] - print '\n'.join(content_lines) - goniec = socket.socket() - goniec.connect((goniec_settings['host'], goniec_settings['port'])) - goniec.sendall('\n'.join(content_lines)) - goniec.close() - except socket.error: - pass +from jfr_playoff.filemanager import PlayoffFileManager def main(): s = PlayoffSettings() @@ -277,9 +251,9 @@ def main(): content = generate_content(grid, phase_settings, match_info, s.get('teams'), grid_width, grid_height, page_settings, s.get('canvas') if s.has_section('canvas') else {}, leaderboard) - output_file = s.get('output') - output_path = write_content(content, output_file) - script_path = copy_scripts(output_path) - send_files(s.get('goniec'), output_path, [output_file, script_path]) + file_manager = PlayoffFileManager(s) + file_manager.write_content(content) + file_manager.copy_scripts() + file_manager.send_files() main() |