Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of monkey patching to match KiCad EDA pretty print #33

Open
FabriceSalvaire opened this issue Oct 23, 2022 · 2 comments
Open

Example of monkey patching to match KiCad EDA pretty print #33

FabriceSalvaire opened this issue Oct 23, 2022 · 2 comments

Comments

@FabriceSalvaire
Copy link

KiCad EDA indents sexp contextually. Thus, the prettyfier requires a Symbol stack to decide whether to break or not.

from sexpdata import loads, dumps, tosexp, Delimiters, Symbol



ROUND_2_SYMBOLS = ('at',)

@tosexp.register(float)
def _(obj, **kwds):
    if kwds['car_stack'][-1] in ROUND_2_SYMBOLS:
        _ = round(obj, 2)
        return f'{_:.2f}'
    return str(obj)



BREAK_OPENER_SYMBOLS = ('symbol', 'property', 'effects', 'pin', 'name', 'number', 'rectangle', 'stroke', 'fill')
BREAK_CLOSER_SYMBOLS = ('kicad_symbol_lib', 'symbol', 'property', 'pin', 'rectangle')
BREAK_PREFIX_SYMBOLS = ('kicad_symbol_lib',)

def dont_break(car_stack, str_car):
    return (str_car == 'effects' and car_stack[-1] in ('name', 'number'))

@tosexp.register(Delimiters)
def _(self, **kwds):
    expr_separator = ' '
    exprs_indent = ''
    break_prefix_opener = ''
    break_prefix_closer = ''
    suffix_break = ''

    car = self.I[0]
    if isinstance(car, Symbol):
        str_car = str(car)
        kwds.setdefault('car_stack', [])
        if str_car in BREAK_OPENER_SYMBOLS and not dont_break(kwds['car_stack'], str_car):
            exprs_indent = '  '
            break_prefix_opener = '\n' + exprs_indent
        if str_car in BREAK_CLOSER_SYMBOLS:
            break_prefix_closer = '\n' + exprs_indent
        kwds['car_stack'].append(str_car)
        if str_car in BREAK_PREFIX_SYMBOLS:
            suffix_break = '\n'

    exprs = expr_separator.join(tosexp(x, **kwds) for x in self.I)
    indented_exprs = '\n'.join(exprs_indent + line.rstrip() for line in exprs.splitlines(True))
    indented_exprs = indented_exprs[len(exprs_indent):]

    if kwds.get('car_stack', None):
        kwds['car_stack'].pop()

    return (
        break_prefix_opener + self.__class__.opener +
        indented_exprs +
        break_prefix_closer + self.__class__.closer + suffix_break
    )
@jd-boyd
Copy link
Owner

jd-boyd commented Oct 23, 2022

I'm not entirely clear on what you're suggesting. Are you suggesting that this should feel the documentation? If so I'd love to see you make a patch for that. Otherwise if you're suggesting an addition to the code, could you make it match for that and do a PR for it? Thank you very much. I do like the idea of prettier kicad stuff.

@FabriceSalvaire
Copy link
Author

Well, I don't know if we can improve the code for such feature. I tried to write clean code, but it is a hack for KiCad conventions...
Maybe we could first add this as an example in the documentation. And see later if someone can merge this as a generic custom prettifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants