def bom_plugin_remove_entry(name, re_flags=re.IGNORECASE): '''Remove a BOM plugin entry to the Eeschema configuration file.''' config = read_config_file(PATH_EESCHEMA_CONFIG) bom_plugins_raw = [p for p in config if p.startswith("bom_plugins")] new_list = [] new_list.append(sexpdata.Symbol("plugins")) changes = False if len(bom_plugins_raw) == 1: bom_plugins_raw = after(bom_plugins_raw[0], "bom_plugins=") bom_plugins_raw = de_escape(bom_plugins_raw) bom_list = sexpdata.loads(bom_plugins_raw) if sys.platform.startswith(PLATFORM_WINDOWS_STARTS_WITH): name = name.replace("\\", '/') for plugin in bom_list[1:]: search = plugin[1] if sys.platform.startswith(PLATFORM_WINDOWS_STARTS_WITH): search = plugin[1].replace("\\", '/') if re.findall(name, search, re_flags): changes = True # The name in really in the 'name'. continue # We want to delete this entry. else: for entry in plugin[2:]: if entry[0] == sexpdata.Symbol('opts') and re.findall(r'nickname\s*=\s*'+name, entry[1], re_flags): changes = True continue # The name is in the 'nickname'. new_list.append(plugin) # This plugin remains on the list. if changes: s = sexpdata.dumps(new_list) config = update_config_file(config, "bom_plugins", escape(s)) write_config_file(PATH_EESCHEMA_CONFIG, config)
def remove_bom_plugin_entry(kicad_config_path, name, re_flags=re.IGNORECASE): # Remove a BOM plugin enttry to the Eeschema configuration file. config = read_config_file(os.path.join(kicad_config_path, "eeschema")) bom_plugins_raw = [p for p in config if p.startswith("bom_plugins")] new_list = [] new_list.append(sexpdata.Symbol("plugins")) changes = False if len(bom_plugins_raw) == 1: bom_plugins_raw = after(bom_plugins_raw[0], "bom_plugins=") bom_plugins_raw = de_escape(bom_plugins_raw) bom_list = sexpdata.loads(bom_plugins_raw) for plugin in bom_list[1:]: if re.findall(name, plugin[1], re_flags): changes = True # The name in really in the 'name'. continue # We want to delete this entry. else: for entry in plugin[2:]: if entry[0]==sexpdata.Symbol('opts') and\ re.findall('nickname\s*=\s*'+name, entry[1], re_flags): changes = True continue # The name is in the 'nickname'. new_list.append(plugin) # This plugin remains on the list. if changes: s = sexpdata.dumps(new_list) config = update_config_file(config, "bom_plugins", escape(s)) write_config_file(os.path.join(kicad_config_path, "eeschema"), config)
def fields_add_entry(values_modify, re_flags=re.IGNORECASE): '''Add a list of fields to the Eeschema template.''' if type(values_modify) is not list: values_modify = [values_modify] config = read_config_file(PATH_EESCHEMA_CONFIG) values = [p for p in config if p.startswith("FieldNames")] changes = False if len(values) == 1: values = after(values[0], "FieldNames=") values = de_escape(values) values = sexpdata.loads(values) # TODO validate it, was using an undefined variable `name` if sys.platform.startswith(PLATFORM_WINDOWS_STARTS_WITH): values = values.replace("\\", '/') for idx, value_modify in enumerate(values_modify): value_found = False for idx, value in enumerate(values[1:]): search = value[1] if sys.platform.startswith(PLATFORM_WINDOWS_STARTS_WITH): search = value[1].replace("\\", '/') if re.findall(value_modify, search[1], re_flags): value_found = True if not value_found: values.append([sexpdata.Symbol('field'), [sexpdata.Symbol('name'), value_modify]]) changes = True if changes: s = sexpdata.dumps(values) config = update_config_file(config, "FieldNames", escape(s)) write_config_file(PATH_EESCHEMA_CONFIG, config)
def parse_list(lst): if (lst[0] == sexpdata.Symbol('L')): ret = parse_L(lst) return ret elif (lst[0] == sexpdata.Symbol('M')): ret = parse_M(lst) return ret else: raise errors.BlinkParsingError
def parse_M(lst): if (lst[0] != sexpdata.Symbol('M') or len(lst) < 2): raise errors.BlinkParsingError ret = data.MapValue(lst[1]) lst = lst[2:] while (len(lst) > 0): if (lst[0] == sexpdata.Symbol(':table') and len(lst) >= 2): ret.table = lst[1] lst = lst[2:] else: # skip lst = lst[1:] return ret
def parse_L(lst): if (lst[0] != sexpdata.Symbol('L') or len(lst) < 2): raise errors.BlinkParsingError ret = data.Counter(lst[1]) lst = lst[2:] while (len(lst) > 0): if (lst[0] == sexpdata.Symbol(':init') and len(lst) >= 2): ret.init = lst[1] lst = lst[2:] elif (lst[0] == sexpdata.Symbol(':synth-tool') and len(lst) >= 2): ret.synth_tool = lst[1] lst = lst[2:] elif (lst[0] == sexpdata.Symbol(':device') and len(lst) >= 2): ret.device = lst[1] lst = lst[2:] elif (lst[0] == sexpdata.Symbol(':at') and len(lst) >= 2): ret.at = parse_list(lst[1]) lst = lst[2:] elif (lst[0] == sexpdata.Symbol(':iomap') and len(lst) >= 2): ret.iomap = parse_iomap(lst[1]) lst = lst[2:] elif (lst[0] == sexpdata.Symbol(':period') and len(lst) >= 2): ret.period = parse_period(lst[1]) lst = lst[2:] elif (lst[0] == sexpdata.Symbol(':out') and len(lst) >= 2): ret.port = parse_port(lst[1]) lst = lst[2:] else: lst = lst[1:] return ret
def main(): if len(sys.argv) < 2: print("Usage: kicad_backport.py FILE_NAME") return 1 fn = sys.argv[1] with open(fn, "rt") as f: text = f.read() sexpr = sexpdata.loads(text) is_schematics = False schematics = None if sexpr[0] == sexpdata.Symbol('kicad_symbol_lib'): # KiCad symbol library new format # First element - symbol kicad_symbol_lib # Rest - lists with first symbol is key, rest represent value # version: 20200629 # host: kicad_symbol_editor "version" # symbol PART_NAME body = sexpr[1:] elif sexpr[0] == sexpdata.Symbol('kicad_sch'): # KiCad schematics new format is_schematics = True schematics = Schematics() for entry in sexpr[1:]: e_type = entry[0].value() if e_type == 'lib_symbols': body = entry[1:] else: schematics.parse_entry(entry) else: print("Invalid symbol lib") return 2 library = Library(body) fn_base, _ = os.path.splitext(fn) if is_schematics: fn_lib = fn_base + '-cache.lib' else: fn_lib = fn_base + '.lib' with open(fn_lib, "wt") as f: f.write(library.serialize_lib(is_schematics)) if is_schematics: fn_sch = fn_base + '.sch' with open(fn_sch, "wt") as f: f.write(schematics.serialize_sch()) if not is_schematics: fn_dcm = fn_base + '.dcm' with open(fn_dcm, "wt") as f: f.write(library.serialize_dcm()) return 0
def eval_in_emacs(method_name, args): global epc_client import sexpdata args = [sexpdata.Symbol(method_name)] + list(map(sexpdata.Quoted, args)) # type: ignore sexp = sexpdata.dumps(args) epc_client.call("eval-in-emacs", [sexp]) # type: ignore
def get_emacs_func_result(method_name, args): global epc_client import sexpdata args = [sexpdata.Symbol(method_name)] + list(map(sexpdata.Quoted, args)) # type: ignore sexp = sexpdata.dumps(args) result = epc_client.call_sync("get-emacs-func-result", [sexp]) # type: ignore return result if result != [] else False
def bom_plugin_add_entry(name, cmd, nickname=None, re_flags=re.IGNORECASE, put_first=True, set_default=True): '''Add a BOM plugin entry to the Eeschema configuration file.''' config = read_config_file(PATH_EESCHEMA_CONFIG) bom_plugins_raw = [p for p in config if p.startswith("bom_plugins")] new_list = [] new_list.append(sexpdata.Symbol("plugins")) if len(bom_plugins_raw) == 1: bom_plugins_raw = after(bom_plugins_raw[0], "bom_plugins=") bom_plugins_raw = de_escape(bom_plugins_raw) bom_list = sexpdata.loads(bom_plugins_raw) if sys.platform.startswith(PLATFORM_WINDOWS_STARTS_WITH): name = name.replace("\\", '/') for plugin in bom_list[1:]: search = plugin[1] if sys.platform.startswith(PLATFORM_WINDOWS_STARTS_WITH): search = plugin[1].replace("\\", '/') if re.findall(name, search, re_flags): if not nickname: return # Plugin already added and don't have nickname. for entry in plugin[2:]: if entry[0] == sexpdata.Symbol('opts') and re.findall( r'nickname\s*=\s*' + nickname, entry[1], re_flags): return # Plugin already added with this nickname. new_list.append(plugin) if not nickname: new_list.append([ sexpdata.Symbol('plugin'), sexpdata.Symbol(name), [sexpdata.Symbol('cmd'), cmd] ]) else: new_list.append([ sexpdata.Symbol('plugin'), name, [sexpdata.Symbol('cmd'), cmd], [sexpdata.Symbol('opts'), 'nickname={}'.format(nickname)] ]) if len(new_list): # Put KiCost at first. if put_first: new_list.insert(1, new_list[-1]) del new_list[-1] config = update_config_file(config, "bom_plugins", escape(sexpdata.dumps(new_list))) write_config_file(PATH_EESCHEMA_CONFIG, config) if set_default: import fileinput for line in fileinput.input(PATH_EESCHEMA_CONFIG, inplace=True): if line.strip().startswith('bom_plugin_selected='): line = 'bom_plugin_selected={}\n'.format(nickname) sys.stdout.write(line)
def parse(source): t = sx.loads(source) funcs = {} main = [] for l in t: if l[0] == sx.Symbol("define"): func(funcs, l) else: main.append(translate_expr(l)) return funcs, main
def identify(parsed): # LC = num -> "<num>" if type(parsed) == int: return str(parsed) # LC = id -> "<id>" elif type(parsed) == sexpdata.Symbol: return sexpdata.dumps(parsed) head = parsed[0] # LC = (/ id => LC) -> "(lambda <id> : LC)" if head == sexpdata.Symbol('/'): lc1 = identify(parsed[1]) lc2 = identify(parsed[3]) return "(lambda " + lc1 + ": " + lc2 + ")" # LC = (+ LC LC) -> "(LC1 + LC2)" elif head == sexpdata.Symbol('+'): lc1 = identify(parsed[1]) lc2 = identify(parsed[2]) return "(" + lc1 + " + " + lc2 + ")" # LC = (* LC LC) -> "(LC1 * LC2)" elif head == sexpdata.Symbol('*'): lc1 = identify(parsed[1]) lc2 = identify(parsed[2]) return "(" + lc1 + " * " + lc2 + ")" # LC = (ifleq0 LC LC LC) -> # if (LC1 <= 0): # LC2 # else: # LC3 elif head == sexpdata.Symbol('ifleq0'): cond = identify(parsed[1]) c1 = identify(parsed[2]) c2 = identify(parsed[3]) return "if (" + cond + " <= 0):\n " + c1 + "\nelse:\n " + c2 # LC = (println LC) -> "print(LC)" elif head == sexpdata.Symbol('println'): return "print(" + identify(parsed[1]) + ")" # LC = (LC LC) -> "(LC1(LC2))" else: return "(" + identify(head) + "(" + identify(parsed[1]) + "))"
def from_string(dfa_string): """Constructs an instance of DFA from a string""" data = sexpdata.loads(dfa_string) if data[0] != sexpdata.Symbol('defdfa'): raise ValueError("dfa_string %s is improperly formatted." % (dfa_string)) name = data[1].value() clean_symbols = lambda l: [ d._val if isinstance(d, Symbol) else d for d in l ] alphabet = clean_symbols(data[2][0]) state_list = clean_symbols(data[2][1]) start_state = data[2][2] accept_states = clean_symbols(data[2][3]) transition_list = [clean_symbols(d) for d in data[3]] return DFA(name, alphabet, state_list, start_state, accept_states, transition_list)
def add_bom_plugin_entry(kicad_config_path, name, cmd, nickname=None, re_flags=re.IGNORECASE): # Add a BOM plugin enttry to the Eeschema configuration file. config = read_config_file(os.path.join(kicad_config_path, "eeschema")) bom_plugins_raw = [p for p in config if p.startswith("bom_plugins")] new_list = [] new_list.append(sexpdata.Symbol("plugins")) if len(bom_plugins_raw) == 1: bom_plugins_raw = after(bom_plugins_raw[0], "bom_plugins=") bom_plugins_raw = de_escape(bom_plugins_raw) bom_list = sexpdata.loads(bom_plugins_raw) for plugin in bom_list[1:]: if re.findall(name, plugin[1], re_flags): if not nickname: return # Plugin already added and don't have nickname. for entry in plugin[2:]: print(entry, '<<<', entry[1], '<<<', 'nickname\s*=\s*' + nickname) if entry[0]==sexpdata.Symbol('opts') and\ re.findall('nickname\s*=\s*'+nickname, entry[1], re_flags): return # Plugin already added with this nickname. new_list.append(plugin) if not nickname: new_list.append([ sexpdata.Symbol('plugin'), sexpdata.Symbol(name), [sexpdata.Symbol('cmd'), cmd] ]) else: new_list.append([ sexpdata.Symbol('plugin'), name, [sexpdata.Symbol('cmd'), cmd], [sexpdata.Symbol('opts'), 'nickname={}'.format(nickname)] ]) config = update_config_file(config, "bom_plugins", escape(sexpdata.dumps(new_list))) write_config_file(os.path.join(kicad_config_path, "eeschema"), config)
def add_bom_plugin_entry(kicad_config_path, name, cmd, nickname=None, re_flags=re.IGNORECASE): '''Add a BOM plugin entry to the Eeschema configuration file.''' config = read_config_file(os.path.join(kicad_config_path, "eeschema")) bom_plugins_raw = [p for p in config if p.startswith("bom_plugins")] new_list = [] new_list.append(sexpdata.Symbol("plugins")) if len(bom_plugins_raw)==1: bom_plugins_raw = after(bom_plugins_raw[0], "bom_plugins=") bom_plugins_raw = de_escape(bom_plugins_raw) bom_list = sexpdata.loads(bom_plugins_raw) if sys.platform.startswith(WINDOWS_STARTS_WITH): name = name.replace("\\",'/') for plugin in bom_list[1:]: search = plugin[1] if sys.platform.startswith(WINDOWS_STARTS_WITH): search = plugin[1].replace("\\",'/') if re.findall(name, search, re_flags): if not nickname: return # Plugin already added and don't have nickname. for entry in plugin[2:]: if entry[0]==sexpdata.Symbol('opts') and\ re.findall('nickname\s*=\s*'+nickname, entry[1], re_flags): return # Plugin already added with this nickname. new_list.append(plugin) if not nickname: new_list.append([sexpdata.Symbol('plugin'), sexpdata.Symbol(name), [sexpdata.Symbol('cmd'), cmd]]) else: new_list.append([sexpdata.Symbol('plugin'), name, [sexpdata.Symbol('cmd'), cmd], [sexpdata.Symbol('opts'), 'nickname={}'.format(nickname)]] ) if len(new_list): # Put KiCost at first. new_list.insert(1, new_list[-1]) del new_list[-1] config = update_config_file(config, "bom_plugins", escape( sexpdata.dumps(new_list) )) write_config_file(os.path.join(kicad_config_path, "eeschema"), config)
def is_op(lst, op): return lst[0] == sexpdata.Symbol(op)
def test_nested_list_with_symbol(self): c = [sexpdata.Symbol('a'), 2, [sexpdata.Symbol('b')]] expected = ['a', 2, ['b']] actual = render_sexpdata_symbol_to_string(c) self.assertEqual(expected, actual)
'footprint': next(f).strip().translate(input_filter), 'value': next(f).strip().translate(input_filter) }) if line.startswith('('): n = {'name': next(f).strip(), 'nodes': []} for line in f: if not line.startswith(')'): a = line.strip().split(',') n['nodes'].append({'ref': a[0], 'pin': a[1]}) else: nets.append(n) break parts_sexp = [[ sexpdata.Symbol('comp'), [sexpdata.Symbol('ref'), sexpdata.Symbol(part['ref'])], [sexpdata.Symbol('value'), sexpdata.Symbol(part['value'])], [sexpdata.Symbol('footprint'), sexpdata.Symbol(part['footprint'])]] for part in parts] lib_sexp = [ sexpdata.Symbol('libraries'), [sexpdata.Symbol('library'), [sexpdata.Symbol('logical'), sexpdata.Symbol('Device')], [sexpdata.Symbol('uri'), sexpdata.Symbol('/usr/share/kicad/library/Device.lib'), sexpdata.Symbol('/usr/share/kicad/library/Symbol.lib')]]] signal_sexp = [sexpdata.Symbol('nets')] for i, sig in enumerate(nets, start=1):
def test_symbol(self): c = sexpdata.Symbol('a') expected = 'a' actual = render_sexpdata_symbol_to_string(c) self.assertEqual(expected, actual)