Skip to content

Commit

Permalink
Pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunklau committed Oct 20, 2014
1 parent d31b394 commit bbf96c3
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 59 deletions.
21 changes: 12 additions & 9 deletions python/multicorn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ def list_any_or_all(self):
def __repr__(self):
if self.is_list_operator:
value = '%s(%s)' % (
'ANY' if self.list_any_or_all == ANY
else 'ALL',
self.value)
'ANY' if self.list_any_or_all == ANY else 'ALL',
self.value)
operator = self.operator[0]
else:
value = self.value
Expand Down Expand Up @@ -135,9 +134,11 @@ def execute(self, quals, columns):

@property
def rowid_column(self):
"""Returns a column name which will act as a rowid column, for delete/update
operations. This can be either an existing column name, or a made-up one.
This column name should be subsequently present in every returned resultset.
"""Returns a column name which will act as a rowid column,
for delete/update operations. This can be either an existing column
name, or a made-up one.
This column name should be subsequently present in every
returned resultset.
"""
raise NotImplementedError("This FDW does not support the writable API")

Expand Down Expand Up @@ -181,7 +182,8 @@ def sub_commit(self, level):
class TransactionAwareForeignDataWrapper(ForeignDataWrapper):

def __init__(self, fdw_options, fdw_columns):
super(TransactionAwareForeignDataWrapper, self).__init__(fdw_options, fdw_columns)
super(TransactionAwareForeignDataWrapper, self).__init__(
fdw_options, fdw_columns)
self._init_transaction_state()

def _init_transaction_state(self):
Expand All @@ -191,7 +193,8 @@ def insert(self, values):
self.current_transaction_state.append(('insert', values))

def update(self, oldvalues, newvalues):
self.current_transaction_state.append(('update', (oldvalues, newvalues)))
self.current_transaction_state.append(
('update', (oldvalues, newvalues)))

def delete(self, oldvalues):
self.current_transaction_state.append(('delete', oldvalues))
Expand All @@ -216,7 +219,7 @@ def _resolve_name(name, package, level):
dot = package.rindex('.', 0, dot)
except ValueError:
raise ValueError("attempted relative import beyond top-level "
"package")
"package")
return "%s.%s" % (package[:dot], name)


Expand Down
1 change: 0 additions & 1 deletion python/multicorn/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@
except NameError:
# Python3
basestring_ = str

5 changes: 3 additions & 2 deletions python/multicorn/fsfdw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def __init__(self, options, columns):
self.content_column = options.get('content_column', None)
self.filename_column = options.get('filename_column', None)
self.file_mode = int(options.get('file_mode', '700'), 8)
self.structured_directory = StructuredDirectory(root_dir, pattern,
file_mode=self.file_mode)
self.structured_directory = StructuredDirectory(
root_dir, pattern,
file_mode=self.file_mode)
self.folder_columns = [key[0] for key in
self.structured_directory._path_parts_properties
if key]
Expand Down
2 changes: 1 addition & 1 deletion python/multicorn/fsfdw/structuredfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def open(self, shared_lock=True, fail_if=None):
# Do nothing if we already have a file descriptor
else:
if (self._fd is None or
not (fcntl.fcntl(self._fd, fcntl.F_GETFL) & os.O_RDWR)):
not (fcntl.fcntl(self._fd, fcntl.F_GETFL) & os.O_RDWR)):
# Open it with an exclusive lock, sync mode, and fail if the
# file already exists.
dirname = os.path.dirname(self.full_filename)
Expand Down
15 changes: 8 additions & 7 deletions python/multicorn/gcfdw.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from multicorn import ForeignDataWrapper
import gc
import sys
import random
from multicorn.compat import unicode_, basestring_
from multicorn.compat import unicode_


class MyClass(object):
Expand All @@ -11,6 +10,7 @@ def __init__(self, num, rand):
self.num = num
self.rand = rand


class GCForeignDataWrapper(ForeignDataWrapper):

def execute(self, quals, columns):
Expand All @@ -31,11 +31,12 @@ def execute(self, quals, columns):
except (UnicodeEncodeError, UnicodeDecodeError):
obj = unicode_("<NA>")
result.append({'object': obj,
'type': unicode_(tobj),
'id': unicode_(id(obj)),
'refcount': unicode_(sys.getrefcount(obj))})
'type': unicode_(tobj),
'id': unicode_(id(obj)),
'refcount': unicode_(sys.getrefcount(obj))})
return result


class MemStressFDW(ForeignDataWrapper):

def __init__(self, options, columns):
Expand All @@ -48,5 +49,5 @@ def execute(self, quals, columns):
for i in range(self.nb):
num = i / 100.
yield {'value': str(MyClass(i, num)),
'i': i,
'num': num}
'i': i,
'num': num}
2 changes: 1 addition & 1 deletion python/multicorn/gitfdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def execute(self, quals, columns):
def enc(unicode_str):
"""Encode the string in the self given encoding."""
return unicode_str.encode(self.encoding)
for log in brigit.Git(self.path).pretty_log():
for log in brigit.Git(self.path).pretty_log():
yield {
'author_name': enc(log["author"]['name']),
'author_email': enc(log["author"]['email']),
Expand Down
41 changes: 22 additions & 19 deletions python/multicorn/imapfdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@


STANDARD_FLAGS = {
'seen': 'Seen',
'flagged': 'Flagged',
'delete': 'Deleted',
'draft': 'Draft',
'recent': 'Recent'
'seen': 'Seen',
'flagged': 'Flagged',
'delete': 'Deleted',
'draft': 'Draft',
'recent': 'Recent'
}

SEARCH_HEADERS = ['BCC', 'CC', 'FROM', 'TO']
Expand Down Expand Up @@ -73,7 +73,7 @@ def __init__(self, options, columns):
self.host = options.get('host', None)
if self.host is None:
log_to_postgres('You MUST set the imap host',
ERROR)
ERROR)
self.port = options.get('port', None)
self.ssl = options.get('ssl', False)
self.login = options.get('login', None)
Expand All @@ -87,7 +87,8 @@ def __init__(self, options, columns):

def get_rel_size(self, quals, columns):
"""Inform the planner that it can be EXTREMELY costly to use the
payload column, and that a query on Message-ID will return only one row."""
payload column, and that a query on Message-ID will return
only one row."""
width = len(columns) * 100
nb_rows = 1000000
if self.payload_column in columns:
Expand All @@ -97,7 +98,8 @@ def get_rel_size(self, quals, columns):
if qual.field_name.lower() == 'in-reply-to' and\
qual.operator == '=':
nb_rows = 10
if qual.field_name.lower() == 'message-id' and qual.operator == '=':
if (qual.field_name.lower() == 'message-id' and
qual.operator == '='):
nb_rows = 1
break
return (nb_rows, width)
Expand All @@ -119,8 +121,8 @@ def imap_agent(self):
return self._imap_agent

def get_path_keys(self):
"""Helps the planner by supplying a list of list of access keys, as well
as a row estimate for each one."""
"""Helps the planner by supplying a list of list of access keys,
as well as a row estimate for each one."""
return [(('Message-ID',), 1), (('From',), 100), (('To',), 100),
(('In-Reply-To',), 10)]

Expand Down Expand Up @@ -153,12 +155,12 @@ def _make_condition(self, key, operator, value):
# Contains on flags
return ' '.join(['%s%s' % (prefix,
(STANDARD_FLAGS.get(atom.lower(), '%s %s'
% ('KEYWORD', atom)))) for atom in value])
% ('KEYWORD', atom)))) for atom in value])
elif operator == '&&':
# Overlaps on flags => Or
values = ['(%s%s)' %
(prefix, (STANDARD_FLAGS.get(atom.lower(), '%s %s' %
('KEYWORD', atom)))) for atom in value]
('KEYWORD', atom)))) for atom in value]
return make_or(values)
else:
value = '\\\\%s' % value
Expand All @@ -184,17 +186,18 @@ def extract_conditions(self, quals):
if qual.list_any_or_all == ANY:
values = [
'(%s)' % self._make_condition(qual.field_name,
qual.operator[0], value)
qual.operator[0], value)
for value in qual.value]
conditions.append(make_or(values))
elif qual.list_any_or_all == ALL:
conditions.extend([
self._make_condition(qual.field_name, qual.operator[0],
value)
value)
for value in qual.value])
else:
# its not a list, so everything is fine
conditions.append(self._make_condition(qual.field_name,
conditions.append(self._make_condition(
qual.field_name,
qual.operator, qual.value))
conditions = [x for x in conditions if x not in (None, '()')]
return conditions
Expand All @@ -211,8 +214,8 @@ def execute(self, quals, columns):
elif column == self.internaldate_column:
col_to_imap[column] = 'INTERNALDATE'
else:
col_to_imap[column] = 'BODY[HEADER.FIELDS (%s)]' %\
column.upper()
col_to_imap[column] = ('BODY[HEADER.FIELDS (%s)]' %
column.upper())
headers.append(column)
try:
conditions = self.extract_conditions(quals) or ['ALL']
Expand All @@ -237,10 +240,10 @@ def execute(self, quals, columns):
if charset:
try:
item[column] = decoded_header.decode(
charset)
charset)
except LookupError:
log_to_postgres('Unknown encoding: %s' %
charset, WARNING)
charset, WARNING)
else:
item[column] = decoded_header
yield item
2 changes: 1 addition & 1 deletion python/multicorn/sqlalchemyfdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def delete(self, rowid):

def _get_column_type(self, format_type):
"""Blatant ripoff from PG_Dialect.get_column_info"""
## strip (*) from character varying(5), timestamp(5)
# strip (*) from character varying(5), timestamp(5)
# with time zone, geometry(POLYGON), etc.
attype = re.sub(r'\(.*\)', '', format_type)

Expand Down
28 changes: 15 additions & 13 deletions python/multicorn/testfdw.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from multicorn import ForeignDataWrapper
from multicorn.compat import unicode_
from .utils import log_to_postgres, WARNING, ERROR
from itertools import cycle
from datetime import datetime
Expand Down Expand Up @@ -40,32 +39,36 @@ def _as_generator(self, quals, columns):
line = {}
for column_name, column in self.columns.items():
if self.test_type == 'list':
line[column_name] = [column_name, next(random_thing),
index, '%s,"%s"' % (column_name, index),
'{some value, \\" \' 2}']
line[column_name] = [
column_name, next(random_thing),
index, '%s,"%s"' % (column_name, index),
'{some value, \\" \' 2}']
elif self.test_type == 'dict':
line[column_name] = {"column_name": column_name,
"repeater": next(random_thing),
"index": index,
"maybe_hstore": "a => b"}
line[column_name] = {
"column_name": column_name,
"repeater": next(random_thing),
"index": index,
"maybe_hstore": "a => b"}
elif self.test_type == 'date':
line[column_name] = datetime(2011, (index % 12) + 1,
next(random_thing), 14,
30, 25)
elif self.test_type == 'int':
line[column_name] = index
elif self.test_type == 'encoding':
line[column_name] = b'\xc3\xa9\xc3\xa0\xc2\xa4'.decode('utf-8')
line[column_name] = (b'\xc3\xa9\xc3\xa0\xc2\xa4'
.decode('utf-8'))
elif self.test_type == 'nested_list':
line[column_name] = [[column_name, column_name], [next(random_thing), '{some value, \\" 2}'],
[index, '%s,"%s"' % (column_name, index)]]
line[column_name] = [
[column_name, column_name],
[next(random_thing), '{some value, \\" 2}'],
[index, '%s,"%s"' % (column_name, index)]]
else:
line[column_name] = '%s %s %s' % (column_name,
next(random_thing),
index)
yield line


def execute(self, quals, columns):
log_to_postgres(str(sorted(quals)))
log_to_postgres(str(sorted(columns)))
Expand All @@ -76,7 +79,6 @@ def execute(self, quals, columns):
else:
return self._as_generator(quals, columns)


def get_rel_size(self, quals, columns):
if self.test_type == 'planner':
return (10000000, len(columns) * 10)
Expand Down
10 changes: 5 additions & 5 deletions python/multicorn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ def _log_to_postgres(message, level=0, hint=None, detail=None):


REPORT_CODES = {
DEBUG: 0,
INFO: 1,
WARNING: 2,
ERROR: 3,
CRITICAL: 4
DEBUG: 0,
INFO: 1,
WARNING: 2,
ERROR: 3,
CRITICAL: 4
}


Expand Down
2 changes: 2 additions & 0 deletions python/multicorn/xmlfdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import ForeignDataWrapper
from xml.sax import ContentHandler, make_parser


class MulticornXMLHandler(ContentHandler):

def __init__(self, elem_tag, columns):
Expand Down Expand Up @@ -46,6 +47,7 @@ def endElement(self, name):
elif name in self.columns:
self.tag = None


class XMLFdw(ForeignDataWrapper):
"""A foreign data wrapper for accessing xml files.
Expand Down

0 comments on commit bbf96c3

Please sign in to comment.