Skip to content

Commit 912c7fc

Browse files
committed
Refactor optional features
1 parent 06ad290 commit 912c7fc

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

seleniumbase/core/mysql.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
"""
2-
Wrapper for MySQL DB functions to make life easier.
3-
"""
4-
5-
import sys
6-
import time
7-
from seleniumbase import config as sb_config
8-
from seleniumbase.config import settings
9-
from seleniumbase.core import settings_parser
1+
"""Wrapper for MySQL DB functions."""
102

113

124
class DatabaseManager:
13-
"""
14-
This class wraps MySQL database methods for easy use.
15-
"""
5+
"""This class wraps MySQL database methods for easy use."""
166

177
def __init__(self, database_env="test", conf_creds=None):
18-
"""
19-
Create a connection to the MySQL DB.
20-
"""
8+
"""Create a connection to the MySQL DB."""
9+
import fasteners
10+
import sys
11+
import time
12+
from importlib.util import find_spec
13+
from seleniumbase import config as sb_config
14+
from seleniumbase.config import settings
15+
from seleniumbase.core import settings_parser
16+
from seleniumbase.fixtures import constants
17+
from seleniumbase.fixtures import shared_utils
18+
19+
pip_find_lock = fasteners.InterProcessLock(
20+
constants.PipInstall.FINDLOCK
21+
)
22+
with pip_find_lock: # Prevent multi-processes mode issues
23+
if not find_spec("pymysql"):
24+
if sys.version_info >= (3, 6):
25+
shared_utils.pip_install("pymysql", version="1.0.2")
26+
else:
27+
shared_utils.pip_install("pymysql", version="0.10.1")
2128
import pymysql
2229

2330
db_server = settings.DB_HOST
@@ -70,27 +77,21 @@ def __init__(self, database_env="test", conf_creds=None):
7077
raise Exception("Unable to connect to Database after 3 retries.")
7178

7279
def query_fetch_all(self, query, values):
73-
"""
74-
Executes a db query, gets all the values, and closes the connection.
75-
"""
80+
"""Execute db query, get all the values, and close the connection."""
7681
self.cursor.execute(query, values)
7782
retval = self.cursor.fetchall()
7883
self.__close_db()
7984
return retval
8085

8186
def query_fetch_one(self, query, values):
82-
"""
83-
Executes a db query, gets the first value, and closes the connection.
84-
"""
87+
"""Execute db query, get the first value, and close the connection."""
8588
self.cursor.execute(query, values)
8689
retval = self.cursor.fetchone()
8790
self.__close_db()
8891
return retval
8992

9093
def execute_query(self, query, values):
91-
"""
92-
Executes a query to the test_db and closes the connection afterwards.
93-
"""
94+
"""Execute db query, close the connection, and return the results."""
9495
retval = self.cursor.execute(query, values)
9596
self.__close_db()
9697
return retval

seleniumbase/core/s3_manager.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
"""
2-
Methods for uploading/managing files on Amazon S3.
3-
"""
4-
from seleniumbase.config import settings
5-
from seleniumbase.fixtures import shared_utils
1+
"""Methods for uploading/managing files on Amazon S3."""
62

73
already_uploaded_files = []
84

95

106
class S3LoggingBucket(object):
11-
"""
12-
A class for uploading log files from tests to Amazon S3.
13-
Those files can then be shared easily.
14-
"""
7+
"""A class for uploading log files from tests to Amazon S3.
8+
Those files can then be shared easily."""
9+
from seleniumbase.config import settings
1510

1611
def __init__(
1712
self,
@@ -20,23 +15,26 @@ def __init__(
2015
selenium_access_key=settings.S3_SELENIUM_ACCESS_KEY,
2116
selenium_secret_key=settings.S3_SELENIUM_SECRET_KEY,
2217
):
23-
try:
24-
from boto.s3.connection import S3Connection
25-
except Exception:
26-
shared_utils.pip_install("boto", version="2.49.0")
27-
from boto.s3.connection import S3Connection
18+
import fasteners
19+
from importlib.util import find_spec
20+
from seleniumbase.fixtures import constants
21+
from seleniumbase.fixtures import shared_utils
22+
23+
pip_find_lock = fasteners.InterProcessLock(
24+
constants.PipInstall.FINDLOCK
25+
)
26+
with pip_find_lock: # Prevent multi-processes mode issues
27+
if not find_spec("boto"):
28+
shared_utils.pip_install("boto", version="2.49.0")
29+
from boto.s3.connection import S3Connection
2830

2931
self.conn = S3Connection(selenium_access_key, selenium_secret_key)
3032
self.bucket = self.conn.get_bucket(log_bucket)
3133
self.bucket_url = bucket_url
3234

3335
def get_key(self, file_name):
3436
"""Create a new Key instance with the given name."""
35-
try:
36-
from boto.s3.key import Key
37-
except Exception:
38-
shared_utils.pip_install("boto", version="2.49.0")
39-
from boto.s3.key import Key
37+
from boto.s3.key import Key
4038

4139
return Key(bucket=self.bucket, name=file_name)
4240

0 commit comments

Comments
 (0)