Metadata-Version: 2.4
Name: CompilerDirectives
Version: 1.0.5
Summary: Python pre-proccess Compiler Directives
Author-email: Nick Causton <Development@AssociatedData.co.uk>
License-Expression: MIT
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.15
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# **Pre-process Compiler Directives**

## Normal usage:

import CompilerDirectives

import Constants	# file containing our constant definitions

## Examples
These are shipped in CompilerDiectives\tests

Main.py:
```
# Main.py

DIAG_PY2_SAVE = True

import CompilerDirectives
CompilerDirectives.PreProcess.stale_pyc_del(__file__)

# import os
# CompilerDirectives.PreProcess.stale_pyc_del(os.path.dirname(__file__))

import Constants
import Module1
```

Constants.py:
```
# Constants.py

#DEFINE CONST1 'big'
#DEFINE CONST2 'wide'
#DEF CONST3 3
#DEF CONST4 4
```
Module1.py:
```
# Module1.py

print("#DEFINE/#UNDEF test")
#DEFINE a 1
#DEFINE b 'bee', c 3, d 4, e 5, f 6
#UNDEF c, e, k
#COMP 'print(' +  str(__consts__) + ')'

print("#Replace Constants test")
x = a
y = b
print(f"{x=}, {y=}")

print("#INCLUDE test")
if True:
    #INCLUDE 'module2.py', 'non-existant.py'

print("#IFDEF test")
#IFDEF a
print("#IFDEF a - a exists - correct")
#ELSE
print("#IFDEF a - a does not exist - wrong")
#ENDIF

print("#IFNDEF test")
#IFNDEF a
print("#IFNDEF a - a does not exist - wrong")
#ELSE
print("#IFNDEF a - a exists - correct")
#ENDIF

print("#IF / ELIF test")
#IF False
print("#IF False - wrong")
#ELIF False
print("#ELIF False - wrong")
#ELIF True
print("#ELIF True - correct")
#ELIF True
print("#ELIF True(2) - wrong")
#ENDIF

print("#COMP (compile time) test")
#COMP 'print("Hello ' + CONST1 + ' ' + CONST2 +  ' world")'
```
Module2.py:
```
# Module2.py

print("in Module 2a... - correct")

print("in Module 2b... - correct")
```
## Diagnostics:

For diagnostic purposes an intermediate modified file can be saved in the `__pycache__` directory by setting the diagnostic variable:

`DIAG_PY2_SAVE = True`

Setting this to True also enables display of speed testing in the Python Console.
The writing of this file is automatically disabled when Python is run with `__debug__` False.


This can be either done before the import:

`DIAG_PY2_SAVE = True`

`import CompilerDirectives`



Or after:

`import CompilerDirectives`

`CompilerDirectives.PreProcess.DIAG_PY2_SAVE = True`

## Compiler directives:

Define constant 

`#DEFINE / #DEF <const> <value> `

Undefine constant

`#UNDEF <const>`

Compile time conditionals

`#IF <expr> / #ELIF <expr> / #ENDIF `

Compile time definition conditionals

`#IFDEF <expr> / #IFNDEF <expr> / #ENDIF `

Include file

`#INCLUDE <file>`

‘Compile’ time expression evaluation, remove ‘#COMP’ from resulting line

`#COMP <expr>`	

Multiple items can be included on the same line delimited by ‘,’ (commas):

`#DEF <const1> <value1>, <const2> <value2>`

`#UNDEF <const1>, <const2>`

`#INCLUDE <file1>, <file2>`

## Constant replacement:

After `CompilerDirectives` is imported all further imports will be subject to replacement of the Constants with their respective values if previously defined.
All files imported can contain further constant definitions and un-definitions which will be added to / removed from the application level ‘global’ Constants list.

Constant replacement will happen only once for each import with the resulting byte-code being saved in the `*.pyc` `__pycache__ `file.  When a modified file is imported which contains any compiler directives all *.pyc files in the `__pycache__` directory which are older than that imported file will be deleted so as to force their re-compilation the next time they are accessed.

## Development:

For development purposes the following can be used to remove all earlier *.pyc files than whenever the test module was last modified so that these then get recreated and hence pre-processed.:

`import CompilerDirectives`

`CompilerDirectives.PreProcess.stale_pyc_del(__file__)`

Alternatively to remove all earlier `*.pyc` files on each run so that `CompilerDirectives` runs every time regardless of whether the test module is modified or not pass just the directory.:

`import os`

`import CompilerDirectives`

`CompilerDirectives.PreProcess.stale_pyc_del(os.path.dirname(__file__))`

## Known issue:

There is a known issue with `tokenize.tokenize` not indenting & dedenting correctly when a `#CompilerDirective` (comment) is the first line after aforementioned indentation change.

