reactonite package

Submodules

reactonite.Config module

class reactonite.Config.Config(config_path, load=False)[source]

Bases: object

A Class to manage and maintain project configuration. One can add/remove/modify config variables and save/load it easily

config_path

Path to configuration file from where to load/save

Type

str

config

Config settings dictionary

Type

dict

Parameters
  • config_path (str) – Path to configuration file from where to load/save

  • load (bool) – Whether to load config on object creation, default is False

add_to_config(config_name, config_value)[source]

Adds/updates a variable to the config

get(key)[source]

Gets a config variable

get_config()[source]

Returns the configuration as a dictionary

load_config()[source]

Loads the configuration from the config_path.

Raises

FileNotFoundError – Raised if config file not found at the config_path

save_config()[source]

Saves the configuration to the config_path in JSoN format. It first creates the file if it doesn’t exist, then writes into it.

reactonite.Constants module

class reactonite.Constants.DEFAULTS[source]

Bases: object

Default variables used in code execution

INIT_FILES_DIR

Directory for initial files for setup.

Type

str

SRC_DIR

Source directory for reactonite codebase.

Type

str

DEST_DIR

Destination directory for React codebase.

Type

str

CONFIG_FILE_NAME

Config file name for config variables.

Type

str

PROPS_MAP

Mapping for HTML to React props

Type

dict

reactonite.CreateHtmlCss module

reactonite.CreateHtmlCss.saveCss(content, loc)[source]

Takes body of the POST response from GrapesJS and saves CSS File

Parameters
  • content (str) – CSS Content

  • loc (str) – Location where we have to save the CSS File

reactonite.CreateHtmlCss.saveHtml(content, loc)[source]

Takes body of the POST response from GrapesJS and saves HTML File

Parameters
  • content (str) – HTML Content

  • loc (str) – Location where we have to save the HTML File

reactonite.Helpers module

reactonite.Helpers.create_dir(path)[source]

Creates directory at the given path if it doesn’t exist.

Parameters

path (str) – Path to directory which needs to be created.

Raises

RuntimeError – Raised if directory can’t be created.

reactonite.Helpers.create_file(path)[source]

Creates the file at the given path if it doesn’t exist.

Parameters

path (str) – Path to file which needs to be created.

Raises

RuntimeError – Raised if file can’t be created.

reactonite.Helpers.get_parent_dir(path)[source]

Returns location of the parent directory for a given path.

Parameters

path (str) – Path of the file or folder for which we need the parent directory.

Returns

Location of the parent directory

Return type

str

reactonite.Helpers.write_to_json_file(path, content)[source]

Writes content to a json file at the given path. Raises exception if file not exists.

Parameters
  • path (str) – Path to file where content will be dumped.

  • content (dict) – Dictonary to be dumped into the file.

Raises
  • FileNotFoundError – Raised if file doesn’t exist.

  • RuntimeError – Raised if not enough permissions to write in file

reactonite.NodeWrapper module

class reactonite.NodeWrapper.NodeWrapper[source]

Bases: object

Node wrapper to execute commands corresponding to node js using python.

npx

Commandline to be used for npx according to system(Linux, Windows)

Type

str

npm

Commandline to be used for npm according to system(Linux, Windows)

Type

str

node

Commandline to be used for node according to system(Linux, Windows)

Type

str

build(working_dir)[source]

Create an optimized build of your app in the build folder

Parameters

working_dir (str) – Directory containing npm project root

check_react_install()[source]

Checks the installation of Nodejs/npm/npx. If npm is not available it throws an error.

Raises

RuntimeError – Raised if Nodejs/npm/npx is not available.

create_react_app(project_name, rename_to, working_dir='.')[source]

Creates a new react app and renames it as specified.

Parameters
  • project_name (str) – Project name to be used to create the app

  • rename_to (str) – Renames the created React app to this

  • working_dir (str) – Working dir to run commands inside

install(package_name, working_dir)[source]

Installs the given package in npm and saves in package.json

Parameters
  • package_name (str) – Package to be installed.

  • working_dir (str) – Directory containing npm project root

install_grapesjs(project_dir)[source]
prettify(path, working_dir='.')[source]

Runs code formatting using prettier on the given path

Parameters
  • path (str) – Filepath or directory to run prettier on

  • working_dir (str) – Directory from which command is run

start(working_dir)[source]

Runs the command npm start in the given working directory

Parameters

working_dir (str) – Directory to execute the command in.

start_grapesjs(project_dir)[source]

reactonite.PropsMap module

reactonite.ReactoniteWatcher module

class reactonite.ReactoniteWatcher.ReactoniteWatcher(config_settings, patterns='*', ignore_patterns='', ignore_directories=False, case_sensitive=True, recursive=True)[source]

Bases: object

A file/directory watcher to report events incase they are modified/created/deleted.

src_dir

Path of the source direectory to watch and report for events.

Type

str

dest_dir

Path of the destination direectory to write transpiled code.

Type

str

config_settings

Path to src_dir and dest_dir as dict object, stored in config.json

Type

dict

patterns

Pattern of files/directories to watch, defaults to “*”

Type

str, optional

ignore_patterns

Pattern of files/directories to ignore or not watch, defaults to “”

Type

str, optional

ignore_directories

Parameter whether the watcher should ignore directories or not, defaults to False

Type

bool, optional

case sensitive

Parameter explaining whether file/directory names are case-sensitive or not, defaults to True

Type

bool

recursive

Parameter whether the watcher should recursively watch inside directories or not, defaults to True

Type

bool

start()[source]

Runs the watchdog service on the given path. Handles various events to different functions as per the requirement

reactonite.Transpiler module

class reactonite.Transpiler.AttributesParser(*, convert_charrefs=True)[source]

Bases: html.parser.HTMLParser

Extends HTMLParser to extract tags with attributes from a given HTML string

Call feed method of HTMLParser to generate data and then retriece it from the object of the class. Here’s an usage example:

attributes_parser = AttributesParser() attributes_parser.feed(“YOUR_HTML_STRING”) tag_with_attributes = attributes_parser.data print(tag_with_attributes)

data

Stores the tags with their attributes

Type

list

handle_starttag(tag, attrs)[source]

Overrides the original handler for start tag and appends the tags to data.

Parameters
  • tag (str) – Name of tag being parsed

  • attrs (list) – List of attrs corresponding to the current tag

class reactonite.Transpiler.ReactCodeMapper(src_dir, dest_dir, props_map)[source]

Bases: object

Class to convert tags and props from HTML to React

Call getReactMap method for converting tags fed for HTML and get corresponding React Mapping. Here’s an usage example:

reactCodeMapper = ReactCodeMapper(source_dir, destination_dir, props_map) react_map = reactCodeMapper.getReactMap(tag_with_attributes) print(react_map)

CUSTOM_TAG_HANDLERS

Stores mapping correspoding to tags which are handled seperately.

Type

dict

src_dir

Source directory for the HTML codebase.

Type

str

dest_dir

Destination directory for the React codebase.

Type

str

props_map

Mapping of attrs for HTML to React from props_map.py

Type

dict

add_to_import

Stores imports corresponding to variables created during transpilation.

Type

list

add_variables

Stores newly created variables during transpilation.

Type

list

Saves wether Link tag needs to be imported for current page.

Type

bool, optional

getReactMap(tags, filepath_from_src)[source]

Wrapper to generate React Map object comprising of all data needed to convert HTML to React

Parameters
  • tags (dict) – HTML attributes extracted using AttributesParser

  • filepath_from_src (str) – Path to file from src directory

Returns

Final mapping of tags with imports and varibles for React, if any attribute is None then tag needs to be deleted

Return type

dict

class reactonite.Transpiler.Transpiler(config_settings, props_map, verbose=False, create_project=False)[source]

Bases: object

Transpiler responsible for translating HTML code to React

project_name

Name of the project as stored in config

Type

str

src_dir

Path of the source directory within the project directory

Type

str

dest_dir

Path to the transpiled React app within the project directory

Type

str

index_routes

Stores Routes data corresponding to different pages for index.js

Type

dict

parser

Specify which parser to use for reading HTML files, defaults to “html.parser”

Type

str, optional

verbose

Specify the verbosity of the transpiler, defaults to False

Type

bool, optional

transpileFile(filepath)[source]

Transpiles the source HTML file given at the given filepath to a React code, which is then copied over to the React build directory, if not HTML file then get’s copied directly.

Parameters

filepath (str) – Path to the source HTML file which is to be transpiled

Raises

RuntimeError – Raised if the source html file is not found

transpile_project(copy_static=True)[source]

Runs initial checks like ensuring the source directories exist, and the source file is present. After that, copies non html files and transpiles the source.

Parameters

copy_static (bool, optional) – Will copy non .html files if True, only .html files will be transpiled if False, default True

Raises

RuntimeError – Raised source html file is missing.

reactonite.main module

reactonite.main.fetchCodeFromGrapesjs()[source]

Module contents