From a3a53a445e67ddcbbf8e4e9f077a2a7c47e15b3c Mon Sep 17 00:00:00 2001 From: Mark Shui Hu Date: Mon, 19 Feb 2024 17:15:08 +0100 Subject: [PATCH] move to src folder --- .pre-commit-config.yaml | 6 +--- pyproject.toml | 6 ++-- .../textmate_grammar}/__init__.py | 0 .../textmate_grammar}/elements.py | 14 ++++++++-- .../textmate_grammar}/exceptions.py | 0 .../textmate_grammar}/grammars/__init__.py | 0 .../grammars/markdown/LICENSE.txt | 0 .../grammars/markdown/__init__.py | 11 +++----- .../grammars/markdown/grammar.yaml | 0 .../grammars/matlab/__init__.py | 6 ++-- .../grammars/matlab/grammar.yaml | 0 .../grammars/matlab/license.txt | 0 .../textmate_grammar}/handler.py | 0 .../textmate_grammar}/language.py | 0 .../textmate_grammar}/logger.py | 0 .../textmate_grammar}/parser.py | 0 test/regression/__init__.py | 28 ++++++++----------- test/regression/test_matlab.py | 16 +++++------ 18 files changed, 40 insertions(+), 47 deletions(-) rename {textmate_grammar => src/textmate_grammar}/__init__.py (100%) rename {textmate_grammar => src/textmate_grammar}/elements.py (96%) rename {textmate_grammar => src/textmate_grammar}/exceptions.py (100%) rename {textmate_grammar => src/textmate_grammar}/grammars/__init__.py (100%) rename {textmate_grammar => src/textmate_grammar}/grammars/markdown/LICENSE.txt (100%) rename {textmate_grammar => src/textmate_grammar}/grammars/markdown/__init__.py (72%) rename {textmate_grammar => src/textmate_grammar}/grammars/markdown/grammar.yaml (100%) rename {textmate_grammar => src/textmate_grammar}/grammars/matlab/__init__.py (93%) rename {textmate_grammar => src/textmate_grammar}/grammars/matlab/grammar.yaml (100%) rename {textmate_grammar => src/textmate_grammar}/grammars/matlab/license.txt (100%) rename {textmate_grammar => src/textmate_grammar}/handler.py (100%) rename {textmate_grammar => src/textmate_grammar}/language.py (100%) rename {textmate_grammar => src/textmate_grammar}/logger.py (100%) rename {textmate_grammar => src/textmate_grammar}/parser.py (100%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d929476..2576b80 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,14 +1,10 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.2.1 + rev: v0.2.2 hooks: # Run the linter. - id: ruff args: [--fix] # Run the formatter. - id: ruff-format - - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.8.0' # Use the sha / tag you want to point at - hooks: - - id: mypy \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 1870322..d02c73c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ license = "MIT" readme = "README.md" repository = "https://github.com/watermarkhu/textmate-grammar-python" keywords = ["textmate", "tokenization"] -packages = [{include = "textmate_grammar"}] +packages = [{include = "textmate_grammar", from = "src"}] [tool.poetry.dependencies] python = "^3.11" @@ -30,8 +30,8 @@ types-pyyaml = "^6.0.12.12" ##################################### ruff ##################################### ruff = "^0.2.1" [tool.ruff] -include = ["pyproject.toml", "textmate_grammar/**/*.py"] -exclude = ["textmate_grammar/grammars/"] +include = ["pyproject.toml", "src/textmate_grammar/**/*.py"] +exclude = ["src/textmate_grammar/grammars/"] line-length = 100 indent-width = 4 diff --git a/textmate_grammar/__init__.py b/src/textmate_grammar/__init__.py similarity index 100% rename from textmate_grammar/__init__.py rename to src/textmate_grammar/__init__.py diff --git a/textmate_grammar/elements.py b/src/textmate_grammar/elements.py similarity index 96% rename from textmate_grammar/elements.py rename to src/textmate_grammar/elements.py index 6db08bd..7007642 100644 --- a/textmate_grammar/elements.py +++ b/src/textmate_grammar/elements.py @@ -176,6 +176,7 @@ def find( stop_tokens: str | list[str] = "", verbosity: int = -1, stack: list[str] | None = None, + attribute: str = "_subelements", ) -> Generator[tuple[ContentElement, list[str]], None, None]: """Find the next subelement that match the input token(s). @@ -195,7 +196,8 @@ def find( if verbosity: verbosity -= 1 - for child in self._subelements: + children: list[ContentElement] = getattr(self, attribute, self._subelements) + for child in children: if stop_tokens and ( child.token in stop_tokens or (stop_tokens == ["*"] and child.token not in tokens) @@ -212,10 +214,16 @@ def find( return None def findall( - self, tokens: str | list[str], stop_tokens: str | list[str] = "", verbosity: int = -1 + self, + tokens: str | list[str], + stop_tokens: str | list[str] = "", + verbosity: int = -1, + attribute: str = "_subelements", ) -> list[tuple[ContentElement, list[str]]]: """Returns subelements that match the input token(s).""" - return list(self.find(tokens, stop_tokens=stop_tokens, verbosity=verbosity)) + return list( + self.find(tokens, stop_tokens=stop_tokens, verbosity=verbosity, attribute=attribute) + ) def flatten(self) -> list[tuple[tuple[int, int], str, list[str]]]: """Converts the object to a flattened array of tokens per index.""" diff --git a/textmate_grammar/exceptions.py b/src/textmate_grammar/exceptions.py similarity index 100% rename from textmate_grammar/exceptions.py rename to src/textmate_grammar/exceptions.py diff --git a/textmate_grammar/grammars/__init__.py b/src/textmate_grammar/grammars/__init__.py similarity index 100% rename from textmate_grammar/grammars/__init__.py rename to src/textmate_grammar/grammars/__init__.py diff --git a/textmate_grammar/grammars/markdown/LICENSE.txt b/src/textmate_grammar/grammars/markdown/LICENSE.txt similarity index 100% rename from textmate_grammar/grammars/markdown/LICENSE.txt rename to src/textmate_grammar/grammars/markdown/LICENSE.txt diff --git a/textmate_grammar/grammars/markdown/__init__.py b/src/textmate_grammar/grammars/markdown/__init__.py similarity index 72% rename from textmate_grammar/grammars/markdown/__init__.py rename to src/textmate_grammar/grammars/markdown/__init__.py index 0dec44e..dfca420 100644 --- a/textmate_grammar/grammars/markdown/__init__.py +++ b/src/textmate_grammar/grammars/markdown/__init__.py @@ -1,13 +1,10 @@ -from pathlib import Path import shutil -import yaml +from pathlib import Path +import yaml tmLanguageFile = ( - Path(__file__).parents[3] - / "syntaxes" - / "markdown" - / "markdown.tmLanguage.base.yaml" + Path(__file__).parents[3] / "syntaxes" / "markdown" / "markdown.tmLanguage.base.yaml" ) tmLanguageYAML = Path(__file__).parent / "grammar.yaml" @@ -15,7 +12,7 @@ if tmLanguageFile.exists(): shutil.copyfile(tmLanguageFile, tmLanguageYAML) -with open(tmLanguageYAML, "r") as file: +with open(tmLanguageYAML) as file: try: GRAMMAR = yaml.load(file.read(), Loader=yaml.CLoader) except ImportError: diff --git a/textmate_grammar/grammars/markdown/grammar.yaml b/src/textmate_grammar/grammars/markdown/grammar.yaml similarity index 100% rename from textmate_grammar/grammars/markdown/grammar.yaml rename to src/textmate_grammar/grammars/markdown/grammar.yaml diff --git a/textmate_grammar/grammars/matlab/__init__.py b/src/textmate_grammar/grammars/matlab/__init__.py similarity index 93% rename from textmate_grammar/grammars/matlab/__init__.py rename to src/textmate_grammar/grammars/matlab/__init__.py index 2a0e32a..9a17c42 100644 --- a/textmate_grammar/grammars/matlab/__init__.py +++ b/src/textmate_grammar/grammars/matlab/__init__.py @@ -1,7 +1,7 @@ -from pathlib import Path import plistlib -import yaml +from pathlib import Path +import yaml tmLanguageFile = ( Path(__file__).parents[3] @@ -20,7 +20,7 @@ with open(tmLanguageYAML, "w") as f: f.write(yaml.dump(GRAMMAR, indent=2)) else: - with open(tmLanguageYAML, "r") as file: + with open(tmLanguageYAML) as file: try: GRAMMAR = yaml.load(file.read(), Loader=yaml.CLoader) except ImportError: diff --git a/textmate_grammar/grammars/matlab/grammar.yaml b/src/textmate_grammar/grammars/matlab/grammar.yaml similarity index 100% rename from textmate_grammar/grammars/matlab/grammar.yaml rename to src/textmate_grammar/grammars/matlab/grammar.yaml diff --git a/textmate_grammar/grammars/matlab/license.txt b/src/textmate_grammar/grammars/matlab/license.txt similarity index 100% rename from textmate_grammar/grammars/matlab/license.txt rename to src/textmate_grammar/grammars/matlab/license.txt diff --git a/textmate_grammar/handler.py b/src/textmate_grammar/handler.py similarity index 100% rename from textmate_grammar/handler.py rename to src/textmate_grammar/handler.py diff --git a/textmate_grammar/language.py b/src/textmate_grammar/language.py similarity index 100% rename from textmate_grammar/language.py rename to src/textmate_grammar/language.py diff --git a/textmate_grammar/logger.py b/src/textmate_grammar/logger.py similarity index 100% rename from textmate_grammar/logger.py rename to src/textmate_grammar/logger.py diff --git a/textmate_grammar/parser.py b/src/textmate_grammar/parser.py similarity index 100% rename from textmate_grammar/parser.py rename to src/textmate_grammar/parser.py diff --git a/test/regression/__init__.py b/test/regression/__init__.py index 88fbc99..67e1a7f 100644 --- a/test/regression/__init__.py +++ b/test/regression/__init__.py @@ -1,45 +1,39 @@ -import re -import os import logging -import warnings +import os import platform +import re import subprocess -from pathlib import Path +import warnings from abc import ABC, abstractmethod from itertools import groupby +from pathlib import Path - -from textmate_grammar.language import LanguageParser import textmate_grammar - +from textmate_grammar.language import LanguageParser MODULE_ROOT = Path(textmate_grammar.__path__[0]) -INDEX = MODULE_ROOT.parent / "test" / "regression" / "node_root" / "index.js" +INDEX = MODULE_ROOT.parents[1] / "test" / "regression" / "node_root" / "index.js" logging.getLogger().setLevel(logging.DEBUG) logging.getLogger("textmate_grammar").setLevel(logging.INFO) if platform.system() != "Linux": - warnings.warn(f"Regression tests on {os.name} is not supported") + warnings.warn(f"Regression tests on {os.name} is not supported", stacklevel=1) -elif ( - "CI" not in os.environ or not os.environ["CI"] or "GITHUB_RUN_ID" not in os.environ -): +elif "CI" not in os.environ or not os.environ["CI"] or "GITHUB_RUN_ID" not in os.environ: nvm_dir = Path(os.environ["HOME"]) / ".nvm" nvm_script = nvm_dir / "nvm.sh" env = os.environ.copy() env["NVM_DIR"] = str(nvm_dir) if not nvm_script.exists(): - raise EnvironmentError( + raise OSError( 'Node environment not setup. Please run "bash install.sh" in the test/regression directory. ' ) - pipe = subprocess.Popen( - f". {nvm_script}; env", stdout=subprocess.PIPE, shell=True, env=env - ) + pipe = subprocess.Popen(f". {nvm_script}; env", stdout=subprocess.PIPE, shell=True, env=env) output = pipe.communicate()[0] - NODE_ENV = dict((line.split("=", 1) for line in output.decode().splitlines())) + NODE_ENV = dict(line.split("=", 1) for line in output.decode().splitlines()) else: NODE_ENV = os.environ.copy() diff --git a/test/regression/test_matlab.py b/test/regression/test_matlab.py index 8957510..2314124 100755 --- a/test/regression/test_matlab.py +++ b/test/regression/test_matlab.py @@ -1,10 +1,11 @@ -from pathlib import Path import logging -import pytest +from pathlib import Path -from textmate_grammar.language import LanguageParser +import pytest from textmate_grammar.grammars import matlab -from . import RegressionTestClass, MODULE_ROOT +from textmate_grammar.language import LanguageParser + +from . import MODULE_ROOT, RegressionTestClass logging.getLogger().setLevel(logging.DEBUG) logging.getLogger("textmate_grammar").setLevel(logging.INFO) @@ -12,7 +13,7 @@ test_files = ( [ - str(MODULE_ROOT.parent / "syntaxes" / "matlab" / (file + ".m")) + str(MODULE_ROOT.parents[1] / "syntaxes" / "matlab" / (file + ".m")) for file in [ "Account", "AnEnum", @@ -23,10 +24,7 @@ "PropertyValidation", ] ] - + [ - str(test) - for test in (MODULE_ROOT.parent / "syntaxes" / "matlab" / "test").glob("*.m") - ] + + [str(test) for test in (MODULE_ROOT.parents[1] / "syntaxes" / "matlab" / "test").glob("*.m")] + [ str(Path(__file__).parent.resolve() / "matlab" / (file + ".m")) for file in ["test_multiple_inheritance_ml"]