diff options
author | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2022-11-28 00:32:17 +0100 |
---|---|---|
committer | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2022-11-30 23:32:47 +0100 |
commit | 51f0ead16d60c63d1b069f807e3513bba11d8947 (patch) | |
tree | 50094f57aeb7f9562341908864624199f4d7bc4a /pyGHDL/dom/formatting | |
parent | 473ed87bb505916e74441f01508c109bf39b30a7 (diff) | |
download | ghdl-51f0ead16d60c63d1b069f807e3513bba11d8947.tar.gz ghdl-51f0ead16d60c63d1b069f807e3513bba11d8947.tar.bz2 ghdl-51f0ead16d60c63d1b069f807e3513bba11d8947.zip |
Converted string formatting to f-strings.
Diffstat (limited to 'pyGHDL/dom/formatting')
-rw-r--r-- | pyGHDL/dom/formatting/prettyprint.py | 283 |
1 files changed, 79 insertions, 204 deletions
diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py index a72a32862..14d13e2c5 100644 --- a/pyGHDL/dom/formatting/prettyprint.py +++ b/pyGHDL/dom/formatting/prettyprint.py @@ -117,14 +117,14 @@ class PrettyPrint: def formatDesign(self, design: Design, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}Libraries:".format(prefix=prefix)) + buffer.append(f"{prefix}Libraries:") for library in design.Libraries.values(): - buffer.append("{prefix} - Name: {name}".format(prefix=prefix, name=library.Identifier)) + buffer.append(f"{prefix} - Name: {library.Identifier}") for line in self.formatLibrary(library, level + 2): buffer.append(line) - buffer.append("{prefix}Documents:".format(prefix=prefix)) + buffer.append(f"{prefix}Documents:") for document in design.Documents: - buffer.append("{prefix} - Path: '{doc!s}':".format(doc=document.Path, prefix=prefix)) + buffer.append(f"{prefix} - Path: '{document.Path!s}':") for line in self.formatDocument(document, level + 2): buffer.append(line) @@ -133,48 +133,36 @@ class PrettyPrint: def formatLibrary(self, library: Library, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}Entities:".format(prefix=prefix)) + buffer.append(f"{prefix}Entities:") for entity in library.Entities: - buffer.append( - "{prefix} - {name}({architectures})".format( - prefix=prefix, - name=entity.Identifier, - architectures=", ".join([a.Identifier for a in entity.Architectures]), - ) - ) - buffer.append("{prefix}Packages:".format(prefix=prefix)) + buffer.append(f"{prefix} - {entity.Identifier}({', '.join([a.Identifier for a in entity.Architectures])})") + buffer.append(f"{prefix}Packages:") for package in library.Packages: if isinstance(package, Package): - buffer.append("{prefix} - {name}".format(prefix=prefix, name=package.Identifier)) + buffer.append(f"{prefix} - {package.Identifier}") elif isinstance(package, PackageInstantiation): - buffer.append( - "{prefix} - {name} instantiate from {package}".format( - prefix=prefix, - name=package.Identifier, - package=package.PackageReference, - ) - ) - buffer.append("{prefix}Configurations:".format(prefix=prefix)) + buffer.append(f"{prefix} - {package.Identifier} instantiate from {package.PackageReference}") + buffer.append(f"{prefix}Configurations:") for configuration in library.Configurations: - buffer.append("{prefix} - {name}".format(prefix=prefix, name=configuration.Identifier)) - buffer.append("{prefix}Contexts:".format(prefix=prefix)) + buffer.append(f"{prefix} - {configuration.Identifier}") + buffer.append(f"{prefix}Contexts:") for context in library.Contexts: - buffer.append("{prefix} - {name}".format(prefix=prefix, name=context.Identifier)) + buffer.append(f"{prefix} - {context.Identifier}") return buffer def formatDocument(self, document: Document, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}Entities:".format(prefix=prefix)) + buffer.append(f"{prefix}Entities:") for entity in document.Entities: for line in self.formatEntity(entity, level + 1): buffer.append(line) - buffer.append("{prefix}Architectures:".format(prefix=prefix)) + buffer.append(f"{prefix}Architectures:") for architecture in document.Architectures: for line in self.formatArchitecture(architecture, level + 1): buffer.append(line) - buffer.append("{prefix}Packages:".format(prefix=prefix)) + buffer.append(f"{prefix}Packages:") for package in document.Packages: if isinstance(package, Package): gen = self.formatPackage @@ -183,15 +171,15 @@ class PrettyPrint: for line in gen(package, level + 1): buffer.append(line) - buffer.append("{prefix}PackageBodies:".format(prefix=prefix)) + buffer.append(f"{prefix}PackageBodies:") for packageBodies in document.PackageBodies: for line in self.formatPackageBody(packageBodies, level + 1): buffer.append(line) - buffer.append("{prefix}Configurations:".format(prefix=prefix)) + buffer.append(f"{prefix}Configurations:") for configuration in document.Configurations: for line in self.formatConfiguration(configuration, level + 1): buffer.append(line) - buffer.append("{prefix}Contexts:".format(prefix=prefix)) + buffer.append(f"{prefix}Contexts:") for context in document.Contexts: for line in self.formatContext(context, level + 1): buffer.append(line) @@ -201,60 +189,44 @@ class PrettyPrint: def formatEntity(self, entity: Entity, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append( - "{prefix}- Name: {name}\n{prefix} File: {file}\n{prefix} Position: {line}:{column}".format( - name=entity.Identifier, - prefix=prefix, - file=entity.Position.Filename.name, - line=entity.Position.Line, - column=entity.Position.Column, - ) - ) - buffer.append("{prefix} Generics:".format(prefix=prefix)) + buffer.append(f"{prefix}- Name: {entity.Identifier}\n{prefix} File: {entity.Position.Filename.name}\n{prefix} Position: {entity.Position.Line}:{entity.Position.Column}") + buffer.append(f"{prefix} Generics:") for generic in entity.GenericItems: for line in self.formatGeneric(generic, level + 1): buffer.append(line) - buffer.append("{prefix} Ports:".format(prefix=prefix)) + buffer.append(f"{prefix} Ports:") for port in entity.PortItems: for line in self.formatPort(port, level + 1): buffer.append(line) - buffer.append("{prefix} Declared:".format(prefix=prefix)) + buffer.append(f"{prefix} Declared:") for item in entity.DeclaredItems: for line in self.formatDeclaredItems(item, level + 1): buffer.append(line) - buffer.append("{prefix} Statements:".format(prefix=prefix)) + buffer.append(f"{prefix} Statements:") for item in entity.Statements: - buffer.append("{prefix} ...".format(prefix=prefix)) - buffer.append("{prefix} Architecures:".format(prefix=prefix)) + buffer.append(f"{prefix} ...") + buffer.append(f"{prefix} Architectures:") for item in entity.Architectures: - buffer.append("{prefix} - {name}".format(prefix=prefix, name=item.Identifier)) + buffer.append(f"{prefix} - {item.Identifier}") return buffer def formatArchitecture(self, architecture: Architecture, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append( - "{prefix}- Name: {name}\n{prefix} File: {file}\n{prefix} Position: {line}:{column}".format( - name=architecture.Identifier, - prefix=prefix, - file=architecture.Position.Filename.name, - line=architecture.Position.Line, - column=architecture.Position.Column, - ) - ) - buffer.append("{prefix} Entity: {entity}".format(entity=architecture.Entity.SymbolName, prefix=prefix)) - buffer.append("{prefix} Declared:".format(prefix=prefix)) + buffer.append(f"{prefix}- Name: {architecture.Identifier}\n{prefix} File: {architecture.Position.Filename.name}\n{prefix} Position: {architecture.Position.Line}:{architecture.Position.Column}") + buffer.append(f"{prefix} Entity: {architecture.Entity.SymbolName}") + buffer.append(f"{prefix} Declared:") for item in architecture.DeclaredItems: for line in self.formatDeclaredItems(item, level + 2): buffer.append(line) - buffer.append("{prefix} Hierarchy:".format(prefix=prefix)) + buffer.append(f"{prefix} Hierarchy:") for item in architecture.Statements: for line in self.formatHierarchy(item, level + 2): buffer.append(line) - buffer.append("{prefix} Statements:".format(prefix=prefix)) + buffer.append(f"{prefix} Statements:") for item in architecture.Statements: - buffer.append("{prefix} ...".format(prefix=prefix)) + buffer.append(f"{prefix} ...") # for line in self.formatStatements(item, level + 2): # buffer.append(line) @@ -263,12 +235,12 @@ class PrettyPrint: def formatComponent(self, component: Component, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}- Component: {name}".format(name=component.Identifier, prefix=prefix)) - buffer.append("{prefix} Generics:".format(prefix=prefix)) + buffer.append(f"{prefix}- Component: {component.Identifier}") + buffer.append(f"{prefix} Generics:") for generic in component.GenericItems: for line in self.formatGeneric(generic, level + 1): buffer.append(line) - buffer.append("{prefix} Ports:".format(prefix=prefix)) + buffer.append(f"{prefix} Ports:") for port in component.PortItems: for line in self.formatPort(port, level + 1): buffer.append(line) @@ -278,16 +250,8 @@ class PrettyPrint: def formatPackage(self, package: Package, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append( - "{prefix}- Name: {name}\n{prefix} File: {file}\n{prefix} Position: {line}:{column}".format( - name=package.Identifier, - prefix=prefix, - file=package.Position.Filename.name, - line=package.Position.Line, - column=package.Position.Column, - ) - ) - buffer.append("{prefix} Declared:".format(prefix=prefix)) + buffer.append(f"{prefix}- Name: {package.Identifier}\n{prefix} File: {package.Position.Filename.name}\n{prefix} Position: {package.Position.Line}:{package.Position.Column}") + buffer.append(f"{prefix} Declared:") for item in package.DeclaredItems: for line in self.formatDeclaredItems(item, level + 1): buffer.append(line) @@ -297,9 +261,9 @@ class PrettyPrint: def formatPackageInstance(self, package: PackageInstantiation, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}- Name: {name}".format(name=package.Identifier, prefix=prefix)) - buffer.append("{prefix} Package: {name!s}".format(prefix=prefix, name=package.PackageReference)) - buffer.append("{prefix} Generic Map: ...".format(prefix=prefix)) + buffer.append(f"{prefix}- Name: {package.Identifier}") + buffer.append(f"{prefix} Package: {package.PackageReference!s}") + buffer.append(f"{prefix} Generic Map: ...") # for item in package.GenericItems: # for line in self.formatGeneric(item, level + 1): # buffer.append(line) @@ -309,8 +273,8 @@ class PrettyPrint: def formatPackageBody(self, packageBody: PackageBody, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}- Name: {name}".format(name=packageBody.Identifier, prefix=prefix)) - buffer.append("{prefix} Declared:".format(prefix=prefix)) + buffer.append(f"{prefix}- Name: {packageBody.Identifier}") + buffer.append(f"{prefix} Declared:") for item in packageBody.DeclaredItems: for line in self.formatDeclaredItems(item, level + 1): buffer.append(line) @@ -320,14 +284,14 @@ class PrettyPrint: def formatConfiguration(self, configuration: Configuration, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}- Name: {name}".format(name=configuration.Identifier, prefix=prefix)) + buffer.append(f"{prefix}- Name: {configuration.Identifier}") return buffer def formatContext(self, context: Context, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level - buffer.append("{prefix}- Name: {name}".format(name=context.Identifier, prefix=prefix)) + buffer.append(f"{prefix}- Name: {context.Identifier}") return buffer @@ -337,21 +301,13 @@ class PrettyPrint: elif isinstance(generic, GenericTypeInterfaceItem): return self.formatGenericType(generic, level) else: - raise PrettyPrintException( - "Unhandled generic kind '{kind}' for generic '{name}'.".format( - kind=generic.__class__.__name__, name=generic.Identifiers[0] - ) - ) + raise PrettyPrintException(f"Unhandled generic kind '{generic.__class__.__name__}' for generic '{generic.Identifiers[0]}'.") def formatPort(self, port: Union[NamedEntity, PortInterfaceItem], level: int = 0) -> StringBuffer: if isinstance(port, PortSignalInterfaceItem): return self.formatPortSignal(port, level) else: - raise PrettyPrintException( - "Unhandled port kind '{kind}' for port '{name}'.".format( - kind=port.__class__.__name__, name=port.Identifiers[0] - ) - ) + raise PrettyPrintException(f"Unhandled port kind '{port.__class__.__name__}' for port '{port.Identifiers[0]}'.") def formatGenericConstant(self, generic: GenericConstantInterfaceItem, level: int = 0) -> StringBuffer: buffer = [] @@ -373,12 +329,7 @@ class PrettyPrint: buffer = [] prefix = " " * level - buffer.append( - "{prefix} - type {name}".format( - prefix=prefix, - name=generic.Identifier, - ) - ) + buffer.append(f"{prefix} - type {generic.Identifier}") return buffer @@ -404,7 +355,7 @@ class PrettyPrint: if isinstance(item, BaseConstant): if isinstance(item, Constant): - default = " := {expr}".format(expr=str(item.DefaultExpression)) + default = f" := {item.DefaultExpression!s}" else: default = "" @@ -430,7 +381,7 @@ class PrettyPrint: prefix=prefix, name=", ".join(item.Identifiers), subtype=self.formatSubtypeIndication(item.Subtype, "signal", item.Identifiers[0]), - initValue=" := {expr}".format(expr=str(item.DefaultExpression)) + initValue=f" := {item.DefaultExpression!s}" if item.DefaultExpression is not None else "", ) @@ -444,74 +395,41 @@ class PrettyPrint: ) ) elif isinstance(item, (FullType, IncompleteType)): - buffer.append("{prefix}- {type}".format(prefix=prefix, type=self.formatType(item))) + buffer.append(f"{prefix}- {self.formatType(item)}") elif isinstance(item, Subtype): - buffer.append( - "{prefix}- subtype {name} is ?????".format( - prefix=prefix, - name=item.Identifier, - ) - ) + buffer.append(f"{prefix}- subtype {item.Identifier} is ?????") elif isinstance(item, Alias): - buffer.append( - "{prefix}- alias {name} is ?????".format( - prefix=prefix, - name=item.Identifier, - ) - ) + buffer.append(f"{prefix}- alias {item.Identifier} is ?????") elif isinstance(item, Function): - buffer.append( - "{prefix}- function {name} return {returnType!s}".format( - prefix=prefix, name=item.Identifier, returnType=item.ReturnType - ) - ) + buffer.append(f"{prefix}- function {item.Identifier} return {item.ReturnType!s}") elif isinstance(item, Procedure): - buffer.append( - "{prefix}- procedure {name}".format( - prefix=prefix, - name=item.Identifier, - ) - ) + buffer.append(f"{prefix}- procedure {item.Identifier}") elif isinstance(item, Component): for line in self.formatComponent(item, level): buffer.append(line) elif isinstance(item, Attribute): - buffer.append( - "{prefix}- attribute {name} : {type!s}".format(prefix=prefix, name=item.Identifier, type=item.Subtype) - ) + buffer.append(f"{prefix}- attribute {item.Identifier} : {item.Subtype!s}") elif isinstance(item, AttributeSpecification): - buffer.append( - "{prefix}- attribute {name!s} of {entity} : {entityClass} is {value}".format( - prefix=prefix, - name=item.Attribute, - entity="????", - entityClass="????", - value="????", - ) - ) + buffer.append(f"{prefix}- attribute {item.Attribute!s} of {'????'} : {'????'} is {'????'}") elif isinstance(item, UseClause): buffer.append("{prefix}- use {names}".format(prefix=prefix, names=", ".join([str(n) for n in item.Names]))) elif isinstance(item, Package): - buffer.append("{prefix}- package {name} is ..... end package".format(prefix=prefix, name=item.Identifier)) + buffer.append(f"{prefix}- package {item.Identifier} is ..... end package") elif isinstance(item, PackageInstantiation): - buffer.append( - "{prefix}- package {name} is new {name2!s} generic map (.....)".format( - prefix=prefix, name=item.Identifier, name2=item.PackageReference - ) - ) + buffer.append(f"{prefix}- package {item.Identifier} is new {item.PackageReference!s} generic map (.....)") elif isinstance(item, DefaultClock): - buffer.append("{prefix}- default {name} is {expr}".format(prefix=prefix, name=item.Identifier, expr="...")) + buffer.append(f"{prefix}- default {item.Identifier} is {'...'}", ) else: - raise PrettyPrintException("Unhandled declared item kind '{name}'.".format(name=item.__class__.__name__)) + raise PrettyPrintException(f"Unhandled declared item kind '{item.__class__.__name__}'.") return buffer def formatType(self, item: BaseType) -> str: - result = "type {name} is ".format(name=item.Identifier) + result = f"type {item.Identifier} is " if isinstance(item, IncompleteType): result += "" elif isinstance(item, IntegerType): - result += "range {range!s}".format(range=item.Range) + result += f"range {item.Range!s}" elif isinstance(item, EnumeratedType): result += "(........)" elif isinstance(item, PhysicalType): @@ -529,13 +447,13 @@ class PrettyPrint: elif isinstance(item, ProtectedTypeBody): result += "protected body ..... end protected body" else: - raise PrettyPrintException("Unknown type '{name}'".format(name=item.__class__.__name__)) + raise PrettyPrintException(f"Unknown type '{item.__class__.__name__}'") return result def formatSubtypeIndication(self, subtypeIndication, entity: str, name: str) -> str: if isinstance(subtypeIndication, SimpleSubtypeSymbol): - return "{type}".format(type=subtypeIndication.SymbolName) + return f"{subtypeIndication.SymbolName}" elif isinstance(subtypeIndication, ConstrainedCompositeSubtypeSymbol): constraints = [] for constraint in subtypeIndication.Constraints: @@ -543,102 +461,59 @@ class PrettyPrint: return "{type}({constraints})".format(type=subtypeIndication.SymbolName, constraints=", ".join(constraints)) else: - raise PrettyPrintException( - "Unhandled subtype kind '{type}' for {entity} '{name}'.".format( - type=subtypeIndication.__class__.__name__, entity=entity, name=name - ) - ) + raise PrettyPrintException(f"Unhandled subtype kind '{subtypeIndication.__class__.__name__}' for {entity} '{name}'.") def formatInitialValue(self, item: WithDefaultExpressionMixin) -> str: if item.DefaultExpression is None: return "" - return " := {expr!s}".format(expr=item.DefaultExpression) + return f" := {item.DefaultExpression!s}" def formatHierarchy(self, statement: ConcurrentStatement, level: int = 0) -> StringBuffer: buffer = [] prefix = " " * level if isinstance(statement, ProcessStatement): - buffer.append("{prefix}- {label}: process(...)".format(prefix=prefix, label=statement.Label)) + buffer.append(f"{prefix}- {statement.Label}: process(...)") elif isinstance(statement, EntityInstantiation): - buffer.append( - "{prefix}- {label}: entity {name}".format(prefix=prefix, label=statement.Label, name=statement.Entity) - ) + buffer.append(f"{prefix}- {statement.Label}: entity {statement.Entity}") elif isinstance(statement, ComponentInstantiation): - buffer.append( - "{prefix}- {label}: component {name}".format( - prefix=prefix, label=statement.Label, name=statement.Component - ) - ) + buffer.append(f"{prefix}- {statement.Label}: component {statement.Component}") elif isinstance(statement, ConfigurationInstantiation): - buffer.append( - "{prefix}- {label}: configuration {name}".format( - prefix=prefix, label=statement.Label, name=statement.Configuration - ) - ) + buffer.append(f"{prefix}- {statement.Label}: configuration {statement.Configuration}") elif isinstance(statement, ConcurrentBlockStatement): - buffer.append("{prefix}- {label}: block".format(prefix=prefix, label=statement.Label)) + buffer.append(f"{prefix}- {statement.Label}: block") for stmt in statement.Statements: for line in self.formatHierarchy(stmt, level + 2): buffer.append(line) elif isinstance(statement, IfGenerateStatement): - buffer.append( - "{prefix}- {label}: if {condition} generate".format( - prefix=prefix, - label=statement.Label, - condition=statement.IfBranch.Condition, - ) - ) + buffer.append(f"{prefix}- {statement.Label}: if {statement.IfBranch.Condition} generate") for stmt in statement.IfBranch.Statements: for line in self.formatHierarchy(stmt, level + 2): buffer.append(line) for elsifBranch in statement.ElsifBranches: - buffer.append( - "{prefix} {label}: elsif {condition} generate".format( - prefix=prefix, - label=statement.Label, - condition=elsifBranch.Condition, - ) - ) + buffer.append(f"{prefix} {statement.Label}: elsif {elsifBranch.Condition} generate") for stmt in elsifBranch.Statements: for line in self.formatHierarchy(stmt, level + 2): buffer.append(line) if statement.ElseBranch is not None: - buffer.append("{prefix} {label}: else generate".format(prefix=prefix, label=statement.Label)) + buffer.append(f"{prefix} {statement.Label}: else generate") for stmt in statement.ElseBranch.Statements: for line in self.formatHierarchy(stmt, level + 2): buffer.append(line) elif isinstance(statement, CaseGenerateStatement): - buffer.append( - "{prefix}- {label}: case {expression} generate".format( - prefix=prefix, - label=statement.Label, - expression=statement.SelectExpression, - ) - ) + buffer.append(f"{prefix}- {statement.Label}: case {statement.SelectExpression} generate") for case in statement.Cases: - buffer.append("{prefix} {case!s}".format(prefix=prefix, label=case.Label, case=case)) + buffer.append(f"{prefix} {case!s}") for stmt in case.Statements: for line in self.formatHierarchy(stmt, level + 2): buffer.append(line) elif isinstance(statement, ForGenerateStatement): - buffer.append( - "{prefix}- {label}: for {index} in {range} generate".format( - prefix=prefix, - label=statement.Label, - index=statement.LoopIndex, - range=statement.Range, - ) - ) + buffer.append(f"{prefix}- {statement.Label}: for {statement.LoopIndex} in {statement.Range} generate") for stmt in statement.Statements: for line in self.formatHierarchy(stmt, level + 2): buffer.append(line) elif isinstance(statement, ConcurrentProcedureCall): - buffer.append( - "{prefix}- {label}: {name!s}(...)".format( - prefix=prefix, label=statement.Label, name=statement.Procedure - ) - ) + buffer.append(f"{prefix}- {statement.Label}: {statement.Procedure!s}(...)") return buffer |