Example #1
0
def add_duration_to_workflow(func, date_time, workflow, duration_name):
    """
    在 workflow 增加一个区间的开始时间戳和结束时间戳
    :param func: 返回区间函数的 function, 需要4个返回值, 区间开始时间(文本格式), 开始时间戳(ms), 结束时间(文本), 结束时间戳(ms)
    :param date_time: datetime
    :param workflow: workflow 对象
    :param duration_name: 区间名称 day/hour/minute
    :return:
    """
    start_of_duration, duration_start_ms, end_of_duration, duration_end_ms = func(
        date_time)
    duration_total = '%s, %s' % (duration_start_ms, duration_end_ms)
    duration_total1 = '%s %s' % (duration_start_ms, duration_end_ms)

    ms_of_duration_item = workflow_util.add_wf_item(
        workflow,
        title='ms timestamp of %s' % duration_name,
        subtitle='%s %s (start of %s)' %
        (duration_start_ms, start_of_duration, duration_name),
        arg=duration_start_ms)
    ms_of_duration_item.add_modifier(
        'alt',
        subtitle='%s %s (end of %s)' %
        (duration_end_ms, end_of_duration, duration_name),
        arg=duration_end_ms)
    duration_total_item = workflow_util.add_wf_item(
        workflow,
        title='%s [%s, %s]' %
        (duration_name, start_of_duration, end_of_duration),
        subtitle=duration_total,
        arg=duration_total)
    duration_total_item.add_modifier('alt',
                                     subtitle=duration_total1,
                                     arg=duration_total1,
                                     valid=True)
def flow(args):
    """
    读取剪贴板, repeat
    :param args 命令行参数, -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "c:")
    opts_dict = dict(opts)

    # 读取剪贴板内容
    txt = opts_dict.get('-c')
    txt = workflow_util.remove_blank_exclude_newline(txt)
    txt_list = txt.split('\n')
    # 按照 / split, 长度填充为两位
    txt_list = map(lambda row: '-'.join(map(lambda n: n.rjust(2, '0'), row.split(r'/'))), txt_list)
    txt = '\n'.join(txt_list)

    # workflow
    wf = Workflow3()

    # split 得到的结果
    workflow_util.add_wf_item(wf, title='date format result', subtitle="%s" % repr(txt),
                              arg=txt)

    wf.send_feedback()
def flow(args):
    """
    读取剪贴板, repeat
    :param args 命令行参数, -a 重复次数, -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)

    # 重复次数
    count = 1
    if opts_dict.get('-a'):
        try:
            count = int(args[1])
        except ValueError as e:
            pass

    # 读取剪贴板内容
    txt = opts_dict.get('-c')

    repeat_result = txt * count

    # workflow
    wf = Workflow3()

    # split 得到的结果
    workflow_util.add_wf_item(wf, title='repeated result', subtitle="%s * %s" % (repr(txt), count),
                              arg=repeat_result)

    wf.send_feedback()
Example #4
0
def flow(args):
    """
    包装字符串, 按照换行 /n 切分之后包装
    :param args 命令行参数, -a 包装的字符串, 默认 , -c 剪贴板内容
    :return:
    """

    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)

    # 包装的字符串
    wrapper = ''
    if opts_dict.get('-a'):
        wrapper = args[1]

    # 读取剪贴板内容
    txt = opts_dict.get('-c')
    # 移除回车字符
    txt = workflow_util.remove_carriage_return(txt)
    txt_list = txt.split('\n')
    # 使用字符包装
    txt_list = map(lambda x: wrapper + x + wrapper, txt_list)

    # workflow
    wf = Workflow3()

    # result
    result = '\n'.join(txt_list)

    workflow_util.add_wf_item(wf, title=result, subtitle="wrapper with " + wrapper, arg=result, copytext=result)

    wf.send_feedback()
Example #5
0
def flow(args):
    """
    workflow主要逻辑, 从SQL平台查询结果中提取一列数据
    :param args: -a 命令行参数, 1 - 字符串类型, 2 - 数值类型, -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)

    param = args[1] if opts_dict.get('-a') else '2'
    clip_content = opts_dict.get('-c')

    if not clip_content:
        return

    # 根据 \n 拆分成行
    row_list = clip_content.split('\n')

    # 按照列拆分
    row_list = list(map(lambda r: r.split('\t'), row_list))

    # 存储最后的结果
    column_value_list = []
    # 如果行数少于 3 行, 无法确定一行的列数, 取第 1 行的第一个元素和第 2 行的最后一个元素作为结果
    if len(row_list) <= 2:
        column_value_list.append(row_list[0][0])
        if len(row_list) > 1:
            column_value_list.append(row_list[1][-1])
        pass
    else:
        # 一行的列数
        column_count = len(row_list[1])
        # 选中的行的 index
        selected_column_idx = column_count - len(row_list[0])
        for i, row in enumerate(row_list):
            # 第1行取第1列
            if i == 0:
                column_value_list.append(row[0])
            # 列数过少, 跳过
            elif len(row) <= selected_column_idx:
                continue
            # 按照索引获取
            else:
                column_value_list.append(row[selected_column_idx])

    # workflow
    wf = Workflow3()

    if param == '1':
        column_values_str = '\n'.join(map(lambda c: "'" + c + "'", column_value_list))
    else:
        column_values_str = '\n'.join(column_value_list)

    workflow_util.add_wf_item(wf, title=repr(column_values_str), subtitle=r'column values', arg=column_values_str,
                              valid=True)

    wf.send_feedback()
Example #6
0
def flow(args):
    """
    移除 \r, 按照 \n 拆分行, 按照 \t 拆分列, 转换为 markdown 格式的表格
    :param args: -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "c:")
    opts_dict = dict(opts)
    # 读取剪贴板内容
    txt = opts_dict.get('-c')

    if txt is None:
        txt = ''
    txt = txt.replace('\r', '')
    # 拆分成行
    row_list = txt.split('\n')
    # 再拆分一次
    sheet = list(map(lambda r: r.split('\t'), row_list))

    max_width_list = []
    # 得到每一列的最大宽度
    for row in sheet:
        for index, val in enumerate(row):
            if index < len(max_width_list):
                max_width_list[index] = max(len(val), max_width_list[index])
            else:
                max_width_list.append(len(val))

    # 根据长度向右填充字符
    for row in sheet:
        for index, val in enumerate(row):
            # 向右补全字符
            row[index] = val.ljust(max_width_list[index], ' ')

    # workflow
    wf = Workflow3()

    # 没有表头
    md_row_list = list(map(lambda r: '| ' + ' | '.join(r) + ' |', sheet))

    head_separator = '| ' + ' | '.join(
        list(map(lambda l: '-' * l, max_width_list))) + ' |'

    # 插入表头和数据分隔符
    md_row_list.insert(1, head_separator)

    markdown_table = '\n'.join(md_row_list)

    workflow_util.add_wf_item(wf,
                              title=md_row_list[0],
                              subtitle='Markdown Table',
                              arg=markdown_table)

    wf.send_feedback()
Example #7
0
def flow(args):
    """
    读取剪贴板, 移除空白行, 按照 \n 拆分之后对 list 进行分割
    :param args 命令行参数, -a 分片大小, 默认 100 -c 剪贴板内容
    :return:
    """

    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)

    # 默认大小 100
    size = 100
    if opts_dict.get('-a'):
        try:
            size = int(args[1])
        except ValueError as e:
            pass
    # 非法参数, 暂不处理

    # 读取剪贴板内容
    txt = opts_dict.get('-c')
    txt = workflow_util.remove_blank_exclude_newline(txt)
    txt_list = txt.split('\n')
    # 拆分 list
    partition_list = partition_by(txt_list, size)

    # 第一种格式, 按照原有顺序保持一列的格式, 在不同组之间使用多个 \n 分隔
    partition_str_list = map(lambda l: '\n'.join(l), partition_list)
    one_column_result = '\n\n'.join(partition_str_list)

    # 第二种格式, 拆分成多列, 可以直接粘贴到 excel 中
    # 扩充一下最后一个 list 的长度, 方便统一使用 zip 函数处理
    last_partition = partition_list[-1]
    partition_list[-1] = last_partition + [''] * (size - len(last_partition))
    row_list = zip(*partition_list)
    multi_column_result = '\n'.join(map(lambda t: '\t'.join(t), row_list))

    # workflow
    wf = Workflow3()
    workflow_util.add_wf_item(wf,
                              title='split to %s partitions, one column' %
                              len(partition_list),
                              subtitle='total count %s' % len(txt_list),
                              arg=one_column_result)

    workflow_util.add_wf_item(wf,
                              title='split to %s partitions, multi column' %
                              len(partition_list),
                              subtitle='total count %s' % len(txt_list),
                              arg=multi_column_result)

    wf.send_feedback()
Example #8
0
def main():
    # workflow
    wf = Workflow3()
    img_base64 = image_bytes_base64_encode(read_clipboard_image())
    if not img_base64:
        workflow_util.add_wf_item(wf, title='not image in clipboard', subtitle=None, arg=None)
    else:
        image_name = 'clipboard_%s' % time.strftime("%Y%m%d%H%M%S", time.localtime())

        md_image_with_var = build_md_image_with_var(image_name, img_base64)
        md_image_without_var = build_md_image_without_var(image_name, img_base64)

        # 使用变量插入图片
        workflow_util.add_wf_item(wf, title=image_name,
                                  subtitle='md base64 image use var, length %s' % len(md_image_with_var),
                                  arg=md_image_with_var)
        # 不使用变量插入图片
        workflow_util.add_wf_item(wf, title=image_name,
                                  subtitle='md base64 image not var, length %s' % len(md_image_without_var),
                                  arg=md_image_without_var)
        # 仅有图片的base64字符串
        workflow_util.add_wf_item(wf, title=image_name, subtitle='only image base64, length %s' % len(img_base64),
                                  arg=img_base64)

    wf.send_feedback()
Example #9
0
def main():
    # 读取剪贴板内容
    txt = pyperclip.paste()
    # 替换/r/n 为 /n
    txt = txt.replace('\r\n', '\n')
    rows = txt.split('\n')
    record_list = list(map(row_to_record, rows))
    values_sql = ',\n'.join(record_list)
    # workflow
    wf = Workflow3()
    workflow_util.add_wf_item(wf,
                              title="values SQL",
                              subtitle="total count %s" % len(rows),
                              arg=values_sql)
    wf.send_feedback()
Example #10
0
def flow(args):
    """
    workflow主要方法, 替换剪贴板内容 \r\n 为 \n
    :param args: 命令行参数 -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "c:")
    opts_dict = dict(opts)
    txt = remove_return(opts_dict.get('-c'))

    # workflow
    wf = Workflow3()

    workflow_util.add_wf_item(wf, title=repr(txt), subtitle=r'replace \r\n with \n', arg=txt, valid=True)

    wf.send_feedback()
Example #11
0
def flow(args, clip_content):
    """
    生成给定数量的uuid
    :param args: 命令行参数, 数量
    :param clip_content:
    :return:
    """
    count = 1
    if len(args) > 1 and args[1]:
        try:
            arg_count = int(args[1])
            if arg_count > 1:
                count = arg_count
        except ValueError as e:
            pass

    uuid_list = [str(uuid.uuid1()) for i in range(1, count + 1)]

    # workflow
    wf = Workflow3()

    # 不做处理
    uuid_lines = '\n'.join(uuid_list)
    workflow_util.add_wf_item(wf,
                              title='original uuid',
                              subtitle='%s...' % uuid_list[0],
                              arg=uuid_lines)

    # 去掉 -
    simple_uuid_list = list(map(lambda x: x.replace('-', ''), uuid_list))
    simple_uuid_lines = '\n'.join(simple_uuid_list)
    workflow_util.add_wf_item(wf,
                              title='uuid remove -',
                              subtitle='%s...' % simple_uuid_list[0],
                              arg=simple_uuid_lines)

    # 大写
    upper_uuid_list = list(map(lambda x: x.upper(), uuid_list))
    upper_uuid_lines = '\n'.join(upper_uuid_list)
    workflow_util.add_wf_item(wf,
                              title='uuid upper case',
                              subtitle='%s...' % upper_uuid_list[0],
                              arg=upper_uuid_lines)

    # 大写去掉 -
    upper_simple_uuid_list = list(
        map(lambda x: x.replace('-', '').upper(), uuid_list))
    upper_simple_uuid_lines = '\n'.join(upper_simple_uuid_list)
    workflow_util.add_wf_item(wf,
                              title='uuid upper case remove -',
                              subtitle='%s...' % upper_simple_uuid_list[0],
                              arg=upper_simple_uuid_lines)

    wf.send_feedback()
Example #12
0
def flow(args):
    """
    读取剪贴板, split 参数
    :param args 命令行参数, -a 切分的分隔符, 默认 , -c 剪贴板的内容
    :return:
    """

    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)

    # split的字符串, 默认值 ,
    splitter = ',' if not opts_dict.get('-a') else opts_dict.get('-a')

    # 读取剪贴板内容
    txt = opts_dict.get('-c')
    # 移除空白字符
    txt = workflow_util.remove_blank(txt)
    # 移除两端的括号
    txt = workflow_util.un_wrap_brackets(txt)

    txt_list = txt.split(splitter)

    # workflow
    wf = Workflow3()

    split_result = '\n'.join(txt_list)

    # split 得到的结果
    workflow_util.add_wf_item(wf,
                              title=split_result,
                              subtitle="splitter with " + splitter,
                              arg=split_result)

    # 移除两端的单引号
    split_unwrap_list = map(workflow_util.un_wrap_quote, txt_list)
    split_unwrap_result = '\n'.join(split_unwrap_list)
    workflow_util.add_wf_item(wf,
                              title=split_unwrap_result,
                              subtitle="splitter with %s, unwrap quote" %
                              splitter,
                              arg=split_unwrap_result)

    wf.send_feedback()
Example #13
0
def flow(args):
    """
    workflow主要方法, 移除开头的缩进
    :param args: 命令行参数 -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "c:")
    opts_dict = dict(opts)
    txt = un_tab(opts_dict.get('-c'))

    # workflow
    wf = Workflow3()

    workflow_util.add_wf_item(wf,
                              title=repr(txt),
                              subtitle=r'untab result',
                              arg=txt,
                              valid=True)

    wf.send_feedback()
Example #14
0
def flow(args, clip_content):
    """
    读取剪贴板, 移除空白行, 排序
    :param args: 命令行参数, 1 - 字符串类型, 2 - 数值类型
    :param clip_content: 剪贴板内容
    :return:
    """
    # 参数
    param = args[1] if len(args) > 1 else None
    if param not in ('1', '2'):
        param = None

    if param is not None:
        param = int(param)

    # 读取剪贴板内容
    txt = clip_content
    txt = workflow_util.remove_blank_exclude_newline(txt)
    txt_list = txt.split('\n')

    param = analysis_operation(txt_list, param)

    # workflow
    wf = Workflow3()

    if param == 1:
        # 字符串排序
        txt_list.sort()
    elif param == 2:
        # 数值排序
        txt_list.sort(key=int)

    # 增加括号
    sorted_list = '\n'.join(txt_list)
    workflow_util.add_wf_item(wf, title=repr(sorted_list), subtitle='sorted_list', arg=sorted_list)

    wf.send_feedback()
Example #15
0
def flow(args):
    """
    计算给定两个日期之间的天数
    :param args: 命令行参数, -a 最多两个日期, 空格分隔
    :return:
    """

    # workflow
    start_date, end_date = None, None
    wf = Workflow3()
    valid = True

    if len(args) < 2 or not args[1]:
        valid = False
    else:
        arg_arr = workflow_util.strip(re.sub(r'\s+', ' ', args[1])).split(' ')
        wf.logger.info('workflow_days arg_arr %s' % arg_arr)
        start_date = parse_date(arg_arr[0])
        end_date = parse_date(arg_arr[1]) if len(arg_arr) > 1 else datetime.now().date()
        if not start_date or not end_date:
            valid = False

    if not valid:
        workflow_util.add_wf_item(wf, title='illegal args %s' % args[1:])
    else:
        duration_days = abs((start_date - end_date).days)
        start_date_str = start_date.strftime('%Y-%m-%d')
        end_date_str = end_date.strftime('%Y-%m-%d')

        workflow_util.add_wf_item(wf, title='%s days' % duration_days, subtitle='days between (%s, %s]' % (
            start_date_str, end_date_str), copytext=duration_days, valid=True,
                                  arg=duration_days)

        include_duration_days = duration_days + 1
        workflow_util.add_wf_item(wf, title='%s days' % include_duration_days, subtitle='days between [%s, %s]' % (
            start_date_str, end_date_str), copytext=include_duration_days, valid=True,
                                  arg=include_duration_days)

    wf.send_feedback()
Example #16
0
def flow(args):
    """
    读取剪贴板, 或者从命令行获取参数
    命令行可以指定操作类型(0, 1, 2, 3), 此时时间数据从剪贴板获取
    命令行不指定操作类型时时间数据优先从命令行获取, 命令行为空则使用剪贴板数据
    :param args 命令行参数 包含2个参数, -a 程序的参数, -c 剪贴板内容
    :return:
    """
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)
    operation, txt_content = analysis_operation_and_data(
        opts_dict.get('-a'), opts_dict.get('-c'))

    # workflow
    wf = Workflow3()
    if operation == '0':
        timestamp = time.time()
        timestamp_s = int(timestamp)
        timestamp_ms = int(timestamp * 1000)
        now = time.localtime(timestamp)

        date_str_1 = time.strftime("%Y-%m-%d", now)
        date_str_2 = time.strftime("%Y%m%d", now)

        current_second_1 = time.strftime(DATE_TIME_FORMAT, now)
        current_second_2 = time.strftime("%Y%m%d%H%M%S", now)

        # yyyy-MM-dd 格式
        current_date_item = workflow_util.add_wf_item(wf,
                                                      title='current date',
                                                      subtitle=date_str_1,
                                                      arg=date_str_1,
                                                      valid=True)
        # 使用按键切换格式
        # yyyyMMdd 格式
        current_date_item.add_modifier('alt',
                                       subtitle=date_str_2,
                                       arg=date_str_2,
                                       valid=True)
        # yyyy-MM-dd HH:mm:ss 格式
        current_second_item = workflow_util.add_wf_item(
            wf,
            title='current second',
            subtitle=current_second_1,
            arg=current_second_1)
        # yyyyMMddHHmmss 格式
        current_second_item.add_modifier('alt',
                                         subtitle=current_second_2,
                                         arg=current_second_2,
                                         valid=True)

        # 当前时间戳 ms
        current_timestamp_item = workflow_util.add_wf_item(
            wf,
            title='current timestamp',
            subtitle='%s(ms)' % timestamp_ms,
            arg=timestamp_ms)

        # 当天起止时间 ms
        add_duration_to_workflow(duration_of_day, now, wf, "day")

        # 小时的起止时间 ms
        add_duration_to_workflow(duration_of_hour, now, wf, "hour")

        # 分钟的起止时间 ms
        add_duration_to_workflow(duration_of_minute, now, wf, "minute")

        # s 时间戳
        current_timestamp_item.add_modifier('alt',
                                            subtitle='%s(s)' % timestamp_s,
                                            arg=timestamp_s,
                                            valid=True)

    elif operation == '1':
        # ms 时间戳转换为时间
        time_from_ms = parse_datetime_with_timestamp_ms(txt_content)
        time_str_from_ms = format_time(
            time_from_ms
        ) if time_from_ms else 'Invalid ms timestamp "%s"' % txt_content

        # s 时间戳转换为时间
        time_from_s = parse_datetime_with_timestamp_s(txt_content)
        time_str_from_s = format_time(
            time_from_s
        ) if time_from_s else 'Invalid second timestamp "%s"' % txt_content

        workflow_util.add_wf_item(
            wf,
            title=time_str_from_ms,
            subtitle=('time parse from ms timestamp "%s"' %
                      txt_content) if time_from_ms else '',
            arg=time_str_from_ms,
            valid=(time_from_ms is not None))
        # 当天起止时间 ms
        add_duration_to_workflow(duration_of_day, time_from_ms, wf, "day")

        # 小时的起止时间 ms
        add_duration_to_workflow(duration_of_hour, time_from_ms, wf, "hour")

        # 分钟的起止时间 ms
        add_duration_to_workflow(duration_of_minute, time_from_ms, wf,
                                 "minute")

        workflow_util.add_wf_item(
            wf,
            title=time_str_from_s,
            subtitle='time parse from second timestamp "%s"' %
            txt_content if time_from_s else '',
            arg=time_str_from_s,
            valid=(time_from_s is not None))
    elif operation == '2':
        # 时间转换为标准格式
        date_time = parse_time(txt_content)

        if not date_time:
            workflow_util.add_wf_item(wf,
                                      title='Invalid datetime str "%s"' %
                                      txt_content,
                                      subtitle='',
                                      arg=None,
                                      valid=False)
        else:
            timestamp_s = parse_timestamp_s(date_time)
            date_time_str = format_time(date_time)

            # ms 时间戳 ms 值 000
            second_ms_timestamp_item = workflow_util.add_wf_item(
                wf,
                title='ms timestamp of second, "%s"' % date_time_str,
                subtitle='%s (start of second)' % (timestamp_s * 1000),
                arg=timestamp_s * 1000)

            # ms 时间戳 ms 值 999
            second_ms_timestamp_item.add_modifier(
                'alt',
                subtitle='%s (end of second)' % (timestamp_s * 1000 + 999),
                arg=timestamp_s * 1000 + 999,
                valid=True)

            # 当天起止时间 ms
            add_duration_to_workflow(duration_of_day, date_time, wf, "day")

            # 小时的起止时间 ms
            add_duration_to_workflow(duration_of_hour, date_time, wf, "hour")

            # 分钟的起止时间 ms
            add_duration_to_workflow(duration_of_minute, date_time, wf,
                                     "minute")

            # 秒级时间戳
            workflow_util.add_wf_item(wf,
                                      title='second timestamp, "%s"' %
                                      date_time_str,
                                      subtitle=timestamp_s,
                                      arg=timestamp_s)
    elif operation == '3':
        # 批量将时间戳转换为字符串
        date_time_list = parse_datetime_with_ts_ms_batch(txt_content)
        # 使用逗号分隔
        date_time_sec_str = ', '.join(
            map(lambda dt: time.strftime(DATE_TIME_FORMAT, dt),
                date_time_list))
        workflow_util.add_wf_item(wf,
                                  title='parse million second datetime batch',
                                  subtitle=date_time_sec_str,
                                  arg=date_time_sec_str)
        # 使用换行分割
        date_time_sec_str_lines = '\n'.join(
            map(lambda dt: time.strftime(DATE_TIME_FORMAT, dt),
                date_time_list))
        workflow_util.add_wf_item(
            wf,
            title='parse million second datetime batch lines',
            subtitle=date_time_sec_str_lines,
            arg=date_time_sec_str_lines)

    wf.send_feedback()
Example #17
0
def flow(args):
    """
    读取剪贴板, 移除空白行, 使用给定的参数进行 join
    :param args: -a 指定操作类型, 1 - 字符串类型, 2 - 数值类型, 其他参数作为包装字符串的字符 -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)
    param = '2' if not opts_dict.get('-a') else opts_dict.get('-a')

    # 读取剪贴板内容
    txt = opts_dict.get('-c')
    txt = workflow_util.remove_blank_exclude_newline(txt)
    txt_list = txt.split('\n')

    # workflow
    wf = Workflow3()

    if param == '1':
        # 字符串, 追加单引号
        txt_list = workflow_util.wrap_with_symbol(txt_list)
    elif param == '2':
        # 不需要添加引号
        pass
    else:
        txt_list = workflow_util.wrap_with_symbol(txt_list, param)

    # 增加括号
    with_brackets = '(' + ',\n'.join(txt_list) + ')'
    workflow_util.add_wf_item(wf,
                              title=with_brackets,
                              subtitle='with_brackets',
                              arg=with_brackets)

    # 去掉换行符
    one_line = ', '.join(txt_list)
    one_line_item = workflow_util.add_wf_item(wf,
                                              title=one_line,
                                              subtitle='one_line',
                                              arg=one_line)
    # 不加空格
    one_line_no_space = ','.join(txt_list)
    one_line_item.add_modifier('alt',
                               subtitle='one_line_no_space',
                               arg=one_line_no_space,
                               valid=True)

    # 去掉换行增加括号
    one_line_with_brackets = '(' + ', '.join(txt_list) + ')'
    one_line_with_brackets_item = workflow_util.add_wf_item(
        wf,
        title=one_line_with_brackets,
        subtitle='one_line_with_brackets',
        arg=one_line_with_brackets)
    # 不加空格
    one_line_with_brackets_no_space = '(' + ','.join(txt_list) + ')'
    one_line_with_brackets_item.add_modifier(
        'alt',
        subtitle='one_line_with_brackets_no_space',
        arg=one_line_with_brackets_no_space,
        valid=True)

    # 去重, 保留原始顺序
    txt_set = list(set(txt_list))
    txt_set.sort(key=txt_list.index)

    # 只进行去重, 换行, 不增加其他分隔符
    distinct_with_line = '\n'.join(txt_set)
    workflow_util.add_wf_item(wf,
                              title=distinct_with_line,
                              subtitle='distinct_with_line',
                              arg=distinct_with_line)

    # 增加括号
    with_brackets_distinct = '(' + ',\n'.join(txt_set) + ')'
    workflow_util.add_wf_item(wf,
                              title=with_brackets_distinct,
                              subtitle='distinct_with_brackets',
                              arg=with_brackets_distinct)
    # 去掉换行符
    one_line_distinct = ', '.join(txt_set)
    one_line_distinct_item = workflow_util.add_wf_item(
        wf,
        title=one_line_distinct,
        subtitle='distinct_one_line',
        arg=one_line_distinct)
    # 不加空格
    one_line_distinct_no_space = ','.join(txt_set)
    one_line_distinct_item.add_modifier('alt',
                                        subtitle='distinct_one_line_no_space',
                                        arg=one_line_distinct_no_space,
                                        valid=True)

    # 去掉换行增加括号
    one_line_with_brackets_distinct = '(' + ', '.join(txt_set) + ')'
    one_line_with_brackets_distinct_item = workflow_util.add_wf_item(
        wf,
        title=one_line_with_brackets_distinct,
        subtitle='distinct_one_line_with_brackets',
        arg=one_line_with_brackets_distinct)
    # 不加空格
    one_line_with_brackets_distinct_no_space = '(' + ','.join(txt_set) + ')'
    one_line_with_brackets_distinct_item.add_modifier(
        'alt',
        subtitle='distinct_one_line_with_brackets_no_space',
        arg=one_line_with_brackets_distinct_no_space,
        valid=True)

    wf.send_feedback()
Example #18
0
def flow(args):
    """
    读取剪贴板, 移除 \r, 按照 \n 拆分行, 按照 \t 拆分列, 使用给定的参数对所有列进行 join
    :param args 命令行参数, -a 1-字符串, 2-数值, -c 剪贴板内容
    :return:
    """
    # 参数
    opts, _ = getopt.getopt(args, "a:c:")
    opts_dict = dict(opts)

    # 读取剪贴板内容
    param = opts_dict.get('-a')
    txt = opts_dict.get('-c')

    if txt is None:
        txt = ''
    txt = txt.replace('\r', '')
    # 拆分成行
    row_list = txt.split('\n')
    # 再拆分一次
    sheet = list(map(lambda row: row.split('\t'), row_list))
    # 移除每一行的空数据
    sheet = list(map(workflow_util.remove_blank_element, sheet))

    # workflow
    wf = Workflow3()

    if param == '1':
        # 字符串, 追加单引号
        sheet = list(
            map(lambda row: workflow_util.wrap_with_symbol(row), sheet))
    elif param == '2':
        # 不需要添加引号
        pass
    else:
        sheet = list(
            map(lambda row: workflow_util.wrap_with_symbol(row, param), sheet))

    # 转置处理
    sheet_transpose = transpose_2d(sheet)

    # 第一行增加左括号
    sheet[0] = list(map(lambda s: '(' + s, sheet[0]))
    # 最后一行增加右括号
    for i in range(0, len(sheet)):
        for j in range(0, len(sheet[i])):
            # 每列的最后一行增加右括号
            if i == len(sheet) - 1 or j + 1 > len(sheet[i + 1]):
                sheet[i][j] = sheet[i][j] + ')'
            else:
                # 其他的增加逗号
                sheet[i][j] = sheet[i][j] + ','

    # 按照原有excel行列格式处理
    excel_format = '\n'.join(map(lambda txt_list: '\t'.join(txt_list), sheet))
    workflow_util.add_wf_item(wf,
                              title=excel_format,
                              subtitle='excel_format',
                              arg=excel_format)

    transpose_result = '\n'.join(
        map(lambda txt_list: ','.join(txt_list), sheet_transpose))
    workflow_util.add_wf_item(wf,
                              title=transpose_result,
                              subtitle='transpose_result',
                              arg=transpose_result)

    #
    # # 为了防止有特殊字符导致粘贴到 excel 中不能正确分列, 每一个单元格使用双引号处理
    # # 增加括号
    # with_brackets = '\t'.join(map(lambda txt_list: '"(' + ',\n'.join(txt_list) + ')"', sheet))
    # workflow_util.add_wf_item(wf, title=with_brackets, subtitle='with_brackets', copytext=with_brackets)
    # # 去掉换行符
    # one_line = '\t'.join(map(lambda txt_list: '"' + ', '.join(txt_list) + '"', sheet))
    # workflow_util.add_wf_item(wf, title=one_line, subtitle='one_line', copytext=one_line)
    # # 去掉换行增加括号
    # one_line_with_brackets = '\t'.join(map(lambda txt_list: '"(' + ', '.join(txt_list) + ')"', sheet))
    # workflow_util.add_wf_item(wf, title=one_line_with_brackets, subtitle='one_line_with_brackets',
    #                           copytext=one_line_with_brackets)
    #
    # # joiner 不加空格
    # # 去掉换行符
    # one_line_no_space = '\t'.join(map(lambda txt_list: '"' + ','.join(txt_list) + '"', sheet))
    # workflow_util.add_wf_item(wf, title=one_line_no_space, subtitle='one_line_no_space', copytext=one_line_no_space)
    # # 去掉换行增加括号
    # one_line_with_brackets_no_space = '\t'.join(map(lambda txt_list: '"(' + ','.join(txt_list) + ')"', sheet))
    # workflow_util.add_wf_item(wf, title=one_line_with_brackets_no_space, subtitle='one_line_with_brackets_no_space',
    #                           copytext=one_line_with_brackets_no_space)

    wf.send_feedback()