diff --git a/kibot/drill_marks.py b/kibot/drill_marks.py index 42206374..629da870 100644 --- a/kibot/drill_marks.py +++ b/kibot/drill_marks.py @@ -6,7 +6,7 @@ from pcbnew import (PCB_PLOT_PARAMS) from .error import KiPlotConfigurationError from .out_any_layer import AnyLayerOptions -from kibot.macros import macros, document # noqa: F401 +from .macros import macros, document # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/layer.py b/kibot/layer.py index c9c49a89..22f3eea2 100644 --- a/kibot/layer.py +++ b/kibot/layer.py @@ -9,7 +9,7 @@ from .gs import GS from re import match from .error import (PlotError, KiPlotConfigurationError) -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class Layer(Optionable): diff --git a/kibot/mcpy/core.py b/kibot/mcpy/core.py index 86be296a..462c5bd6 100644 --- a/kibot/mcpy/core.py +++ b/kibot/mcpy/core.py @@ -100,7 +100,7 @@ def expand_macros(tree, bindings): return expansion -def find_macros(tree): +def find_macros(tree, name): """ Looks for `from ... import macros, ...` statements in the module body and returns a dict with names and implementations for found macros or an empty @@ -109,7 +109,7 @@ def find_macros(tree): bindings = {} for index, statement in enumerate(tree.body): if _is_macro_import(statement): - bindings.update(_get_macros(statement)) + bindings.update(_get_macros(statement, name)) # Remove all names to prevent macro names to be used module = statement.module tree.body[index] = copy_location( @@ -122,7 +122,7 @@ def find_macros(tree): def _is_macro_import(statement): """ - A "macro import" is a statement with the form of + A 'macro import' is a statement with the form of from ... import macros, ... @@ -136,10 +136,24 @@ def _is_macro_import(statement): return is_macro_import -def _get_macros(macroimport): +def _fix_level(modulename, name, level): + """ Translates the relative import to an absolute one """ + path = name.split('.') + index = -2 + while level: + modulename = path[index]+'.'+modulename + level -= 1 + index -= 1 + return modulename + + +def _get_macros(macroimport, name): """ Returns a map with names and macros from the macro import statement. """ + if macroimport.level: + macroimport.module = _fix_level(macroimport.module, name, macroimport.level) + macroimport.level = 0 modulename = macroimport.module __import__(modulename) module = sys.modules[modulename] diff --git a/kibot/mcpy/importhooks.py b/kibot/mcpy/importhooks.py index 2f8ee943..091a4784 100644 --- a/kibot/mcpy/importhooks.py +++ b/kibot/mcpy/importhooks.py @@ -10,7 +10,7 @@ def source_to_xcode(self, data, path, *, _optimize=-1): '''Intercepts the source to code transformation and expand the macros before compiling to actual code.''' tree = ast.parse(data) - module_macro_bindings = find_macros(tree) + module_macro_bindings = find_macros(tree, self.name) expansion = expand_macros(tree, bindings=module_macro_bindings) return compile(expansion, path, 'exec', dont_inherit=True, optimize=_optimize) diff --git a/kibot/out_any_drill.py b/kibot/out_any_drill.py index b635eb58..b2e982d2 100644 --- a/kibot/out_any_drill.py +++ b/kibot/out_any_drill.py @@ -8,7 +8,7 @@ from pcbnew import (PLOT_FORMAT_HPGL, PLOT_FORMAT_POST, PLOT_FORMAT_GERBER, PLOT PLOT_FORMAT_PDF, wxPoint) from .optionable import (Optionable, BaseOptions) from .gs import GS -from kibot.macros import macros, document # noqa: F401 +from .macros import macros, document # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_any_layer.py b/kibot/out_any_layer.py index ebfc6ab3..f98eeda2 100644 --- a/kibot/out_any_layer.py +++ b/kibot/out_any_layer.py @@ -12,7 +12,7 @@ from .error import (PlotError, KiPlotConfigurationError) from .optionable import BaseOptions from .layer import Layer from .gs import GS -from kibot.macros import macros, document # noqa: F401 +from .macros import macros, document # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_base.py b/kibot/out_base.py index e57d96bb..35bda345 100644 --- a/kibot/out_base.py +++ b/kibot/out_base.py @@ -4,7 +4,7 @@ # License: GPL-3.0 # Project: KiBot (formerly KiPlot) from .reg_out import RegOutput -from kibot.macros import macros, document # noqa: F401 +from .macros import macros, document # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_bom.py b/kibot/out_bom.py index d44f88fb..0172d871 100644 --- a/kibot/out_bom.py +++ b/kibot/out_bom.py @@ -12,7 +12,7 @@ from re import compile, IGNORECASE from .gs import GS from .optionable import Optionable, BaseOptions from .error import KiPlotConfigurationError -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from .bom.columnlist import ColumnList, BoMError from .bom.bom import do_bom from . import log diff --git a/kibot/out_dxf.py b/kibot/out_dxf.py index b257914f..e40b67f4 100644 --- a/kibot/out_dxf.py +++ b/kibot/out_dxf.py @@ -6,7 +6,7 @@ from pcbnew import (PLOT_FORMAT_DXF, SKETCH, FILLED) from .out_any_layer import AnyLayer from .drill_marks import DrillMarks -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class DXFOptions(DrillMarks): diff --git a/kibot/out_excellon.py b/kibot/out_excellon.py index 95126ce6..27959cd1 100644 --- a/kibot/out_excellon.py +++ b/kibot/out_excellon.py @@ -5,7 +5,7 @@ # Project: KiBot (formerly KiPlot) from pcbnew import EXCELLON_WRITER from .out_any_drill import AnyDrill -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class ExcellonOptions(AnyDrill): diff --git a/kibot/out_gerb_drill.py b/kibot/out_gerb_drill.py index 1588e9a9..d3a62774 100644 --- a/kibot/out_gerb_drill.py +++ b/kibot/out_gerb_drill.py @@ -5,7 +5,7 @@ # Project: KiBot (formerly KiPlot) from pcbnew import GERBER_WRITER from .out_any_drill import AnyDrill -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class Gerb_DrillOptions(AnyDrill): diff --git a/kibot/out_gerber.py b/kibot/out_gerber.py index 73987b3f..ab333e28 100644 --- a/kibot/out_gerber.py +++ b/kibot/out_gerber.py @@ -8,7 +8,7 @@ from pcbnew import (PLOT_FORMAT_GERBER, FromMM, ToMM) from .out_any_layer import (AnyLayer, AnyLayerOptions) from .error import KiPlotConfigurationError -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class GerberOptions(AnyLayerOptions): diff --git a/kibot/out_hpgl.py b/kibot/out_hpgl.py index 42c717b2..11ab5723 100644 --- a/kibot/out_hpgl.py +++ b/kibot/out_hpgl.py @@ -7,7 +7,7 @@ from pcbnew import (PLOT_FORMAT_HPGL, SKETCH, FILLED) from .misc import AUTO_SCALE from .out_any_layer import AnyLayer from .drill_marks import DrillMarks -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class HPGLOptions(DrillMarks): diff --git a/kibot/out_ibom.py b/kibot/out_ibom.py index 7a444421..06437762 100644 --- a/kibot/out_ibom.py +++ b/kibot/out_ibom.py @@ -4,7 +4,7 @@ from .misc import (CMD_IBOM, URL_IBOM, BOM_ERROR) from .gs import (GS) from .kiplot import (check_script) from .optionable import BaseOptions -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_kibom.py b/kibot/out_kibom.py index 5dce49a0..2573785f 100644 --- a/kibot/out_kibom.py +++ b/kibot/out_kibom.py @@ -12,7 +12,7 @@ from .kiplot import (check_script) from .gs import (GS) from .optionable import Optionable, BaseOptions from .error import KiPlotConfigurationError -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_pcbdraw.py b/kibot/out_pcbdraw.py index d1c32b8c..a7b6545e 100644 --- a/kibot/out_pcbdraw.py +++ b/kibot/out_pcbdraw.py @@ -11,7 +11,7 @@ from .misc import (PCBDRAW, PCBDRAW_ERR) from .error import KiPlotConfigurationError from .gs import (GS) from .optionable import (BaseOptions, Optionable) -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_pdf.py b/kibot/out_pdf.py index dd301d40..7fd47eb4 100644 --- a/kibot/out_pdf.py +++ b/kibot/out_pdf.py @@ -8,7 +8,7 @@ from pcbnew import (PLOT_FORMAT_PDF, FromMM, ToMM) from .out_any_layer import AnyLayer from .drill_marks import DrillMarks -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_pdf_pcb_print.py b/kibot/out_pdf_pcb_print.py index 8c45e7ed..7ad7aeb7 100644 --- a/kibot/out_pdf_pcb_print.py +++ b/kibot/out_pdf_pcb_print.py @@ -9,7 +9,7 @@ from .gs import (GS) from .kiplot import check_script, exec_with_retry from .misc import (CMD_PCBNEW_PRINT_LAYERS, URL_PCBNEW_PRINT_LAYERS, PDF_PCB_PRINT) from .optionable import BaseOptions -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from .layer import Layer from . import log diff --git a/kibot/out_pdf_sch_print.py b/kibot/out_pdf_sch_print.py index 6699a2a7..49300aaa 100644 --- a/kibot/out_pdf_sch_print.py +++ b/kibot/out_pdf_sch_print.py @@ -8,7 +8,7 @@ from .gs import (GS) from .kiplot import check_eeschema_do, exec_with_retry from .misc import (CMD_EESCHEMA_DO, PDF_SCH_PRINT) from .optionable import BaseOptions -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_position.py b/kibot/out_position.py index 418b0a7b..5fb87731 100644 --- a/kibot/out_position.py +++ b/kibot/out_position.py @@ -10,7 +10,7 @@ from datetime import datetime from pcbnew import (IU_PER_MM, IU_PER_MILS) from .optionable import BaseOptions from .gs import GS -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class PositionOptions(BaseOptions): diff --git a/kibot/out_ps.py b/kibot/out_ps.py index cda2b955..5caaf8a8 100644 --- a/kibot/out_ps.py +++ b/kibot/out_ps.py @@ -9,7 +9,7 @@ from pcbnew import (PLOT_FORMAT_POST, SKETCH, FILLED, FromMM, ToMM) from .misc import AUTO_SCALE from .out_any_layer import AnyLayer from .drill_marks import DrillMarks -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class PSOptions(DrillMarks): diff --git a/kibot/out_step.py b/kibot/out_step.py index 1df5e718..d259c027 100644 --- a/kibot/out_step.py +++ b/kibot/out_step.py @@ -9,7 +9,7 @@ from .error import KiPlotConfigurationError from .misc import (KICAD2STEP, KICAD2STEP_ERR) from .gs import (GS) from .optionable import BaseOptions -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/out_svg.py b/kibot/out_svg.py index a558f7f2..347c4628 100644 --- a/kibot/out_svg.py +++ b/kibot/out_svg.py @@ -8,7 +8,7 @@ from pcbnew import (PLOT_FORMAT_SVG, FromMM, ToMM) from .out_any_layer import AnyLayer from .drill_marks import DrillMarks -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 class SVGOptions(DrillMarks): diff --git a/kibot/out_svg_sch_print.py b/kibot/out_svg_sch_print.py index d8533f7d..16d5f02d 100644 --- a/kibot/out_svg_sch_print.py +++ b/kibot/out_svg_sch_print.py @@ -9,7 +9,7 @@ from .gs import (GS) from .kiplot import check_eeschema_do, exec_with_retry from .misc import (CMD_EESCHEMA_DO, SVG_SCH_PRINT) from .optionable import BaseOptions -from kibot.macros import macros, document, output_class # noqa: F401 +from .macros import macros, document, output_class # noqa: F401 from . import log logger = log.get_logger(__name__) diff --git a/kibot/pre_check_zone_fills.py b/kibot/pre_check_zone_fills.py index d8ab95df..41b9d969 100644 --- a/kibot/pre_check_zone_fills.py +++ b/kibot/pre_check_zone_fills.py @@ -4,7 +4,7 @@ # License: GPL-3.0 # Project: KiBot (formerly KiPlot) from .error import (KiPlotConfigurationError) -from kibot.macros import macros, pre_class # noqa: F401 +from .macros import macros, pre_class # noqa: F401 @pre_class diff --git a/kibot/pre_drc.py b/kibot/pre_drc.py index 1df5b989..94027d7a 100644 --- a/kibot/pre_drc.py +++ b/kibot/pre_drc.py @@ -4,7 +4,7 @@ # License: GPL-3.0 # Project: KiBot (formerly KiPlot) from sys import (exit) -from kibot.macros import macros, pre_class # noqa: F401 +from .macros import macros, pre_class # noqa: F401 from .error import (KiPlotConfigurationError) from .gs import (GS) from .kiplot import check_script, exec_with_retry diff --git a/kibot/pre_erc.py b/kibot/pre_erc.py index 1afad21d..4c31ab7c 100644 --- a/kibot/pre_erc.py +++ b/kibot/pre_erc.py @@ -4,7 +4,7 @@ # License: GPL-3.0 # Project: KiBot (formerly KiPlot) from sys import (exit) -from kibot.macros import macros, pre_class # noqa: F401 +from .macros import macros, pre_class # noqa: F401 from .gs import (GS) from .kiplot import check_eeschema_do, exec_with_retry from .error import (KiPlotConfigurationError) diff --git a/kibot/pre_filters.py b/kibot/pre_filters.py index 191d710f..2ff6a546 100644 --- a/kibot/pre_filters.py +++ b/kibot/pre_filters.py @@ -8,7 +8,7 @@ import os from .gs import GS from .error import KiPlotConfigurationError from .optionable import Optionable -from kibot.macros import macros, document, pre_class # noqa: F401 +from .macros import macros, document, pre_class # noqa: F401 from .log import get_logger logger = get_logger(__name__) diff --git a/kibot/pre_ignore_unconnected.py b/kibot/pre_ignore_unconnected.py index 0f78f8a3..caaabfd8 100644 --- a/kibot/pre_ignore_unconnected.py +++ b/kibot/pre_ignore_unconnected.py @@ -3,7 +3,7 @@ # Copyright (c) 2020 Instituto Nacional de TecnologĂ­a Industrial # License: GPL-3.0 # Project: KiBot (formerly KiPlot) -from kibot.macros import macros, pre_class # noqa: F401 +from .macros import macros, pre_class # noqa: F401 from .error import (KiPlotConfigurationError) diff --git a/kibot/pre_update_xml.py b/kibot/pre_update_xml.py index a8fbc82e..44112bfe 100644 --- a/kibot/pre_update_xml.py +++ b/kibot/pre_update_xml.py @@ -4,7 +4,7 @@ # License: GPL-3.0 # Project: KiBot (formerly KiPlot) from sys import (exit) -from kibot.macros import macros, pre_class # noqa: F401 +from .macros import macros, pre_class # noqa: F401 from .error import (KiPlotConfigurationError) from .gs import (GS) from .kiplot import check_eeschema_do, exec_with_retry