PyForTool
Python-fortran-tool
Loading...
Searching...
No Matches
Public Member Functions | List of all members
pyfortool.cosmetics.Cosmetics Class Reference
Inheritance diagram for pyfortool.cosmetics.Cosmetics:
Inheritance graph
[legend]

Public Member Functions

 upperCase (self)
 
 lowerCase (self)
 
 indent (self, nodeToUpdate=None, indentProgramunit=0, indentBranch=2, exclDirectives=None)
 
 removeEmptyLines (self)
 
 removeComments (self, exclDirectives=None, pattern=None)
 
 updateContinuation (self, nodeToUpdate=None, align=True, removeALL=False, addBegin=True, removeBegin=False)
 
 updateSpaces (self, beforeOp=1, afterOp=1, inOperator=True, beforeComma=0, afterComma=1, beforeParenthesis=0, afterParenthesis=0, beforeAffectation=1, afterAffectation=1, inAffectation=True, beforeRangeDelim=0, afterRangeDelim=0, beforeUseDelim=0, afterUseDelim=1, beforeDeclDelim=1, afterDeclDelim=1, inDeclDelim=True, afterTypeDecl=1, beforeEqDo=0, afterEqDo=0, beforeEqCall=0, afterEqCall=0, beforeEqInit=0, afterEqInit=0, beforeEndcnt=1, afterBegincnt=1, afterIfwherecase=1, beforeThen=1, beforeIfaction=1, afterProgunit=1, endOfLine=True, afterName=0, inName=True, beforeCmdsep=0, afterCmdsep=1, adjacentKeywords=__NO_VALUE__, afterKeywords=__NO_VALUE__)
 
 changeIfStatementsInIfConstructs (self, singleItem=None)
 
 removeEmptyCONTAINS (self)
 

Detailed Description

Code formatting and style transformation methods.

Provides utilities for modifying code appearance including case,
indentation, spacing, and comment handling.

Definition at line 35 of file cosmetics.py.

Member Function Documentation

◆ changeIfStatementsInIfConstructs()

pyfortool.cosmetics.Cosmetics.changeIfStatementsInIfConstructs (   self,
  singleItem = None 
)
Convert one-line IF statements to IF-THEN-ENDIF blocks.

Parameters
----------
singleItem : Element, optional
    Specific IF statement node to convert. If None, converts all.

Transformation
-------------
Before:
    IF(A=B) print*, "C"

After:
    IF(A=B) THEN
        print*, "C"
    END IF

Examples
--------
>>> pft = PYFT('input.F90')
>>> pft.changeIfStatementsInIfConstructs()

Definition at line 916 of file cosmetics.py.

Here is the call graph for this function:

◆ indent()

pyfortool.cosmetics.Cosmetics.indent (   self,
  nodeToUpdate = None,
  indentProgramunit = 0,
  indentBranch = 2,
  exclDirectives = None 
)
Fix code indentation.

Parameters
----------
nodeToUpdate : Element, optional
    Specific node to indent. If None, indents entire scope.
indentProgramunit : int, optional
    Number of spaces for program unit contents. Default is 0.
indentBranch : int, optional
    Number of spaces for nested constructs (do, if, etc.). Default is 2.
exclDirectives : list, optional
    Lines to exclude from indentation:
    - None: Exclude '!$OMP' lines (default).
    - []: Include all lines.
    - ['!$acc', '!$mnh']: Custom directive list.

Returns
-------
self
    Returns self for chaining.

Examples
--------
>>> pft = PYFT('input.F90')
>>> pft.indent()  # Default indentation
>>> pft.indent(indentProgramunit=0, indentBranch=4)  # Custom

Definition at line 74 of file cosmetics.py.

◆ lowerCase()

pyfortool.cosmetics.Cosmetics.lowerCase (   self)
Convert all FORTRAN keywords to lowercase.

Examples
--------
>>> pft = PYFT('MIXED.F90')
>>> pft.lowerCase()
# WRITE -> write

Definition at line 59 of file cosmetics.py.

Here is the call graph for this function:

◆ removeComments()

pyfortool.cosmetics.Cosmetics.removeComments (   self,
  exclDirectives = None,
  pattern = None 
)
Remove comments from the source code.

Parameters
----------
exclDirectives : list, optional
    Comments to preserve (directives to exclude):
    - None: Preserve '!$OMP', '!$mnh', '!$ACC' lines (default).
    - []: Remove all comments.
    - ['!$acc']: Preserve only specific directives.
pattern : str or re.Pattern, optional
    Only remove comments matching this pattern.

Examples
--------
>>> pft = PYFT('input.F90')
>>> pft.removeComments()  # Keep OpenMP/directives
>>> pft.removeComments(exclDirectives=[])  # Remove all
>>> pft.removeComments(pattern=re.compile(r'!.*TODO'))

Definition at line 220 of file cosmetics.py.

Here is the caller graph for this function:

◆ removeEmptyCONTAINS()

pyfortool.cosmetics.Cosmetics.removeEmptyCONTAINS (   self)
Remove the CONTAINS statement if this section is empty

Definition at line 996 of file cosmetics.py.

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

◆ removeEmptyLines()

pyfortool.cosmetics.Cosmetics.removeEmptyLines (   self)
Remove empty lines

Definition at line 207 of file cosmetics.py.

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

◆ updateContinuation()

pyfortool.cosmetics.Cosmetics.updateContinuation (   self,
  nodeToUpdate = None,
  align = True,
  removeALL = False,
  addBegin = True,
  removeBegin = False 
)
:param nodeToUpdate: if None, the entire xml is updated
:param align: True to align begin of continued lines
:param removeALL: True to suppress all the continuation line characters ('&')
:param addBegin: True to add missing continuation line characters ('&')
                 at the begining of lines
:param removeBegin: True to suppress continuation line characters ('&')
                    at the begining of lines

When suppressed, the '&' are replaced by a space character
Comments after a '&' are lost

Definition at line 279 of file cosmetics.py.

◆ updateSpaces()

pyfortool.cosmetics.Cosmetics.updateSpaces (   self,
  beforeOp = 1,
  afterOp = 1,
  inOperator = True,
  beforeComma = 0,
  afterComma = 1,
  beforeParenthesis = 0,
  afterParenthesis = 0,
  beforeAffectation = 1,
  afterAffectation = 1,
  inAffectation = True,
  beforeRangeDelim = 0,
  afterRangeDelim = 0,
  beforeUseDelim = 0,
  afterUseDelim = 1,
  beforeDeclDelim = 1,
  afterDeclDelim = 1,
  inDeclDelim = True,
  afterTypeDecl = 1,
  beforeEqDo = 0,
  afterEqDo = 0,
  beforeEqCall = 0,
  afterEqCall = 0,
  beforeEqInit = 0,
  afterEqInit = 0,
  beforeEndcnt = 1,
  afterBegincnt = 1,
  afterIfwherecase = 1,
  beforeThen = 1,
  beforeIfaction = 1,
  afterProgunit = 1,
  endOfLine = True,
  afterName = 0,
  inName = True,
  beforeCmdsep = 0,
  afterCmdsep = 1,
  adjacentKeywords = __NO_VALUE__,
  afterKeywords = __NO_VALUE__ 
)
:param beforeOp, afterOp: number of spaces before and after operators
:param inOperator: True to suppress spaces in operators
:param beforeComma, afterComma: number of spaces before and after commas
:param beforeParenthesis, afterParenthesis: number of spaces before and after parenthesis
:param beforeAffectation, afterAffectation: number of spaces before and after
                                            affectations or associations
:param inAffectation: True to suppress spaces in affectations and in association ('= >')
:param beforeRangeDelim, afterRangeDelim: number of spaces before and after range delimiters
:param beforeUseDelim, afterUseDelim: number of spaces before and after use delimiters (':')
:param beforeDeclDelim, afterDeclDelim: number of spaces before and after declaration and
                                        enumerator delimiter ('::')
:param inDeclDelim: True to suppress spaces in declaration and enumerator delimiter (': :')
:param afterTypeDecl: number of spaces after the type in a declaration w/o '::'
                        (e.g. 'INTEGER I'); also for enumerators (minimum 1)
:param beforeEqDo, afterEqDo: number of spaces before and after '=' sign in DO and
                              FORALL statements
:param beforeEqCall, afterEqCall: number of spaces before and after '=' sign
                                  in CALL statement
:param beforeEqInit, afterEqInit: number of spaces before and after '=' sign for init values
:param beforeEndcnt, afterBegincnt: number of spaces before a continuation chararcter at the
                                    end of the line and after a continuation character
                                    at the begining of a line
:param afterIfwherecase: number of spaces after the IF, ELSEIF, WHERE, ELSEWHERE,
                         SELECTCASE, CASE and FORALL keywords
:param beforeThen: number of spaces before the THEN keyword
:param beforeIfaction: number of spaces
                       between IF condition and action in one-line IF statement and
                       between FORALL specification and affectation in one-line FORALL
                               statement and
                       between WHERE mask and action in one-line WHERE statement
:param afterProgunit: between the program unit type (e.g. SUBROUTINE) and its name
:param endOfLine: True to suppress spaces at the end of the line
:param afterName: number of spaces after an indentifier, type or attribute name
:param inName: True to suppress spaces in identifier names
:param beforeCmdsep, afterCmdsep: number of spaces before and after command separator (';')
:param adjacentKeywords: describes the number of spaces to introduce between adjancent
                         keywords when this is legal (the list comes from the table
                         "6.2 Adjacent keywords where separating blanks are optional" of the
                         F2008 norm and has been complemented by "end select",
                         "implicit none" and "module procedure"; for the last two,
                         a minimum of 1 is required).
                         The allowed dictionnary keys are:
                              - block_data
                              - double_precision
                              - else_if
                              - else_where
                              - end_associate
                              - end_block
                              - end_block_data
                              - end_critical
                              - end_do
                              - end_enum
                              - end_file
                              - end_forall
                              - end_function
                              - end_if
                              - end_interface
                              - end_module
                              - end_procedure
                              - end_program
                              - end_selec
                              - end_select
                              - end_submodule
                              - end_subroutine
                              - end_team
                              - end_type
                              - end_where
                              - go_to
                              - in_out
                              - select_case
                              - select_type
                              - implicit_none
                              - module_procedure
                         For example, use {'end_do':1} to write 'END DO' or
                                          {'end_do':0} to write 'ENDDO' or
                                          {'end_do':None} to not update the writting
                         or use adjacentKeywords=None to disable everything
:param afterKeywords: describes the number of spaces to introduce after keywords.
                       Some keywords need a more sophisticated treatment and are controled
                       by specific keys (e.g. CASE).
                       The keys are the keyword in lowercase, some names can be tricky
                       to guess (e.g. the key for ENDFILE is 'end-file'). By default
                       only a few are defined.
                       Use afterKeywords=None to disable everything.

To not update spaces, put None instead of an integer and False in booleans.
For example, to not change number of spaces after a comma, use afterComma=None

Updates are done in the following order:

Definition at line 483 of file cosmetics.py.

Here is the call graph for this function:

◆ upperCase()

pyfortool.cosmetics.Cosmetics.upperCase (   self)
Convert all FORTRAN keywords to uppercase.

Examples
--------
>>> pft = PYFT('mixed.F90')
>>> pft.upperCase()
# WRITE -> write becomes WRITE

Definition at line 44 of file cosmetics.py.

Here is the call graph for this function:

The documentation for this class was generated from the following file: