Coder Social home page Coder Social logo

toml's Introduction

toml

Read and write .toml files. works in MicroPython and CircuitPython

Usage:

Method one (defaults to using the file /settings.toml):

import toml

toml.getenv("key") # defaults to ./settings.toml file
toml.getenv("key",file="/my_file.toml",default="value to use if key not found")
toml.getenv("WIFI",cache=True,subst=True) # replace any $VARIABLES found inside the key (e.g. welcome="Hi from $HOSTNAME >>>" etc)

toml.setenv("key","value") # put None for value to delete the key. # accepts file=
toml.subst_env("Put a $key in a string") # accepts default= file== and ${key} syntax

HOSTNAME, GRN, YEL, NORM, BLU, WHT, LED, LEDINV, WIFI = toml.getenv(["HOSTNAME","GRN","YEL","NORM","BLU","WHT","LED","LEDINV","WIFI"],subst=True,include=True)  # get lots at once, following any #include files as well

Method two (specify your own .toml file)

import toml
t = toml.toml("my_settings_tst.toml")
t.getenv("USER")
t.getenv("WIFI",subst=True)
t.setenv("PASSWORD","mypass")
t.subst_env("My password is $PASSWORD !")

Features/Drawbacks

*. Makes backups before overwriting when changing/adding new toml values (adds _old to the end of the filename) *. Can handle multi-line strings and escape characters etc *. Only handles basic formatting of numbers, strings (does do multi-line), dict, list, and tuples *uses json to load/save the latter 3) *. Extends the .toml standard by allowing $VARIABLES to be expanded (subst=True) *. Allows for #include otherfile.toml nested toml files as well (and even #include $SOMEVAR if the caller does subst=True)

How to install

  1. Grab the toml.py file (or the binary toml.mpy if you're using micropython 1.24)
  2. Upload it into / or /lib folder

Example

MicroPython v1.24.0-preview.114.g652083de6 on 2024-07-19; ESP32S CAM module no SPIRAM and OV2640 with ESP32
Type "help()" for more information.
>>> import toml
>>> toml.getenv("dir")
'ls -Flatr'
>>> toml.setenv("fred","wilma")
>>> toml.getenv("fred")
'wilma'
>>> import sh
Time set to: 2024-07-20 23:54:04
Esp32cam72:/ mpy$ tail settings.toml

dir = "ls -Flatr"
foo = [{"ssid": "mynet0", "channel": 9, "password": "hithere", "hidden": true}, {"ssid": "mynet1", "channel": 9, "password": "hithere", "hidden": false}]
fred = "wilma"
Esp32cam72:/ mpy$ 

toml's People

Contributors

gitcnd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.