diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f284411 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022-present RGBCube + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +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 OR COPYRIGHT HOLDERS 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. \ No newline at end of file diff --git a/README.md b/README.md index c9a4566..04194f3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,47 @@ -# Dotted Markup Language +# 🈷️ Dotted Markup Language +Translate text to and from DML with ease. + +## 📥 Installation + +Execute `pip install dotted-ml`. + +Add `import dml` to the top of your project. + +## ♿ Usage + +### 🚮 Encoding + +```python +import dml + +mycode = """ +myvar = "Hello, world!" +print(myvar) +""" + +dml.encode(mycode, out="mycode.dml") +``` + +`mycode.dml`: + +``` +܁٠܁٠۰܁܁٠܁܁٠܁۰܁܁܁܁٠٠܁۰܁܁܁٠܁܁٠۰܁܁٠٠٠٠܁۰܁܁܁٠٠܁٠۰܁٠٠٠٠٠۰܁܁܁܁٠܁۰܁٠٠٠٠٠۰܁٠٠٠܁٠۰܁٠٠܁٠٠٠۰܁܁٠٠܁٠܁۰܁܁٠܁܁٠٠۰܁܁٠܁܁٠٠۰܁܁٠܁܁܁܁۰܁٠܁܁٠٠۰܁٠٠٠٠٠۰܁܁܁٠܁܁܁۰܁܁٠܁܁܁܁۰܁܁܁٠٠܁٠۰܁܁٠܁܁٠٠۰܁܁٠٠܁٠٠۰܁٠٠٠٠܁۰܁٠٠٠܁٠۰܁٠܁٠۰܁܁܁٠٠٠٠۰܁܁܁٠٠܁٠۰܁܁٠܁٠٠܁۰܁܁٠܁܁܁٠۰܁܁܁٠܁٠٠۰܁٠܁٠٠٠۰܁܁٠܁܁٠܁۰܁܁܁܁٠٠܁۰܁܁܁٠܁܁٠۰܁܁٠٠٠٠܁۰܁܁܁٠٠܁٠۰܁٠܁٠٠܁۰܁٠܁٠۰ +``` + +### ♻️ Decoding + +```python +import dml + +dml.decode_file("mycode.dml", out="mycode.py") +``` + +`mycode.py`: + +```python + +myvar = "Hello, world!" +print(myvar) + +``` \ No newline at end of file diff --git a/dml/__init__.py b/dml/__init__.py new file mode 100644 index 0000000..4e21533 --- /dev/null +++ b/dml/__init__.py @@ -0,0 +1,16 @@ +""" +Dotted Markup Language + +Translate text to and from DML with ease. +""" + +__title__ = "DML" +__author__ = "RGBCube" +__license__ = "MIT" +__copyright__ = "Copyright (c) 2022-present RGBCube" +__version__ = "1.0.0" + +from .encoder import * +from .errors import * +from .fileutils import * +from .run import * diff --git a/dml/encoder.py b/dml/encoder.py new file mode 100644 index 0000000..ddff0ef --- /dev/null +++ b/dml/encoder.py @@ -0,0 +1,61 @@ +import typing as t + +from .errors import DecodeError +from .symbols import symbols as s + +__all__ = ("encode", "decode") + + +def encode(text: t.Union[t.Generator, t.List[str], str], *, out: str = None) -> t.Optional[t.Generator[str, None, None]]: + """Encodes text into Dotted Markup Language (DML) + + Arguments: + text (Union[Generator, List[str], str]): The text to encode. + out (str): The filepath to write the encoded file to. If not specified, the encoded text will be returned as a generator. + + Returns: + Optional[Generator[str, None, None]]: The encoded text as a generator. + """ + + def inner() -> t.Generator[str, None, None]: + for char_ in text: + yield format(ord(char_), "b").replace("0", s["0"]).replace("1", s["1"]) + s["stop"] + + if out: + out = out + ".dml" if not out.endswith(".dml") else out + with open(out, "w") as f: + for char in inner(): + f.write(char) + else: + return inner() + + +def decode(text: t.Union[t.Generator, t.List[str], str], *, out: str = None) -> t.Optional[t.Generator[str, None, None]]: + """Decodes text from Dotted Markup Language (DML) + + Arguments: + text (Union[Generator, List[str], str]): The text to decode. + out (str): The filepath to write the decoded file to. If not specified, the decoded text will be returned as a generator. + + Returns: + Optional[Generator[str, None, None]]: The decoded text as a generator. + + Raises: + DecodeError: If the text is not valid DML. + """ + + def inner() -> t.Generator[str, None, None]: + nonlocal text + if isinstance(text, str): + text = [e + s["stop"] for e in text.split(s["stop"]) if e] + for char_ in text: + if any(e not in s.values() for e in char_): + raise DecodeError(f"Invalid character: '{char_}', expected {s['1']}, {s['0']} or {s['stop']}") + yield chr(int(char_[:-1].replace(s['0'], "0").replace(s['1'], "1"), 2)) + + if out: + with open(out, "w") as f: + for char in inner(): + f.write(char) + else: + return inner() diff --git a/dml/errors.py b/dml/errors.py new file mode 100644 index 0000000..d4ec0fc --- /dev/null +++ b/dml/errors.py @@ -0,0 +1,11 @@ +__all__ = ("DottedMarkupLanguageException", "DecodeError") + + +class DottedMarkupLanguageException(Exception): + """Base class for all exceptions in this module.""" + pass + + +class DecodeError(DottedMarkupLanguageException): + """Raised when there is an error decoding a string.""" + pass diff --git a/dml/fileutils.py b/dml/fileutils.py new file mode 100644 index 0000000..549459c --- /dev/null +++ b/dml/fileutils.py @@ -0,0 +1,67 @@ +import typing as t + +from .encoder import encode, decode +from .errors import DecodeError +from .symbols import symbols as s + +__all__ = ("decode_file", "encode_file") + + +def encode_file(fp: str, *, out: str = None) -> t.Optional[t.Generator[str, None, None]]: + """Encodes a file to DML. + + Arguments: + fp (str): The filepath to the file to encode. + out (str): The filepath to write the encoded file to. If not specified, the encoded text will be returned as a generator. + + Returns: + Optional[Generator[str, None, None]]: The encoded text as a generator. + """ + + def read_file() -> t.Generator[str, None, None]: + with open(fp) as f_: + while char_ := f_.read(1): + yield char_ + + if out: + out = out + ".dml" if not out.endswith(".dml") else out + with open(out, "w") as f: + for char in encode(read_file()): + f.write(char) + else: + return encode(read_file()) + + +def decode_file(fp: str, *, out: str = None) -> t.Optional[t.Generator[str, None, None]]: + """Decodes a file that has DML encoded in it. + + Arguments: + fp (str): The filepath to the file to decode. + out (str): The filepath to write the decoded file to. If not specified, the decoded text will be returned as a generator. + + Returns: + Optional[Generator[str, None, None]]: The decoded text as a generator. + + Raises: + DecodeError: If the file is not valid DML. + """ + + def read_file() -> t.Generator[str, None, None]: + buffer = "" + with open(fp) as f_: + while char_ := f_.read(1): + if char_ not in s.values(): + raise DecodeError(f"Invalid character: '{char_}', expected {s['1']}, {s['0']} or {s['stop']}") + elif char_ != s["stop"]: + buffer += char_ + else: + yield buffer + s["stop"] + buffer = "" + + if out: + out = out + ".dml" if not out.endswith(".dml") else out + with open(out, "w") as f: + for char in decode(read_file()): + f.write(char) + else: + return decode(read_file()) diff --git a/dml/run.py b/dml/run.py new file mode 100644 index 0000000..fe9eddc --- /dev/null +++ b/dml/run.py @@ -0,0 +1,12 @@ +from .encoder import decode +from .fileutils import decode_file + +__all__ = ("run_file", "run") + + +def run(dml: str) -> None: + exec("".join(decode(dml))) + + +def run_file(fp: str) -> None: + exec("".join(decode_file(fp))) diff --git a/dml/symbols.py b/dml/symbols.py new file mode 100644 index 0000000..8a924a1 --- /dev/null +++ b/dml/symbols.py @@ -0,0 +1,5 @@ +symbols = { + "1": "\u0701", # Syriac Supralinear Full Stop + "0": "\u0660", # Arabic-Indic Digit Zero + "stop": "\u06F0", # Extended Arabic-Indic Digit Zero +} diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0a8eff6 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +from pathlib import Path + +from setuptools import setup + +this_directory = Path(__file__).parent +long_description = (this_directory / "README.md").read_text() + +setup( + name="dotted-ml", + description="Translate text to and from DML with ease.", + long_description=long_description, + long_description_content_type='text/markdown', + url="https://github.com/RGBCube/dml", + version="1.0.0", + author="RGBCube", + py_modules=["dml"], + license="MIT" +)