Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

PEP8 cleanups #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions testgres/testgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

import os
import random
import socket
# import socket
import subprocess
import pwd
import tempfile
import shutil
import time
import six

# Try to use psycopg2 by default. If psycopg2 isn"t available then use
# Try to use psycopg2 by default. If psycopg2 isn"t available then use
# pg8000 which is slower but much more portable because uses only
# pure-Python code
try:
Expand All @@ -44,21 +44,28 @@


registered_nodes = []
last_assigned_port = int(random.random() * 16384) + 49152;
last_assigned_port = int(random.random() * 16384) + 49152
pg_config_data = {}


"""
Predefined exceptions
"""
class ClusterException(Exception): pass
class QueryException(Exception): pass
class ClusterException(Exception):
"""
Predefined exceptions
"""
pass


class QueryException(Exception):
"""
Predefined exceptions
"""
pass


"""
Transaction wrapper returned by Node
"""
class NodeConnection(object):
"""
Transaction wrapper returned by Node
"""
def __init__(self, parent_node, dbname):
self.parent_node = parent_node

Expand All @@ -78,21 +85,22 @@ def __exit__(self, type, value, tb):
self.connection.close()

def begin(self, isolation_level=0):
levels = [ 'read uncommitted',
'read committed',
'repeatable read',
'serializable' ]
levels = ['read uncommitted',
'read committed',
'repeatable read',
'serializable']

print(type(isolation_level))
# Check if level is int [0..3]
if isinstance(isolation_level, int) and \
isolation_level in range(0, 4):
if (isinstance(isolation_level, int) and
isolation_level in range(0, 4)):

# Replace index with isolation level type
isolation_level = levels[isolation_level]

# Or it might be a string
elif isinstance(isolation_level, str) and \
str.lower(isolation_level) in levels:
elif (isinstance(isolation_level, six.text_type) and
isolation_level.lower() in levels):

# Nothing to do here
pass
Expand Down Expand Up @@ -148,13 +156,13 @@ def error_filename(self):

@property
def connstr(self):
return "port=%s" % self.port
return "port=%s" % self.port
# return "port=%s host=%s" % (self.port, self.host)

def get_bin_path(self, filename):
""" Returns full path to an executable """
pg_config = get_config()
if not "BINDIR" in pg_config:
if "BINDIR" not in pg_config:
return filename
else:
return "%s/%s" % (pg_config.get("BINDIR"), filename)
Expand Down Expand Up @@ -403,6 +411,7 @@ def get_username():
""" Returns current user name """
return pwd.getpwuid(os.getuid())[0]


def get_config():
global pg_config_data

Expand All @@ -422,19 +431,22 @@ def get_config():

return pg_config_data


def version_to_num(version):
"""Converts PostgreSQL version to number for easier comparison"""
import re

if not version:
return 0
parts = version.split(".")
while len(parts) < 3: parts.append("0")
while len(parts) < 3:
parts.append("0")
num = 0
for part in parts:
num = num*100 + int(re.sub("[^\d]", "", part))
return num


def get_new_node(name):
global registered_nodes
global last_assigned_port
Expand Down Expand Up @@ -462,12 +474,14 @@ def get_new_node(name):

return node


def clean_all():
global registered_nodes
for node in registered_nodes:
node.cleanup()
registered_nodes = []


def stop_all():
global registered_nodes
for node in registered_nodes:
Expand Down
6 changes: 4 additions & 2 deletions testgres/tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
import time
from testgres import get_new_node, clean_all, stop_all
# import time
# from testgres import clean_all
from testgres import get_new_node, stop_all


class SimpleTest(unittest.TestCase):

Expand Down