From f14ddbc913b3fb278ec48a4004ebce4737b93355 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 23 Feb 2018 10:40:06 +0100 Subject: Getting rid of global mysql imports --- jfr_playoff/data.py | 1 - jfr_playoff/db.py | 28 +++++++++++++++++++--------- jfr_playoff/matchinfo.py | 8 +++----- jfr_playoff/tournamentinfo.py | 8 +++----- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index c588610..a9c3a0c 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -1,4 +1,3 @@ -import mysql from cached_property import cached_property from jfr_playoff.db import PlayoffDB diff --git a/jfr_playoff/db.py b/jfr_playoff/db.py index 6825fd2..b7c3726 100644 --- a/jfr_playoff/db.py +++ b/jfr_playoff/db.py @@ -1,8 +1,5 @@ import sys -import mysql.connector - - class PlayoffDB(object): db_cursor = None @@ -11,6 +8,7 @@ class PlayoffDB(object): def __init__(self, settings): reload(sys) sys.setdefaultencoding("latin1") + import mysql.connector self.database = mysql.connector.connect( user=settings['user'], password=settings['pass'], @@ -25,11 +23,23 @@ class PlayoffDB(object): self.db_cursor.execute(sql.replace('#db#', db_name), params) def fetch(self, db_name, sql, params): - self.__execute_query(db_name, sql, params) - row = self.db_cursor.fetchone() - return row + import mysql.connector + try: + self.__execute_query(db_name, sql, params) + row = self.db_cursor.fetchone() + return row + except mysql.connector.Error as e: + raise IOError( + message=str(e), filename=db_name, + errno=e.errno, strerror=str(e)) def fetch_all(self, db_name, sql, params): - self.__execute_query(db_name, sql, params) - results = self.db_cursor.fetchall() - return results + import mysql.connector + try: + self.__execute_query(db_name, sql, params) + results = self.db_cursor.fetchall() + return results + except mysql.connector.Error as e: + raise IOError( + message=str(e), filename=db_name, + errno=e.errno, strerror=str(e)) diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index 538c67d..f65c23c 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -1,8 +1,6 @@ import re from urlparse import urljoin -import mysql - import jfr_playoff.sql as p_sql from jfr_playoff.dto import Match, Team from jfr_playoff.remote import RemoteUrl as p_remote @@ -153,7 +151,7 @@ class MatchInfo: raise KeyError('database not configured') self.info.teams = self.__get_db_teams( self.info.teams, not scores_fetched) - except (mysql.connector.Error, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError): self.info.teams = self.__get_html_teams( self.info.teams, not scores_fetched) except (TypeError, IndexError, KeyError, IOError, ValueError): @@ -242,7 +240,7 @@ class MatchInfo: if self.database is None: raise KeyError('database not configured') boards_played, boards_to_play = self.__get_db_board_count() - except (mysql.connector.Error, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError): try: boards_played, boards_to_play = self.__get_html_board_count() except (TypeError, IndexError, KeyError, IOError, ValueError): @@ -287,7 +285,7 @@ class MatchInfo: raise KeyError('database not configured') self.info.link = self.__get_db_running_link( link_match.group(1), link_match.group(2)) - except (mysql.connector.Error, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError): try: self.info.link = self.__get_html_running_link() except (TypeError, IndexError, KeyError, IOError, ValueError): diff --git a/jfr_playoff/tournamentinfo.py b/jfr_playoff/tournamentinfo.py index 90637bc..ee96214 100644 --- a/jfr_playoff/tournamentinfo.py +++ b/jfr_playoff/tournamentinfo.py @@ -1,8 +1,6 @@ from math import ceil import re -import mysql - import jfr_playoff.sql as p_sql from jfr_playoff.remote import RemoteUrl as p_remote @@ -118,7 +116,7 @@ class TournamentInfo: teams = [] try: teams = self.__get_db_results() - except (mysql.connector.Error, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError): try: teams = self.__get_html_results() except (TypeError, IndexError, KeyError, IOError, ValueError): @@ -132,7 +130,7 @@ class TournamentInfo: def is_finished(self): try: return self.__get_db_finished() - except (mysql.connector.Error, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError): try: return self.__get_html_finished() except (TypeError, IndexError, KeyError, IOError, ValueError): @@ -142,7 +140,7 @@ class TournamentInfo: def get_results_link(self, suffix='leaderb.html'): try: return self.__get_db_link(suffix) - except (mysql.connector.Error, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError): try: return self.__get_html_link(suffix) except (KeyError, ValueError): -- cgit v1.2.3 From cab639df56676443dc7cd8b5ef3dc9ac5dae830b Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 23 Feb 2018 16:05:11 +0100 Subject: Updated docs on mysql import Fixes #16 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 86f9d7b..278b3a4 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Dla wersji skompilowanej: jakiś współczesny system rodziny MS Windows. Dla wersji skryptowej: * Python 2.7.x, co najmniej 2.7.9, ze standardowym zestawem bibliotek - * MySQL connector dla Pythona + * MySQL connector dla Pythona (opcjonalnie, w przypadku użycia bazy danych jako źródła wyników) * `pip install -r requirements.txt` Instalacja -- cgit v1.2.3