1
Fork 0
mirror of https://github.com/RGBCube/DML synced 2025-07-27 07:37:46 +00:00

Initial Commit

This commit is contained in:
RGBCube 2022-03-19 22:15:26 +03:00 committed by GitHub
parent 66739939c2
commit 1e815a2dc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 243 additions and 1 deletions

7
LICENSE Normal file
View file

@ -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.

View file

@ -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)
```

16
dml/__init__.py Normal file
View file

@ -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 *

61
dml/encoder.py Normal file
View file

@ -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()

11
dml/errors.py Normal file
View file

@ -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

67
dml/fileutils.py Normal file
View file

@ -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())

12
dml/run.py Normal file
View file

@ -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)))

5
dml/symbols.py Normal file
View file

@ -0,0 +1,5 @@
symbols = {
"1": "\u0701", # Syriac Supralinear Full Stop
"0": "\u0660", # Arabic-Indic Digit Zero
"stop": "\u06F0", # Extended Arabic-Indic Digit Zero
}

18
setup.py Normal file
View file

@ -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"
)