Added a macro decorator to register output classes.

This commit is contained in:
Salvador E. Tropea 2020-06-25 11:32:13 -03:00
parent 46a20404a4
commit 5ce3da2bcc
15 changed files with 57 additions and 94 deletions

View File

@ -1,4 +1,5 @@
from ast import (Assign, Name, Attribute, Expr, Num, Str, NameConstant, Load, Store, UnaryOp, USub) from ast import (Assign, Name, Attribute, Expr, Num, Str, NameConstant, Load, Store, UnaryOp, USub,
ClassDef, Call, ImportFrom, alias)
def document(sentences, to_source, **kw): def document(sentences, to_source, **kw):
@ -55,3 +56,20 @@ def document(sentences, to_source, **kw):
prev = s prev = s
# Return the modified AST # Return the modified AST
return sentences return sentences
def output_class(tree, **kw):
if isinstance(tree, ClassDef):
# Create the register call
name = tree.name
reg_name = name.lower()
# BaseOutput.register member:
attr = Attribute(value=Name(id='BaseOutput', ctx=Load()), attr='register', ctx=Load())
# Function call to it passing reg_name and name
do_register = Expr(value=Call(func=attr, args=[Str(s=reg_name), Name(id=name, ctx=Load())], keywords=[]))
# Create the import
do_import = ImportFrom(module='out_base', names=[alias(name='BaseOutput', asname=None)], level=1)
return [do_import, tree, do_register]
return tree

View File

@ -1,10 +1,10 @@
from pcbnew import PLOT_FORMAT_DXF from pcbnew import PLOT_FORMAT_DXF
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from .out_base import (BaseOutput)
from .out_any_layer import (AnyLayer) from .out_any_layer import (AnyLayer)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class DXF(AnyLayer): class DXF(AnyLayer):
""" """
DXF (Drawing Exchange Format) DXF (Drawing Exchange Format)
@ -41,7 +41,3 @@ class DXF(AnyLayer):
def _configure_plot_ctrl(self, po, output_dir): def _configure_plot_ctrl(self, po, output_dir):
super()._configure_plot_ctrl(po, output_dir) super()._configure_plot_ctrl(po, output_dir)
po.SetDXFPlotPolygonMode(self.polygon_mode) po.SetDXFPlotPolygonMode(self.polygon_mode)
# Register it
BaseOutput.register('dxf', DXF)

View File

@ -1,9 +1,9 @@
from pcbnew import (EXCELLON_WRITER) from pcbnew import (EXCELLON_WRITER)
from .out_base import (BaseOutput)
from .out_any_drill import (AnyDrill) from .out_any_drill import (AnyDrill)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class Excellon(AnyDrill): class Excellon(AnyDrill):
""" Excellon drill format """ Excellon drill format
This is the main format for the drilling machine. This is the main format for the drilling machine.
@ -26,7 +26,3 @@ class Excellon(AnyDrill):
drill_writer.SetOptions(self.mirror_y_axis, self.minimal_header, offset, self.pth_and_npth_single_file) drill_writer.SetOptions(self.mirror_y_axis, self.minimal_header, offset, self.pth_and_npth_single_file)
drill_writer.SetFormat(self.metric_units, EXCELLON_WRITER.DECIMAL_FORMAT) drill_writer.SetFormat(self.metric_units, EXCELLON_WRITER.DECIMAL_FORMAT)
return drill_writer return drill_writer
# Register it
BaseOutput.register('excellon', Excellon)

View File

@ -1,15 +1,16 @@
from pcbnew import (GERBER_WRITER) from pcbnew import (GERBER_WRITER)
from .out_base import (BaseOutput)
from .out_any_drill import (AnyDrill) from .out_any_drill import (AnyDrill)
from kiplot.macros import macros, output_class # noqa: F401
class GerbDrill(AnyDrill): @output_class
class Gerb_Drill(AnyDrill):
""" Gerber drill format """ Gerber drill format
This is the information for the drilling machine in gerber format. This is the information for the drilling machine in gerber format.
You can create a map file for documentation purposes. You can create a map file for documentation purposes.
This output is what you get from the 'File/Fabrication output/Drill Files' menu in pcbnew. """ This output is what you get from the 'File/Fabrication output/Drill Files' menu in pcbnew. """
def __init__(self, name, type, description): def __init__(self, name, type, description):
super(GerbDrill, self).__init__(name, type, description) super(Gerb_Drill, self).__init__(name, type, description)
def _configure_writer(self, board, offset): def _configure_writer(self, board, offset):
drill_writer = GERBER_WRITER(board) drill_writer = GERBER_WRITER(board)
@ -17,7 +18,3 @@ class GerbDrill(AnyDrill):
drill_writer.SetFormat(5) drill_writer.SetFormat(5)
drill_writer.SetOptions(offset) drill_writer.SetOptions(offset)
return drill_writer return drill_writer
# Register it
BaseOutput.register('gerb_drill', GerbDrill)

View File

@ -1,10 +1,10 @@
from pcbnew import (PLOT_FORMAT_GERBER) from pcbnew import (PLOT_FORMAT_GERBER)
from .out_base import (BaseOutput)
from .out_any_layer import (AnyLayer) from .out_any_layer import (AnyLayer)
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class Gerber(AnyLayer): class Gerber(AnyLayer):
""" Gerber format """ Gerber format
This is the main fabrication format for the PCB. This is the main fabrication format for the PCB.
@ -52,7 +52,3 @@ class Gerber(AnyLayer):
po.SetCreateGerberJobFile(self.create_gerber_job_file) po.SetCreateGerberJobFile(self.create_gerber_job_file)
po.SetUseGerberAttributes(self.use_gerber_x2_attributes) po.SetUseGerberAttributes(self.use_gerber_x2_attributes)
po.SetIncludeGerberNetlistInfo(self.use_gerber_net_attributes) po.SetIncludeGerberNetlistInfo(self.use_gerber_net_attributes)
# Register it
BaseOutput.register('gerber', Gerber)

View File

@ -1,10 +1,10 @@
from pcbnew import (PLOT_FORMAT_HPGL) from pcbnew import (PLOT_FORMAT_HPGL)
from .out_base import (BaseOutput)
from .out_any_layer import (AnyLayer) from .out_any_layer import (AnyLayer)
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class HPGL(AnyLayer): class HPGL(AnyLayer):
""" HPGL (Hewlett & Packard Graphics Language) """ HPGL (Hewlett & Packard Graphics Language)
Exports the PCB for plotters and laser printers. Exports the PCB for plotters and laser printers.
@ -41,7 +41,3 @@ class HPGL(AnyLayer):
def _configure_plot_ctrl(self, po, output_dir): def _configure_plot_ctrl(self, po, output_dir):
super()._configure_plot_ctrl(po, output_dir) super()._configure_plot_ctrl(po, output_dir)
po.SetHPGLPenDiameter(self.pen_width) po.SetHPGLPenDiameter(self.pen_width)
# Register it
BaseOutput.register('hpgl', HPGL)

View File

@ -1,15 +1,15 @@
import os import os
from subprocess import (check_output, STDOUT, CalledProcessError) from subprocess import (check_output, STDOUT, CalledProcessError)
from .out_base import (BaseOutput)
from .misc import (CMD_IBOM, URL_IBOM, BOM_ERROR) from .misc import (CMD_IBOM, URL_IBOM, BOM_ERROR)
from .kiplot import (GS, check_script) from .kiplot import (GS, check_script)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
from . import log from . import log
logger = log.get_logger(__name__) logger = log.get_logger(__name__)
class IBoM(BaseOutput): @output_class
class IBoM(BaseOutput): # noqa: F821
""" IBoM (Interactive HTML BoM) """ IBoM (Interactive HTML BoM)
Generates an interactive web page useful to identify the position of the components in the PCB. Generates an interactive web page useful to identify the position of the components in the PCB.
For more information: https://github.com/INTI-CMNB/InteractiveHtmlBom For more information: https://github.com/INTI-CMNB/InteractiveHtmlBom
@ -45,7 +45,3 @@ class IBoM(BaseOutput):
logger.debug('Output from command: '+e.output.decode()) logger.debug('Output from command: '+e.output.decode())
exit(BOM_ERROR) exit(BOM_ERROR)
logger.debug('Output from command:\n'+cmd_output.decode()+'\n') logger.debug('Output from command:\n'+cmd_output.decode()+'\n')
# Register it
BaseOutput.register('ibom', IBoM)

View File

@ -1,17 +1,17 @@
import os import os
from glob import (glob) from glob import (glob)
from subprocess import (check_output, STDOUT, CalledProcessError) from subprocess import (check_output, STDOUT, CalledProcessError)
from .out_base import (BaseOutput)
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from .misc import (CMD_KIBOM, URL_KIBOM, BOM_ERROR) from .misc import (CMD_KIBOM, URL_KIBOM, BOM_ERROR)
from .kiplot import (GS, check_script) from .kiplot import (GS, check_script)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
from . import log from . import log
logger = log.get_logger(__name__) logger = log.get_logger(__name__)
class KiBoM(BaseOutput): @output_class
class KiBoM(BaseOutput): # noqa: F821
""" KiBoM (KiCad Bill of Materials) """ KiBoM (KiCad Bill of Materials)
Used to generate the BoM in HTML or CSV format using the KiBoM plug-in. Used to generate the BoM in HTML or CSV format using the KiBoM plug-in.
For more information: https://github.com/INTI-CMNB/KiBoM For more information: https://github.com/INTI-CMNB/KiBoM
@ -52,7 +52,3 @@ class KiBoM(BaseOutput):
for f in glob(os.path.join(output_dir, prj)+'*.tmp'): for f in glob(os.path.join(output_dir, prj)+'*.tmp'):
os.remove(f) os.remove(f)
logger.debug('Output from command:\n'+cmd_output.decode()) logger.debug('Output from command:\n'+cmd_output.decode())
# Register it
BaseOutput.register('kibom', KiBoM)

View File

@ -1,10 +1,10 @@
from pcbnew import PLOT_FORMAT_PDF from pcbnew import PLOT_FORMAT_PDF
from .out_base import BaseOutput
from .out_any_layer import AnyLayer from .out_any_layer import AnyLayer
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class PDF(AnyLayer): class PDF(AnyLayer):
""" PDF (Portable Document Format) """ PDF (Portable Document Format)
Exports the PCB to the most common exhange format. Suitable for printing. Exports the PCB to the most common exhange format. Suitable for printing.
@ -37,7 +37,3 @@ class PDF(AnyLayer):
def config(self, outdir, options, layers): def config(self, outdir, options, layers):
super().config(outdir, options, layers) super().config(outdir, options, layers)
self._drill_marks = self._drill_marks_map[self._drill_marks] self._drill_marks = self._drill_marks_map[self._drill_marks]
# Register it
BaseOutput.register('pdf', PDF)

View File

@ -1,23 +1,23 @@
import os import os
from subprocess import (call) from subprocess import (call)
from .out_base import BaseOutput
from .pre_base import BasePreFlight from .pre_base import BasePreFlight
from .error import (KiPlotConfigurationError, PlotError) from .error import (KiPlotConfigurationError, PlotError)
from .kiplot import (check_script, GS) from .kiplot import (check_script, GS)
from .misc import (CMD_PCBNEW_PRINT_LAYERS, URL_PCBNEW_PRINT_LAYERS, PDF_PCB_PRINT) from .misc import (CMD_PCBNEW_PRINT_LAYERS, URL_PCBNEW_PRINT_LAYERS, PDF_PCB_PRINT)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
from . import log from . import log
logger = log.get_logger(__name__) logger = log.get_logger(__name__)
class PDFPcbPrint(BaseOutput): @output_class
class PDF_Pcb_Print(BaseOutput): # noqa: F821
""" PDF PCB Print (Portable Document Format) """ PDF PCB Print (Portable Document Format)
Exports the PCB to the most common exhange format. Suitable for printing. Exports the PCB to the most common exhange format. Suitable for printing.
This is the main format to document your PCB. This is the main format to document your PCB.
This output is what you get from the 'File/Print' menu in pcbnew. """ This output is what you get from the 'File/Print' menu in pcbnew. """
def __init__(self, name, type, description): def __init__(self, name, type, description):
super(PDFPcbPrint, self).__init__(name, type, description) super(PDF_Pcb_Print, self).__init__(name, type, description)
# Options # Options
with document: with document:
self.output_name = '' self.output_name = ''
@ -58,7 +58,3 @@ class PDFPcbPrint(BaseOutput):
if ret: if ret:
logger.error(CMD_PCBNEW_PRINT_LAYERS+' returned %d', ret) logger.error(CMD_PCBNEW_PRINT_LAYERS+' returned %d', ret)
exit(PDF_PCB_PRINT) exit(PDF_PCB_PRINT)
# Register it
BaseOutput.register('pdf_pcb_print', PDFPcbPrint)

View File

@ -1,21 +1,21 @@
import os import os
from subprocess import (call) from subprocess import (call)
from .out_base import BaseOutput
from .kiplot import (check_eeschema_do, GS) from .kiplot import (check_eeschema_do, GS)
from .misc import (CMD_EESCHEMA_DO, PDF_SCH_PRINT) from .misc import (CMD_EESCHEMA_DO, PDF_SCH_PRINT)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
from . import log from . import log
logger = log.get_logger(__name__) logger = log.get_logger(__name__)
class PDFSchPrint(BaseOutput): @output_class
class PDF_Sch_Print(BaseOutput): # noqa: F821
""" PDF Schematic Print (Portable Document Format) """ PDF Schematic Print (Portable Document Format)
Exports the PCB to the most common exhange format. Suitable for printing. Exports the PCB to the most common exhange format. Suitable for printing.
This is the main format to document your schematic. This is the main format to document your schematic.
This output is what you get from the 'File/Print' menu in eeschema. """ This output is what you get from the 'File/Print' menu in eeschema. """
def __init__(self, name, type, description): def __init__(self, name, type, description):
super(PDFSchPrint, self).__init__(name, type, description) super(PDF_Sch_Print, self).__init__(name, type, description)
self._sch_related = True self._sch_related = True
# Options # Options
with document: with document:
@ -38,7 +38,3 @@ class PDFSchPrint(BaseOutput):
new = os.path.abspath(os.path.join(output_dir, self.output)) new = os.path.abspath(os.path.join(output_dir, self.output))
logger.debug('Moving '+cur+' -> '+new) logger.debug('Moving '+cur+' -> '+new)
os.rename(cur, new) os.rename(cur, new)
# Register it
BaseOutput.register('pdf_sch_print', PDFSchPrint)

View File

@ -2,12 +2,12 @@ import os
import operator import operator
from datetime import datetime from datetime import datetime
from pcbnew import (IU_PER_MM, IU_PER_MILS) from pcbnew import (IU_PER_MM, IU_PER_MILS)
from .out_base import BaseOutput
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
class Position(BaseOutput): @output_class
class Position(BaseOutput): # noqa: F821
""" Pick & place """ Pick & place
Generates the file with position information for the PCB components, used by the pick and place machine. Generates the file with position information for the PCB components, used by the pick and place machine.
This output is what you get from the 'File/Fabrication output/Footprint poistion (.pos) file' menu in pcbnew. """ This output is what you get from the 'File/Fabrication output/Footprint poistion (.pos) file' menu in pcbnew. """
@ -174,7 +174,3 @@ class Position(BaseOutput):
self._do_position_plot_ascii(board, output_dir, columns, modules, maxlengths) self._do_position_plot_ascii(board, output_dir, columns, modules, maxlengths)
else: # if self.format == 'CSV': else: # if self.format == 'CSV':
self._do_position_plot_csv(board, output_dir, columns, modules) self._do_position_plot_csv(board, output_dir, columns, modules)
# Register it
BaseOutput.register('position', Position)

View File

@ -1,10 +1,10 @@
from pcbnew import PLOT_FORMAT_POST from pcbnew import PLOT_FORMAT_POST
from .out_base import BaseOutput
from .out_any_layer import AnyLayer from .out_any_layer import AnyLayer
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class PS(AnyLayer): class PS(AnyLayer):
""" PS (Postscript) """ PS (Postscript)
Exports the PCB to a format suitable for printing. Exports the PCB to a format suitable for printing.
@ -56,7 +56,3 @@ class PS(AnyLayer):
po.SetFineScaleAdjustX(self.scale_adjust_x) po.SetFineScaleAdjustX(self.scale_adjust_x)
po.SetFineScaleAdjustX(self.scale_adjust_y) po.SetFineScaleAdjustX(self.scale_adjust_y)
po.SetA4Output(self.a4_output) po.SetA4Output(self.a4_output)
# Register it
BaseOutput.register('ps', PS)

View File

@ -1,17 +1,17 @@
import os import os
import re import re
from subprocess import (check_output, STDOUT, CalledProcessError) from subprocess import (check_output, STDOUT, CalledProcessError)
from .out_base import BaseOutput
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from .misc import (KICAD2STEP, KICAD2STEP_ERR) from .misc import (KICAD2STEP, KICAD2STEP_ERR)
from .kiplot import (GS) from .kiplot import (GS)
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
from . import log from . import log
logger = log.get_logger(__name__) logger = log.get_logger(__name__)
class STEP(BaseOutput): @output_class
class STEP(BaseOutput): # noqa: F821
""" STEP (ISO 10303-21 Clear Text Encoding of the Exchange Structure) """ STEP (ISO 10303-21 Clear Text Encoding of the Exchange Structure)
Exports the PCB as a 3D model. Exports the PCB as a 3D model.
This is the most common 3D format for exchange purposes. This is the most common 3D format for exchange purposes.
@ -81,7 +81,3 @@ class STEP(BaseOutput):
logger.debug('Output from command: '+e.output.decode()) logger.debug('Output from command: '+e.output.decode())
exit(KICAD2STEP_ERR) exit(KICAD2STEP_ERR)
logger.debug('Output from command:\n'+cmd_output.decode()) logger.debug('Output from command:\n'+cmd_output.decode())
# Register it
BaseOutput.register('step', STEP)

View File

@ -1,10 +1,10 @@
from pcbnew import PLOT_FORMAT_SVG from pcbnew import PLOT_FORMAT_SVG
from .out_base import BaseOutput
from .out_any_layer import AnyLayer from .out_any_layer import AnyLayer
from .error import KiPlotConfigurationError from .error import KiPlotConfigurationError
from kiplot.macros import macros, document # noqa: F401 from kiplot.macros import macros, document, output_class # noqa: F401
@output_class
class SVG(AnyLayer): class SVG(AnyLayer):
""" SVG (Scalable Vector Graphics) """ SVG (Scalable Vector Graphics)
Exports the PCB to a format suitable for 2D graphics software. Exports the PCB to a format suitable for 2D graphics software.
@ -37,7 +37,3 @@ class SVG(AnyLayer):
def config(self, outdir, options, layers): def config(self, outdir, options, layers):
super().config(outdir, options, layers) super().config(outdir, options, layers)
self._drill_marks = self._drill_marks_map[self._drill_marks] self._drill_marks = self._drill_marks_map[self._drill_marks]
# Register it
BaseOutput.register('svg', SVG)