Skip to content
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

Add lint and CI #2

Merged
merged 1 commit into from
Apr 12, 2023
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
28 changes: 0 additions & 28 deletions .github/workflows/format.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

on:
push:
branches: [ main ]
pull_request:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry install
- name: Run yapf, isort, and flake8
run: |
poetry run tox -e yapf,isort,flake8
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test

on:
push:
branches: [ main ]
pull_request:

jobs:
tests:
runs-on: ${{ matrix.platform }}
strategy:
max-parallel: 4
matrix:
platform: [ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry run python -m pip install tox-gh-actions
poetry install
- name: Test with tox
run: poetry run tox
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist
__pycache__
__pycache__
.coverage
.tox/
4 changes: 2 additions & 2 deletions bqemulatormanager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from bqemulatormanager.manager import Manager
from bqemulatormanager.emulator import Emulator
from bqemulatormanager.schema import SchemaManager
from bqemulatormanager.manager import Manager
from bqemulatormanager.schema import SchemaManager
13 changes: 8 additions & 5 deletions bqemulatormanager/emulator.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import subprocess


class PortOccupiedError(Exception):
pass


class Emulator:
running_flg = False

def __init__(self, project_name: str, port: int, launch_emulator: bool = True, debug_mode:bool = False):
def __init__(self, project_name: str, port: int, launch_emulator: bool = True, debug_mode: bool = False):
self.project_name = project_name

if launch_emulator:
log_level = 'debug' if debug_mode else 'info'

if is_port_in_use(port):
raise PortOccupiedError(f'port {port} is occupied.')
self.running_flg = True
self.proc = subprocess.Popen(["bigquery-emulator", "--project", f"{project_name}", "--port", f"{port}" "--log-level", f"{log_level}"], shell=True)
self.structure = {}
self.proc = subprocess.Popen(["bigquery-emulator", "--project", f"{project_name}", "--port", f"{port}"
"--log-level", f"{log_level}"], shell=True)

def __del__(self):
if self.running_flg:
self.proc.terminate()


def is_port_in_use(port: int) -> bool:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
return s.connect_ex(('localhost', port)) == 0
15 changes: 10 additions & 5 deletions bqemulatormanager/manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Dict, List

import pandas as pd
from google.api_core.client_options import ClientOptions
Expand All @@ -11,8 +11,13 @@

class Manager:

def __init__(self, project: str = 'test', port: int = 9050, schema_path: str = 'master_schema.yaml',
launch_emulator:bool=True, debug_mode:bool=False, max_pool:int=20):
def __init__(self,
project: str = 'test',
port: int = 9050,
schema_path: str = 'master_schema.yaml',
launch_emulator: bool = True,
debug_mode: bool = False,
max_pool: int = 20):

original_port = port
for i in range(max_pool):
Expand All @@ -31,7 +36,7 @@ def __init__(self, project: str = 'test', port: int = 9050, schema_path: str = '
prod_client = bigquery.Client(project)

self.schema_manager = SchemaManager(client=prod_client, master_path=schema_path)
self.structure = {}
self.structure: Dict[str, Dict[str, bool]] = {}
self.project_name = project

def __enter__(self):
Expand Down Expand Up @@ -73,7 +78,7 @@ def create_table(self, dataset_name: str, table_name: str, schema: List[bigquery

table = bigquery.Table(f'{self.project_name}.{dataset_name}.{table_name}', schema=schema)
self.client.create_table(table)
self.structure[dataset_name][table_name] = {}
self.structure[dataset_name][table_name] = True

def query(self, sql: str) -> pd.DataFrame:
return self.client.query(sql).to_dataframe(create_bqstorage_client=False)
Loading