0% found this document useful (0 votes)
9 views34 pages

Jinja Programming

Uploaded by

chauhandhruv509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

Jinja Programming

Uploaded by

chauhandhruv509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MACG 2019/2/20

JINJA2 & META PROGRAMING


▸ Xcode

▸ Template

▸ Python
A.

▸ Don’t Repeat Yourself



switch enumX_value {
case enumX_a: return “a”;
case enumX_b: return “b”;
case enumX_c: return “c”;
}
▸ C

▸ ☠ ☠

▸ ☠ ☠
PYTHON & JINJA2
▸ Jinja2 Python ([Link]

▸ Python Xcode


PYTHON & JINJA2

JINJA2
(Python string)

TEMPLATE

(Python string)

SOURCE CODE
JINJA2
(Python Dictionary)

DICTIONARY
PYTHON & JINJA2

(Python string)
template = '''\
TEMPLATE struct {{ name }} {
{{ type }} {{ argu }};
{{ type }} * pointer_{{ argu }};
};
'''

(Python Dictionary)

DICTIONARY
{“name”:”Apple”, “type”:”int”, “argu”:”seed”}
PYTHON & JINJA2

(Python string)
template = '''\
TEMPLATE struct {{ name }} {
{{ type }} {{ argu }};
{{ type }} * pointer_{{ argu }};
};
'''
JINJA2

(Python Dictionary)

DICTIONARY
{“name”:”Apple”, “type”:”int”, “argu”:”seed”}
PYTHON & JINJA2

struct Apple {
SOURCE CODE
int seed;
int * pointer_seed;
};
PYTHON & JINJA2

JINJA2

▸ (KeyPath )

▸ Python ( SnakeCase CamelCase )

▸ For IF


PYTHON & JINJA2

JINJA2

switch enumX_value {
case enumX_a: return “a”;
case enumX_b: return “b”;
case enumX_c: return “c”;
}
PYTHON & JINJA2

(Python string) template = '''\


switch enumX_value {
TEMPLATE {% for e in enums %}
case enumX_{{ e }}: return “{{ e }}”;
{% endfor %}
}
''' for

(Python Dictionary)

DICTIONARY
{“enums”:[“a”,”b”,”c”]}
XCODE

▸ Python3, Jinja2

▸ Xcode Target Run Script

▸ Python3
XCODE

PYTHON

(Non build Project File) (Python string)


TEMPLATE TEMPLATE
FILE
(Python string) (build Project File)

SOURCE CODE SOURCE CODE


JINJA2
(Non build Project File) (Python Dictionary)
DICTIONARY
DICTIONARY
FILE
XCODE

▸ BFElement

▸ Mac ES SDK

▸ C++, Objective-C Template

▸ Template: 20KB, Dictionary:30KB -> Source: 745KB


XCODE

BFELEMENT
TEMPLATE ‣
FILE


Jinja2


DICTIONARY
FILE ‣

INI ‣

Python INI
XCODE

‣ Enum C++, Objective-C

‣ String C++, Objective-C

SOURCE CODE ‣ Enum->String C++, Objective-C

‣ String->Enum C++, Objective-C

‣ C++Enum <-> Objctive-C Enum [Toll Free Bridge ]

‣ C++ String <-> Objctive-C String [ Byte ]



XCODE

Demo
▸ Template

▸ Jinja2

▸ Python3 Jinja

▸ BFElement
-BFELEMENT -

▸ Template


-BFELEMENT -

ORIGIN/FEATURE/QL600700 JANUARY 8, 2019 [0F36884]

▸ [Link] - BFElement Jinja2 python

▸ output - Jinja2

▸ src - Jinja2

▸ create_definition.py - Jinja2

▸ database - Dictionary File

▸ element - Element File

▸ element_set - ElementSet File

▸ template - Jinja2 Template


-BFELEMENT -

FILE -ELEMENT FILE-

▸ 1 INI

▸ 1 1

Section - Element

key - Element

type - Element
values - (type:enum ) Element
-BFELEMENT -

FILE -ELEMENTSET FILE-


▸ ( )
INI

Section - ElementSet

meta - ElementSet

keys - Element (Element key )


-BFELEMENT -

FILE -ELEMENTSET FILE-


▸ keys

▸ KeyName[]
▸ KeyName

▸ KeyName{SubKeyName}[]
▸ (SubKeyName, KeyName ) Dictionary
-BFELEMENT -

- ELEMENT
▸ src/database/element ini
‣ Section -

‣ key - section

‣ type - ( enum, int, float, bool, string )


‣ values - enum value:__Null
‣ value Element value

-BFELEMENT -

-
▸ python

import os
from stat import *
rootDir = ([Link]([Link](__file__))) + "/"
bfElementDir = rootDir + “ BFElement ”
import sys
[Link](bfElementDir + "src")
import create_definition

elementDirName = "element/"
elementSetDirName = "element_set/"
databaseDir = bfElementDir + "src/database/"
elementDir = databaseDir + elementDirName
elementSetDir = databaseDir + elementSetDirName

create_definition.makeElementSetSource(elementSetDir,
elementDir,
,
)
-BFELEMENT -

-
▸ Template
‣ mainNamespace : “bf”
‣ subNamespaceElement : “element”
‣ subNamespaceElementSet “element_set”
‣ stringClass : “String”
‣ enumClass : “Number”
‣ elementDictionary : (meta, Element) Dictionary
‣ Element :
‣ key
‣ type
‣ values
‣ elementSetDictionary : (meta, elementSet) Dictionary
‣ ElementSet
‣ meta
‣ keys
‣ elements : Element( ArrayOfElement DictionaryOfArrayOfElement )
-BFELEMENT -

-
▸ ElementSet Key

▸ KeyName[] → ArrayOfElement
▸ KeyName{SubKeyName}[] → DictionaryOfArrayOfElement

‣ ArrayOfElement : Element value Element

‣ key : element key


‣ type : [Link]
‣ element : Element
‣ DictionaryOfArrayOfElement :(extra_key_element value, key_element value )
Dictionary Element
‣ key : key_element key
‣ type : Type.dict_of_array
‣ key_element : Element
‣ extra_key_element : Dictionary Key Element
-BFELEMENT -

TEMPLATE FILE UTILITY

▸ inSyntax -c ”_”

▸ snakeCase - camel case snake case

▸ camelCase - snake case camel case

▸ headLower -

▸ create_definition.py
-BFELEMENT -

TEMPLATE FILE

▸ upper -

▸ title -

▸ replace -

▸ …

▸ string

You might also like