Skip to main content

simple python module for KoiLang parsing

Project description

Kola

Simple python module for KoiLang parsing.

License PyPI Python Version

Installation

From pip:

pip install KoiLang

From source code:

python setup.py build_ext --inplace
python setup.py install

What is KoiLang

KoiLang is a markup language while is easy to read for people. There is an simple example.

#hello KoiLang
I am glad to meet you!

In KoiLang, file is divided into 'command' part and 'text' part. The formation of command part is like C preprocessor directive, using '#' as starting. And text is surrounding commands.

#command "This is a command"
This is a text.

Each command can have several arguments behind the command name. Valid argument type include integer, float, literal and string.

#arg_int    1 0b101 0x6CF
#arg_float  1.0 2e-2
#arg_literal __name__
#arg_string "A string"

Here "literal" is a valid python variety name containing letter,digit, underline and not starting with digit. Usually it is same as a string.

There is another kind of arguments -- keyword arguments which formation is as this:

#kwargs key(value)

And another format:
#keyargs_list key(item0, item1)

And the third:
#kwargs_dict key(x: 11, y: 45, z: 14)

All the arguments can be put together

#draw Line 2 pos0(x: 0, y: 0) pos1(x: 16, y: 16) \
    thickness(2) color(255, 255, 255)

What can Kola module do

Kola module provides a fast way to translate KoiLang command into a python function call.

Above command #draw will convert to function call below:

draw(
    "Line", 2,
    pos0={"x": 0, "y": 0},
    pos1={"x": 16, "y": 16},
    thickness=2,
    color=[255, 255, 255]
)

Kola mudule just create a bridge from kola file to Python script. The bridge, the main class of Kola module, is KoiLang class. There is a simple example.

Example

Let's image a simple situation, where you want to create some small files. Manual creating is complex and time-consuming. Here is a way to solve that. We can use a single kola file to write all my text. Then use commands to devide these text in to different files.

#file "hello.txt" encoding("utf-8")
Hello world!
And there are all my friends.

#space hello

    #file "Bob.txt"
    Hello Bob.

    #file "Alice.txt"
    Hello Alice.

#endspace

#end
import os
from typing import Optional, TextIO
from kola import KoiLang, kola_command, kola_text


class MultiFileManager(KoiLang):
    def __init__(self) -> None:
        super().__init__()
        self._file: Optional[TextIO] = None
    
    def __del__(self) -> None:
        if self._file:
            self._file.close()
    
    @kola_command
    def space(self, name: str) -> None:
        path = name.replace('.', '/')
        if not os.path.isdir(path):
            os.makedirs(path)
        os.chdir(path)
    
    @kola_command
    def endspace(self) -> None:
        os.chdir("..")
        self.end()
    
    @kola_command
    def file(self, path: str, encoding: str = "utf-8") -> None:
        if self._file:
            self._file.close()
        path_dir = os.path.dirname(path)
        if path_dir:
            os.makedirs(path_dir, exist_ok=True)
        self._file = open(path, "w", encoding=encoding)
    
    @kola_command
    def end(self) -> None:
        if self._file:
            self._file.close()
            self._file = None
    
    @kola_text
    def text(self, text: str) -> None:
        if not self._file:
            raise OSError("write texts before the file open")
        self._file.write(text)

And input this in terminal:

python -m kola kolafile.kola -s script.py

Or directly add in script:

if __name__ = "__main__":
    FMultiFileManager().parse_file("kolafile.kola")

You will see new files in your work dir.

workdir
│      
│  hello.txt
│      
└─hello
    Alice.txt
    Bob.txt

What is more

The most difference between KoiLang and other markup language like YAML which is data-centric is that KoiLang more pay attention to the command. Yeah, text in Kola file is a special command named @text too. In fact, the core idea of Kola is to separate data and instructions. The kola file is the data to execute commands, and the python script is the instructions. Then Kola module just mix they together. It can be considered as a simple virtual machine engine. if you want, you can even build a Python virtual machine (of course, I guess no one like to do that).

On the other hand, text is also an important feature of Kola, which is a separate part, independent of context during parsing. The text is the soul of a Kola file. Any commands just are used to tell the computer what to do with the text. Though you can make a Kola file with only commands, it is not recommended. Instead, you ought to consider switching to another language.

Bugs/Requests

Please send bug reports and feature requests through github issue tracker. Kola is open to any constructive suggestions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

KoiLang-0.1.0b3.tar.gz (172.1 kB view details)

Uploaded Source

Built Distributions

KoiLang-0.1.0b3-cp38-cp38-win_amd64.whl (87.2 kB view details)

Uploaded CPython 3.8 Windows x86-64

KoiLang-0.1.0b3-cp36-cp36m-win_amd64.whl (96.6 kB view details)

Uploaded CPython 3.6m Windows x86-64

File details

Details for the file KoiLang-0.1.0b3.tar.gz.

File metadata

  • Download URL: KoiLang-0.1.0b3.tar.gz
  • Upload date:
  • Size: 172.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for KoiLang-0.1.0b3.tar.gz
Algorithm Hash digest
SHA256 05c0fbbf5ac66ff43d96055c33bfe691bae499385fb515e81747d2e5e057911a
MD5 30a89d2e2d8361988e4d4e0901cdcc47
BLAKE2b-256 6ddc0d766fa0d8022567cbbd20849cc0c837b802ef596efcaf34ff85527a051a

See more details on using hashes here.

File details

Details for the file KoiLang-0.1.0b3-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: KoiLang-0.1.0b3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 87.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for KoiLang-0.1.0b3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 87df2ce139489e5a5ea67d9656ff0f713f0d4071488ab15d45f8ce78a9995d25
MD5 44bc286fc60820306ab6a54ba7eebc95
BLAKE2b-256 4092c1c69fa8f00fc41ccdf4ac33fb9f5d586804ec13ef2f5acb2d8644ca4804

See more details on using hashes here.

File details

Details for the file KoiLang-0.1.0b3-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for KoiLang-0.1.0b3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 9ea7011f8e468849abbef2fb87be95a7094af1997252ce57e1518ca303c39eb7
MD5 9ff58fdb28fd9814fa082c350ab83097
BLAKE2b-256 7459b36d98dda6088530fbe936d48c61fb81d4832e3f14b6106e6be85a30e7a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page