Skip to content

Commit

Permalink
fixed issues for 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
daimor committed Aug 22, 2024
1 parent b39e402 commit ad37d27
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 91 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"

steps:
- uses: actions/checkout@v3
Expand All @@ -39,13 +40,8 @@ jobs:
pip install -r requirements-iris.txt
pip install -e .
- name: Run IRIS Container
run: |
docker run --rm -d -p 1972:1972 intersystemsdc/iris-community \
-a 'iris session iris -U %SYS "##class(Security.Users).UnExpireUserPasswords(\"*\")"'
- name: Run unit tests
run: coverage run --source irissqlcli -m pytest
run: pytest --container containers.intersystems.com/intersystems/iris-community:latest

# - name: Run Linters
# if: matrix.python-version == '3.10'
Expand Down
1 change: 1 addition & 0 deletions irissqlcli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__version__ = "0.5.5"
from . import main
from . import utils


def cli():
Expand Down
49 changes: 28 additions & 21 deletions irissqlcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import shutil
import threading
import traceback
from urllib.parse import urlparse
from collections import namedtuple
from time import time
from getpass import getuser
Expand All @@ -37,6 +36,7 @@
from prompt_toolkit.shortcuts import CompleteStyle, PromptSession

from irissqlcli.completion_refresher import CompletionRefresher
from irissqlcli.utils import parse_uri

from .__init__ import __version__
from .clitoolbar import create_toolbar_tokens_func
Expand All @@ -48,6 +48,7 @@
from .style import style_factory, style_factory_output
from .packages.encodingutils import utf8tounicode, text_type
from .packages import special
from .packages.special import NO_QUERY
from .packages.prompt_utils import confirm, confirm_destructive_query

COLOR_CODE_REGEX = re.compile(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))")
Expand Down Expand Up @@ -76,7 +77,6 @@ class IRISSQLCliQuitError(Exception):


class IRISSqlCli(object):

default_prompt = "[SQL]\\u@\\h:\\d> "
max_len_prompt = 45

Expand Down Expand Up @@ -161,6 +161,14 @@ def register_special_commands(self):
aliases=("tableformat", "\\T"),
case_sensitive=True,
)
special.register_special_command(
self.echo_test,
".echo",
"",
"Outputs the value.",
case_sensitive=True,
arg_type=special.PARSED_QUERY,
)
special.register_special_command(
self.change_prompt_format,
"prompt",
Expand All @@ -180,6 +188,14 @@ def change_table_format(self, arg, **_):
msg += "\n\t{}".format(table_type)
yield (None, None, None, msg)

def echo_test(self, arg, **_):
msg = arg
if len(msg) > 1:
msg = msg[1:-1] if msg[0] == "'" and msg[-1] == "'" else msg
msg = msg[1:-1] if msg[0] == '"' and msg[-1] == '"' else msg
print(msg)
yield (None, None, None, None)

def change_prompt_format(self, arg, **_):
"""
Change the prompt format.
Expand Down Expand Up @@ -425,7 +441,7 @@ def execute_command(self, text, handle_closed_connection=True):
# if handle_closed_connection:
# self._handle_server_closed_connection(text)
except (IRISSQLCliQuitError, EOFError) as e:
raise
raise e
except Exception as e:
logger.error("sql: %r, error: %r", text, e)
logger.error("traceback: %r", traceback.format_exc())
Expand Down Expand Up @@ -529,7 +545,6 @@ def get_output_margin(self, status=None):
return margin

def initialize_logging(self):

log_file = self.config["main"]["log_file"]
if log_file == "default":
log_file = config_location() + "log"
Expand Down Expand Up @@ -814,7 +829,13 @@ def format_status(cur, status):
help="Force password prompt.",
)
@click.option("-v", "--version", is_flag=True, help="Version of irissqlcli.")
@click.option("-n", "--nspace", "namespace_opt", help="namespace name to connect to.")
@click.option(
"-n",
"--nspace",
"namespace_opt",
envvar="IRIS_NAMESPACE",
help="namespace name to connect to.",
)
@click.option(
"-q",
"--quiet",
Expand Down Expand Up @@ -891,8 +912,8 @@ def cli(
uri, hostname, port, namespace, username
)

namespace = namespace_opt or namespace or "USER"
username = username_opt or username
namespace = namespace or namespace_opt or "USER"
username = username or username_opt
irissqlcli = IRISSqlCli(
prompt_passwd,
quiet,
Expand Down Expand Up @@ -980,20 +1001,6 @@ def cli(
exit(1)


def parse_uri(uri, hostname=None, port=None, namespace=None, username=None):
parsed = urlparse(uri)
embedded = False
if str(parsed.scheme).startswith("iris"):
namespace = parsed.path.split("/")[1] if parsed.path else None or namespace
username = parsed.username or username
password = parsed.password or None
hostname = parsed.hostname or hostname
port = parsed.port or port
if parsed.scheme == "iris+emb":
embedded = True
return hostname, port, namespace, username, password, embedded


class ISC_StdoutTypeWrapper(object):
def __init__(self, stdout, fileno) -> None:
self._stdout = stdout
Expand Down
6 changes: 3 additions & 3 deletions irissqlcli/packages/parseutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# This matches everything except spaces, parens, colon, comma, and period
"most_punctuations": re.compile(r"([^\.():,\s]+)$"),
# This matches everything except a space.
"all_punctuations": re.compile("([^\s]+)$"),
"all_punctuations": re.compile(r"([^\s]+)$"),
}


Expand Down Expand Up @@ -40,9 +40,9 @@ def last_word(text, include="alphanum_underscore"):
'def'
>>> last_word('bac $def', include='most_punctuations')
'$def'
>>> last_word('bac \def', include='most_punctuations')
>>> last_word('bac \\def', include='most_punctuations')
'\\\\def'
>>> last_word('bac \def;', include='most_punctuations')
>>> last_word('bac \\def;', include='most_punctuations')
'\\\\def;'
>>> last_word('bac::def', include='most_punctuations')
'def'
Expand Down
1 change: 1 addition & 0 deletions irissqlcli/packages/special/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ def export(defn):
return defn


from .main import NO_QUERY, RAW_QUERY, PARSED_QUERY
from . import dbcommands
from . import iocommands
2 changes: 1 addition & 1 deletion irissqlcli/sqlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def __init__(self, supported_formats=(), keyword_casing="auto"):
self.reserved_words = set()
for x in self.keywords:
self.reserved_words.update(x.split())
self.name_pattern = compile("^[_a-z][_a-z0-9\$]*$")
self.name_pattern = compile(r"^[_a-z][_a-z0-9\$]*$")

self.special_commands = []
self.table_formats = supported_formats
Expand Down
16 changes: 15 additions & 1 deletion irissqlcli/sqlexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import traceback

from .packages import special
from .utils import parse_uri

_logger = logging.getLogger(__name__)


class SQLExecute:

schemas_query = """
SELECT
SCHEMA_NAME
Expand Down Expand Up @@ -68,6 +68,17 @@ def __init__(

self.connect()

def from_uri(uri):
hostname, port, namespace, username, password, embedded = parse_uri(uri)
return SQLExecute(
hostname=hostname,
port=port,
namespace=namespace,
username=username,
password=password,
embedded=embedded,
)

def connect(self):
conn_params = {
"hostname": self.hostname,
Expand Down Expand Up @@ -102,6 +113,9 @@ def run(
sqltemp = []
sqlarr = []

statement = "\n".join(
[line for line in statement.split("\n") if not line.startswith("--")]
)
if statement.startswith("--"):
sqltemp = statement.split("\n")
sqlarr.append(sqltemp[0])
Expand Down
15 changes: 15 additions & 0 deletions irissqlcli/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from urllib.parse import urlparse


def parse_uri(uri, hostname=None, port=None, namespace=None, username=None):
parsed = urlparse(uri)
embedded = False
if str(parsed.scheme).startswith("iris"):
namespace = parsed.path.split("/")[1] if parsed.path else None or namespace
username = parsed.username or username
password = parsed.password or None
hostname = parsed.hostname or hostname
port = parsed.port or port
if parsed.scheme == "iris+emb":
embedded = True
return hostname, port, namespace, username, password, embedded
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ codecov
black
wheel
autoflake
testcontainers-iris
2 changes: 1 addition & 1 deletion requirements-iris.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://github.com/intersystems-community/intersystems-irispython/releases/download/3.5.0/intersystems_iris-3.5.0-py3-none-any.whl
https://github.com/intersystems-community/intersystems-irispython/releases/download/3.8.0/intersystems_iris-3.8.0-py3-none-any.whl
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ packages =
irissqlcli

[tool:pytest]
addopts = --capture=sys
testpaths =
tests
addopts = -ra
--capture=sys
--showlocals
--doctest-modules
--doctest-ignore-import-errors
Expand Down
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def open_file(filename):
"prompt_toolkit>=3.0.3,<4.0.0",
"sqlparse >=0.3.0,<0.5",
"configobj >= 5.0.6",
"pendulum ~= 2.1.0",
"pendulum ~= 3.0.0",
"cli_helpers[styles] >= 2.2.1",
]

Expand Down Expand Up @@ -57,10 +57,6 @@ def open_file(filename):
"Operating System :: Unix",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: SQL",
"Topic :: Database",
"Topic :: Database :: Front-Ends",
Expand Down
75 changes: 63 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,64 @@

import os
import pytest
from pytest import Config
from testcontainers.iris import IRISContainer
from utils import (
IRIS_HOSTNAME,
IRIS_PORT,
IRIS_NAMESPACE,
IRIS_USERNAME,
IRIS_PASSWORD,
db_connection,
drop_tables,
)
import irissqlcli.sqlexecute
from irissqlcli.sqlexecute import SQLExecute
from click.testing import CliRunner


def pytest_addoption(parser):
group = parser.getgroup("iris")

group.addoption(
"--container",
action="store",
default=None,
type=str,
help="Docker image with IRIS",
)


def pytest_configure(config: Config):
global iris
iris = None
container = config.getoption("container")
if not container:
pytest.iris_hostname = os.getenv("IRIS_HOSTNAME", "localhost")
pytest.iris_port = int(os.getenv("IRIS_PORT", 1972))
pytest.iris_username = os.getenv("IRIS_USERNAME", "_SYSTEM")
pytest.iris_password = os.getenv("IRIS_PASSWORD", "SYS")
pytest.iris_namespace = os.getenv("IRIS_NAMESPACE", "USER")
pytest.url
return

print("Starting IRIS container:", container)
iris = IRISContainer(
container,
username="irissqlcli",
password="irissqlcli",
namespace="TEST",
license_key=os.path.expanduser("~/iris-community.key"),
)
iris.start()
print("dburi:", iris.get_connection_url())
pytest.dburi = iris.get_connection_url()
pytest.iris_hostname = "localhost"
pytest.iris_port = int(iris.get_exposed_port(1972))
pytest.iris_username = iris.username
pytest.iris_password = iris.password
pytest.iris_namespace = iris.namespace


def pytest_unconfigure(config):
global iris
if iris and iris._container:
print("Stopping IRIS container", iris)
iris.stop()


@pytest.fixture(scope="function")
Expand All @@ -32,12 +80,15 @@ def cursor(connection):

@pytest.fixture
def executor(connection):
return irissqlcli.sqlexecute.SQLExecute(
hostname=IRIS_HOSTNAME,
port=IRIS_PORT,
namespace=IRIS_NAMESPACE,
username=IRIS_USERNAME,
password=IRIS_PASSWORD,
return SQLExecute.from_uri(pytest.dburi)


@pytest.fixture
def runner():
return CliRunner(
env={
"IRIS_URI": pytest.dburi,
}
)


Expand Down
Loading

0 comments on commit ad37d27

Please sign in to comment.