]> Git repositories of Lucie Scarlet - remilia.git/commitdiff
Initial commit
authorLucie Scarlet <lucie@scarlet.moe>
Thu, 5 Sep 2024 14:58:27 +0000 (16:58 +0200)
committerLucie Scarlet <lucie@scarlet.moe>
Thu, 5 Sep 2024 14:58:27 +0000 (16:58 +0200)
.gitignore [new file with mode: 0644]
.gitlab-ci.yml [new file with mode: 0644]
consts.py [new file with mode: 0644]
exceptions.py [new file with mode: 0644]
header.rem [new file with mode: 0644]
importer.py [new file with mode: 0644]
remilia [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..4fa1100
--- /dev/null
@@ -0,0 +1,197 @@
+# Created by https://www.toptal.com/developers/gitignore/api/vim,python
+# Edit at https://www.toptal.com/developers/gitignore?templates=vim,python
+
+### Python ###
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+.pybuilder/
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+#   For a library or package, you might want to ignore these files since the code is
+#   intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# poetry
+#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
+#   This is especially recommended for binary packages to ensure reproducibility, and is more
+#   commonly ignored for libraries.
+#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
+#poetry.lock
+
+# pdm
+#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
+#pdm.lock
+#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
+#   in version control.
+#   https://pdm.fming.dev/#use-with-ide
+.pdm.toml
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
+
+# Cython debug symbols
+cython_debug/
+
+# PyCharm
+#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
+#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
+#  and can be added to the global gitignore or merged into this file.  For a more nuclear
+#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
+#.idea/
+
+### Python Patch ###
+# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
+poetry.toml
+
+# ruff
+.ruff_cache/
+
+# LSP config files
+pyrightconfig.json
+
+### Vim ###
+# Swap
+[._]*.s[a-v][a-z]
+!*.svg  # comment out if you don't need vector files
+[._]*.sw[a-p]
+[._]s[a-rt-v][a-z]
+[._]ss[a-gi-z]
+[._]sw[a-p]
+
+# Session
+Session.vim
+Sessionx.vim
+
+# Temporary
+.netrwhist
+*~
+# Auto-generated tag files
+tags
+# Persistent undo
+[._]*.un~
+
+# End of https://www.toptal.com/developers/gitignore/api/vim,python
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644 (file)
index 0000000..195d65e
--- /dev/null
@@ -0,0 +1,13 @@
+# You can override the included template(s) by including variable overrides
+# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
+# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/pipeline/#customization
+# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
+# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings
+# Note that environment variables can be set in several places
+# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
+stages:
+- test
+sast:
+  stage: test
+include:
+- template: Security/SAST.gitlab-ci.yml
diff --git a/consts.py b/consts.py
new file mode 100644 (file)
index 0000000..c85e46b
--- /dev/null
+++ b/consts.py
@@ -0,0 +1,4 @@
+KW_IMPORT = "!include"
+SIZEOF = len(KW_IMPORT) + 2
+FILE_EXT = ".rem"
+HTML_EXT = ".html"
diff --git a/exceptions.py b/exceptions.py
new file mode 100644 (file)
index 0000000..617b25c
--- /dev/null
@@ -0,0 +1,6 @@
+class CircularImportException(ModuleNotFoundError):
+    pass
+
+
+class UnterminatedQuoteException(ValueError):
+    pass
diff --git a/header.rem b/header.rem
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/importer.py b/importer.py
new file mode 100644 (file)
index 0000000..f07efbe
--- /dev/null
@@ -0,0 +1,91 @@
+from typing import Self, Optional
+from io import TextIOWrapper
+from exceptions import *
+from consts import *
+
+
+class Remilia:
+    def __init__(self: Self, src: str) -> None:
+        self.src = src
+        self.__seen_files: list[str] = [src]
+
+    def parse_file(self: Self) -> bool:
+        done: bool = False
+        with open(self.src) as src:
+            imports: dict[int, list[int]] = {}
+            import_paths: dict[int, list[str]] = {}
+            for i, line in enumerate(src.readlines(), 1):
+                if KW_IMPORT in line:
+                    inter: list[int] = []
+                    inter.append(line.index(KW_IMPORT))
+                    while True:
+                        try:
+                            n: int = inter[-1] + 1
+                            li: int = line.index(KW_IMPORT, n)
+                            inter.append(li)
+                        except ValueError:
+                            break
+                    imports[i] = inter
+                if i in imports:
+                    paths: list[str] = []
+                    for loc in imports[i]:
+                        try:
+                            asz: int = loc + SIZEOF
+                            end: int = line.index('"', asz + 1)
+                            print(f"{end = }")
+                            new_path: str = line[asz:end]
+                            print(f"{new_path = }")
+                            paths.append(new_path)
+                        except ValueError:
+                            raise UnterminatedQuoteException(
+                                f"Could not find another quote at line {i}"
+                            )
+                    import_paths[i] = paths
+            if len(imports) == 0:
+                done = True
+            if not done:
+                self.__read_and_replace(imports, import_paths)
+            return done
+
+    def printall(self):
+        self.parse_file()
+
+    # TODO: make this private
+    def import_file(self: Self, path: str) -> None:
+        if path in self.__seen_files:
+            raise CircularImportException(
+                f"Circular import! I've already seen file {path}!"
+            )
+        self.__seen_files.append(path)
+
+    def __read_and_replace(
+        self: Self, imports: dict[int, list[int]], import_paths: dict[int, list[str]]
+    ) -> None:
+        with open(self.src.replace(FILE_EXT, HTML_EXT), "rw") as src:
+            final_str: str = ""
+            for i, line in enumerate(src.readlines(), 1):
+                if i in imports.keys():
+                    for n, __import in enumerate(imports[i]):
+                        repl_str = (
+                            line[:__import]
+                            + self.__get_file_contents(import_paths[i][n])
+                            + line[__import + len(import_paths[i][n]) + 1]
+                        )
+                    pass
+
+    def __get_file_contents(self: Self, file: str) -> str:
+        self.import_file(file)
+        with open(file) as f:
+            return f.read()
+
+
+def replace_at_location(original_string, start_index, length, replacement_string):
+    # Calculate the end index of the string to be replaced
+    end_index = start_index + length
+
+    # Create the new string by slicing and concatenating
+    new_string = (
+        original_string[:start_index] + replacement_string + original_string[end_index:]
+    )
+
+    return new_string
diff --git a/remilia b/remilia
new file mode 100644 (file)
index 0000000..bf3c03b
--- /dev/null
+++ b/remilia
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+from importer import Remilia
+import sys
+
+def mmain() -> int:
+    if len(sys.argv) < 2:
+        print(f"Usage: {sys.argv[0]} <file>")
+        return -1
+    compiler: Remilia = Remilia(sys.argv[1])
+    while True:
+        done: bool = compiler.parse_file()
+        if done: break
+
+    return 0
+
+def main() -> int:
+    test: Remilia = Remilia("./test.txt")
+    test.import_file("./test2.txt")
+    test.printall()
+    return 0
+
+if __name__ == '__main__':
+    raise SystemExit(main())