Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Oct 13, 2023
0 parents commit 6885c5f
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
end_of_line = lf

[*.{yml,yaml,yug}]
indent_style = space
indent_size = 2

[grammars/*.txt]
insert_final_newline = false
1 change: 1 addition & 0 deletions .github/.templateMarker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KOLANICH/python_project_boilerplate.py
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
allow:
- dependency-type: "all"
15 changes: 15 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: typical python workflow
uses: KOLANICH-GHActions/typical-python-workflow@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
__pycache__
*.pyc
*.pyo
*.pgt
*.dot
/*.egg-info
/build
/dist
/.eggs
/tests/grammars
monkeytype.sqlite3
*.srctrlprj
*.srctrldb
*.srctrlbm
55 changes: 55 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest

variables:
DOCKER_DRIVER: overlay2
SAST_ANALYZER_IMAGE_TAG: latest
SAST_DISABLE_DIND: "true"
SAST_CONFIDENCE_LEVEL: 5
CODECLIMATE_VERSION: latest

include:
- template: SAST.gitlab-ci.yml
- template: Code-Quality.gitlab-ci.yml

.build:
tags:
- shared
- linux
stage: build
interruptible: true
variables:
GIT_DEPTH: "1"
PYTHONUSERBASE: ${CI_PROJECT_DIR}/python_user_packages

before_script:
- export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables`
#- git clone --depth=1 --filter=sparse:path=src/python https://github.com/waxeye-org/waxeye.git
- git clone --depth=1 https://github.com/waxeye-org/waxeye.git
- cd ./waxeye/src/python
- python3 ./setup.py bdist_wheel
- pip3 install --upgrade ./dist/*.whl
- cd ../../../

cache:
paths:
- /usr/local/site-packages
- /usr/local/lib/python*/site-packages

script:
- python3 setup.py bdist_wheel
- pip3 install --user --upgrade ./dist/*.whl
- cd ./tests
#- coverage run -a --branch --source=UniGrammar -m pytest --junitxml=./rspec.xml --forked ./test*.py
#- coverage report -m || true
#- coveralls || true
#- codecov || true
#- cd ..
- mkdir wheels
- mv ./dist/*.whl ./wheels/AptSourcesList-0.CI-py3-none-any.whl

artifacts:
paths:
- wheels
- $PYTHONUSERBASE
reports:
junit: ./rspec.xml
1 change: 1 addition & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No codes of conduct!
6 changes: 6 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include UNLICENSE
include *.md
include tests
global-include .editorconfig
global-include *.pgt
global-include *.pglr
13 changes: 13 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
UniGrammarRuntimeCore.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/)
===================
![GitLab Build Status](https://gitlab.com/UniGrammar/UniGrammarRuntimeCore.py/badges/master/pipeline.svg)
[![Coveralls Coverage](https://img.shields.io/coveralls/UniGrammar/UniGrammarRuntimeCore.py.svg)](https://coveralls.io/r/UniGrammar/UniGrammarRuntimeCore.py)
![GitLab Coverage](https://gitlab.com/UniGrammar/UniGrammarRuntimeCore.py/badges/master/coverage.svg)
[![N∅ dependencies](https://shields.io/badge/-N%E2%88%85_deps!-0F0)
[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://codeberg.org/KOLANICH-tools/antiflash.py)

Core of UniGrammar runtime contains a common framework with minimal dependencies that can be used when creating or wrapping parser generators without actually using `UniGrammar`. It is needed because if you are using OOP, you still have to have some interface, and it would be better if that interface is compatible to the one used in `UniGrammar`, so we won't have to rewrap stuff again and again. There are no stability guarantees though. You are expected to be able to do necessary changes in your code if you use it.

It is splitted into a separate package because we don't want to require its users to install `UniGrammarRuntime` and its dependencies.

It doesn't use `UniGrammar` package as a namespace because this stuff is incompatible to `pip install -e .` (components installed as `-e` break every other components, an it is probably a bug in python), so it has to have an own name `UniGrammarRuntimeCore`.
24 changes: 24 additions & 0 deletions UNLICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org/>
34 changes: 34 additions & 0 deletions UniGrammarRuntimeCore/ICompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import typing
from abc import ABC, abstractmethod
from pathlib import Path


class ICompiler(ABC):
"""Just a base class for compilers compiling grammars DSLs into objects."""

__slots__ = ()

@abstractmethod
def compileStr(self, grammarText: str, target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None):
"""Parses the DSL string and generates internal object from it. `fileName` must not be used for retrieving source, instead it must provide hints, useful for caching or more meaningful error messages and may be just ignored if the backend doesn't support its usage this way."""
raise NotImplementedError

def compileFile(self, grammarFile: Path, target: typing.Any = None):
"""Parses the DSL file and generates internal object from it. May be better because of caching, lower memory overhead and more meaningful error messages."""
return self.compileStr(grammarFile.read_text(encoding="utf-8"), target, fileName=grammarFile)

def compile(self, grammar: typing.Union[Path, str], target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None):
"""Just a convenience method dispatching between `compileStr` and `compileFile`"""
if isinstance(grammar, Path):
assert fileName is None
return self.compileFile(grammar, target)
return self.compileStr(grammar, target, grammar)


class DummyCompiler(ICompiler):
"""Returns text as a string"""

__slots__ = ()

def compileStr(self, grammarText: str, target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None):
return grammarText
70 changes: 70 additions & 0 deletions UniGrammarRuntimeCore/IParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import typing
from abc import ABC, abstractmethod
from pathlib import Path

from .ICompiler import ICompiler


class IParser(ABC):
"""Just initializes the parser and parses a string into a parse tree specific to backend"""

__slots__ = ()

NAME = None # Must match the name of the backend used in `META.name` in the backend in `UniGrammar.backends`

@abstractmethod
def __call__(self, s: str):
"""parses the stuff"""
raise NotImplementedError


class IParserFactory(ABC):
"""Produces instances of `IParser` classes and carries a common state. Should be a singleton."""

__slots__ = ()
PARSER_CLASS = None

def __init__(self):
self.__class__.ensureInitialized()

@classmethod
def ensureInitialized(cls):
"""Check if a module of the parser is immported, and if not, import it and save into a variable stored in the class"""
pass

def fromInternal(self, internalRepr: typing.Any, target: str = None) -> typing.Any:
return self.__class__.PARSER_CLASS(internalRepr)


class IParserFactoryFromSource(IParserFactory, ICompiler): # pylint: disable=abstract-method
"""A parser that is constructed from a text string representing a grammar in DSL rather than a precompiled file. This class has additional metods for DRY"""

__slots__ = ()

def fromStr(self, grammarText: str, target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None):
"""Constructs a parser from a `str` with parser-specific DSL"""
return self.fromInternal(self.compileStr(grammarText, target, fileName))

def fromFile(self, grammarFile: Path, target: typing.Any = None):
"""Constructs a parser from a file with parser-specific DSL"""
return self.fromInternal(self.compileFile(grammarText, target, fileName))


class IParserFactoryFromPrecompiled(IParserFactory):
"""A parser that is constructed from a python file. This class has additional metods for DRY"""

__slots__ = ()

@abstractmethod
def processEvaledGlobals(self, globalz: dict, grammarName: str):
"""
1. returns the ctor
2. does other stuff
"""
raise NotImplementedError

def compile(self, sourceOrAST: typing.Union[str, "ast.Module"], grammarName):
compiled = compile(sourceOrAST, grammarName + ".py", "exec", optimize=2)
globalz = {}
eval(compiled, globalz)
return self.processEvaledGlobals(globalz, grammarName)
13 changes: 13 additions & 0 deletions UniGrammarRuntimeCore/PoolManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PoolManager:
__slots__ = ("constructed",)

def __init__(self):
self.constructed = {}

def __call__(self, constuctorClass: type, *args, **kwargs):
if constuctorClass not in self.constructed:
obj = constuctorClass(*args, **kwargs)
self.constructed[obj.__class__] = obj
else:
obj = self.constructed[constuctorClass]
return obj
Empty file.
37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[build-system]
requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"]
build-backend = "setuptools.build_meta"

[project]
name = "UniGrammarRuntimeCore"
authors = [{name = "KOLANICH"}]
description = "The core of framework for runtime for UniGrammar. Can be used by third-party tools not using UniGrammar, but when it is desireable to make integration with UniGrammar easier."
readme = "ReadMe.md"
keywords = ["grammars", "UniGrammar"]
license = {text = "Unlicense"}
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: Public Domain",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Security",
"Topic :: Text Processing",
]
requires-python = ">=3.4"
dynamic = ["version"]

[project.urls]
Homepage = "https://codeberg.org/UniGrammar/UniGrammarRuntimeCore.py"

[tool.setuptools]
zip-safe = true
include-package-data = true

[tool.setuptools.packages]
find = {namespaces = false}

[tool.setuptools_scm]

0 comments on commit 6885c5f

Please sign in to comment.