Example #1
0
 def import_footprint(self, name):
     s = None
     for f in self.files:
         s = sexpdata.load(open(f, 'r'))
         if _convert_sexp_symbol_to_string(s[1]) == name:
             break
     if s is None:
         raise Exception("Footprint %s not found" % (name))
     return self._import_footprint(s)
def load_execution_time(execution_stats_file):
    with open(execution_stats_file) as f:
        execution_stats_sexp = sexpdata.load(f)
        m = inlining_tree.sexp_to_map(execution_stats_sexp)
        execution_time = inlining_tree.geometric_mean([
                inlining_tree.parse_time(inlining_tree.unpack_atom(x))
                for x in m["raw_execution_time"]
        ])
        return execution_time
Example #3
0
 def import_footprint(self, name):
   s = None
   for f in self.files:
     s = sexpdata.load(open(f, 'r'))
     if _convert_sexp_symbol_to_string(s[1]) == name:
       break
   if s is None:
     raise Exception("Footprint %s not found" % (name))
   return self._import_footprint(s)
Example #4
0
def main (argv=sys.argv):

    with open (argv[1]) as f:
        tree = sexpdata.load (f)

    pads = get_pth_pads (tree)

    vias = get_vias (tree)

    annulus_test (tree, pads, vias)
    stacked_via_test (tree, pads, vias)
Example #5
0
def main():
    rotations = load_rotations()
    if len(sys.argv) <2:
        print("Please supply filename on command line")
        raise Exception("No filename")
    filename = sys.argv[1]
    with open(filename) as f:
        doc = sexpdata.load(f)

    file_dir = os.path.dirname(filename)
    if len(file_dir) > 0:
        os.chdir(os.path.dirname(filename))
    handle_doc(doc, rotations)
Example #6
0
def detect(fn):
  if os.path.isdir(fn) and '.pretty' in fn:
    return "3"
  try:
    l = sexpdata.load(open(fn, 'r'))
    if (l[0] == S('module')): return "3"
    return None
  except IOError:
    # allow new .kicad_mod files!
    if '.kicad_mod' in fn: return "3"
    return None
  except:
    return None
Example #7
0
def detect(fn):
    if os.path.isdir(fn) and '.pretty' in fn:
        return "3"
    try:
        l = sexpdata.load(open(fn, 'r'))
        if (l[0] == S('module')): return "3"
        return None
    except IOError:
        # allow new .kicad_mod files!
        if '.kicad_mod' in fn: return "3"
        return None
    except:
        return None
Example #8
0
 def list_names(self):
     l = []
     for f in self.files:
         s = sexpdata.load(open(f, 'r'))
         name = _convert_sexp_symbol_to_string(s[1])
         fp = self._import_footprint(s)
         desc = None
         for x in fp:
             if x['type'] == 'meta':
                 if 'desc' in x:
                     desc = x['desc']
                     break
         l.append((name, desc))
     return l
Example #9
0
 def list_names(self):
   l = []
   for f in self.files:
     s = sexpdata.load(open(f, 'r'))
     name = _convert_sexp_symbol_to_string(s[1])
     fp = self._import_footprint(s)
     desc = None
     for x in fp:
        if x['type'] == 'meta':
          if 'desc' in x:
            desc = x['desc']
            break
     l.append((name, desc))
   return l
Example #10
0
def parse_solution(src: IO[str]) -> Dict[Definition, int]:
    condition = src.readline().strip()
    if condition != 'sat':
        raise ValueError("Not satisfiable")
    model = sexpdata.load(src)
    model = strip_symbols(model)
    if model[0] != 'model':
        raise ValueError("Top-level element is not a model")
    definitions: Dict[str, int] = {}
    for definition in model[1:]:
        if definition[0] != 'define-fun':
            continue
        name = definition[1]
        value = definition[4]
        definitions[name] = value
    values: Dict[Definition, int] = {
        Definition.from_name(x): y
        for x, y in definitions.items()
    }
    return values
Example #11
0
def parse_kicad_sym(path):
    result = {}
    with io.open(path, 'r', encoding='utf-8') as dcm:
        top = sexpdata.load(dcm)
        assert (top[0] == Symbol('kicad_symbol_lib'))
        for symbol in [s for s in top[1:] if s[0] == Symbol('symbol')]:
            comp = None
            datasheet = ''
            description = ''
            for prop in [p for p in symbol[1:] if p[0] == Symbol('property')]:
                if prop[1] == "Value":
                    comp = prop[2]
                elif prop[1] == "Datasheet":
                    datasheet = prop[2]
                elif prop[1] == "ki_description":
                    description = prop[2]
            if comp is not None:
                result[comp] = (description, '' if datasheet == '' else
                                f'[Datasheet]({datasheet})')
    return result
Example #12
0
def main():
    model = sys.argv[1]
    version = sys.argv[2]
    version = version.lower()

    all_data = []
    for exp in py_common.INITIAL_EXPERIMENTS:
        fname = "./report_plots/reward_assignment/data/%s/%s/feature_reward_pair_%s.sexp" % (
            model, exp, version.lower())
        print "Loading data from", fname
        with open(fname, "r") as f:
            all_data.extend(parse(sexpdata.load(f), exp_name=exp))

    if not os.path.exists("report_plots/machine_learning/%s/%s" %
                          (model, version)):
        os.makedirs("report_plots/machine_learning/%s/%s" % (model, version))

    with open(
            "report_plots/machine_learning/%s/%s/data.pickle" %
        (model, version), "wb") as f:
        pickle.dump(all_data, f)
Example #13
0
# Extract the bookmark data from the DJVU document
retval = 0
retval = retval | os.system(
    "djvused %s -u -e 'print-outline' > %s/bmarks.out" % (args.src, workpath))
print("Bookmarks extracted.")

# Check for zero-length outline
if os.stat("%s/bmarks.out" % workpath).st_size > 0:

    # Extract the metadata from the PDF document
    retval = retval | os.system(
        "pdftk %s dump_data_utf8 > %s/pdfmetadata.out" % (args.dest, workpath))
    print("Original PDF metadata extracted.")

    # Parse the sexpr
    pdfbmarks = walk_bmarks(sexpdata.load(open(workpath + '/bmarks.out')), 0)

    # Integrate the parsed bookmarks into the PDF metadata
    p = re.compile('NumberOfPages: [0-9]+')
    metadata = open("%s/pdfmetadata.out" % workpath, 'r').read()

    for m in p.finditer(metadata):
        loc = int(m.end())

        newoutput = metadata[:loc] + "\n" + pdfbmarks[:-1] + metadata[loc:]

        # Update the PDF metadata
        open("%s/pdfmetadata.in" % workpath, 'w').write(newoutput)
        retval = retval | os.system(
            "pdftk %s update_info_utf8 %s output %s" %
            (args.dest, workpath + '/pdfmetadata.in', finaldest))
Example #14
0
def parse_uniprot_ecs(uniprot_seq_ids_file,
                      raise_on_error=True,
                      logger=logger):
    """ This function parses a metacyc uniprot-seq-ids.dat file to produce a
        data frame which includes (ec,uniprot_id) mappings.

        The uniprot-seq-ids.dat file directly gives the mapping between these
        in lisp S-exp format.

        Args:
            uniprot_seq_ids_file (string): path to the uniprot-seq-ids.dat file

        Returns:
            pd.DataFrame with columns: ec, uniprot_id
    """
    data = sexpdata.load(open(uniprot_seq_ids_file))
    ec_uniprot_list = []

    for entry in data:
        ec = entry[1]
        # we sometimes have lists of size 1 of ECs. It is not clear why this
        # happens, so just parse it out
        if validation_utils.validate_is_sequence(ec, raise_on_invalid=False):
            # if the size is not 1, then this is unexpected
            if len(ec) > 1:
                msg = (
                    "Unexpected sequence when parsing S-exp EC from uniprot_"
                    "seqids_file. {}".format(data))
                if raise_on_error:
                    raise ValueError(msg)
                else:
                    logger.warning(msg)
            ec = ec[0]

        # the EC is sometimes as "Symbol"; this is okay
        if isinstance(ec, sexpdata.Symbol):
            ec = ec.value()

        for uniprot_id in entry[2:]:
            # we occasionally parse out lists of size 1 of symbols
            # it is unclear to me when this happens
            if validation_utils.validate_is_sequence(uniprot_id,
                                                     raise_on_invalid=False):
                # if the size of the list is not 1, then this is unexpected
                if len(uniprot_id) > 1:
                    msg = ("Unexpected sequence when parsing S-exp uniprot_"
                           "seq_ids_file. {}".format(data))
                    if raise_on_error:
                        raise ValueError(msg)
                    else:
                        logger.warning(msg)

                uniprot_id = uniprot_id[0]

            if isinstance(uniprot_id, sexpdata.Symbol):
                uniprot_id = uniprot_id.value()

            ec_uniprot = {"ec": ec, "uniprot_id": uniprot_id}

            ec_uniprot_list.append(ec_uniprot)

    ec_uniprot_df = pd.DataFrame(ec_uniprot_list)

    return ec_uniprot_df
Example #15
0
def load_tree_from_rundir(substep_dir, bin_name, preprocessing, output_dir):
    logging.info("Loading tree from %s" % substep_dir)
    substep_tmp_dir = os.path.join(substep_dir, "tmp")

    if not os.path.exists(substep_tmp_dir):
        os.mkdir(substep_tmp_dir)

    try:
        with open(os.devnull, 'w') as FNULL:
            logging.info("Created tar process for %s" % substep_dir)
            subprocess.call([
                "tar", "xzvf",
                os.path.join(substep_dir, "artifacts.tar"), "-C",
                substep_tmp_dir
            ],
                            stdout=FNULL,
                            stderr=FNULL)

        data_collector_file = os.path.join(
            substep_tmp_dir, bin_name + ".0.data_collector.v1.sexp")
        execution_stats_file = os.path.join(substep_dir,
                                            "execution_stats.sexp")

        if not os.path.exists(data_collector_file):
            logging.info("Dropping %s due to missing data collector file" %
                         substep_dir)
            return None

        if not os.path.exists(execution_stats_file):
            logging.info("Dropping %s due to missing execution stats" %
                         substep_dir)
            return None

        if preprocessing:
            assert isinstance(preprocessing, tuple)

            if preprocessing[0] == "path_patching":
                experiment_subdir = preprocessing[1]
                cleaner_script = os.path.join(
                    os.path.dirname(os.path.realpath(__file__)), "scripts",
                    "clean-with-path-patching")
                proc = subprocess.Popen([
                    cleaner_script, data_collector_file, bin_name,
                    experiment_subdir
                ])
                proc.wait()

                if proc.returncode != 0:
                    raise RuntimeError(
                        "scripts/clean-with-path-patching failed for arguments %s %s %s"
                        % (data_collector_file, bin_name, experiment_subdir))
            else:
                assert False

        tree_file = tempfile.NamedTemporaryFile(delete=True)
        try:
            proc = subprocess.Popen([
                "../_build/default/tools/tree_tools.exe", "v1",
                "decisions-to-tree", data_collector_file, "-expand", "-output",
                tree_file.name
            ])

            proc.wait()
            if proc.returncode != 0:
                raise RuntimeError(
                    "decisions-to-tree failed for arguments %s -expand" %
                    data_collector_file)

            tree = build_tree_from_str(tree_file.read())
            shutil.copy(data_collector_file,
                        os.path.join(output_dir, "decisions.sexp"))
            shutil.copy(tree_file.name,
                        os.path.join(output_dir, "expanded-tree.sexp"))
        finally:
            tree_file.close()

        if tree is None:
            logging.info("Dropping %s because cannot parse sexp correctly" %
                         substep_dir)
            return None

        with open(execution_stats_file) as f:
            execution_stats_sexp = sexpdata.load(f)
            m = sexp_to_map(execution_stats_sexp)
            execution_time = geometric_mean(
                [parse_time(unpack_atom(x)) for x in m["raw_execution_time"]])
        shutil.copy(execution_stats_file,
                    os.path.join(output_dir, "execution_stats.sexp"))

        logging.info("Done with %s" % substep_dir)
    finally:
        logging.info("Removed %s" % substep_tmp_dir)
        if os.path.exists(substep_tmp_dir):
            shutil.rmtree(substep_tmp_dir)
    return (tree, execution_time)
Example #16
0
 def list(self):
     for f in self.files:
         l = sexpdata.load(open(f, 'r'))
         print _convert_sexp_symbol_to_string(l[1])
Example #17
0
def replace_lib(tarlib, deslib):
    rename_ext_lib(tarlib, deslib)
    change_net_lib(tarlib, deslib)


### Main from here ###
# Input file
infile = open("in.edif", "r")

# Output file
outfile = open("out.edif", "w")
logfile = open("log.edif", "w")

# Read a line from infile
data = sexpdata.load(infile)
infile.close()

# List Data
list_ext(0)
logfile.write('All EXT Instance:\n')
logfile.write(str(extinslist))
logfile.write('\nConverting begin\n')

# Replace from map.edif file
# map.edif:
# <tarlib> <tarinst> <deslib> <desinste>\n
#
# map.edif exp:
# hb180enh_digitalcell_S sizeB OD0003_sch_common sizeB
# hb180enh nch5_iso_nbl 1830bd15ba nch_svt_iso_nbl_5p0v
Example #18
0
 def __init__(self, fileobj):
     self.sexpr = sexpdata.load(fileobj)
     self.version = None
     self.host = None
     self.host_version = None
     self.page = None
     self.general = {}
     self.setup = {}
     self.nets = {}
     self.layers = {}
     self.pcbplotparams = {}
     if self.sexpr[0].value() != 'kicad_pcb':
         raise ValueError("Not a kicad pcb file")
         
     def parsedict(dictout, node, subtrees = {}):
         for g in node[1:]:
             name = g[0].value()
             if name in subtrees:
                 parsedict(subtrees[name], g)
             elif len(g) == 2:
                 dictout[g[0].value()] = g[1]
             else:
                 dictout[g[0].value()] = g[1:]
         
     for e in self.sexpr[1:]:
         sym = e[0].value()
         if sym == 'version':
             self.version = e[1]
         elif sym == 'host':
             self.host = e[1].value()
             self.host_version = e[2]
         elif sym == 'page':
             self.page = e[1].value()
         elif sym == 'general':
             parsedict(self.general, e)
         elif sym == 'setup':
             parsedict(self.setup, e, {'pcbplotparams' : self.pcbplotparams})
         elif sym == 'net':
             self.nets[int(e[1])] = sym2str(e[2])
         elif sym == 'module':
             x, y, angle = None, None, 0
             for si in e[2:]:
                 if type(si) is sexpdata.Symbol:
                     continue
                 sym = si[0].value()
                 if sym == 'at':
                     x, y = float(si[1]), float(si[2])
                     if len(si) > 3:
                         angle = si[3]
                 elif sym == 'pad':
                     pad_type = sym2str(si[2])
                     pad_shape = sym2str(si[3])
                     padx, pady = None, None
                     padw, padh = None, None
                     padangle = angle
                     layers = None
                     net = 0
                     drillx = 0
                     drilly = 0
                     for pat in si[4:]:
                         sym = pat[0].value()
                         if sym == 'at':
                             paddx, paddy = rotdeg((float(pat[1]), float(pat[2])), angle)
                             padx, pady = float(x + paddx), float(y + paddy)
                             if len(pat) > 3:
                                 padangle = pat[3]
                         elif sym == 'size':
                             padw, padh = float(pat[1]), float(pat[2])
                         elif sym == 'drill' and len(pat) > 1:
                             if type(pat[1]) in [float, int]:
                                 drillx, drilly = float(pat[1]), float(pat[1])
                             elif sym2str(pat[1]) == 'oval':
                                 drillx = float(pat[2])
                                 if len(pat) == 3:
                                     drilly = drillx
                                 else:
                                     drilly = float(pat[3])
                         elif sym == 'layers':
                             layers = map(sym2str, pat[1:])
                         elif sym == 'net':
                             net = pat[1]
                     if padangle in [90, 270]:
                         padw, padh = padh, padw
                         drillx, drilly = drilly, drillx
                     self.add_pad(PCBPad(padx, pady, padw, padh, pad_shape, pad_type, layers, net, drillx, drilly))
         elif sym == 'zone':
             net = None
             layer = None
             for si in e[1:]:
                 sym = si[0].value()
                 if sym == 'net':
                     net = si[1]
                 elif sym == 'layer':
                     layer = sym2str(si[1])
                 elif sym == 'filled_polygon':
                     pts = []
                     for xy in si[1][1:]:
                         if sym2str(xy[0]) != 'xy':
                             raise ValueError, "Invalid item inside filled_polygon"
                         pts.append((float(xy[1]), float(xy[2])))
                     lp = self.get_layer(layer).polygons
                     if net not in lp:
                         lp[net] = [pts]
                     else:
                         lp[net].append(pts)
         elif sym == 'segment':
             start = None
             end = None
             width = None
             net = None
             for si in e[1:]:
                 sym = si[0].value()
                 if sym == 'start':
                     start = (si[1], si[2])
                 if sym == 'end':
                     end = (si[1], si[2])
                 if sym == 'width':
                     width = si[1]
                 if sym == 'layer':
                     layer = sym2str(si[1])
                 if sym == 'net':
                     net = si[1]
             self.get_layer(layer).segments.append(Segment(start = start, end = end, width = width, layer = layer, net = net))
         elif sym == 'gr_line':
             start = None
             end = None
             width = None
             for si in e[1:]:
                 sym = si[0].value()
                 if sym == 'start':
                     start = (si[1], si[2])
                 if sym == 'end':
                     end = (si[1], si[2])
                 if sym == 'width':
                     width = si[1]
                 if sym == 'layer':
                     layer = sym2str(si[1])
             self.get_layer(layer).gr_lines.append(GraphicLine(start = start, end = end, width = width, layer = layer))
Example #19
0
def parse(p):
    try:
        _, x, y, deg = p
        return (translate([x, -y, 0.0])(rotate(deg, [0, 0, 1])(hole)))
    except Exception:
        _, x, y = p
        return (translate([x, -y, 0.0])(hole))


def parse_hole(p):
    _, x, y = p
    return (translate([x, -y, 0.0])(circle(1.1, segments=30)))


f = load(open("./right.kicad_pcb"))
y = list(
    filter(
        lambda x: x[0] == Symbol("module")
        if isinstance(x, list) else False, f))
z = list(filter(lambda x: x[1] == Symbol("base:ToprePad"), y))
sws = list(
    map(
        lambda y: list(
            filter(
                lambda x: x[0] == Symbol("at")
                if isinstance(x, list) else False, y))[0], z))
z = list(
    filter(lambda x: x[1] == Symbol("MountingHole:MountingHole_2.2mm_M2"), y))
holes = list(
    map(
Example #20
0
    return ret


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Compare KiCad netlists')
    parser.add_argument('first_netlist')
    parser.add_argument('second_netlist')
    parser.add_argument('--quiet', action='store_true')

    args = parser.parse_args()

    fn_a = args.first_netlist
    fn_b = args.second_netlist

    with open(fn_a, 'r') as f:
        a = sexpdata.load(f)

    with open(fn_b, 'r') as f:
        b = sexpdata.load(f)

    # Extract the nets portion
    a = extract_nets(a)
    b = extract_nets(b)

    if a is None:
        if not args.quiet:
            print("Could not read nets from {}".format(fn_a))
        sys.exit(-1)

    if b is None:
        if not args.quiet:
Example #21
0
# Extract the bookmark data from the DJVU document 
# (scratch)
retval = 0
retval = retval | os.system("djvused %s -u -e 'print-outline' > %s/bmarks.out" % (args.src, tmp))
print "Bookmarks extracted."

# Check for zero-length outline
if os.stat("%s/bmarks.out" % tmp).st_size > 0:

    # Extract the metadata from the PDF document
    retval = retval | os.system("pdftk %s dump_data_utf8 > %s/pdfmetadata.out" % (args.dest, tmp))
    print "Original PDF metadata extracted."

    # Parse the sexpr
    pdfbmarks = walk_bmarks(sexpdata.load(open(tmp + '/bmarks.out')), 0)

    # Integrate the parsed bookmarks into the PDF metadata
    p = re.compile('NumberOfPages: [0-9]+')
    metadata = open("%s/pdfmetadata.out" % tmp, 'r').read()

    for m in p.finditer(metadata):
        loc = int(m.end())

        newoutput = metadata[:loc] + "\n" + pdfbmarks[:-1] + metadata[loc:]

        # Update the PDF metadata
        open("%s/pdfmetadata.in" % tmp, 'w').write(newoutput)
        retval = retval | os.system("pdftk %s update_info_utf8 %s output %s" % (args.dest, tmp + '/pdfmetadata.in', finaldest))

else:
argParser.add_argument('-i',
                       '--input-file',
                       dest='input_file',
                       default="netlists/c17.edif",
                       help="EDIF input file")
argParser.add_argument('-o',
                       '--output-file',
                       dest='output_file',
                       default="STDOUT",
                       help="POETS graph XML file")

args = argParser.parse_args()

with open(args.input_file, 'r') as edif:
    # true=None, prevent t being intepreted as True
    e = sexpdata.load(edif, true=None)

#s = sexpdata.car(e)
#print(type(s))
#print(s.__dict__)
#print(inspect.getmembers(s, predicate=inspect.ismethod))

AVAILABLE_CELLS = list(gateMappings.keys())
# dictionary for arrays in EDIF
arrays = {}


def get_op(lst):
    return lst[0].value()

Example #23
0
 def load(f, domain):
     data = utils.desymbolize(sexpdata.load(f))
     return Problem.deserialize(data, domain)
Example #24
0
 def list(self):
   for f in self.files:
     l = sexpdata.load(open(f, 'r'))
     print _convert_sexp_symbol_to_string(l[1])
Example #25
0
#!/usr/bin/env python3
import sexpdata
import sys


def walk_bmarks(bmarks, level=0):
    output = ''
    wroteTitle = False
    for j in bmarks:
        if isinstance(j, list):
            output = output + walk_bmarks(j, level + 1)
        elif isinstance(j, str):
            if not wroteTitle:
                output = output + "BookmarkBegin\nBookmarkTitle: %s\nBookmarkLevel: %d\n" % (
                    j, level)
                wroteTitle = True
            else:
                output = output + "BookmarkPageNumber: %s\n" % j.split('#')[1]
                wroteTitle = False
        else:
            pass
    return output


fn = sys.argv[1]
data = sexpdata.load(open(fn))
pdfbmarks = walk_bmarks(data)

print(pdfbmarks)
def load_lc(filename):
    """ Load a lambda calculus file into sexpdata """
    with open(filename, 'r') as f:
        return load(f)
Example #27
0
 def load(f):
     data = utils.desymbolize(sexpdata.load(f))
     return Domain.deserialize(data)
Example #28
0
def load(s, **kwargs):
    return _dictify(sexpdata.load(s, **kwargs))
Example #29
0
from cooltools import memcached
from cooltools import with_multi_args

from time import strftime, gmtime

import smoked_rpc

proxy_app = Sanic()

import config

# lisp magic
# literally - all software is magic, because it causes change in reality in accordance with will
data_file = open('data.lisp')
data_model = sexpdata.load(data_file)
data_file.close()

# TODO - switch from rpc method to some more generic method for data source


def gen_entity_multi_handler(entity_dict):
    defargs = []
    has_count = False
    rpc_method_name = car(entity_dict['rpc_method']).value()
    for arg in car(cdr(entity_dict['rpc_method'])):
        if type(arg) is sexpdata.Symbol:
            if arg.value() == '!count':
                has_count = True
            else:
                defargs.append(arg.value())