From 0878d53a550512234ebc5ace852cbb1725e300f2 Mon Sep 17 00:00:00 2001 From: Jonathan Golder Date: Thu, 18 Oct 2018 14:46:30 +0200 Subject: [PATCH] Move configration to separate class/file To be able to include it in other files without whole EuroExchangeBot class Issue #1 (https://git.golderweb.de/wiki/jogobot-euroexchange/issues/1) --- euroexchange/config.py | 45 ++++++++++++++++++++++++++++++++++++ euroexchange/euroexchange.py | 42 ++++++++++++++------------------- 2 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 euroexchange/config.py diff --git a/euroexchange/config.py b/euroexchange/config.py new file mode 100644 index 0000000..ed4a14f --- /dev/null +++ b/euroexchange/config.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# config.py +# +# Copyright 2018 Jonathan Golder +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# +""" +Loads configuration of EuroExchangeBot +""" + +import os + +import jogobot + + +class Config(): + """ + Configuration container for EuroExchangeBot + """ + + base_dir = os.path.expanduser(jogobot.config["euroexchange"]["base_dir"]) + working_dir = os.path.join( base_dir, "working_dir" ) + gnuplot_script_dir = os.path.join(base_dir, "gnuplot_scripts") + gnuplot = jogobot.config["euroexchange"]["gnuplot_bin"] + data_source = jogobot.config["euroexchange"]["data_source"] + zip_file = jogobot.config["euroexchange"]["data_zip_filename"] + csv_file = jogobot.config["euroexchange"]["data_csv_filename"] + upload_comment = jogobot.config["euroexchange"]["upload_comment"] diff --git a/euroexchange/euroexchange.py b/euroexchange/euroexchange.py index 6b8b813..30afd42 100644 --- a/euroexchange/euroexchange.py +++ b/euroexchange/euroexchange.py @@ -33,6 +33,7 @@ import pywikibot.specialbots import jogobot +from config import Config class EuroExchangeBotJob(): """ @@ -53,15 +54,6 @@ class EuroExchangeBotJob(): class EuroExchangeBot( pywikibot.bot.BaseBot ): - base_dir = os.path.expanduser(jogobot.config["euroexchange"]["base_dir"]) - working_dir = os.path.join( base_dir, "working_dir" ) - gnuplot_script_dir = os.path.join(base_dir, "gnuplot_scripts") - gnuplot = jogobot.config["euroexchange"]["gnuplot_bin"] - data_source = jogobot.config["euroexchange"]["data_source"] - zip_file = jogobot.config["euroexchange"]["data_zip_filename"] - csv_file = jogobot.config["euroexchange"]["data_csv_filename"] - upload_comment = jogobot.config["euroexchange"]["upload_comment"] - def __init__( self, genFactory, **kwargs ): # Init working directory @@ -85,7 +77,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): """ #Normalize working dir - self.wdir = os.path.realpath(type(self).working_dir) + self.wdir = os.path.realpath(Config.working_dir) if os.path.exists(self.wdir): @@ -108,7 +100,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): """ # Check if zip file exists - if os.path.exists( os.path.join(self.wdir, type(self).zip_file) ): + if os.path.exists( os.path.join(self.wdir, Config.zip_file) ): # If file is outdated, remove data input files if not self.is_zip_uptodate(): @@ -132,7 +124,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): @rtype bool """ # Get file stat - stat = os.stat( os.path.join(self.wdir, type(self).zip_file) ) + stat = os.stat( os.path.join(self.wdir, Config.zip_file) ) # Get file modification datetime mdt = datetime.datetime.fromtimestamp( stat.st_mtime ) @@ -158,8 +150,8 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): Deletes data input files """ - input_files = ( os.path.join(self.wdir, type(self).zip_file), - os.path.join(self.wdir, type(self).csv_file) ) + input_files = ( os.path.join(self.wdir, Config.zip_file), + os.path.join(self.wdir, Config.csv_file) ) for f in input_files: os.remove( f ) @@ -170,9 +162,9 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): Download the zipfile from EZB """ # Download the file and save it locally - with urllib.request.urlopen(type(self).data_source) as response,\ + with urllib.request.urlopen(Config.data_source) as response,\ open( os.path.join(self.wdir, - type(self).zip_file), 'wb') as out_file: + Config.zip_file), 'wb') as out_file: shutil.copyfileobj(response, out_file) @@ -182,7 +174,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): response.info()["Last-Modified"]) # Set ctime to value from http header - os.utime( os.path.join(self.wdir, type(self).zip_file), + os.utime( os.path.join(self.wdir, Config.zip_file), (datetime.datetime.now().timestamp(), mdate.timestamp()) ) # Log @@ -192,14 +184,14 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): """ Extract csv file from zip archive """ - if not os.path.exists( os.path.join(self.wdir, type(self).csv_file) ): + if not os.path.exists( os.path.join(self.wdir, Config.csv_file) ): with zipfile.ZipFile( - os.path.join(self.wdir, type(self).zip_file)) as zipobj: + os.path.join(self.wdir, Config.zip_file)) as zipobj: zipobj.extract( os.path.basename( - os.path.join(self.wdir, type(self).csv_file)), + os.path.join(self.wdir, Config.csv_file)), path=self.wdir ) def load_jobs( self ): @@ -211,7 +203,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): """ # Load json jobs file - with open( os.path.join(self.base_dir, "jobs.json"), "r") as fd: + with open( os.path.join(Config.base_dir, "jobs.json"), "r") as fd: jobs_js = json.load( fd ) # yield each job @@ -294,12 +286,12 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): @type job: EuroExchangeBotJob """ - cmd = shlex.split ( type(self).gnuplot + " " + os.path.realpath( - os.path.join( type(self).gnuplot_script_dir, + cmd = shlex.split ( Config.gnuplot + " " + os.path.realpath( + os.path.join( Config.gnuplot_script_dir, job.script + ".plt" ) ) ) plt_env = os.environ.copy() - plt_env["INFILE"] = type(self).csv_file + plt_env["INFILE"] = Config.csv_file plt_env["OUTFILE"] = job.image subprocess.check_call( cmd, cwd=self.wdir, env=plt_env ) @@ -331,7 +323,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ): @type job: EuroExchangeBotJob """ - comment = type(self).upload_comment + comment = Config.upload_comment filename = job.image filepath = [ os.path.join(self.wdir, job.image) ]