Skip to content

A parser, AST manipulator and pretty-printer for the language of Kivy written in Scala.

License

Notifications You must be signed in to change notification settings

theorydudes/kivy-astmanip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kivy-astmanip

Bintray Coverage

A parser, AST manipulator and pretty-printer for the language of Kivy written in Scala.

Installation

Kivy-astmanip can be directly imported using sbt:

resolvers += Resolver.bintrayRepo("tizuck", "maven")

libraryDependencies += "com.github.theorydudes" %% "kivy-astmanip" % "1.0.0"

Kivy

Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.

Kivy is based on python but specifies its own language to define user interface structure. Kivy-Files are parsed and executed by python-code at runtime.

A defnition for the language of Kivy can be found here: Kivy-Language

Example

The following section assumes the imports:

import com.github.theorydudes.util.KivyParser
import com.github.theorydudes.util.KivyParser._

Imagine you have the following Kivy-File consisting of a Root-Module Plot and a LineGraph with some properties:

Plot:
  LineGraph:
    background_normal: ''
    background_color: [0,0,0,1]
    bg_color: ['Grey','000']
    orientation: 'vertical'
    padding: '5dp'
    spacing: '5dp'
    mode: "Line"
    id: line_graph

Furthermore in this scenario the color of the LineGraph is dependent on some server-response and you need a way to change the bg_color attribute according to this response in Scala. This is where kivy-astmanip comes in handy:

val serverResponse = "red"

val parser = KivyParser(Path("path/to/file.kv"))
val parseResult = parser.root

if(parseResult.isSuccess){
  val manipulatedRoot = serverResponse match {
    case "red" =>
      parseResult
        .get
        .rewrite({case Python("['Grey','000']") => Python("['Red','000']")})
  }
  println(manipulatedRoot.pretty)
}
 

Here, the original Plot defined in the Kivy-file from above is parsed and the AST is then manipulated using the rewrite function.

The KivyPrettyPrinter prints the AST:

Plot:
  LineGraph:
    background_normal: ''
    background_color: [0,0,0,1]
    bg_color: ['Red','000']
    orientation: 'vertical'
    padding: '5dp'
    spacing: '5dp'
    mode: "Line"
    id: line_graph

Future plans

  1. Providing a full documentation of model, parser and pretty printer
  2. Java-Interface

Inspiration

The grammar used to parse kivy-files is based on this ANTLR3-grammar.

About

A parser, AST manipulator and pretty-printer for the language of Kivy written in Scala.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages