Skip to content
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
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint with Black

on:
pull_request:
push:
branches:
- main

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install Black
run: pip install black

- name: Run Black
run: black --check .
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
test:
pytest .
pytest .

fmt:
pip install -q black
black .
36 changes: 19 additions & 17 deletions cloudquery/sdk/docs/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_dict(self):
"title": self.title,
"description": self.description,
"columns": [col.to_dict() for col in self.columns],
"relations": [rel.to_dict() for rel in self.relations]
"relations": [rel.to_dict() for rel in self.relations],
}


Expand All @@ -40,7 +40,7 @@ def to_dict(self):
"name": self.name,
"type": self.type,
"is_primary_key": self.is_primary_key,
"is_incremental_key": self.is_incremental_key
"is_incremental_key": self.is_incremental_key,
}


Expand All @@ -58,9 +58,9 @@ def generate(self, directory: str, format: str):

def _generate_json(self, directory: str):
json_tables = self._jsonify_tables(self._tables)
buffer = bytes(json.dumps(json_tables, indent=2, ensure_ascii=False), 'utf-8')
buffer = bytes(json.dumps(json_tables, indent=2, ensure_ascii=False), "utf-8")
output_path = pathlib.Path(directory) / "__tables.json"
with output_path.open('wb') as f:
with output_path.open("wb") as f:
f.write(buffer)
return None

Expand Down Expand Up @@ -88,10 +88,12 @@ def _jsonify_tables(self, tables):

def _generate_markdown(self, directory: str):
env = jinja2.Environment()
env.globals['indent_to_depth'] = self._indent_to_depth
env.globals['all_tables_entry'] = self._all_tables_entry
env.globals["indent_to_depth"] = self._indent_to_depth
env.globals["all_tables_entry"] = self._all_tables_entry
all_tables_template = env.from_string(ALL_TABLES)
rendered_all_tables = all_tables_template.render(plugin_name=self._plugin_name, tables=self._tables)
rendered_all_tables = all_tables_template.render(
plugin_name=self._plugin_name, tables=self._tables
)
formatted_all_tables = self._format_markdown(rendered_all_tables)

with open(os.path.join(directory, "README.md"), "w") as f:
Expand All @@ -111,9 +113,9 @@ def _render_table(self, directory: str, env: jinja2.Environment, table: Table):

def _all_tables_entry(self, table: Table):
env = jinja2.Environment()
env.globals['indent_to_depth'] = self._indent_to_depth
env.globals['all_tables_entry'] = self._all_tables_entry
env.globals['indent_table_to_depth'] = self._indent_table_to_depth
env.globals["indent_to_depth"] = self._indent_to_depth
env.globals["all_tables_entry"] = self._all_tables_entry
env.globals["indent_table_to_depth"] = self._indent_table_to_depth
entry_template = env.from_string(ALL_TABLES_ENTRY)
return entry_template.render(table=table)

Expand All @@ -129,15 +131,15 @@ def _indent_table_to_depth(table: Table) -> str:
@staticmethod
def _indent_to_depth(text: str, depth: int) -> str:
indentation = depth * 4 # You can adjust the number of spaces as needed
lines = text.split('\n')
indented_lines = [(' ' * indentation) + line for line in lines]
return '\n'.join(indented_lines)
lines = text.split("\n")
indented_lines = [(" " * indentation) + line for line in lines]
return "\n".join(indented_lines)

@staticmethod
def _format_markdown(text: str) -> str:
re_match_newlines = re.compile(r'\n{3,}')
re_match_headers = re.compile(r'(#{1,6}.+)\n+')
re_match_newlines = re.compile(r"\n{3,}")
re_match_headers = re.compile(r"(#{1,6}.+)\n+")

text = re_match_newlines.sub(r'\n\n', text)
text = re_match_headers.sub(r'\1\n\n', text)
text = re_match_newlines.sub(r"\n\n", text)
text = re_match_headers.sub(r"\1\n\n", text)
return text
1 change: 0 additions & 1 deletion cloudquery/sdk/internal/memdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

from .memdb import MemDB
28 changes: 15 additions & 13 deletions cloudquery/sdk/internal/memdb/memdb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from cloudquery.sdk import plugin
from cloudquery.sdk import message
from cloudquery.sdk import schema
Expand All @@ -8,17 +7,20 @@
NAME = "memdb"
VERSION = "development"


class MemDB(plugin.Plugin):
def __init__(self) -> None:
super().__init__(NAME, VERSION)
self._tables: List[schema.Table] = []
self._memory_db : Dict[str, pa.record] = {
"test_table": pa.record_batch([pa.array([1, 2, 3])], names=["test_column"])
}

def get_tables(self, options : plugin.TableOptions = None) -> List[plugin.Table]:
return self._tables

def sync(self, options: plugin.SyncOptions) -> Generator[message.SyncMessage, None, None]:
for table, record in self._memory_db.items():
yield message.SyncInsertMessage(record)
super().__init__(NAME, VERSION)
self._tables: List[schema.Table] = []
self._memory_db: Dict[str, pa.record] = {
"test_table": pa.record_batch([pa.array([1, 2, 3])], names=["test_column"])
}

def get_tables(self, options: plugin.TableOptions = None) -> List[plugin.Table]:
return self._tables

def sync(
self, options: plugin.SyncOptions
) -> Generator[message.SyncMessage, None, None]:
for table, record in self._memory_db.items():
yield message.SyncInsertMessage(record)
3 changes: 1 addition & 2 deletions cloudquery/sdk/internal/servers/plugin_v3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

from .plugin import PluginServicer
from .plugin import PluginServicer
18 changes: 11 additions & 7 deletions cloudquery/sdk/internal/servers/plugin_v3/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def Init(self, request: plugin_pb2.Init.Request, context):
return plugin_pb2.Init.Response()

def GetTables(self, request: plugin_pb2.GetTables.Request, context):
tables = self._plugin.get_tables(TableOptions(tables=request.tables, skip_tables=request.skip_tables))
tables = self._plugin.get_tables(
TableOptions(tables=request.tables, skip_tables=request.skip_tables)
)
schema = tables_to_arrow_schemas(tables)
tablesBytes = []
for s in schema:
Expand Down Expand Up @@ -51,13 +53,15 @@ def Sync(self, request, context):
writer.write_batch(msg.record)
writer.close()
buf = sink.getvalue().to_pybytes()
yield plugin_pb2.Sync.Response(insert=plugin_pb2.Sync.MessageInsert(
record=buf
))
yield plugin_pb2.Sync.Response(
insert=plugin_pb2.Sync.MessageInsert(record=buf)
)
elif isinstance(msg, SyncMigrateTableMessage):
yield plugin_pb2.Sync.Response(migrate_table=plugin_pb2.Sync.MessageMigrateTable(
table=msg.table.to_arrow_schema().serialize().to_pybytes()
))
yield plugin_pb2.Sync.Response(
migrate_table=plugin_pb2.Sync.MessageMigrateTable(
table=msg.table.to_arrow_schema().serialize().to_pybytes()
)
)
else:
# unknown sync message type
raise NotImplementedError()
Expand Down
2 changes: 1 addition & 1 deletion cloudquery/sdk/plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .plugin import Plugin, Table, TableOptions, SyncOptions
from .plugin import Plugin, Table, TableOptions, SyncOptions
43 changes: 22 additions & 21 deletions cloudquery/sdk/scalar/binary.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError
from .scalar import NULL_VALUE


class Binary(Scalar):
def __init__(self, valid: bool = False, value: bytes = None):
self._valid = valid
self._value = value
self._valid = valid
self._value = value

def __eq__(self, scalar: Scalar) -> bool:
if scalar is None:
return False
if type(scalar) == Binary:
return self._value == scalar._value and self._valid == scalar._valid
return False
if scalar is None:
return False
if type(scalar) == Binary:
return self._value == scalar._value and self._valid == scalar._valid
return False

def __str__(self) -> str:
return str(self._value) if self._valid else NULL_VALUE
return str(self._value) if self._valid else NULL_VALUE

@property
def value(self):
return self._value
return self._value

def set(self, scalar):
if scalar is None:
return
if scalar is None:
return

if type(scalar) == bytes:
self._valid = True
self._value = scalar
elif type(scalar) == str:
self._valid = True
self._value = scalar.encode()
else:
raise ScalarInvalidTypeError("Invalid type for Binary scalar")
if type(scalar) == bytes:
self._valid = True
self._value = scalar
elif type(scalar) == str:
self._valid = True
self._value = scalar.encode()
else:
raise ScalarInvalidTypeError("Invalid type for Binary scalar")
49 changes: 25 additions & 24 deletions cloudquery/sdk/scalar/bool.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError, NULL_VALUE
from typing import Any


def parse_string_to_bool(input_string):
true_strings = ['true', 't', 'yes', 'y', '1']
false_strings = ['false', 'f', 'no', 'n', '0']
true_strings = ["true", "t", "yes", "y", "1"]
false_strings = ["false", "f", "no", "n", "0"]

lower_input = input_string.lower()

Expand All @@ -15,34 +15,35 @@ def parse_string_to_bool(input_string):
else:
raise ScalarInvalidTypeError("Invalid boolean string: {}".format(input_string))


class Bool(Scalar):
def __init__(self, valid: bool = False, value: bool = False) -> None:
self._valid = valid
self._value = value
self._valid = valid
self._value = value

def __eq__(self, scalar: Scalar) -> bool:
if scalar is None:
return False
if type(scalar) == Bool:
return self._value == scalar._value and self._valid == scalar._valid
return False
if scalar is None:
return False
if type(scalar) == Bool:
return self._value == scalar._value and self._valid == scalar._valid
return False

def __str__(self) -> str:
return str(self._value) if self._valid else NULL_VALUE
return str(self._value) if self._valid else NULL_VALUE

@property
def value(self):
return self._value
return self._value

def set(self, value: Any):
if value is None:
return
if value is None:
return

if type(value) == bool:
self._value = value
elif type(value) == str:
self._value = parse_string_to_bool(value)
else:
raise ScalarInvalidTypeError("Invalid type for Bool scalar")
self._valid = True
if type(value) == bool:
self._value = value
elif type(value) == str:
self._value = parse_string_to_bool(value)
else:
raise ScalarInvalidTypeError("Invalid type for Bool scalar")

self._valid = True
50 changes: 25 additions & 25 deletions cloudquery/sdk/scalar/date32.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@

from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError, NULL_VALUE
from datetime import datetime, time
from typing import Any


class Date32(Scalar):
def __init__(self, valid: bool = False, value: bool = False) -> None:
self._valid = valid
self._value = value
self._valid = valid
self._value = value

def __eq__(self, scalar: Scalar) -> bool:
if scalar is None:
return False
if type(scalar) == Date32:
return self._value == scalar._value and self._valid == scalar._valid
return False
if scalar is None:
return False
if type(scalar) == Date32:
return self._value == scalar._value and self._valid == scalar._valid
return False

def __str__(self) -> str:
return str(self._value) if self._valid else NULL_VALUE
return str(self._value) if self._valid else NULL_VALUE

@property
def value(self):
return self._value
return self._value

def set(self, value: Any):
if value is None:
return
if value is None:
return

if type(value) == datetime:
self._value = value
elif type(value) == str:
self._value = datetime.strptime(value, "%Y-%m-%d")
elif type(value) == time:
self._value = datetime.combine(datetime.today(), value)
else:
raise ScalarInvalidTypeError("Invalid type for Bool scalar")
self._valid = True
if type(value) == datetime:
self._value = value
elif type(value) == str:
self._value = datetime.strptime(value, "%Y-%m-%d")
elif type(value) == time:
self._value = datetime.combine(datetime.today(), value)
else:
raise ScalarInvalidTypeError("Invalid type for Bool scalar")

self._valid = True
Loading