Example #1
0
def main(wf):
    import common
    common.log = log

    log.debug('-' * 40)

    common.set_locale()

    lc, encoding = locale.getlocale()
    log.debug('args : {}'.format(wf.args))
    log.debug('locale : {}  encoding : {}'.format(lc, encoding))

    if len(wf.args):
        query = wf.args[0]
    else:
        query = None

    log.debug('query : {}'.format(query))

    if not query or query in ('0', 'now', 'today'):
        dt = date.today()
    else:
        dt = common.parse_query(query)

    if not dt:  # Didn't understand query
        wf.add_item("Couldn't understand '{}'".format(query),
                    "Use 'datehelp' for help on formatting",
                    valid=False, icon=ICON_ERROR)
        wf.send_feedback()
        return 0
    log.debug('date : {0.year}-{0.month}-{0.day}'.format(dt))
    # get date formats
    for i, fmt in enumerate(common.get_formats()):
        value = unicode(common.date_with_format(dt, fmt), encoding)
        wf.add_item(value,
                    'Copy to clipboard',
                    arg=value,
                    valid=True,
                    uid='date-{:02d}'.format(i),
                    icon='icon.png')
    wf.send_feedback()
    log.debug('finished.')
Example #2
0
def main(wf):
    from docopt import docopt
    import common
    common.log = log

    log.debug('-' * 40)

    common.set_locale()

    args = docopt(__doc__, argv=wf.args)
    log.debug('args : {}'.format(args))
    fmt = args.get('<format>')

    date_formats = wf.settings.get('date_formats', [])

    # add a new format
    if args.get('new'):
        if fmt in common.get_formats():
            wf.add_item("'{}' already exists".format(fmt),
                        'Try something else',
                        valid=False,
                        icon=ICON_WARNING)
        elif not common.format_valid(fmt):
            wf.add_item("'{}' is not a valid date format".format(fmt),
                        "Use 'datehelp' for formatting help",
                        valid=False,
                        icon=ICON_ERROR)
        else:
            value = unicode(common.date_with_format(date.today(), fmt),
                            'utf-8')
            wf.add_item("Save '{}' to custom formats".format(fmt),
                        'e.g. {}'.format(value),
                        valid=True,
                        arg=fmt,
                        icon='icon.png')

        wf.send_feedback()

    # show existing custom formats
    elif args.get('show'):

        if not date_formats:  # warn of no formats
            wf.add_item('You have no custom formats',
                        "Use 'dtadd' to add a custom format",
                        valid=False,
                        icon=ICON_INFO)

        else:  # list custom formats
            for f in date_formats:
                value = unicode(common.date_with_format(date.today(), f),
                                'utf-8')
                wf.add_item(f,
                            'e.g. {}'.format(value),
                            valid=True,
                            arg=f,
                            icon='icon.png')

        wf.send_feedback()

    # add new format
    elif args.get('add'):

        if fmt not in date_formats:
            date_formats.append(fmt)
            wf.settings['date_formats'] = date_formats
            print("'{}' saved".format(fmt))
        else:
            print("'{}' already exists".format(fmt))

    # delete existing format
    elif args.get('delete'):

        if fmt not in date_formats:
            print("'{}' does not exist".format(fmt))
        else:
            date_formats.remove(fmt)
            wf.settings['date_formats'] = date_formats
            print("'{}' deleted".format(fmt))

    # delete custom formats and restore defaults
    elif args.get('reset'):

        wf.settings['date_formats'] = common.get_default_formats()
        log.debug('Formats restored to defaults')
        log.debug(wf.settings['date_formats'])

    log.debug('finished.')
def main(wf):
    from docopt import docopt
    import common
    common.log = log

    log.debug('-' * 40)

    common.set_locale()

    args = docopt(__doc__, argv=wf.args)
    log.debug('args : {}'.format(args))
    fmt = args.get('<format>')

    date_formats = wf.settings.get('date_formats', [])

    # add a new format
    if args.get('new'):
        if fmt in common.get_formats():
            wf.add_item("'{}' already exists".format(fmt),
                        'Try something else',
                        valid=False,
                        icon=ICON_WARNING)
        elif not common.format_valid(fmt):
            wf.add_item("'{}' is not a valid date format".format(fmt),
                        "Use 'datehelp' for formatting help",
                        valid=False,
                        icon=ICON_ERROR)
        else:
            value = unicode(common.date_with_format(date.today(), fmt),
                            'utf-8')
            wf.add_item("Save '{}' to custom formats".format(fmt),
                        'e.g. {}'.format(value),
                        valid=True,
                        arg=fmt,
                        icon='icon.png')

        wf.send_feedback()

    # show existing custom formats
    elif args.get('show'):

        if not date_formats:  # warn of no formats
            wf.add_item('You have no custom formats',
                        "Use 'dtadd' to add a custom format",
                        valid=False,
                        icon=ICON_INFO)

        else:  # list custom formats
            for f in date_formats:
                value = unicode(common.date_with_format(date.today(), f),
                                'utf-8')
                wf.add_item(f,
                            'e.g. {}'.format(value),
                            valid=True,
                            arg=f,
                            icon='icon.png')

        wf.send_feedback()

    # add new format
    elif args.get('add'):

        if fmt not in date_formats:
            date_formats.append(fmt)
            wf.settings['date_formats'] = date_formats
            print("'{}' saved".format(fmt))
        else:
            print("'{}' already exists".format(fmt))

    # delete existing format
    elif args.get('delete'):

        if fmt not in date_formats:
            print("'{}' does not exist".format(fmt))
        else:
            date_formats.remove(fmt)
            wf.settings['date_formats'] = date_formats
            print("'{}' deleted".format(fmt))

    # delete custom formats and restore defaults
    elif args.get('reset'):

        wf.settings['date_formats'] = common.get_default_formats()
        log.debug('Formats restored to defaults')
        log.debug(wf.settings['date_formats'])

    log.debug('finished.')