Exemplo n.º 1
0
def trans(sens, compat, src, genxrefs):
    """Format a jmnedict trans element.
        s -- A sense object."""

    fmt = []
    nlist = getattr(sens, '_misc', [])
    kwtab = getattr(XKW, 'NAME_TYPE')
    for x in nlist:
        # 'kwtab' contains only 'misc' keywords that are used
        # in jmnedict so we want to ignore KeyErrors caused by
        # encountering other (eg jmdict) keywords.  (IS-225)
        try:
            fmt.append('<name_type>&%s;</name_type>' % kwtab[x.kw].kw)
        except KeyError:
            pass
    if genxrefs:
        xr = sens_xrefs(sens, src, compat)
        fmt.extend(xr)
    eng_id = KW.LANG['eng'].id
    for g in getattr(sens, '_gloss', []):
        lang = getattr(g, 'g_lang', eng_id)
        lang_attr = (' xml:lang="%s"' %
                     XKW.LANG[lang].kw) if lang != eng_id else ''
        fmt.append('<trans_det%s>%s</trans_det>' % (lang_attr, esc(g.txt)))
    if fmt:
        fmt.insert(0, '<trans>')
        fmt.append('</trans>')
    return fmt
Exemplo n.º 2
0
def info(entr, compat=None, genhists=False, last_imported=None):
    fmt = []
    if not compat:
        x = getattr(entr, 'srcnote', None)
        if x: fmt.append('<srcnote>%s</srcnote>' % esc(entr.srcnote))
        x = getattr(entr, 'notes', None)
        if x: fmt.append('<notes>%s</notes>' % esc(entr.notes))
    if genhists:
        for n, x in enumerate(getattr(entr, '_hist', [])):
            fmt.extend(
                audit(x,
                      compat,
                      force_created=(n == 0
                                     and (last_imported is None
                                          or entr.seq > last_imported))))
    if fmt:
        fmt.insert(0, '<info>')
        fmt.append('</info>')
    return fmt
Exemplo n.º 3
0
def lsrc(x):
    fmt = []
    attrs = []
    if x.lang != XKW.LANG['eng'].id or not x.wasei:
        attrs.append('xml:lang="%s"' % XKW.LANG[x.lang].kw)
    if x.part: attrs.append('ls_type="part"')
    if x.wasei: attrs.append('ls_wasei="y"')
    attr = (' ' if attrs else '') + ' '.join(attrs)
    if not x.txt: fmt.append('<lsource%s/>' % attr)
    else: fmt.append('<lsource%s>%s</lsource>' % (attr, esc(x.txt)))
    return fmt
Exemplo n.º 4
0
def kanj(k):
    fmt = []
    fmt.append('<k_ele>')
    fmt.append('<keb>%s</keb>' % esc(k.txt))
    fmt.extend(kwds(k, '_inf', 'KINF', 'ke_inf', sort=True))
    fmt.extend([
        '<ke_pri>%s</ke_pri>' % s
        for s in jdb.freq2txts(getattr(k, '_freq', []))
    ])
    fmt.append('</k_ele>')
    return fmt
Exemplo n.º 5
0
def rdng(r, k, compat):
    fmt = []
    fmt.append('<r_ele>')
    fmt.append('<reb>%s</reb>' % esc(r.txt))
    fmt.extend(restrs(r, k))
    fmt.extend(kwds(r, '_inf', 'RINF', 're_inf', sort=True))
    fmt.extend([
        '<re_pri>%s</re_pri>' % s
        for s in jdb.freq2txts(getattr(r, '_freq', []))
    ])
    if not compat: fmt.extend(audio(r))
    fmt.append('</r_ele>')
    return fmt
Exemplo n.º 6
0
def audit(h, compat=None, force_created=False):
    global XKW
    fmt = []
    fmt.append('<audit>')
    fmt.append('<upd_date>%s</upd_date>' % h.dt.date().isoformat())
    if not compat or compat == 'jmdicthist':
        if getattr(h, 'notes', None):
            fmt.append('<upd_detl>%s</upd_detl>' % esc(h.notes))
        if getattr(h, 'stat', None):
            fmt.append('<upd_stat>%s</upd_stat>' % XKW.STAT[h.stat].kw)
        if getattr(h, 'unap', None): fmt.append('<upd_unap/>')
        if getattr(h, 'email', None):
            fmt.append('<upd_email>%s</upd_email>' % esc(h.email))
        if getattr(h, 'name', None):
            fmt.append('<upd_name>%s</upd_name>' % esc(h.name))
        if getattr(h, 'refs', None):
            fmt.append('<upd_refs>%s</upd_refs>' % esc(h.refs))
        if getattr(h, 'diff', None):
            fmt.append('<upd_diff>%s</upd_diff>' % esc(h.diff))
    else:
        # When generating JMdict or JMnedict compatible XML,
        # history details (such as hist.note) are considered
        # "not for distribution".  We will generate audit elements
        # for each hist record, but provide only the date, and
        # "entry created" or "entry amended".  History records
        # added to imported entries should always result in
        # "entry amended" except if the first record says
        # "entry created".  The first history record on post-
        # import entries should always be "entry created".
        # Catch is we can't tell from an entry itself whether
        # it is an imported entry, or was created post-import,
        # so we rely on the caller to set 'force_created' to
        # true for the first hist record in post-import entries.
        if force_created or h.notes == "Entry created": note = "Entry created"
        else: note = "Entry amended"
        fmt.append('<upd_detl>%s</upd_detl>' % note)
    fmt.append('</audit>')
    return fmt
Exemplo n.º 7
0
def corpus(corpora):
    KW = jdb.KW
    fmt = []
    for c in corpora:
        kwo = KW.SRC[c]
        fmt.append('<corpus id="%d">' % kwo.id)
        fmt.append('<co_name>%s</co_name>' % kwo.kw)
        if getattr(kwo, 'descr', None):
            fmt.append('<co_descr>%s</co_descr>' % esc(KW.SRC[c].descr))
        if getattr(kwo, 'dt', None):
            fmt.append('<co_date>%s</co_date>' % KW.SRC[c].dt)
        if getattr(kwo, 'notes', None):
            fmt.append('<co_notes>%s</co_notes>' % esc(KW.SRC[c].notes))
        if getattr(kwo, 'seq', None):
            fmt.append('<co_sname>%s</co_sname>' % esc(KW.SRC[c].seq))
        if getattr(kwo, 'sinc', None):
            fmt.append('<co_sinc>%d</co_sinc>' % KW.SRC[c].sinc)
        if getattr(kwo, 'smin', None):
            fmt.append('<co_smin>%d</co_smin>' % KW.SRC[c].smin)
        if getattr(kwo, 'smax', None):
            fmt.append('<co_smax>%d</co_smax>' % KW.SRC[c].smax)
        fmt.append('</corpus>')
    return fmt
Exemplo n.º 8
0
def gloss(g, compat=None):
    fmt = []
    attrs = []
    if g.lang != XKW.LANG['eng'].id:
        attrs.append('xml:lang="%s"' % XKW.LANG[g.lang].kw)
    # As of DTD rev 1.09 ginf attributes are added to glosses so
    # we no longer do this only for the 'compat is None' condition.
    #FIXME: this will produce "g_type" attributes in jmnedict
    # in violation of the jmnedict DTD if there are .ginf items
    # in the data.  We fragilely count on there not being any.
    if g.ginf != XKW.GINF['equ'].id:
        attrs.append('g_type="%s"' % XKW.GINF[g.ginf].kw)
    attr = (' ' if attrs else '') + ' '.join(attrs)
    fmt.append("<gloss%s>%s</gloss>" % (attr, esc(g.txt)))
    return fmt
Exemplo n.º 9
0
def xref(xref, src):
    """
        Generate a formatter xml string for a single xref.

        xref -- The xref object to be formatted.  The xref must
          have an augmented target attribute (as produced by calling
          augment_xrefs()) since that infomation is required to
          generate the kanji and reading texts, and an error will
          be raised if not.

        src -- Corpus id number of the entry that contains 'xref'.
          If 'src' is true, enhanced XML will be generated.
          If not, legacy JMdict XML will be generated.

        The returned xref string will have a "%s" where the target
        sense number would go, which the caller is expected to
        replace with the sense number or not, as desired.
        """
    targobj = xref.TARG
    k = r = ''
    if getattr(xref, 'kanj', None):
        k = targobj._kanj[xref.kanj - 1].txt
    if getattr(xref, 'rdng', None):
        r = targobj._rdng[xref.rdng - 1].txt
    if k and r: target = k + '\u30FB' + r  # \u30FB is mid-height dot.
    else: target = k or r

    tag = 'xref'
    attrs = []
    if src:
        attrs.append('type="%s"' % XKW.XREF[xref.typ].kw)
        attrs.append('seq="%s"' % targobj.seq)
        if targobj.src != src:
            attrs.append('corp="%s"' % jdb.KW.SRC[targobj.src].kw)
        if getattr(xref, 'notes', None):
            attrs.append('note="%s"' % esc(xref.notes))
    else:
        if xref.typ == XKW.XREF['ant'].id: tag = 'ant'

    attr = (' ' if attrs else '') + ' '.join(attrs)
    return '<%s%s>%s%%s</%s>' % (tag, attr, target, tag)
def item_rss(links):
  for episode in find_episodes(links):
    minutes = episode['minutes']
    duration = "{}:{}:00".format(minutes//60, minutes%60)

    # Some old episodes use 'Sept' instead of 'Sep' for the month, and my podcast listener doesn't like that.
    date = episode['date'].split(' ')
    date[1] = date[1][:3]
    date = ' '.join(date)
    yield episode['nr'], itemtemplate.substitute(
      NR=esc(str(episode['nr'])),
      NR4=esc(str(episode['nr']).rjust(4, '0')),
      DATE=esc(date),
      DURATION=esc(duration),
      TITLE=esc(episode['title']),
      DESCRIPTION=esc(episode['description']))
Exemplo n.º 11
0
def sens(s, kanj, rdng, compat, src, genxrefs=True, prev_pos=None):
    """
        Format a sense.
        fmt -- A list to which formatted text lines will be appended.
        s -- The sense object to format.
        kanj -- The kanji object of the entry that 's' belongs to.
        rdng -- The reading object of the entry that 's' belongs to.
        compat -- See function entr().  We assume in sens() that if
            compat is not None it is =='jmdictxxx', that is, if it 
            were 'jmnedict', trans() would have been called rather 
            than sens().
        src -- If 'compat' is None, this should be the value of the
            entry's .src attribute.  It is passed to the xref() func
            which needs it when formatting enhanced xml xrefs.  If
            'compat' is not None, this parameter is ignored.
        genxrefs -- If false, do not attempt to format xrefs.  This
            will prevent an exception if the entry has only ordinary
            xrefs rather than augmented xrefs.
        prev_pos -- If not None, should be set to the pos list of
            the previous sense, or an empty list if this is the
            first sense of an entry.  This function will mutate
            'prev_pos' (if not None) to the current pos list before
            returning so that usually, sens() will be called with
            with an empty list on the first sense of an entry, and
            te same list on subsequent calls.  It is used to suppress
            pos values when they are the same as in the prevuious
            sense per the JMdict DTD.
            If None, an explict pos will be generated in each sense.
        """
    fmt = []
    fmt.append('<sense>')

    fmt.extend(restrs(s, kanj, '_stagk'))
    fmt.extend(restrs(s, rdng, '_stagr'))

    this_pos = [x.kw for x in getattr(s, '_pos', [])]
    if not prev_pos or prev_pos != this_pos:
        fmt.extend(kwds(s, '_pos', 'POS', 'pos'))
        if prev_pos is not None: prev_pos[:] = this_pos

    xr = sens_xrefs(s, src, compat)
    fmt.extend(xr)

    fmt.extend(kwds(s, '_fld', 'FLD', 'field'))
    fmt.extend(kwds(s, '_misc', 'MISC', 'misc'))

    notes = getattr(s, 'notes', None)
    if notes: fmt.append('<s_inf>%s</s_inf>' % esc(notes))

    lsource = getattr(s, '_lsrc', None)
    if lsource:
        for x in lsource:
            fmt.extend(lsrc(x))

    fmt.extend(kwds(s, '_dial', 'DIAL', 'dial'))

    for x in s._gloss:
        fmt.extend(gloss(x, compat))

    fmt.append('</sense>')
    return fmt
Exemplo n.º 12
0
def escape(s):
    return esc(s).replace("&", "_")
Exemplo n.º 13
0
    def export_kml(self, filename):
        """ Exports all networks into a KML file with placemarks for each network """
        try:
            # Initialise KML document
            kml = simplekml.Kml()

            # URL for circle icon
            circleUrl = "http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png"

            # Create some styles for different coloured icons
            amberCircle = simplekml.Style()
            amberCircle.iconstyle.icon.href = circleUrl
            amberCircle.iconstyle.color = simplekml.Color.orange

            greenCircle = simplekml.Style()
            greenCircle.iconstyle.icon.href = circleUrl
            greenCircle.iconstyle.color = simplekml.Color.green

            redCircle = simplekml.Style()
            redCircle.iconstyle.icon.href = circleUrl
            redCircle.iconstyle.color = simplekml.Color.red

            # Calulate date deltas to color code networks
            minus_twelve = date.today()+relativedelta(months=-12)
            minus_eighteen = date.today()+relativedelta(months=-18)
            # Loop through the network data, adding once placemark per network.
            for net in self.json_data:

                # Ensure all the fields we reference are actually present
                fields = ["ssid", "channel", "encryption",
                          "type", "netid", "firsttime", "lastupdt"]
                for f in fields:
                    if not net[f]:
                        net[f] = ""

                net["ssid"] = "".join(
                    i for i in net["ssid"] if 31 < ord(i) < 127)
                net["ssid"] = esc(net["ssid"])

                point = kml.newpoint(name=net["ssid"], coords=[
                    (net["trilong"], net["trilat"])])

                # Parse date of individual networks
                updated_date = date_parser.parse(net["lastupdt"]).date()

                # Colorise networks based on the time they were last seen
                if updated_date > minus_twelve:
                    point.style = greenCircle
                elif (updated_date > minus_eighteen) and (updated_date < minus_twelve):
                    point.style = amberCircle
                else:
                    point.style = redCircle

                # Add all the extra data to the KML point
                ed = point.extendeddata
                ed.newdata(name="SSID", value=net["ssid"])
                ed.newdata(name="Channel", value=net["channel"])
                ed.newdata(name="Encryption", value=net["encryption"])
                ed.newdata(name="Type", value=net["type"])
                ed.newdata(name="BSSID", value=net["netid"])
                ed.newdata(name="First Seen", value=net["firsttime"])
                ed.newdata(name="Last Updated", value=net["lastupdt"])

            kml.save(filename)

        except TypeError:
            cprint("[ERROR] Export to KML: No data to export!",
                   "red", attrs=["bold"])