static-api-generator

Library for generating static API


Keywords
static api generator, api, api-generator, conversion, data-transformation, json, loader, python, python3
License
MIT
Install
pip install static-api-generator==0.0.2

Documentation

Python static API generator

Build Status Coverage Status Python versions PyPi

This library allows you to generate from source text files a static API as json-files.

How it works

"Core" of library is the APIGenerator that finds the source files for converting in the specified directory, passes them to the loader, and writes converted content of them to the target directory.

Loader is a class that takes a path to a file with a supported extension, performs data conversion, and returns the resulting dictionary (dict), which after that will be accepted by the generator and written to the destination file as json.

To develop your own loaders, a basic one is implemented BaseLoader that describes the basic logic for working with files, validation and simple conversion. As an example implemented TxtLoader, it supports the processing of txt-files.

Usage examples

from static_api_generator.generator import APIGenerator
from static_api_generator.loaders import TxtLoader


api_gen = APIGenerator('/home/user/some_text_files/', '/home/user/static_api/', TxtLoader)
api_gen.generatre()

Txt files located in /home/user/some_text_files/ will be written as json to a directory /home/user/static_api/ with the hierarchy saved.

If the source text file looks like this

---
title: What is Lorem Ipsum?
category: Lorem Ipsum
...
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

then the conversion result will be

{
    "title": "What is Lorem Ipsum?",
    "category": "Lorem Ipsum",
    "content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.\nLorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.\nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.\nIt was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
}

You can notice that the source files can contain additional metadata in the yaml format, separated --- at the beginning and --- or ... at the end. For metadata reading used PyYaml, which means the conversion is completely in accordance with yaml. For example, metadata can contain nested dictionaries, lists, and so on.

What for?

I wanted to write myself a static blog, which I could host, for example, on GitHub Pages. The best option I thought was Pelican, but that means returning to the past with html template layout, while I wanted an interface on a reactive framework (like Vue.js) with the ability to design components and re-use them.

For the frontend, I decided to use Nuxt.js, because it knows how to generate static, but there was a need for an API. To avoid the need to deploy a server with the API, I started developing this library.