Skip to content

Commit

Permalink
refactor: move queries to root
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Feb 24, 2024
1 parent dc25f31 commit 69ad424
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 38 deletions.
5 changes: 0 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
* text eol=lf

src/** linguist-generated
bindings/** linguist-generated
binding.gyp linguist-generated
Cargo.toml linguist-generated

src/tree_sitter_pymanifest/** -linguist-generated
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ._core import parse, query, highlights

__author__ = 'ObserverOfTime'
__version__ = '0.2.0'
__version__ = '0.4.0'
__license__ = 'MIT'

__all__ = ['parse', 'query', 'highlights']
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from importlib.resources import files
from pathlib import PurePath
from pkg_resources import resource_filename
from sys import platform

from tree_sitter import Language, Parser

_language = Language(
PurePath(__file__).with_name('pymanifest') \
.with_suffix('.dll' if platform == 'win32' else '.so'),
PurePath(__file__).with_name('pymanifest').with_suffix(
{'win32': '.dll', 'darwin': '.dylib'}.get(platform, '.so')
),
'pymanifest'
)

_parser = Parser()
_parser.set_language(_language)

_highlights = files(__package__) / 'queries' / 'highlights.scm'

def parse(source):
"""Parse the given source code"""
if isinstance(source, str):
Expand All @@ -25,6 +28,4 @@ def query(query, node):

def highlights(tree):
"""Return the highlight groups for the given source tree"""
res = resource_filename(__package__, 'queries/highlights.scm')
with open(res, 'r') as hl:
return query(hl.read(), tree.root_node)
return query(_highlights.read_text(), tree.root_node)
2 changes: 1 addition & 1 deletion bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn language() -> Language {
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");

/// The syntax highlighting queries.
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../src/tree_sitter_pymanifest/queries/highlights.scm");
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");

#[cfg(test)]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"tree-sitter": [
{
"scope": "source.pymanifest",
"highlights": "src/tree_sitter_pymanifest/queries/highlights.scm",
"highlights": "queries/highlights.scm",
"file-types": ["MANIFEST.in"]
}
]
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "tree-sitter-pymanifest"
version = "0.3.0"
version = "0.4.0"
description = "A tree-sitter parser for MANIFEST.in files"
keywords = ["tree-sitter", "parser", "lexer"]
classifiers = [
Expand All @@ -10,7 +10,7 @@ classifiers = [
"Topic :: Text Processing :: Linguistic"
]
dependencies = ["tree-sitter~=0.20"]
requires-python = ">=3.8"
requires-python = ">=3.9"

[project.license]
text = "MIT"
Expand All @@ -29,7 +29,7 @@ name = "ObserverOfTime"
email = "chronobserver@disroot.org"

[tool.cibuildwheel]
build = "cp38-*"
build = "cp39-*"

[tool.cibuildwheel.linux]
archs = ["x86_64"]
Expand Down
File renamed without changes.
49 changes: 28 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
#!/usr/bin/env python3

from pathlib import Path
from shutil import copy
from sys import platform
from pathlib import PurePath as Path

from setuptools import Extension, setup
from setuptools.command.build import build
from setuptools.command.build_ext import build_ext
from wheel.bdist_wheel import bdist_wheel

from tree_sitter import Language

class BuildExt(build_ext):
def finalize_options(self):
super().finalize_options()
self._ts_lib = Path(self.build_lib).joinpath(
'tree_sitter_pymanifest', 'pymanifest'
).with_suffix('.dll' if platform == 'win32' else '.so')
class Build(build):
def run(self):
source = Path(__file__).with_name('queries')
dest = Path(self.build_lib) / 'tree_sitter_pymanifest' / 'queries'
self.copy_tree(str(source), str(dest))
super().run()

class BuildExt(build_ext):
def copy_extensions_to_source(self):
new_file = Path(__file__).parent.joinpath(
'src', 'tree_sitter_pymanifest', self._ts_lib.name
lib_file = Path(__file__).parent.joinpath(
'bindings', 'python',
'tree_sitter_pymanifest',
self._ts_lib.name
)
copy(self._ts_lib, new_file)
self.copy_file(str(self._ts_lib), str(lib_file))

def build_extension(self, _):
self._ts_lib = Path(self.build_lib).joinpath(
'tree_sitter_pymanifest',
'pymanifest' + self.compiler.shared_lib_extension
)
Language.build_library(str(self._ts_lib), [''])


class BdistWheel(bdist_wheel):
def get_tag(self):
python, abi, plat = super().get_tag()
python, abi, platform = super().get_tag()
if python.startswith('cp'):
python, abi = 'cp38', 'abi3'
return python, abi, plat
python, abi = 'cp39', 'abi3'
return python, abi, platform


setup(
packages=[
'tree_sitter_pymanifest',
'tree_sitter_pymanifest.queries'
],
package_dir={'': 'src'},
packages=['tree_sitter_pymanifest'],
package_dir={'': 'bindings/python'},
package_data={
'tree_sitter_pymanifest.queries': ['*.scm']
},
ext_modules=[
Extension(
name='tree_sitter_pymanifest',
Expand All @@ -50,6 +56,7 @@ def get_tag(self):
],
cmdclass={
'bdist_wheel': BdistWheel,
'build_ext': BuildExt
'build_ext': BuildExt,
'build': Build
}
)

0 comments on commit 69ad424

Please sign in to comment.