Move configration to separate class/file
To be able to include it in other files without whole EuroExchangeBot class Issue #1 (#1)
This commit is contained in:
45
euroexchange/config.py
Normal file
45
euroexchange/config.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# config.py
|
||||||
|
#
|
||||||
|
# Copyright 2018 Jonathan Golder <jonathan@golderweb.de>
|
||||||
|
#
|
||||||
|
# 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"]
|
||||||
@@ -33,6 +33,7 @@ import pywikibot.specialbots
|
|||||||
|
|
||||||
import jogobot
|
import jogobot
|
||||||
|
|
||||||
|
from config import Config
|
||||||
|
|
||||||
class EuroExchangeBotJob():
|
class EuroExchangeBotJob():
|
||||||
"""
|
"""
|
||||||
@@ -53,15 +54,6 @@ class EuroExchangeBotJob():
|
|||||||
|
|
||||||
class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
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 ):
|
def __init__( self, genFactory, **kwargs ):
|
||||||
|
|
||||||
# Init working directory
|
# Init working directory
|
||||||
@@ -85,7 +77,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#Normalize working dir
|
#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):
|
if os.path.exists(self.wdir):
|
||||||
|
|
||||||
@@ -108,7 +100,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Check if zip file exists
|
# 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 file is outdated, remove data input files
|
||||||
if not self.is_zip_uptodate():
|
if not self.is_zip_uptodate():
|
||||||
@@ -132,7 +124,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
@rtype bool
|
@rtype bool
|
||||||
"""
|
"""
|
||||||
# Get file stat
|
# 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
|
# Get file modification datetime
|
||||||
mdt = datetime.datetime.fromtimestamp( stat.st_mtime )
|
mdt = datetime.datetime.fromtimestamp( stat.st_mtime )
|
||||||
@@ -158,8 +150,8 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
Deletes data input files
|
Deletes data input files
|
||||||
"""
|
"""
|
||||||
|
|
||||||
input_files = ( os.path.join(self.wdir, type(self).zip_file),
|
input_files = ( os.path.join(self.wdir, Config.zip_file),
|
||||||
os.path.join(self.wdir, type(self).csv_file) )
|
os.path.join(self.wdir, Config.csv_file) )
|
||||||
|
|
||||||
for f in input_files:
|
for f in input_files:
|
||||||
os.remove( f )
|
os.remove( f )
|
||||||
@@ -170,9 +162,9 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
Download the zipfile from EZB
|
Download the zipfile from EZB
|
||||||
"""
|
"""
|
||||||
# Download the file and save it locally
|
# 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,
|
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)
|
shutil.copyfileobj(response, out_file)
|
||||||
|
|
||||||
@@ -182,7 +174,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
response.info()["Last-Modified"])
|
response.info()["Last-Modified"])
|
||||||
|
|
||||||
# Set ctime to value from http header
|
# 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()) )
|
(datetime.datetime.now().timestamp(), mdate.timestamp()) )
|
||||||
|
|
||||||
# Log
|
# Log
|
||||||
@@ -192,14 +184,14 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
"""
|
"""
|
||||||
Extract csv file from zip archive
|
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(
|
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(
|
zipobj.extract(
|
||||||
os.path.basename(
|
os.path.basename(
|
||||||
os.path.join(self.wdir, type(self).csv_file)),
|
os.path.join(self.wdir, Config.csv_file)),
|
||||||
path=self.wdir )
|
path=self.wdir )
|
||||||
|
|
||||||
def load_jobs( self ):
|
def load_jobs( self ):
|
||||||
@@ -211,7 +203,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Load json jobs file
|
# 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 )
|
jobs_js = json.load( fd )
|
||||||
|
|
||||||
# yield each job
|
# yield each job
|
||||||
@@ -294,12 +286,12 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
@type job: EuroExchangeBotJob
|
@type job: EuroExchangeBotJob
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cmd = shlex.split ( type(self).gnuplot + " " + os.path.realpath(
|
cmd = shlex.split ( Config.gnuplot + " " + os.path.realpath(
|
||||||
os.path.join( type(self).gnuplot_script_dir,
|
os.path.join( Config.gnuplot_script_dir,
|
||||||
job.script + ".plt" ) ) )
|
job.script + ".plt" ) ) )
|
||||||
|
|
||||||
plt_env = os.environ.copy()
|
plt_env = os.environ.copy()
|
||||||
plt_env["INFILE"] = type(self).csv_file
|
plt_env["INFILE"] = Config.csv_file
|
||||||
plt_env["OUTFILE"] = job.image
|
plt_env["OUTFILE"] = job.image
|
||||||
|
|
||||||
subprocess.check_call( cmd, cwd=self.wdir, env=plt_env )
|
subprocess.check_call( cmd, cwd=self.wdir, env=plt_env )
|
||||||
@@ -331,7 +323,7 @@ class EuroExchangeBot( pywikibot.bot.BaseBot ):
|
|||||||
@type job: EuroExchangeBotJob
|
@type job: EuroExchangeBotJob
|
||||||
"""
|
"""
|
||||||
|
|
||||||
comment = type(self).upload_comment
|
comment = Config.upload_comment
|
||||||
|
|
||||||
filename = job.image
|
filename = job.image
|
||||||
filepath = [ os.path.join(self.wdir, job.image) ]
|
filepath = [ os.path.join(self.wdir, job.image) ]
|
||||||
|
|||||||
Reference in New Issue
Block a user