Beispiel #1
0
def get_update_labels(repos_sn, label_texts, label_type):

    found_labels = find_labels(repos_sn, label_texts, label_type)

    now = datetime.utcnow();

    LabelSet = dset(ts_label)

    new_labels = LabelSet()
    for text in label_texts:
        if text in found_labels:
            continue

        label = ts_label(repos_sn=repos_sn, label_sn=ts_label_seqno())
        label.label=text
        label.type = label_type
        label.created_ts = now
        label.updated_ts = now

        new_labels._add(label)

    dmerge(new_labels)

    for item in new_labels:
        found_labels[item.label] = item.label_sn

    return found_labels
Beispiel #2
0
def update_category(repos_sn, json_arg):
    """  """

    now = datetime.utcnow();

    text = json_arg['text'];

    print(repos_sn, text)

    labels = []
    level_1 = ''
    for ln in re.split('[\n\r]+', text):
        m = re.search('(^\s+)', ln)
        sp_num = m.end() if m else 0

        if sp_num > 0:
            labels.append(level_1 + '/' + ln.strip())
        else:
            level_1 = ln.strip()
            labels.append(level_1)


    old_labels = get_repos_categories(repos_sn)

    old_set = set(old_labels.keys())
    new_set = set(labels)

    lbl_com_set = old_set.intersection(new_set)
    lbl_ins_set = new_set.difference(lbl_com_set)


    new_labels = get_update_labels(repos_sn, lbl_ins_set, 'C')

    sn_com_list = set(old_labels[n]['label_sn'] for n in lbl_com_set)
    sn_ins_list = set(int(new_labels[n]) for n in lbl_ins_set)


    orig_categories = drecall(ts_repos_categories(repos_sn=repos_sn))



    categories = ts_repos_categories(repos_sn=repos_sn)
    categories.labels = sorted(int(x) for x in sn_ins_list.union(sn_com_list))
    categories.updated_ts = now

    dmerge(categories, orig_categories)

    ##-----------------------------------------------------------------------
    ## 保留文本指定的顺序

    orders = {}
    for i, lbl in enumerate(labels):
        orders[lbl] = i + 1

    dbc << """\
    WITH s AS (
      SELECT UNNEST(labels) AS label_sn
      FROM ts_repos_categories WHERE repos_sn = %(repos_sn)s
    )
    SELECT t.*
    FROM ts_label t JOIN s USING(label_sn)
    """
    dbc << dict(repos_sn=repos_sn)

    LabelSet = dset(ts_label)

    old_label_set = LabelSet(dbc)

    label_set = LabelSet()
    for r in old_label_set:
        r = ts_label(r)
        r.props = copy.deepcopy(r.props) # 需要复制,否则修改的是旧的
        label_set._add(r)

    for r in label_set:
        ordnum = orders[r.label]
        if r.props:
            if 'order' in r.props and r.props['order'] == ordnum:
                continue

            r.props['order'] = ordnum
        else:
            pass
            r.props = {'order': ordnum}
        r.updated_ts = now

    dmerge(label_set, old_label_set)