Added support for relative imports to mcpy

This commit is contained in:
SET 2020-08-19 12:29:38 -03:00
parent 1ba778f7f1
commit 28947ed70d
30 changed files with 47 additions and 33 deletions

View File

@ -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__)

View File

@ -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):

View File

@ -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]

View File

@ -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)

View File

@ -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__)

View File

@ -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__)

View File

@ -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__)

View File

@ -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

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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__)

View File

@ -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__)

View File

@ -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__)

View File

@ -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__)

View File

@ -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

View File

@ -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__)

View File

@ -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):

View File

@ -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):

View File

@ -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__)

View File

@ -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):

View File

@ -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__)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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__)

View File

@ -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)

View File

@ -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