fbdoc  1.0.1
FreeBASIC documentation tool
py_ctypes.bas File Reference

Example code for an external emitter to generate Python bindings. More...

Include dependency graph for py_ctypes.bas:

Go to the source code of this file.

Macros

#define NEW_ENTRY   /* Code(NL & NL & "# " & MID(.Fnam, INSTRREV(.Fnam, SLASH) + 1) & ": " & .LineNo) */
 Macro to place a comment in to the output (file name and line number) More...
 

Functions

FUNCTION_AS_STRING genNLiteral (BYREF_AS_STRING T)
 Transform number literals. More...
 
FUNCTION_AS_STRING genCType (BYVAL_AS_Parser_PTR P)
 Transform a FB type in to a ctype. More...
 
SUB_CDECL py_CreateIni (BYVAL_AS_Parser_PTR P)
 Export the initializer(s) for a variable. More...
 
SUB_CDECL py_entryListPara (BYVAL_AS_Parser_PTR P)
 Emitter to extract the types of a parameter list. More...
 
SUB_CDECL py_function (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is on top of a function body. More...
 
SUB_CDECL py_emitEnumNames (BYVAL_AS_Parser_PTR P)
 Emitter to generate a line for a ENUM block entry. More...
 
SUB_CDECL py_emitBlockNames (BYVAL_AS_Parser_PTR P)
 Emitter to generate a line for a TYPE / UNION block entry. More...
 
SUB_CDECL py_declare (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at a variable declaration. More...
 
SUB_CDECL py_enum (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at the start of a ENUM block. More...
 
SUB_CDECL py_union (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at the start of a UNION block. More...
 
SUB_CDECL py_class (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at the start of a TYPE block. More...
 
SUB_CDECL py_define (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at a #DEFINE line or at the start of a #MACRO More...
 
SUB_CDECL py_include (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at an #INCLUDE line. More...
 
SUB_CDECL py_init (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at the start of a file. More...
 
SUB_CDECL py_exit (BYVAL_AS_Parser_PTR P)
 Emitter called when the Parser is at the end of a input file. More...
 
SUB_CDECL py_CTOR (BYVAL_AS_Options_PTR O)
 Constructor called before the first input gets parsed. More...
 
SUB_CDECL EmitterInit (BYVAL_AS_EmitterIF_PTR Emi, BYREF_AS_STRING Par)
 Initialize the EmitterIF and evaluate parameters. More...
 

Variables

LONG ENUM_COUNT
 A counter for ENUM blocks. More...
 
STRING T1
 The fields type block. More...
 
STRING T2
 The list of type names (, separated) More...
 
STRING CLASSES
 The list of class names (!"\n" separated) More...
 
STRING LIBRARY
 The name of the binary to build. More...
 
STRING IMPORTS
 The list of Python imports. More...
 
STRING HEADER
 The header for the output. More...
 

Detailed Description

Example code for an external emitter to generate Python bindings.

This file contains example source code for an external emitter. The emitter outputs Python declarations from the FB source code, in order to generate Python bindings based on ctypes for libraries compiled in FB.

The author is neither keen on Python syntax, nor on ctypes specials. Rather than a perfect example, this code is more like a quick and dirty solution to generate a Phyton binding for the library libpruio. But interrested users may find some inspiration in the code.

Example:

In order to compute a ctypes binding for libpruio, execute in the folder src/bas/plugins the following command:

fbdoc -e "py_ctypes" -t -pylib=libpruio ../libpruio/src/pruio/pruio.bi

Options

  • -e "py_ctypes": choose external emitter named py_ctypes
  • -t: follow source tree
  • -pylib=libpruio: binary name to include for python code (special plugin option, unknown for fbdoc)
  • ../libpruio/src/pruio/pruio.bi: the file to start at (expects that libpruio source is installed at the same directory level as fbdoc project source)

The output comes at STOUT (in the shell). In order to write it to a file just append > libpruio.py to the command, like

fbdoc -e "py_ctypes" -t -pylib=libpruio ../libpruio/src/pruio/pruio.bi > libpruio.py

\ToDo load RepData from file in CTOR

Since
0.4.0

Definition in file py_ctypes.bas.

Macro Definition Documentation

◆ NEW_ENTRY

#define NEW_ENTRY   /* Code(NL & NL & "# " & MID(.Fnam, INSTRREV(.Fnam, SLASH) + 1) & ": " & .LineNo) */

Macro to place a comment in to the output (file name and line number)

Definition at line 49 of file py_ctypes.bas.

Function Documentation

◆ genNLiteral()

FUNCTION_AS_STRING genNLiteral ( BYREF_AS_STRING  T)

Transform number literals.

Parameters
TThe string to work on
Returns
The transformed number

Helper procedure that checks a string for numerical literals and transforms the FB notation to Python equivalents.

Since
0.4.0

Definition at line 72 of file py_ctypes.bas.

Here is the caller graph for this function:

◆ genCType()

FUNCTION_AS_STRING genCType ( BYVAL_AS_Parser_PTR  P)

Transform a FB type in to a ctype.

Parameters
PThe parser calling this emitter
Returns
A string containing the type

Helper function that transforms a FB type in to the ctypes equivalent in case of a standard type. Otherwise the original type symbol gets returned. Pointers get enclosed by POINTER(...), but ZSTRING PTR gets transformed to c_char_p.

Since
0.4.0

Definition at line 100 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_CreateIni()

SUB_CDECL py_CreateIni ( BYVAL_AS_Parser_PTR  P)

Export the initializer(s) for a variable.

Parameters
Pthe parser calling this emitter

This sub reads the initializer from the input buffer and emits them unchanged. This may be a bunch of text in case of an array initializer.

Definition at line 136 of file py_ctypes.bas.

Here is the caller graph for this function:

◆ py_entryListPara()

SUB_CDECL py_entryListPara ( BYVAL_AS_Parser_PTR  P)

Emitter to extract the types of a parameter list.

Parameters
PThe parser calling this emitter

Emitter that collects the types of the parameter list in global variable T2, to be used in py_function().

Since
0.4.0

Definition at line 188 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_function()

SUB_CDECL py_function ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is on top of a function body.

Parameters
PThe parser calling this emitter

FIXME

Since
0.4.0

Definition at line 203 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_emitEnumNames()

SUB_CDECL py_emitEnumNames ( BYVAL_AS_Parser_PTR  P)

Emitter to generate a line for a ENUM block entry.

Parameters
PThe parser calling this emitter

Emitter that gets called when the parser is in a block (ENUM). It generates a line for each member and writes it (them) to the output.

Since
0.4.0

Definition at line 241 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_emitBlockNames()

SUB_CDECL py_emitBlockNames ( BYVAL_AS_Parser_PTR  P)

Emitter to generate a line for a TYPE / UNION block entry.

Parameters
PThe parser calling this emitter

This emitter gets called when the parser is in a block (TYPE UNION). It generates a line for each member and writes it (them) to the output.

Since
0.4.0

Definition at line 272 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_declare()

SUB_CDECL py_declare ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at a variable declaration.

Parameters
PThe parser calling this emitter

Emitter that generates a variable (or function) declaration and writes it (them) to the output.

Since
0.4.0

Definition at line 339 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_enum()

SUB_CDECL py_enum ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at the start of a ENUM block.

Parameters
PThe parser calling this emitter

FIXME

Since
0.4.0

Definition at line 367 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_union()

SUB_CDECL py_union ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at the start of a UNION block.

Parameters
PThe parser calling this emitter

FIXME

Since
0.4.0

Definition at line 383 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_class()

SUB_CDECL py_class ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at the start of a TYPE block.

Parameters
PThe parser calling this emitter

FIXME

Since
0.4.0

Definition at line 398 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_define()

SUB_CDECL py_define ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at a #DEFINE line or at the start of a #MACRO

Parameters
PThe parser calling this emitter

FIXME

Since
0.4.0

Definition at line 424 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_include()

SUB_CDECL py_include ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at an #INCLUDE line.

Parameters
PThe parser calling this emitter

FIXME

Since
0.4.0

Definition at line 447 of file py_ctypes.bas.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ py_init()

SUB_CDECL py_init ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at the start of a file.

Parameters
PThe parser calling this emitter

This emitter sends the HEADER information to the output once.

Since
0.4.0

Definition at line 461 of file py_ctypes.bas.

Here is the caller graph for this function:

◆ py_exit()

SUB_CDECL py_exit ( BYVAL_AS_Parser_PTR  P)

Emitter called when the Parser is at the end of a input file.

Parameters
PThe parser calling this emitter

This emitter just adds a new line character to the output.

Since
0.4.0

Definition at line 473 of file py_ctypes.bas.

Here is the caller graph for this function:

◆ py_CTOR()

SUB_CDECL py_CTOR ( BYVAL_AS_Options_PTR  O)

Constructor called before the first input gets parsed.

Parameters
OThe Options UDT calling this constructor

FIXME

Since
0.4.0

Definition at line 485 of file py_ctypes.bas.

Here is the caller graph for this function:

◆ EmitterInit()

SUB_CDECL EmitterInit ( BYVAL_AS_EmitterIF_PTR  Emi,
BYREF_AS_STRING  Par 
)

Initialize the EmitterIF and evaluate parameters.

Parameters
EmiThe newly created EmitterIF to fill with our callbacks
ParAdditional command line parameters, not parsed by fbdoc

FIXME

Since
0.4.0

Definition at line 516 of file py_ctypes.bas.

Here is the call graph for this function:

Variable Documentation

◆ ENUM_COUNT

LONG ENUM_COUNT

A counter for ENUM blocks.

Definition at line 51 of file py_ctypes.bas.

◆ T1

STRING T1

The fields type block.

Definition at line 54 of file py_ctypes.bas.

◆ T2

STRING T2

The list of type names (, separated)

Definition at line 55 of file py_ctypes.bas.

◆ CLASSES

STRING CLASSES

The list of class names (!"\n" separated)

Definition at line 56 of file py_ctypes.bas.

◆ LIBRARY

STRING LIBRARY

The name of the binary to build.

Definition at line 57 of file py_ctypes.bas.

◆ IMPORTS

STRING IMPORTS

The list of Python imports.

Definition at line 58 of file py_ctypes.bas.

◆ HEADER

STRING HEADER

The header for the output.

Definition at line 59 of file py_ctypes.bas.