Пример #1
0
def frontend_config1(fname='/etc/gwms-frontend/frontend.xml'):
    """Reconfig the frontend

    :type fname: :obj:`str`
    :arg fname: Frontend configuration file (Default: '/etc/gwms-frontend/frontend.xml')

    """
    # select certificates DN (DigiCert/OSG)
    XML_COLLECTOR = XML_COLLECTOR1
    XML_SECURITY = XML_SECURITY1
    XML_WMS_COLLECTOR = XML_WMS_COLLECTOR1
    XML_SCHEDD = XML_SCHEDD1
    if is_osg_certificate():
        XML_COLLECTOR = XML_COLLECTOR2
        XML_SECURITY = XML_SECURITY2
        XML_WMS_COLLECTOR = XML_WMS_COLLECTOR2
        XML_SCHEDD = XML_SCHEDD2
    elif is_incommon_certificate():
        XML_COLLECTOR = XML_COLLECTOR3
        XML_SECURITY = XML_SECURITY3
        XML_WMS_COLLECTOR = XML_WMS_COLLECTOR3
        XML_SCHEDD = XML_SCHEDD3
    if USE_TOKEN:
        # Using the host certificate to pass the certificate/proxy checks
        XML_SECURITY.replace("/etc/gwms-frontend/fe_proxy",
                             "/etc/grid-security/hostcert.pem")
    tree = ET.ElementTree()
    tree.parse(fname)
    # process_logs
    elem = tree.find('log_retention/process_logs')
    elem.append(ET.XML(XML_LOG_ALL))
    elem.append(ET.XML(XML_LOG_BAD))
    # factory
    elem = tree.find('match/factory')
    elem.set('query_expr', 'True')
    # collector
    collectors = tree.find('match/factory/collectors')
    for elem in list(collectors.getchildren()):
        collectors.remove(elem)
    new_coll = T(XML_COLLECTOR).substitute(dict(factory=FACTORY))
    collectors.append(ET.XML(new_coll))
    # security
    elem = tree.find('security')
    tree.getroot().remove(elem)
    elem = T(XML_SECURITY).substitute(dict(frontend=FRONTEND))
    tree.getroot().append(ET.XML(elem))
    # schedd
    schedds = tree.find('match/job/schedds')
    for elem in list(schedds.getchildren()):
        schedds.remove(elem)
    new_schedd = T(XML_SCHEDD).substitute(dict(frontend=FRONTEND))
    schedds.append(ET.XML(new_schedd))
    # WMS collector
    elem = tree.find('collectors')
    tree.getroot().remove(elem)
    elem = T(XML_WMS_COLLECTOR).substitute(dict(frontend=FRONTEND))
    tree.getroot().append(ET.XML(elem))
    # open("%s.new" % fname, 'w')
    tree.write("%s.new" % fname)
Пример #2
0
def reduce_anno_mtd(smpls, tmpl, cls, mtd):
    if not mtd.annos: return
    mname = mtd.name
    for _anno in mtd.annos:

        ##
        ## @Error void mname()
        ##
        if _anno.name == C.A.ERR:
            if not hasattr(cls, "state"):
                raise ReduceError("no state variable")
            setattr(cls.state, "error", mtd)
            state = cls.state.name
            body = T("${state} = ??;").safe_substitute(locals())
            mtd.body = st.to_statements(mtd, body) + mtd.body

        ##
        ## @Assemble typ mname(...)
        ##

        ##
        ## @CFG(other_mname) typ mname(...)
        ##
        elif _anno.name == C.A.CFG:
            # find the designated method
            _, cls_c_name, mtd_c_name = util.explode_mname(_anno.mid)
            if cls_c_name:
                mtd_c = class_lookup(cls_c_name).mtd_by_name(mtd_c_name)
            else:
                mtd_c = cls.mtd_by_name(mtd_c_name)
            # and then copy its body
            if mtd_c: mtd.body = mtd_c[0].body
Пример #3
0
def expand(env):
    # type: (env) -> None
    "Does expand"
    for key, value in env.items():
        value = value.replace('$1', '').strip().replace('"', '')
        env[key] = T(value).substitute(def_env)
    return env
Пример #4
0
def factory_config1(fname='/etc/gwms-factory/glideinWMS.xml',
                    entries=['ITB_FC_CE2', 'ITB_FC_CE3']):
    """Reconfig the factory

    :type fname: :obj:`str`
    :arg fname: Frontend configuration file (Default: '/etc/gwms-factory/glideinWMS.xml')

    """
    tree = ET.ElementTree()
    tree.parse(fname)
    # process_logs
    elem = tree.find('log_retention/process_logs')
    elem.append(ET.XML(XML_LOG_ALL))
    elem.append(ET.XML(XML_LOG_BAD))
    elem = tree.find('condor_tarballs')
    tree.getroot().remove(elem)
    if HTC_VERSION is not None:
        htc_tarballs = T(HTC_TARBALLS1).substitute(
            dict(condor_version=HTC_VERSION))
    else:
        htc_tarballs = HTC_TARBALLS
    tree.getroot().append(ET.XML(htc_tarballs))
    elem = tree.find('entries')
    for i in entries:
        elem.append(ET.XML(ENTRIES[i]))
    # open("%s.new" % fname, 'w')
    tree.write("%s.new" % fname)
Пример #5
0
def gen_nutcracker(port):
    ret = ''
    for i in range(len(HOSTS)):
        m = HOSTS[i]
        for j in range(MASTER_PER_MACHINE):
            xport = port + j
            templete = ' '*8 + "('$m:$xport', '$BASEDIR/nutcracker-$xport'),\n"
            ret += T(templete).s(dict(globals(), **locals()))
    return ret
Пример #6
0
def add_getter(cls, fld, mname):
    mtd_g = Method(clazz=cls, mods=fld.mods, typ=fld.typ, name=mname)
    fname = fld.name
    body = T("return ${fname};").safe_substitute(locals())
    mtd_g.body = st.to_statements(mtd_g, body)
    cls.mtds.append(mtd_g)
    # to replace annotation @Get(e) in expressions
    # or to encode static fields into sketch
    setattr(fld, "getter", mtd_g)
Пример #7
0
def gen_sentinel(port):
    ret = ''
    t_host = HOSTS[:3]
    if len(t_host) < 3:  # host not enough
        t_host = (t_host * 3) [:3]
    for m in t_host:
        templete = ' '*8 + "('$m:$port', '$BASEDIR/sentinel-$port'),\n"
        ret += T(templete).s(dict(globals(), **locals()))
        port += 1
    return ret
Пример #8
0
def gen_redis(port):
    ret = ''
    for i in range(len(HOSTS)):
        for j in range(MASTER_PER_MACHINE):
            slave_port = port + 1000

            m = HOSTS[i]
            s = HOSTS[(i+1)%len(HOSTS)]
            master_name = '%s-%s' % (CLUSTER_NAME, port)
            templete = ' '*8 + "'$master_name:$m:$port:$BASEDIR/redis-$port', '$master_name:$s:$slave_port:$BASEDIR/redis-$slave_port',\n"
            ret += T(templete).s(dict(globals(), **locals()))
            port += 1
    return ret
Пример #9
0
def write_htc_map(fname='/etc/condor/certs/condor_mapfile1'):
    f = open(fname, 'w')
    HTC_CERTMAP = HTC_CERTMAP1
    if is_osg_certificate():
        HTC_CERTMAP = HTC_CERTMAP2
    elif is_incommon_certificate():
        HTC_CERTMAP = HTC_CERTMAP3
    user_subjects = os.environ.get("X509_USER_SUBJECT")
    if user_subjects is None:
        user_subjects = HTC_CERTMAP_USER_DEFAULT
    else:
        us_list = [
            'GSI "%s" vofrontend_service\n' % i
            for i in user_subjects.split('\n') if i
        ]
        user_subjects = ''.join(us_list)
    f.write(user_subjects)
    f.write(
        T(HTC_CERTMAP).substitute(
            dict(frontend=escape(FRONTEND), factory=escape(FACTORY))))
    f.close()
Пример #10
0
def gen_cluster():
    templete = '''
$CLUSTER_NAME = {
    'cluster_name': '$CLUSTER_NAME',
    'user': '******',
    'sentinel':[
$sentinel_section
    ],
    'redis': [
$redis_section
    ],
    'nutcracker': [
$nutcracker_section
    ],
}
    '''

    redis_section      = gen_redis(2000+cluster_id*100)
    nutcracker_section = gen_nutcracker(4000+cluster_id*100)
    sentinel_section   = gen_sentinel(9000+cluster_id*100)

    print T(templete).s(dict(globals(), **locals()))
Пример #11
0
print('Rounding: pi = {:.3}'.format(math.pi))
print('Attributes: pi = {0.pi:.3}'.format(math))
print('And {0[1][0]} more!'.format(['so', ['much', ['more']]]))

# Example: formatting using the % operator
value = '%'
print('Formatting using the %s operator is error-prone.' % value)
print('Values must be specified %s %s.' % ('in', 'order'))
value = ('a', 'tuple')
# Wrap value as (value,); otherwise we get a TypeError
print('If a value is %s, things get complicated!' % (value,))
value = {'data': 'dictionary'}
print('Using a %(data)s key works though.' % value)
print('Rounding: pi = %.3f and e = %.3f.' % (math.pi, math.e))
print('%% has to be escaped when formatting.' % ())

# Example: formatting using the Template class
from math import pi
from string import Template as T # aliased for brevity

print(T('$id work').substitute(id='Keyword arguments'))
print(T('$id works too').substitute({'id': 'A dictionary'}))
print(T('Note the ${id}').substitute(id='differences'))
print(T('pi = ${id}').substitute(id=pi))
print(T('$$ has to be escaped').substitute())

# Example: formatting using literal string interpolation
value = 'string interpolation'
print(f'The use of {value} is awesome!') # more readable
print(f"Another PI example: pi = {pi:.3f}")
Пример #12
0
resulting program will be built to collect coverage data
when run. (coverage data measures which ops in the bf
source get run)
"""

import argparse
from cStringIO import StringIO
from string import Template as T
import os

from common import KNOWN_OPS, read_source, make_hash

BF_BASE = {
    'program_begin': T(r"""
int main() {
    """),
    'program_end': T(r"""
return 0;
}
    """),
}

BF_INTERP = {
    'include':
    T(r"""
#include <string.h>
#include <unistd.h>
    """),
    'program_begin':
    T(r"""
Пример #13
0
def reduce_anno_e(tmpl, cls, mtd, e, pe=None):
    curried = partial(reduce_anno_e, tmpl, cls, mtd)
    if e.kind == C.E.ANNO:
        _anno = e.anno

        ##
        ## Getters
        ##
        if _anno.name in [C.A.STATE, C.A.GET]:
            args = []
            if _anno.name == C.A.STATE:
                # @State(this) -> this.getter()
                if _anno.accessed == C.J.THIS:
                    mtd_g = cls.state.getter
                    f = st.gen_E_id(mtd_g.name)
                # @State(var) -> var.getter()
                else:
                    var = _anno.accessed
                    tname = mtd.vars[var]
                    mtd_g = class_lookup(tname).state.getter
                    f = st.gen_E_dot(st.gen_E_id(var), st.gen_E_id(mtd_g.name))
            # @Get(var.fname, args?) -> var.getFname(args?)
            elif _anno.name == C.A.GET:
                var, fname = _anno.fid.split('.')
                tname = mtd.vars[var]
                mtd_g = class_lookup(tname).fld_by_name(fname).getter
                f = st.gen_E_dot(st.gen_E_id(var), st.gen_E_id(mtd_g.name))
                if hasattr(_anno, "args"): args = _anno.args
            return st.gen_E_call(f, args)

        ##
        ## Setter
        ##
        # @Update(var) -> var.setter(??)
        elif _anno.name == C.A.UPDATE:
            var, fname = _anno.fid.split('.')
            tname = mtd.vars[var]
            mtd_s = class_lookup(tname).fld_by_name(fname).setter
            f = st.gen_E_dot(st.gen_E_id(var), st.gen_E_id(mtd_s.name))
            return st.gen_E_call(f, [st.gen_E_hole()])

        ##
        ## Generator
        ##
        elif _anno.name in [C.A.ALL, C.A.TAG]:
            if _anno.name == C.A.ALL: tag_g = u"gen_all"
            else: tag_g = u"gen_" + _anno.tag  # assume tag name is unique
            # (var.)? @anno
            if not pe: cls_g = cls
            else: cls_g = class_lookup(mtd.vars[unicode(pe.le)])

            if hasattr(cls_g, tag_g): mtd_g = getattr(cls_g, tag_g)
            else:  # introduce generator
                if _anno.name == C.A.ALL: mtds = cls_g.mtds
                else:  # C.A.TAG
                    find_tag = partial(anno.by_attr, {"tag": _anno.tag})
                    mtds = cls_g.mtds_w_anno(find_tag)
                mtd_g = Method(clazz=cls_g, mods=[C.mod.GN], name=tag_g)
                body = "int t = ??;\n"
                for i, mtd in enumerate(mtds):
                    mid = mtd.name
                    case = T("if (t == ${i}) { ${mid}(); }\n").safe_substitute(
                        locals())
                    body = body + case

                body = body + "assert t <= {};".format(len(mtds))
                mtd_g.body = st.to_statements(mtd_g, body)

                cls_g.mtds.append(mtd_g)
                setattr(cls_g, tag_g, mtd_g)

            # @Tag("tag") | @All -> gen_tag | gen_all
            return st.gen_E_id(mtd_g.name)

        ##
        ## Reflection
        ##
        # @New should be handled differently
        # e.g., for "ClassName".@New({ args })
        # Sketch: new ClassName(args);
        # Java: (ClassName)(Class.forName("ClassName").newInstance(args));
        elif _anno.name == C.A.NEW:
            pass

            ##
            ## Comparator
            ##
            # @Compare will be encoded into a regex generator in Sketch
            # and then will be replaced with a proper operator in Java
        elif _anno.name in [C.A.CMP, C.A.CMP_STR]:
            pass

            ##
            ## Observers
            ##
            # event sending
        elif _anno.name == C.A.EVENT:
            pass  # TODO

            # a list that maintains the registered observers
        elif _anno.name == C.A.OBSS:
            if not hasattr(cls, "obs"):
                cls_o_name, _ = find_observer_at_cls(tmpl, cls)
                add_obs(cls, cls_o_name)
            return st.gen_E_id(cls.obs.name)

        # invoke the @Notified method
        elif _anno.name == C.A.NOTI:
            mtd_noti = find_noti(tmpl, cls)
            rcv = _anno.args[0]
            call_noti = st.gen_E_dot(rcv, st.gen_E_id(mtd_noti.name))
            return st.gen_E_call(call_noti, _anno.args[1:])

    elif e.kind in [C.E.BOP, C.E.DOT]:
        e.le = curried(e.le, e)
        e.re = curried(e.re, e)

    elif e.kind == C.E.NEW:
        e.e = curried(e.e)

    elif e.kind == C.E.CALL:
        e.f = curried(e.f)
        map(curried, e.a)

    return e
Пример #14
0
def col_to_struct(cls):
  buf = cStringIO.StringIO()
  cname = cls.name
  sname = trans_ty(cname)
  global _collections
  if sname in _collections:
    logging.debug("collection: {} (duplicated)".format(cname))
    return u''
  else:
    _collections.add(sname)
    logging.debug("collection: " + cname)

  buf.write("struct ${sname} {\n  int idx;\n")

  if C.J.MAP in cname:
    _, k, v = util.of_collection(cname)
    k = trans_ty(k)
    v = trans_ty(v)

    # Map<K,V> -> struct Map_K_V { int idx; K[S] key; V[S] val; }
    buf.write("  ${k}[S] key;\n  ${v}[S] val;\n}\n")

    # Map<K,V>.containsKey -> containsKey_Map_K_V
    buf.write("""
      bit {} (${{sname}} map, ${{k}} k) {{
        int i;
        for (i = 0; map.val[i] != null && i < S; i++) {{
          if (map.key[i] == k) return 1;
        }}
        return 0;
      }}
    """.format(trans_mname(cname, u"containsKey", [k])))

    # Map<K,V>.get -> get_Map_K_V
    buf.write("""
      ${{v}} {} (${{sname}} map, ${{k}} k) {{
        int i;
        for (i = 0; map.val[i] != null && i < S; i++) {{
          if (map.key[i] == k) return map.val[i];
        }}
        return null;
      }}
    """.format(trans_mname(cname, u"get", [k])))

    # Map<K,V>.put -> put_Map_K_V
    buf.write("""
      void {} (${{sname}} map, ${{k}} k, ${{v}} v) {{
        map.key[map.idx] = k;
        map.val[map.idx] = v;
        map.idx = (map.idx + 1) % S;
      }}
    """.format(trans_mname(cname, u"put", [k, v])))

    # Map<K,V>.clear -> clear_Map_K_V
    if util.is_class_name(k): default_k = "null"
    else: default_k = "0"
    buf.write("""
      void {} (${{sname}} map) {{
        map.idx = 0;
        for (int i = 0; i < S; i++) {{
          map.key[i] = {};
          map.val[i] = null;
        }}
      }}
    """.format(trans_mname(cname, u"clear", []), default_k))

  else:
    collection, t = util.of_collection(cname)
    t = trans_ty(t)

    if C.J.QUE in collection: buf.write("  int head;\n")
    # Collection<T> -> struct Collection_T { int idx; T[S] elts; }
    buf.write("  ${t}[S] elts;\n}\n")

    if C.J.STK in collection:
      # Stack<T>.peek -> peek_Stack_T
      buf.write("""
        ${{t}} {} (${{sname}} stk) {{
          if (stk.idx == 0) return null;
          ${{t}} top = stk.elts[stk.idx - 1];
          return top;
        }}
      """.format(trans_mname(cname, u"peek", [])))

      # Stack<T>.push -> push_Stack_T
      buf.write("""
        ${{t}} {} (${{sname}} stk, ${{t}} elt) {{
          stk.elts[stk.idx] = elt;
          stk.idx = (stk.idx + 1) % S;
          return elt;
        }}
      """.format(trans_mname(cname, u"push", [t])))

      # Stack<T>.pop -> pop_Stack_T
      buf.write("""
        ${{t}} {} (${{sname}} stk) {{
          if (stk.idx == 0) return null;
          stk.idx = stk.idx - 1;
          ${{t}} top = stk.elts[stk.idx];
          stk.elts[stk.idx] = null;
          return top;
        }}
      """.format(trans_mname(cname, u"pop", [])))

    elif C.J.QUE in collection:
      # Queue<T>.add -> add_Queue_T
      buf.write("""
        bit {} (${{sname}} que, ${{t}} elt) {{
          que.elts[que.idx] = elt;
          que.idx = (que.idx + 1) % S;
          return true;
        }}
      """.format(trans_mname(cname, u"add", [t])))

      # Queue<T>.remove -> remove_Queue_T
      buf.write("""
        ${{t}} {} (${{sname}} que) {{
          if (que.head == que.idx) return null;
          ${{t}} top = que.elts[que.head];
          que.elts[que.head] = null;
          que.head = (que.head + 1) % S;
          return top;
        }}
      """.format(trans_mname(cname, u"remove", [])))

      # Queue<T>.isEmpty -> isEmpty_Queue_T
      buf.write("""
        bit {} (${{sname}} que) {{
          return que.head == que.idx;
        }}
      """.format(trans_mname(cname, u"isEmpty", [])))

    elif C.J.LST in collection:
      # List<T>.add -> add_List_T
      buf.write("""
        bit {} (${{sname}} lst, ${{t}} elt) {{
          lst.elts[lst.idx] = elt;
          lst.idx = (lst.idx + 1) % S;
          return true;
        }}
      """.format(trans_mname(cname, u"add", [t])))

      # List<T>.remove(T) -> remove_List_T_T
      buf.write("""
        bit {} (${{sname}} lst, ${{t}} elt) {{
          int i;
          for (i = 0; lst.elts[i] != null && i < S; i++) {{
            if (lst.elts[i] == elt) {{
              lst.elts[i] = null;
              int j;
              for (j = i + 1; lst.elts[j] != null && j < lst.idx; j++) {{
                lst.elts[j-1] = lst.elts[j];
              }}
              lst.idx = (lst.idx - 1) % S;
              return true;
            }}
          }}
          return false;
        }}
      """.format(trans_mname(cname, u"remove", [t])))

      # List<T>.remove(int) -> remove_List_T_int
      buf.write("""
        ${{t}} {} (${{sname}} lst, int index) {{
          ${{t}} res = null;
          if (0 <= index && index < lst.idx) {{
            res = lst.elts[index];
            lst.elts[index] = null;
            int i;
            for (i = index + 1; lst.elts[i] != null && i < lst.idx; i++) {{
              lst.elts[i-1] = lst.elts[i];
            }}
            lst.idx = (lst.idx - 1) % S;
          }}
          return res;
        }}
      """.format(trans_mname(cname, u"remove", [C.J.i])))

      # List<T>.get -> get_List_T
      buf.write("""
        ${{t}} {} (${{sname}} lst, int index) {{
          ${{t}} res = null;
          if (0 <= index && index < lst.idx) {{
            res = lst.elts[index];
          }}
          return res;
        }}
      """.format(trans_mname(cname, u"get", [C.J.i])))

      # List<T>.isEmpty -> isEmpty_List_T
      buf.write("""
        bit {} (${{sname}} lst) {{
          return lst.idx == 0;
        }}
      """.format(trans_mname(cname, u"isEmpty", [])))

  return T(buf.getvalue()).safe_substitute(locals())
Пример #15
0
def reduce_anno_fld(smpls, tmpl, cls, fld):
    if not fld.annos: return
    cname = cls.name
    fname = fld.name
    Fname = util.cap_1st_only(fname)
    for _anno in fld.annos:

        ##
        ## @State(@Tag(...) | @All) int _state
        ##
        if _anno.name == C.A.STATE:
            setattr(cls, "state", fld)

            # find all the methods involved in this state variable
            where = _anno.accessed
            if where.name == C.A.ALL:
                mtds = cls.mtds[:]  # quick copy, since mtds will be modified below
            elif where.name == C.A.TAG:
                find_tag = partial(anno.by_attr, {"tag": where.tag})
                mtds = cls.mtds_w_anno(find_tag)
            else:
                mtds = cls.mtd_by_name(where)

            # exclude setter to avoid a recursive call
            if hasattr(fld, "setter"): mtds.remove(fld.setter)
            # exclude error, if exists, to avoid redundant state update
            find_err = partial(anno.by_name, C.A.ERR)
            errs = cls.mtds_w_anno(find_err)
            if errs:
                if len(errs) > 1: raise ReduceError("ambiguous error state")
                err = errs[0]
                mtds.remove(err)

            # then add a statement to update this state variable into those methods
            for mtd in mtds:

                # TODO: currently, @Set should be reduced before @State
                if hasattr(fld, "setter"):  # use setter if exists
                    f_setter = fld.setter.name
                    upd = "${f_setter}(??);"
                else:
                    upd = "${fname} = ??;"

                if errs and err:
                    e_name = err.name
                    trans = "if (??) { " + upd + " } else { ${e_name}(); }"
                else:
                    trans = upd

                body_tmpl = "repeat(??) { if (${fname} == ??) { " + trans + " } }"
                body = T(body_tmpl).safe_substitute(locals())
                mtd.body = st.to_statements(mtd, body) + mtd.body

            # introduce getter to replace @State in expressions later
            name = "get" + Fname
            mtd_g = Method(clazz=cls, typ=fld.typ, name=name)
            body = T("return ${fname};").safe_substitute(locals())
            mtd_g.body = st.to_statements(mtd, body)
            cls.mtds.append(mtd_g)
            setattr(fld, "getter", mtd_g)

        ##
        ## @Multiton({typ_v, ...})? Map<key,value> fname
        ##
        elif _anno.name == C.A.MULTI:
            # assume fld.typ is Map<key,value>
            m, key, value = util.extract_generics(fld.typ)
            if hasattr(_anno, "values"): values = _anno.values
            elif C.J.MAP in fld.typ: values = [value]

            # add a getter
            mname = sample.find_getter(smpls, values, Fname)
            params = [(key, u"key")]
            mtd_g = Method(clazz=cls, typ=value, name=mname, params=params)
            cls.mtds.append(mtd_g)

            # usage 1: Map<String,Object> systemService
            if len(values) > 1:
                pass
                # TODO: collect key-value mappings occurred at the samples
                # TODO: then add code to initialize those mappings into the constructor
                # usage 2: Map<int,View> viewById
            else:  # i.e. values = [value]
                body = T("""
boolean chk = ${fname}.containsKey(key);
if (chk != true) {
  ${fname}.put(key, new ${value}(key));
}
return ${fname}.get(key);
""").safe_substitute(locals())
            mtd_g.body = st.to_statements(mtd_g, body)
            setattr(fld, "getter", mtd_g)

            # initialize this field
            cname = cls.name
            init = cls.mtd_by_sig(cname)
            if not init: init = cls.add_default_init()
            fld_typ = fld.typ
            body = T("${fname} = new ${fld_typ}();").safe_substitute(locals())
            init.body.extend(st.to_statements(init, body))

        ##
        ## @Get typ fname
        ## @Is typ fname
        ##
        elif _anno.name in [C.A.GET, C.A.IS]:
            # special case: Map<String, Object> _elements;
            if C.J.MAP in fld.typ: pass
            # TODO: collect get$T$ occurrences, where $T$ is any types
            # TODO: then add all those getters which look like below:
            # TODO: $T$ get$T$(String key) { return _elements.get(k); }

        ##
        ## @Has typ fname
        ##
        elif _anno.name == C.A.HAS:
            # add a checker, along with a boolean field, say "_setFname"
            name = "_set" + Fname
            fld_h = Field(clazz=cls, typ=C.J.z, name=name)
            cls.flds.append(fld_h)

            mname = sample.find_getter(smpls, [C.J.z], Fname, "has")
            mtd_h = Method(clazz=cls, typ=C.J.z, name=mname)
            body = T("return ${name};").safe_substitute(locals())
            mtd_h.body = st.to_statements(mtd_h, body)

            # find the setter and add a statement: _setFname := true
            if hasattr(fld, "setter"):
                mtd_s = fld.setter
                body = T("${name} = true;").safe_substitute(locals())
                mtd_s.body.extend(st.to_statements(mtd_s, body))

        ##
        ## @Put(typ_b) typ fname
        ##
        elif _anno.name == C.A.PUT:
            # special case: Map<String, Object> _elements;
            if C.J.MAP in fld.typ:
                pass
                # TODO: collect put$T$ occurrences, where $T$ is any types
                # TODO: then add all those putters which look like below:
                # TODO: void put$T$(String k, $T$ v) { _elements.put(k, v); }
            else:
                pass
Пример #16
0
    def __init__(self):
        """初始化函数,主要是为了初始化 HTML_Template 中定义的 HTML 模板
        (以后这些模板还可根据需要继续添加)"""
        self.__e = {
            'script': T(HTML_SCRIPT),  # 脚本模板
            'table': T(HTML_TABLE),  # 表格模板
            'audio': T(HTML_AUDIO),  # 音频模板
            'video': T(HTML_VIDEO),  # 视频模板
            'nobr': T(HTML_NOBR),  # 无换行模板
            'span': T(HTML_SPAN),  # span模板
            'body': T(HTML_BODY),  # body模板
            'div': T(HTML_DIV),  # div模板
            'img': T(HTML_IMG),  # 图片模板
            'nav': T(HTML_NAV),  # 导航模板
            'h1': T(HTML_H1),  # h1模板
            'br': T(HTML_BR),  # 换行符模板
            'a': T(HTML_A),  # 链接模板
            'p': T(HTML_P)  # 段落模板
        }

        # 每个 Py2HTML 的基类都需要生成一个 html 字符串 用来写入文件或者直接交给 Tornado 进行路由
        self.html = None
Пример #17
0
class Formatter(object):
    """All the message formatting happens in this class."""
    lib = {
        "issue":
        T("  ----------------------------------------\n" +
          "  Issue $rule_key\n" + "    Status $status\n" +
          "    Type     $issue_type\n" + "    Created  $created_at\n"),
        "event":
        T("  ----------------------------------------\n" + "  Event $type\n" +
          "    Critical $critical\n" + "    Created  $created_at\n" +
          "    Message  $message\n"),
        "server_facts":
        T("---------------------------\n" + "Server Hostname     $hostname\n" +
          "  Server ID         $id\n" + "  Platform          $platform\n" +
          "  Platform version  $platform_version\n" +
          "  OS version        $os_version\n" +
          "  Group             $group_name\n" +
          "  Primary IP        $primary_ip_address\n" +
          "  Connecting IP     $connecting_ip_address\n" +
          "  State             $state\n" +
          "  State Change      $last_state_change\n"),
        "server_ec2":
        T("---------------------------\n" + "Server Hostname     $hostname\n" +
          "  Server ID         $id\n" + "  Platform          $platform\n" +
          "  Platform version  $platform_version\n" +
          "  OS version        $os_version\n" +
          "  Group             $group_name\n" +
          "  Primary IP        $primary_ip_address\n" +
          "  Connecting IP     $connecting_ip_address\n" +
          "  State             $state\n" +
          "  State Change      $last_state_change\n" + "  EC2:\n" +
          "    Instance ID:    $ec2_instance_id\n" +
          "    Account ID:     $ec2_account_id\n" +
          "    Kernel ID:      $ec2_kernel_id\n" +
          "    Image ID:       $ec2_image_id\n" +
          "    Avail. Zone:    $ec2_availability_zone\n" +
          "    Region:         $ec2_region\n" +
          "    Private IP:     $ec2_private_ip\n" +
          "    Instance Type:  $ec2_instance_type\n" +
          "    Security Grps:  $ec2_security_groups\n"),
        "policy_meta":
        T("    ---------------------------------------\n" +
          "    Policy name $name\n" + "      Policy type   $poltype \n" +
          "      Policy ID     $id\n" + "      Description   $description\n"),
        "group_facts":
        T("---------------------------\n" + "Group name $name\n" +
          "  Group ID      $id\n" + "  Description   $description\n" +
          "  Tag           $tag\n"),
        "task":
        T("---------------------------\n" + "Task ID: $id\n" +
          "  Task Name:      $name\n" + "  Task Args:      $args\n" +
          "  Task Kwargs:    $kwargs\n" + "  Task Started:   $started\n" +
          "  Task Timestamp: $tstamp\n" + "  Task State:     $state\n" +
          "  Exception:      $exception\n")
    }

    @classmethod
    def format_list(cls, items, item_type):
        retval = ""
        for item in items:
            retval = retval + Formatter.format_item(item, item_type)
        return retval

    @classmethod
    def format_item(cls, item, item_type):
        t = Formatter.lib[item_type]
        retval = t.safe_substitute(item)
        return retval

    @classmethod
    def policy_meta(cls, body, poltype):
        """Return one policy in friendly text"""
        body["poltype"] = poltype
        t = Formatter.lib["policy_meta"]
        return t.safe_substitute(body)
Пример #18
0
    '127.0.0.5',
]
MASTER_PER_MACHINE = 2

# gen the "redis" section
port = 2000
for i in range(len(HOSTS)):
    for j in range(MASTER_PER_MACHINE):
        slave_port = port + 1000

        m = HOSTS[i]
        s = HOSTS[(i + 1) % len(HOSTS)]

        #old format
        #templete = "('$m:$port', '$BASEDIR/redis-$port'), ('$s:$slave_port', '$BASEDIR/redis-$slave_port'),"

        #new format:
        master_name = '%s-%s' % (CLUSTER_NAME, port)
        templete = "'$master_name:$m:$port:$BASEDIR/redis-$port', '$master_name:$s:$slave_port:$BASEDIR/redis-$slave_port',"
        print T(templete).s(globals())
        port += 1

# gen the "nutcracker" section
port = 4000
for i in range(len(HOSTS)):
    m = HOSTS[i]
    for j in range(MASTER_PER_MACHINE):
        xport = port + j
        templete = "('$m:$xport', '$BASEDIR/nutcracker-$xport'),"
        print T(templete).s(globals())