Пример #1
0
def mask_id64_as_system(input):
    result = input
    if util.is_str(input):
        result = int(result, 16)
    result &= (2**55) - 1
    if util.is_str(input):
        result = '{0:016X}'.format(result)
    return result
Пример #2
0
def mask_id64_as_body(input):
    result = input
    if util.is_str(input):
        result = int(result, 16)
    result >>= 55
    result &= (2**9) - 1
    if util.is_str(input):
        result = '{0:X}'.format(result)
    return result
Пример #3
0
def _get_sector_class(sect):
    if util.is_str(sect) and sect.lower() in pgdata.ha_sectors:
        return "ha"
    frags = get_sector_fragments(sect) if util.is_str(sect) else sect
    if frags is not None and len(frags) == 4 and frags[
            0] in pgdata.cx_prefixes and frags[2] in pgdata.cx_prefixes:
        return 2
    elif frags is not None and len(frags) in [
            3, 4
    ] and frags[0] in pgdata.cx_prefixes:
        return 1
    else:
        return None
Пример #4
0
def get_mcode(input):
  if util.is_str(input) and len(input) == 1:
    return input.lower()
  elif isinstance(input, numbers.Number) and (int(input) % 10) == 0:
    return chr(int(math.log(input / 10, 2)) + ord('a'))
  else:
    raise ValueError("First argument to get_mcode must be string or numeric and multiple of 10")
Пример #5
0
 def _get_as_filters(self, s):
     if s is None:
         return None
     elif util.is_str(s):
         return self.parse_filter_string(s)
     else:
         return s
Пример #6
0
def get_mcode_cube_width(mcode):
  if util.is_str(mcode):
    return sector_size / pow(2, ord('h') - ord(mcode.lower()))
  elif isinstance(mcode, numbers.Number):
    return mcode
  else:
    raise ValueError("First argument to get_mcode_cube_width must be string or numeric")
Пример #7
0
def calculate_from_id64(input):
    # If input is a string, assume hex
    if util.is_str(input):
        input = int(input, 16)
    # Calculate the shifts we need to do to get the individual fields out
    # Can't tell how long N2 field is (or if the start moves!), assuming ~16 for now
    len_used = 0
    input, mc = util.unpack_and_shift(input, 3)
    len_used += 3  # mc = 0-7 for a-h
    input, boxel_z = util.unpack_and_shift(input, 7 - mc)
    len_used += 7 - mc
    input, sector_z = util.unpack_and_shift(input, 7)
    len_used += 7
    input, boxel_y = util.unpack_and_shift(input, 7 - mc)
    len_used += 7 - mc
    input, sector_y = util.unpack_and_shift(input, 6)
    len_used += 6
    input, boxel_x = util.unpack_and_shift(input, 7 - mc)
    len_used += 7 - mc
    input, sector_x = util.unpack_and_shift(input, 7)
    len_used += 7
    input, n2 = util.unpack_and_shift(input, 55 - len_used)
    input, body_id = util.unpack_and_shift(input, 9)
    # Multiply each X/Y/Z value by the cube width to get actual coords
    boxel_size = 10 * (2**mc)
    coord_x = (sector_x * sector.sector_size) + (boxel_x *
                                                 boxel_size) + (boxel_size / 2)
    coord_y = (sector_y * sector.sector_size) + (boxel_y *
                                                 boxel_size) + (boxel_size / 2)
    coord_z = (sector_z * sector.sector_size) + (boxel_z *
                                                 boxel_size) + (boxel_size / 2)
    coords_internal = vector3.Vector3(coord_x, coord_y, coord_z)
    # Shift the coords to be the origin we know and love
    coords = coords_internal + sector.internal_origin_offset
    return (coords, boxel_size, n2, body_id)
Пример #8
0
def format_sector_name(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return None
    if len(frags) == 4 and frags[2] in pgdata.cx_prefixes:
        return "{0}{1} {2}{3}".format(*frags)
    else:
        return "".join(frags)
Пример #9
0
    def __init__(self, Words, DocIndex=dict(), Results=[], SubSentenceMultiplier=100, WordMultiplier=100, Prg=dict(), WordsDetected=set(), FileSourceBaseName=""):

        if util.is_str(Words): Words = [Words]

        self.Words = Words   # ["apple"]  name is a list with used keywords+operators
                             # if you have more than one elem in Words, you get it after an operator exec

        self.Prg = Prg
        self.WordsDetected = WordsDetected
        self.DocIndex = DocIndex
        self.FileSourceBaseName = FileSourceBaseName
        self.SubSentenceMultiplier = SubSentenceMultiplier
        self.WordMultiplier = WordMultiplier

        self.IsOperator = False
        self.IsKeyword = False
        self.IsGroup = False

        self.Results = [] # list, because set can't differentiate objects with wame values
        self.Explain = []
        self.OperatorName = ""

        if len(Words) == 1:
            Word = Words[0]

            if is_operator(Word):
                self.IsOperator = True
                self.OperatorName = Word

            elif util.is_str(Word):
                # the word can be written with uppercase/lowercase too,
                # in the db I use lowercase version
                self.IsKeyword = True
                self.load_from_docindex([Word.lower()])

                if not self.Results:
                    self.group_words_collect(Word)

        elif len(Words) > 1:
            self.IsGroup = True
            if Results:
                self.Results = Results
Пример #10
0
 def get_system_by_id64(self, id64, keep_data=False):
     if util.is_str(id64):
         id64 = int(id64, 16)
     coords, cube_width, n2, _ = system.calculate_from_id64(id64)
     # Get a system prototype to steal its name
     sys_proto = pgnames.get_system(coords, cube_width)
     pname = sys_proto.name + str(n2)
     result = self._backend.get_system_by_id64(id64, fallback_name=pname)
     if result is not None:
         return _make_known_system(result, keep_data)
     else:
         return None
Пример #11
0
def _get_sector_from_name(sector_name, allow_ha=True):
    sector_name = get_canonical_name(sector_name, sector_only=True)
    if sector_name is None:
        return None
    if allow_ha and util.is_str(
            sector_name) and sector_name.lower() in pgdata.ha_sectors:
        return pgdata.ha_sectors[sector_name.lower()]
    else:
        frags = get_sector_fragments(sector_name) if util.is_str(
            sector_name) else sector_name
        if frags is not None:
            sc = _get_sector_class(frags)
            if sc == 2:
                # Class 2
                return _c2_get_sector(frags)
            elif sc == 1:
                # Class 1
                return _c1_get_sector(frags)
            else:
                return None
        else:
            return None
Пример #12
0
def _c2_get_sector(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return None
    offset = _c2_get_offset_from_name(frags)
    if offset is None:
        return None

    # Calculate the X/Y/Z positions from the offset
    spos = _get_sector_pos_from_offset(offset, sector.galaxy_size)
    name = format_sector_name(frags)
    return sector.PGSector(spos[0], spos[1], spos[2], name,
                           _get_sector_class(frags))
Пример #13
0
 def TxtToObj(TokenTxtList, Index,
              FileSourceBaseName):  # recursive txt-> TokenObj converter
     TokenObjects = []
     for Elem in TokenTxtList:
         if util.is_str(Elem):
             TokenObjects.append(
                 tokens.TokenObj(Elem,
                                 Index,
                                 Prg=Prg,
                                 WordsDetected=WordsDetected,
                                 FileSourceBaseName=FileSourceBaseName))
         if util.is_list(Elem):
             TokenObjects.append(TxtToObj(Elem, Index, FileSourceBaseName))
     return TokenObjects
Пример #14
0
def _c1_get_infixes(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return None
    if frags[-1] in pgdata.cx_prefixes:
        if frags[-1] in pgdata.c1_prefix_infix_override_map:
            return pgdata.c1_infixes[pgdata.c1_prefix_infix_override_map[
                frags[-1]]]
        else:
            return pgdata.c1_infixes[1]
    elif frags[-1] in pgdata.c1_infixes[1]:
        return pgdata.c1_infixes[2]
    elif frags[-1] in pgdata.c1_infixes[2]:
        return pgdata.c1_infixes[1]
    else:
        return None
Пример #15
0
def from_id64(id, allow_ha=True, allow_known=True):
    if util.is_str(id):
        id = int(id, 16)
    if allow_known:
        with env.use() as data:
            ks = data.get_system_by_id64(mask_id64_as_system(id))
            if ks is not None:
                return ks
    coords, cube_width, n2, _ = calculate_from_id64(id)
    # Get a system prototype to steal its name
    sys_proto = pgnames.get_system(coords, cube_width, allow_ha)
    name = sys_proto.name + str(n2)
    x, y, z = sys_proto.position
    return PGSystem(x,
                    y,
                    z,
                    name,
                    sector=sys_proto.sector,
                    uncertainty=cube_width / 2.0)
Пример #16
0
def _c2_get_offset_from_name(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return

    try:
        # Get the current indexes within prefix runs (3037)
        cur_idx0 = _prefix_offsets[frags[0]][0] + _get_suffixes(
            frags[0]).index(frags[1])
        cur_idx1 = _prefix_offsets[frags[2]][0] + _get_suffixes(
            frags[2]).index(frags[3])
    except:
        # Either the prefix or suffix lookup failed, likely a dodgy name
        log.warning(
            "Failed to look up prefixes/suffixes in _c2_get_offset_from_name; bad sector name?"
        )
        return None

    # Interleave the individual offsets into one big offset
    return util.interleave(cur_idx0, cur_idx1,
                           32)  # Again, length is anyone's guess
Пример #17
0
def _get_suffixes(input, get_all=False):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return None
    wordstart = frags[0]
    if frags[-1] in pgdata.cx_prefixes:
        # Append suffix straight onto a prefix (probably C2)
        suffix_map_idx = pgdata.c2_prefix_suffix_override_map.get(frags[-1], 1)
        result = pgdata.c2_suffixes[suffix_map_idx]
        wordstart = frags[-1]
    else:
        # Likely C1
        if frags[-1] in pgdata.c1_infixes[2]:
            # Last infix is consonant-ish, return the vowel-ish suffix list
            result = pgdata.c1_suffixes[1]
        else:
            result = pgdata.c1_suffixes[2]

    if get_all:
        return result
    else:
        return result[0:_get_prefix_run_length(wordstart)]
Пример #18
0
def is_valid_sector_name(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None or len(frags) == 0 or frags[0] not in pgdata.cx_prefixes:
        return False
    if len(frags) == 4 and frags[2] in pgdata.cx_prefixes:
        # Class 2
        f1idx = pgdata.c2_prefix_suffix_override_map.get(frags[0], 1)
        f3idx = pgdata.c2_prefix_suffix_override_map.get(frags[2], 1)
        return (frags[1] in pgdata.c2_suffixes[f1idx]
                and frags[3] in pgdata.c2_suffixes[f3idx])
    elif len(frags) in [3, 4]:
        # Class 1
        fli_idx = pgdata.c1_prefix_infix_override_map.get(frags[0], 1)
        if frags[1] not in pgdata.c1_infixes[fli_idx]:
            return False
        if len(frags) == 4:
            fli_idx = 2 if fli_idx == 1 else 1
            if frags[2] not in pgdata.c1_infixes[fli_idx]:
                return False
        flastidx = 2 if fli_idx == 1 else 1
        return (frags[-1] in pgdata.c1_suffixes[flastidx])
    else:
        # Class NOPE
        return False
Пример #19
0
def trans_e(mtd, e):
  curried = partial(trans_e, mtd)
  buf = cStringIO.StringIO()
  if e.kind == C.E.ANNO:
    anno = e.anno
    if anno.name == C.A.NEW: pass # TODO

    elif anno.name == C.A.OBJ:
      buf.write("retrieve_{}@log({})".format(util.sanitize_ty(anno.typ), anno.idx))

    # @Compare(exps) => {| exps[0] (< | <= | == | != | >= | >) exps[1] |}
    # @CompareString(exps) => exps[0].eqauls(exps[1])
    elif anno.name in [C.A.CMP, C.A.CMP_STR]:
      le = curried(anno.exps[0])
      re = curried(anno.exps[1])
      if anno.name == C.A.CMP:
        buf.write("{| " + le + " (< | <= | == | != | >= | >) " + re + " |}")
      else:
        buf.write("{}({},{})".format(trans_mname(C.J.STR, u"equals"), le, re))

  elif e.kind == C.E.GEN:
    if e.es:
      buf.write("{| ")
      buf.write(" | ".join(map(curried, e.es)))
      buf.write(" |}")
    else:
      buf.write(C.T.HOLE)

  elif e.kind == C.E.ID:
    if hasattr(e, "ty"): buf.write(trans_ty(e.ty) + ' ')
    fld = None
    if mtd and e.id not in mtd.param_vars:
      fld = find_fld(mtd.clazz.name, e.id)
    if fld: # fname -> self.new_fname (unless the field is static)
      new_fname = trans_fname(fld.clazz.name, e.id, fld.is_static)
      if fld.is_static:
        # access to the static field inside the same class
        if fld.clazz.name == mtd.clazz.name: buf.write(e.id)
        # o.w., e.g., static constant in an interface, call the accessor
        else: buf.write(new_fname + "()")
      else: buf.write('.'.join([C.SK.self, new_fname]))
    elif e.id in [C.J.THIS, C.J.SUP]: buf.write(C.SK.self)
    elif util.is_str(e.id): # constant string, such as "Hello, World"
      str_init = trans_mname(C.J.STR, C.J.STR, [u"char[]", C.J.i, C.J.i])
      s_hash = hash(e.id) % 256 # hash string value itself
      buf.write("{}(new Object(hash={}), {}, 0, {})".format(str_init, s_hash, e.id, len(e.id)))
    else: buf.write(e.id)

  elif e.kind == C.E.UOP:
    buf.write(' '.join([e.op, curried(e.e)]))

  elif e.kind == C.E.BOP:
    buf.write(' '.join([curried(e.le), e.op, curried(e.re)]))

  elif e.kind == C.E.DOT:
    # with package names, e.g., javax.swing.SwingUtilities
    if util.is_class_name(e.re.id) and class_lookup(e.re.id):
      buf.write(curried(e.re))
    elif e.re.id == C.J.THIS: # ClassName.this
      buf.write(C.SK.self)
    else:
      rcv_ty = typ_of_e(mtd, e.le)
      fld = find_fld(rcv_ty, e.re.id)
      new_fname = trans_fname(rcv_ty, e.re.id, fld.is_static)
      if fld.is_static:
        # access to the static field inside the same class
        if mtd and rcv_ty == mtd.clazz.name: buf.write(e.re.id)
        # o.w., e.g., static constant in an interface, call the accessor
        else: buf.write(new_fname + "()")
      else: buf.write('.'.join([curried(e.le), new_fname]))

  elif e.kind == C.E.IDX:
    buf.write(curried(e.e) + '[' + curried(e.idx) + ']')

  elif e.kind == C.E.NEW:
    if e.e.kind == C.E.CALL:
      ty = typ_of_e(mtd, e.e.f)
      cls = class_lookup(ty)
      if cls and cls.has_init:
        arg_typs = map(partial(typ_of_e, mtd), e.e.a)
        mname = trans_mname(cls.name, cls.name, arg_typs)
        obj = "alloc@log({})".format(cls.id)
        args = [obj] + map(unicode, map(curried, e.e.a))
        buf.write("{}({})".format(mname, ", ".join(args)))
      else: # collection or Object
        buf.write(C.J.NEW + ' ' + trans_ty(ty) + "()")
    else: # o.w., array initialization, e.g., new int[] { ... }
      buf.write(str(e.init))

  elif e.kind == C.E.CALL:
    arg_typs = map(partial(typ_of_e, mtd), e.a)

    def trans_call(callee, rcv_ty, rcv):
      if callee.is_static: rcv = None
      logging = None
      if not util.is_collection(callee.clazz.name):
        logging = str(check_logging(mtd, callee)).lower()

      args = util.rm_none([rcv] + map(curried, e.a) + [logging])
      mid = trans_mname(rcv_ty, callee.name, arg_typs)
      return u"{}({})".format(mid, ", ".join(args))

    def dynamic_dispatch(rcv_ty, rcv, acc, callee):
      _dispatched = trans_call(callee, callee.clazz.name, rcv)
      _guarded = "{}.__cid == {} ? {}".format(rcv, callee.clazz.id, _dispatched)
      return "({} : {})".format(_guarded, acc)

    if e.f.kind == C.E.DOT: # rcv.mid
      rcv_ty = typ_of_e(mtd, e.f.le)
      rcv = curried(e.f.le)
      mname = e.f.re.id
      mtd_callees = find_mtds_by_sig(rcv_ty, mname, arg_typs)
      if mtd_callees and 1 < len(mtd_callees): # needs dynamic dispatch
        curried_dispatch = partial(dynamic_dispatch, rcv_ty, rcv)
        # TODO: use least upper bound?
        default_v = util.default_value(mtd_callees[0].typ)
        buf.write(reduce(curried_dispatch, mtd_callees, default_v))
      elif mtd_callees and 1 == len(mtd_callees):
        mtd_callee = mtd_callees[0]
        buf.write(trans_call(mtd_callee, rcv_ty, rcv))
      else: # unresolved, maybe library method
        mid = trans_mname(rcv_ty, mname, arg_typs)
        args = util.rm_none([rcv] + map(curried, e.a))
        buf.write("{}({})".format(mid, ", ".join(args)))

    else: # mid
      mname = e.f.id
      # pre-defined meta information or Sketch primitive functions
      if mname in C.typ_arrays + [u"minimize"]:
        mid = mname
        rcv = None
        args = util.rm_none([rcv] + map(curried, e.a))
        buf.write("{}({})".format(mid, ", ".join(args)))
      elif mname == C.J.SUP and mtd.is_init: # super(...) inside <init>
        sup = class_lookup(mtd.clazz.sup)
        mid = trans_mname(sup.name, sup.name, arg_typs)
        rcv = C.SK.self
        args = util.rm_none([rcv] + map(curried, e.a))
        buf.write("{}({})".format(mid, ", ".join(args)))
      else: # member methods
        mtd_callees = find_mtds_by_sig(mtd.clazz.name, mname, arg_typs)
        if mtd_callees and 1 < len(mtd_callees): # needs dynamic dispatch
          curried_dispatch = partial(dynamic_dispatch, mtd.clazz.name, C.SK.self)
          # TODO: use least upper bound?
          default_v = util.default_value(mtd_callees[0].typ)
          buf.write(reduce(curried_dispatch, mtd_callees, default_v))
        elif mtd_callees and 1 == len(mtd_callees):
          mtd_callee = mtd_callees[0]
          buf.write(trans_call(mtd_callee, mtd.clazz.name, C.SK.self))
        else: # unresolved, maybe library method
          mid = trans_mname(mtd.clazz.name, mname, arg_typs)
          args = util.rm_none([rcv] + map(curried, e.a))
          buf.write("{}({})".format(mid, ", ".join(args)))

  elif e.kind == C.E.CAST:
    # since a family of classes is merged, simply ignore the casting
    buf.write(curried(e.e))

  elif e.kind == C.E.INS_OF:
    ty = typ_of_e(mtd, e.ty)
    cls = class_lookup(ty)
    if cls:
      buf.write(curried(e.e) + ".__cid == " + str(cls.id))
    else:
      logging.debug("unknown type: {}".format(ty))
      buf.write("0")

  else: buf.write(str(e))
  return buf.getvalue()
Пример #20
0
def trans_e(mtd, e):
  curried = partial(trans_e, mtd)
  buf = cStringIO.StringIO()

  if e.kind == C.E.GEN:
    if e.es:
      buf.write("{| ")
      buf.write(" | ".join(map(curried, e.es)))
      buf.write(" |}")
    else:
      buf.write(C.T.HOLE)

  elif e.kind == C.E.ID:
    if hasattr(e, "ty"): buf.write(trans_ty(e.ty) + ' ')
    fld = None
    if mtd and e.id not in mtd.param_vars:
      fld = find_fld(mtd.clazz.name, e.id)
    if fld: # fname -> self.new_fname (unless the field is static)
      new_fname = trans_fname(fld.clazz.name, e.id, fld.is_static)
      if fld.is_static:
        # access to the static field inside the same class
        if fld.clazz.name == mtd.clazz.name: buf.write(e.id)
        # o.w., e.g., static constant in an interface, call the accessor
        else: buf.write(new_fname + "()")
      else: buf.write('.'.join([C.SK.self, new_fname]))
    elif e.id in [C.J.THIS, C.J.SUP]: buf.write(C.SK.self)
    elif util.is_str(e.id): # constant string, such as "Hello, World"
      str_init = trans_mname(C.J.STR, C.J.STR, [u"char[]", C.J.i, C.J.i])
      s_hash = hash(e.id) % 256 # hash string value itself
      buf.write("{}(new Object(hash={}), {}, 0, {})".format(str_init, s_hash, e.id, len(e.id)))
    else: buf.write(e.id)

  elif e.kind == C.E.UOP:
    buf.write(' '.join([e.op, curried(e.e)]))

  elif e.kind == C.E.BOP:
    buf.write(' '.join([curried(e.le), e.op, curried(e.re)]))

  elif e.kind == C.E.DOT:
    # with package names, e.g., javax.swing.SwingUtilities
    if util.is_class_name(e.re.id) and class_lookup(e.re.id):
      buf.write(curried(e.re))
    elif e.re.id == C.J.THIS: # ClassName.this
      buf.write(C.SK.self)
    else:
      rcv_ty = typ_of_e(mtd, e.le)
      fld = find_fld(rcv_ty, e.re.id)
      new_fname = trans_fname(rcv_ty, e.re.id, fld.is_static)
      if fld.is_static:
        # access to the static field inside the same class
        if mtd and rcv_ty == mtd.clazz.name: buf.write(e.re.id)
        # o.w., e.g., static constant in an interface, call the accessor
        else: buf.write(new_fname + "()")
      else: buf.write('.'.join([curried(e.le), new_fname]))

  elif e.kind == C.E.IDX:
    buf.write(curried(e.e) + '[' + curried(e.idx) + ']')

  elif e.kind == C.E.NEW:
    if e.e.kind == C.E.CALL:
      ty = typ_of_e(mtd, e.e.f)
      cls = class_lookup(ty)
      if cls and cls.has_init:
        arg_typs = map(partial(typ_of_e, mtd), e.e.a)
        mname = trans_mname(cls.name, cls.name, arg_typs)
        obj = "alloc@log({})".format(cls.id)
        args = [obj] + map(unicode, map(curried, e.e.a))
        buf.write("{}({})".format(mname, ", ".join(args)))
      else: # collection or Object
        buf.write(C.J.NEW + ' ' + trans_ty(ty) + "()")
    else: # o.w., array initialization, e.g., new int[] { ... }
      buf.write(str(e.init))

  elif e.kind == C.E.CALL:
    arg_typs = map(partial(typ_of_e, mtd), e.a)

    def trans_call(callee, rcv_ty, rcv):
      if callee.is_static: rcv = None
      args = util.rm_none([rcv] + map(curried, e.a))
      mid = trans_mname(rcv_ty, callee.name, arg_typs)
      return u"{}({})".format(mid, ", ".join(args))

    def dynamic_dispatch(rcv_ty, rcv, acc, callee):
      _dispatched = trans_call(callee, callee.clazz.name, rcv)
      _guarded = "{}.__cid == {} ? {}".format(rcv, callee.clazz.id, _dispatched)
      return "({} : {})".format(_guarded, acc)

    if e.f.kind == C.E.DOT: # rcv.mid
      rcv_ty = typ_of_e(mtd, e.f.le)
      rcv = curried(e.f.le)
      mname = e.f.re.id
      mtd_callees = find_mtds_by_sig(rcv_ty, mname, arg_typs)
      if mtd_callees and 1 < len(mtd_callees): # needs dynamic dispatch
        curried_dispatch = partial(dynamic_dispatch, rcv_ty, rcv)
        # TODO: use least upper bound?
        default_v = util.default_value(mtd_callees[0].typ)
        buf.write(reduce(curried_dispatch, mtd_callees, default_v))
      elif mtd_callees and 1 == len(mtd_callees):
        mtd_callee = mtd_callees[0]
        buf.write(trans_call(mtd_callee, rcv_ty, rcv))
      else: # unresolved, maybe library method
        mid = trans_mname(rcv_ty, mname, arg_typs)
        args = util.rm_none([rcv] + map(curried, e.a))
        buf.write("{}({})".format(mid, ", ".join(args)))

    else: # mid
      mname = e.f.id
      # pre-defined meta information or Sketch primitive functions
      if mname in C.typ_arrays + [u"minimize"]:
        mid = mname
        rcv = None
        args = util.rm_none([rcv] + map(curried, e.a))
        buf.write("{}({})".format(mid, ", ".join(args)))
      elif mname == C.J.SUP and mtd.is_init: # super(...) inside <init>
        sup = class_lookup(mtd.clazz.sup)
        mid = trans_mname(sup.name, sup.name, arg_typs)
        rcv = C.SK.self
        args = util.rm_none([rcv] + map(curried, e.a))
        buf.write("{}({})".format(mid, ", ".join(args)))
      else: # member methods
        mtd_callees = find_mtds_by_sig(mtd.clazz.name, mname, arg_typs)
        if mtd_callees and 1 < len(mtd_callees): # needs dynamic dispatch
          curried_dispatch = partial(dynamic_dispatch, mtd.clazz.name, C.SK.self)
          # TODO: use least upper bound?
          default_v = util.default_value(mtd_callees[0].typ)
          buf.write(reduce(curried_dispatch, mtd_callees, default_v))
        elif mtd_callees and 1 == len(mtd_callees):
          mtd_callee = mtd_callees[0]
          buf.write(trans_call(mtd_callee, mtd.clazz.name, C.SK.self))
        else: # unresolved, maybe library method
          mid = trans_mname(mtd.clazz.name, mname, arg_typs)
          args = util.rm_none([rcv] + map(curried, e.a))
          buf.write("{}({})".format(mid, ", ".join(args)))

  elif e.kind == C.E.CAST:
    # since a family of classes is merged, simply ignore the casting
    buf.write(curried(e.e))

  elif e.kind == C.E.INS_OF:
    ty = typ_of_e(mtd, e.ty)
    cls = class_lookup(ty)
    if cls:
      buf.write(curried(e.e) + ".__cid == " + str(cls.id))
    else:
      logging.debug("unknown type: {}".format(ty))
      buf.write("0")

  else: buf.write(str(e))
  return buf.getvalue()
Пример #21
0
def is_operator(Token):
    if util.is_str(Token): # if we got a string, then check in the Operators, else False
        return Token in Operators
    return False
Пример #22
0
def parse_filter_string(s, extra_converters={}):
    entries = s.split(entry_separator)
    output = {}
    # For each separate filter entry...
    for entry in entries:
        ksv = entry_kvseparator_re.split(entry, 1)
        key = ksv[0].strip()
        if key in _conversions:
            multiple = (_conversions[key]['max'] != 1)
        else:
            raise KeyError("Unexpected filter key provided: {0}".format(key))
        ksvlist = ksv[2].strip().split(entry_subseparator)
        # Do we have sub-entries, or just a simple key=value ?
        value = {}
        # For each sub-entry...
        for e in ksvlist:
            eksv = [s.strip() for s in entry_kvseparator_re.split(e, 1)]
            # Is this sub-part a subkey=value?
            if len(eksv) > 1:
                if eksv[0] not in value:
                    value[eksv[0]] = []
                value[eksv[0]].append(Operator(value=eksv[2], op=eksv[1]))
            else:
                # If not, give it a position-based index
                if PosArgs not in value:
                    value[PosArgs] = []
                value[PosArgs].append(Operator(value=eksv[0], op=ksv[1]))
        # Set the value and move on
        if key not in output:
            output[key] = []
        output[key].append(value)

    # For each result
    for k in output.keys():
        # Do we know about it?
        if k in _conversions:
            if _conversions[k]['max'] not in [
                    None, 1
            ] and len(output[k]) > _conversions[k]['max']:
                raise KeyError(
                    "Filter key {} provided more than its maximum {} times".
                    format(k, _conversions[k]['max']))
            # Is it a complicated one, or a simple key=value?
            if isinstance(_conversions[k]['fn'], dict):
                # For each present subkey, check if we know about it
                outlist = output[k]
                # For each subkey...
                for outentry in outlist:
                    # For each named entry in that subkey (and None for positional args)...
                    for ek in outentry:
                        # For each instance of that named entry...
                        for i in range(0, len(outentry[ek])):
                            ev = outentry[ek][i]
                            # Check we have a valid conversion for the provided name
                            conv = _get_valid_conversion(
                                _conversions[k]['fn'], ek, i)
                            if not conv:
                                raise KeyError(
                                    "Unexpected filter subkey provided: {0}".
                                    format(ek))
                            # Check the provided operator is valid in this scenario
                            if ev.operator not in _get_valid_ops(conv):
                                raise KeyError(
                                    "Invalid operator provided for filter subkey '{}/{}'"
                                    .format(k, ek))
                            # Do the conversions
                            specials = _conversions[k]['fn'][ek]['special'] if (
                                ek in _conversions[k]['fn'] and isinstance(
                                    _conversions[k]['fn'][ek], dict)) else []
                            ev.value, continue_conv = _global_conv(
                                ev.value, specials)
                            if continue_conv:
                                if util.is_str(conv):
                                    if conv in extra_converters:
                                        ev.value = extra_converters[conv](
                                            ev.value)
                                    else:
                                        raise ValueError(
                                            "Could not perform conversion for filter subkey '{}/{}' with custom converter '{}'"
                                            .format(k, ek, conv))
                                else:
                                    ev.value = conv(ev.value)
            else:
                # For each entry associated with this key...
                for ovl in output[k]:
                    # For each entry in the positional args list...
                    for ov in ovl[PosArgs]:
                        # Check the provided operator is valid in this scenario
                        if ov.operator not in _get_valid_ops(
                                _conversions[k]['fn']):
                            raise KeyError(
                                "Invalid operator provided for filter key '{}'"
                                .format(k))
                        # Do the conversions
                        ov.value, continue_conv = _global_conv(
                            ov.value, _conversions[k]['special'])
                        if continue_conv:
                            if util.is_str(_conversions[k]['fn']):
                                if _conversions[k]['fn'] in extra_converters:
                                    ov.value = extra_converters[
                                        _conversions[k]['fn']](ov.value)
                                else:
                                    raise ValueError(
                                        "Could not perform conversion for special converter '{}'"
                                        .format(_conversions[k]['fn']))
                            else:
                                ov.value = _conversions[k]['fn'](ov.value)
        else:
            raise KeyError("Unexpected filter key provided: {0}".format(k))

    return output
Пример #23
0
def _c1_get_offset_from_name(input):
    frags = get_sector_fragments(input) if util.is_str(input) else input
    if frags is None:
        return None

    sufs = _get_suffixes(frags[0:-1], True)
    suf_len = len(sufs)

    # Add the total length of all the infixes we've already passed over
    if len(frags) > 3:
        # We have a 4-phoneme name, which means we have to handle adjusting our "coordinates"
        # from individual suffix runs up to fragment3 runs and then to fragment2 runs

        # STEP 1: Acquire the offset for suffix runs, and adjust it
        suf_offset = sufs.index(frags[-1])
        # Check which fragment3 run we're on, and jump us up by that many total run lengths if not the first
        suf_offset += (sufs.index(frags[-1]) // _c1_get_infix_run_length(
            frags[2])) * _c1_get_infix_total_run_length(frags[2])

        # STEP 2: Take our current offset from "suffix space" to "fragment3 space"
        # Divide by the current fragment3's run length
        # Remember the offset that we're at on the current suffix-run
        f3_offset, f3_offset_mod = divmod(suf_offset,
                                          _c1_get_infix_run_length(frags[2]))
        # Multiply by the total run length for this series of fragment3s
        f3_offset *= _c1_get_infix_total_run_length(frags[2])
        # Reapply the f3 offset from earlier
        f3_offset += f3_offset_mod
        # Add the offset of the current fragment3, to give us our overall position in the f3-sequence
        f3_offset += _c1_infix_offsets[frags[2]][0]

        # STEP 3: Take our current offset from "fragment3 space" to "fragment2 space"
        # Divide by the current fragment2's run length
        # Remember the offset that we're at on the current f3-run
        f2_offset, f2_offset_mod = divmod(f3_offset,
                                          _c1_get_infix_run_length(frags[1]))
        # Multiply by the total run length for this series of fragment2s
        f2_offset *= _c1_get_infix_total_run_length(frags[1])
        # Reapply the f2 offset from earlier
        f2_offset += f2_offset_mod
        # Add the offset of the current fragment2, to give us our overall position in the f2-sequence
        f2_offset += _c1_infix_offsets[frags[1]][0]

        # Set this as the global offset to be manipulated by the prefix step
        offset = f2_offset
    else:
        # We have a 3-phoneme name, which means we just have to adjust our coordinates
        # from "suffix space" to "fragment2 space" (since there is no fragment3)

        # STEP 1: Acquire the offset for suffix runs, and adjust it
        suf_offset = sufs.index(frags[-1])

        # STEP 2: Take our current offset from "suffix space" to "fragment2 space"
        # Divide by the current fragment2's run length
        # Remember the offset we're at on the current suffix-run
        f2_offset, f2_offset_mod = divmod(suf_offset,
                                          _c1_get_infix_run_length(frags[1]))
        # Multiply by the total run length for this series of fragment2s
        f2_offset *= _c1_get_infix_total_run_length(frags[1])
        # Reapply the f2 offset from earlier
        f2_offset += f2_offset_mod
        # Add the offset of the current fragment2, to give us our overall position in the f2-sequence
        f2_offset += _c1_infix_offsets[frags[1]][0]

        # Set this as the global offset to be manipulated by the prefix step
        offset = f2_offset

    # Divide by the current prefix's run length, this is now how many iterations of the full 3037 we should have passed over
    # Also remember the current offset's position within a prefix run
    offset, offset_mod = divmod(offset, _get_prefix_run_length(frags[0]))
    # Now multiply by the total run length (3037) to get the actual offset of this run
    offset *= pgdata.cx_prefix_total_run_length
    # Add the infixes/suffix's position within this prefix's part of the overall prefix run
    offset += offset_mod
    # Add the base position of this prefix within the run
    offset += _prefix_offsets[frags[0]][0]
    # Whew!
    return offset