Exemplo n.º 1
0
def run_command(db, cmd, args):
    func = get_command_by_name(db, cmd)
    try:
        if commands[func].locking:
            db.execute(u'begin')
        commands[func](db, args)
        if commands[func].locking:
            db.execute(u'commit')
        current_sheet = dbutil.get_current_sheet(db)
        if not commands[func].read_only:
            if db.config.has_option(current_sheet,
                                    'reporting_url') and db.config.has_option(
                                        'auth', 'username'):
                current_info = dbutil.get_active_info(db, current_sheet)
                report_to_url(
                    db.config.get(current_sheet, 'reporting_url'),
                    db.config.get('auth', 'username'),
                    current_info[1] if current_info else '',
                    (datetime.utcnow() - timedelta(seconds=current_info[0])
                     ).strftime("%Y-%m-%d %H:%M:%S") if current_info else '',
                    cmd, args)
            elif db.config.has_option(current_sheet, 'reporting_url'):
                print "Please specify a username in your configuration."
    except:
        if commands[func].locking:
            db.execute(u'rollback')
        raise
Exemplo n.º 2
0
def run_command(db, cmd, args):
    func = get_command_by_name(db, cmd)
    try:
        if commands[func].locking:
            db.execute(u'begin')
        commands[func](db, args)
        if commands[func].locking:
            db.execute(u'commit')
        current_sheet = dbutil.get_current_sheet(db)
        if not commands[func].read_only:
            if db.config.has_option(current_sheet, 'reporting_url'):
                current_info = dbutil.get_active_info(db, current_sheet)
                status_string = dbutil.get_status_string(db,
                                                         current_sheet,
                                                         exclude=['billable'])
                report_to_url(
                    db.config.get(current_sheet,
                                  'reporting_url'), None, status_string,
                    (datetime.utcnow() - timedelta(seconds=current_info[0])
                     ).strftime("%Y-%m-%d %H:%M:%S") if current_info else '',
                    datetime.now() - timedelta(seconds=current_info[0])
                    if current_info else timedelta(seconds=0),
                    current_info[0] if current_info else 0, cmd, args)
    except Exception:
        import traceback
        traceback.print_exc()
        if commands[func].locking:
            db.execute(u'rollback')
        raise
Exemplo n.º 3
0
def now(db, args):
    parser = OptionParser(usage='''usage: %prog now [TIMESHEET]

Print the current sheet, whether it's active, and if so, how long it
has been active and what notes are associated with the current
period.

If a specific timesheet is given, display the same information for that
timesheet instead.''')
    parser.add_option('-s',
                      '--simple',
                      dest='simple',
                      action='store_true',
                      help='Only display the name \
of the current timesheet.')
    parser.add_option('-n',
                      '--notes',
                      dest='notes',
                      action='store_true',
                      help='Only display the notes \
associated with the current period.')
    opts, args = parser.parse_args(args=args)

    if opts.simple:
        print dbutil.get_current_sheet(db)
        return

    if args:
        sheet = cmdutil.complete(dbutil.get_sheet_names(db), args[0],
                                 'timesheet')
    else:
        sheet = dbutil.get_current_sheet(db)

    entry_count = dbutil.get_entry_count(db, sheet)
    if entry_count == 0:
        raise SystemExit, '%(prog)s: error: sheet is empty. For program \
usage, see "%(prog)s --help".' % {
            'prog': os.path.basename(sys.argv[0])
        }

    running = dbutil.get_active_info(db, sheet)
    notes = ''
    if running is None:
        active = 'not active'
    else:
        duration = str(timedelta(seconds=running[0]))
        if running[1]:
            notes = running[1].rstrip('.')
            active = '%s (%s)' % (duration, notes)
        else:
            active = duration
    if opts.notes:
        print notes
    else:
        print '%s: %s' % (sheet, active)
Exemplo n.º 4
0
def in_(db, args, extra=None):
    parser = OptionParser(usage='''usage: %prog in [NOTES...]

Start the timer for the current timesheet. Must be called before out.
Notes may be specified for this period. This is exactly equivalent to
%prog in; %prog alter''')
    parser.add_option('-s', '--switch', dest='switch', type='string',
                      help='Switch to another timesheet before \
starting the timer.')
    parser.add_option('-d', '--db', dest='database', type='string',
                      help='Switch to another databae from config file')
    parser.add_option('-o', '--out', dest='out', action='store_true',
                      default=False, help='''Clocks out before clocking \
in''')
    parser.add_option('-a', '--at', dest='at', type='string',
                      help='''Set time of clock-in''')
    parser.add_option('-r', '--resume', dest='resume', action='store_true',
                      default=False, help='''Clocks in with status of \
last active period''')
    opts, args = parser.parse_args(args=args)
    if opts.switch:
        sheet = opts.switch
        switch(db, [sheet])
    else:
        sheet = dbutil.get_current_sheet(db)
    if opts.resume and args:
        parser.error('"--resume" already sets a note, and is incompatible \
with arguments.')
    timestamp = cmdutil.parse_date_time_or_now(opts.at)
    if opts.out:
        clock_out(db, timestamp=timestamp)
    running = dbutil.get_active_info(db, sheet)
    if running is not None:
        raise SystemExit, 'error: timesheet already active'
    most_recent_clockout = dbutil.get_most_recent_clockout(db, sheet)
    description = u' '.join(args) or None
    if most_recent_clockout:
        (previous_timestamp, previous_description) = most_recent_clockout
        if timestamp < previous_timestamp:
            raise SystemExit, \
                  'error: time periods could end up overlapping'
        if opts.resume:
            description = previous_description
    db.execute(u'''
    insert into entry (
        sheet, start_time, description, extra
    ) values (?,?,?,?)
    ''', (sheet, timestamp, description, extra))
Exemplo n.º 5
0
def now(db, args):
    parser = OptionParser(usage='''usage: %prog now [TIMESHEET]

Print the current sheet, whether it's active, and if so, how long it
has been active and what notes are associated with the current
period.

If a specific timesheet is given, display the same information for that
timesheet instead.''')
    parser.add_option('-s', '--simple', dest='simple',
                      action='store_true', help='Only display the name \
of the current timesheet.')
    parser.add_option('-n', '--notes', dest='notes',
                      action='store_true', help='Only display the notes \
associated with the current period.')
    opts, args = parser.parse_args(args=args)

    if opts.simple:
        print dbutil.get_current_sheet(db)
        return

    if args:
        sheet = cmdutil.complete(dbutil.get_sheet_names(db), args[0],
                                 'timesheet')
    else:
        sheet = dbutil.get_current_sheet(db)

    entry_count = dbutil.get_entry_count(db, sheet)
    if entry_count == 0:
        raise SystemExit, '%(prog)s: error: sheet is empty. For program \
usage, see "%(prog)s --help".' % {'prog': os.path.basename(sys.argv[0])}

    running = dbutil.get_active_info(db, sheet)
    notes = ''
    if running is None:
        active = 'not active'
    else:
        duration = str(timedelta(seconds=running[0]))
        if running[1]:
            notes = running[1].rstrip('.')
            active = '%s (%s)' % (duration, notes)
        else:
            active = duration
    if opts.notes:
        print notes
    else:
        print '%s: %s' % (sheet, active)
Exemplo n.º 6
0
def now(db, timesheet=None, simple=False, notes=False):
    """Show the status of the current timesheet

    Usage: t (now | info) [options] [<timesheet>]

    Print the current sheet, whether it's active, and if so, how long it
    has been active and what notes are associated with the current
    period.

    If a specific timesheet is given, display the same information for that
    timesheet instead.

    Options:
      -s, --simple  Only display the name of the current timesheet.
      -n, --notes   Only display the notes associated with the current period.
    """
    if simple:
        print(dbutil.get_current_sheet(db))
        return

    if timesheet:
        sheet = cmdutil.complete(dbutil.get_sheet_names(db), timesheet,
                                 'timesheet')
    else:
        sheet = dbutil.get_current_sheet(db)

    entry_count = dbutil.get_entry_count(db, sheet)
    if entry_count == 0:
        raise SystemExit('%(prog)s: error: sheet is empty. For program \
usage, see "%(prog)s --help".' % {'prog': os.path.basename(sys.argv[0])})

    running = dbutil.get_active_info(db, sheet)
    _notes = ''
    if running is None:
        active = 'not active'
    else:
        duration = str(timedelta(seconds=running[0]))
        if running[1]:
            _notes = running[1].rstrip('.')
            active = '%s (%s)' % (duration, _notes)
        else:
            active = duration
    if notes:
        print(_notes)
    else:
        print('%s: %s' % (sheet, active))
Exemplo n.º 7
0
def in_(db, description='', switch=None, out=False, at=None, resume=False,
        extra=None):
    """Start the timer for the current timesheet

    Usage: t (in | start) [options] [<description>...]

    Start the timer for the current timesheet. Must be called before out.
    Notes may be specified for this period. This is exactly equivalent to
    `t in; t alter`.

    Options:
      -s <timesheet>, --switch <timesheet>
                    Switch to another timesheet before starting the timer.
      -o, --out     Clock out before clocking in.
      -a <time>, --at <time>
                    Set time of clock-in.
      -r, --resume  Clock in with description of last active period.
    """
    sheet = switch
    if sheet:
        commands['switch'](db, timesheet=sheet)
    else:
        sheet = dbutil.get_current_sheet(db)
    if resume and description:
        raise SystemExit('"--resume" already sets a description')
    timestamp = cmdutil.parse_date_time_or_now(at)
    if out:
        clock_out(db, timestamp=timestamp)
    running = dbutil.get_active_info(db, sheet)
    if running is not None:
        raise SystemExit('error: timesheet already active')
    most_recent_clockout = dbutil.get_most_recent_clockout(db, sheet)
    description = ' '.join(description) or None
    if most_recent_clockout:
        (previous_timestamp, previous_description) = most_recent_clockout
        if timestamp < previous_timestamp:
            raise SystemExit('error: time periods could end up overlapping')
        if resume:
            description = previous_description
    db.execute('''
    insert into entry (
        sheet, start_time, description, extra
    ) values (?,?,?,?)
    ''', (sheet, timestamp, description, extra))
Exemplo n.º 8
0
def run_command(db, cmd, args):
    func = get_command_by_name(db, cmd)
    try:
        if commands[func].locking:
            db.execute(u'begin')
        commands[func](db, args)
        if commands[func].locking:
            db.execute(u'commit')
        current_sheet = dbutil.get_current_sheet(db)
        if not commands[func].read_only:
            if db.config.has_option(
                        current_sheet,
                        'reporting_url'
                    ):
                current_info = dbutil.get_active_info(db, current_sheet)
                status_string = dbutil.get_status_string(
                    db,
                    current_sheet,
                    exclude=['billable']
                )
                report_to_url(
                        db.config.get(current_sheet, 'reporting_url'),
                        None,
                        status_string,
                        (
                            datetime.utcnow()
                            - timedelta(seconds=current_info[0])
                        ).strftime("%Y-%m-%d %H:%M:%S")
                        if current_info else '',
                        datetime.now() - timedelta(seconds=current_info[0]) if current_info else timedelta(seconds=0),
                        current_info[0] if current_info else 0,
                        cmd,
                        args
                    )
    except Exception:
        import traceback
        traceback.print_exc()
        if commands[func].locking:
            db.execute(u'rollback')
        raise
Exemplo n.º 9
0
def run_command(db, cmd, args):
    func = get_command_by_name(db, cmd)
    try:
        if commands[func].locking:
            db.execute(u'begin')
        commands[func](db, args)
        if commands[func].locking:
            db.execute(u'commit')
        current_sheet = dbutil.get_current_sheet(db)
        if not commands[func].read_only:
            if db.config.has_option(
                        current_sheet,
                        'reporting_url'
                    ) and db.config.has_option(
                        'auth',
                        'username'
                    ):
                current_info = dbutil.get_active_info(db, current_sheet)
                report_to_url(
                        db.config.get(current_sheet, 'reporting_url'),
                        db.config.get('auth', 'username'),
                        current_info[1] if current_info else '',
                        (
                            datetime.utcnow()
                            - timedelta(seconds=current_info[0])
                        ).strftime("%Y-%m-%d %H:%M:%S")
                        if current_info else '',
                        cmd,
                        args
                    )
            elif db.config.has_option(current_sheet, 'reporting_url'):
                print "Please specify a username in your configuration."
    except:
        if commands[func].locking:
            db.execute(u'rollback')
        raise
Exemplo n.º 10
0
def in_(db, args, extra=None):
    parser = OptionParser(usage='''usage: %prog in [NOTES...]

Start the timer for the current timesheet. Must be called before out.
Notes may be specified for this period. This is exactly equivalent to
%prog in; %prog alter''')
    parser.add_option('-s',
                      '--switch',
                      dest='switch',
                      type='string',
                      help='Switch to another timesheet before \
starting the timer.')
    parser.add_option('-o',
                      '--out',
                      dest='out',
                      action='store_true',
                      default=False,
                      help='''Clocks out before clocking \
in''')
    parser.add_option('-a',
                      '--at',
                      dest='at',
                      type='string',
                      help='''Set time of clock-in''')
    parser.add_option('-r',
                      '--resume',
                      dest='resume',
                      action='store_true',
                      default=False,
                      help='''Clocks in with status of \
last active period''')
    opts, args = parser.parse_args(args=args)
    if opts.switch:
        sheet = opts.switch
        switch(db, [sheet])
    else:
        sheet = dbutil.get_current_sheet(db)
    if opts.resume and args:
        parser.error('"--resume" already sets a note, and is incompatible \
with arguments.')
    timestamp = cmdutil.parse_date_time_or_now(opts.at)
    if opts.out:
        clock_out(db, timestamp=timestamp)
    running = dbutil.get_active_info(db, sheet)
    if running is not None:
        raise SystemExit, 'error: timesheet already active'
    most_recent_clockout = dbutil.get_most_recent_clockout(db, sheet)
    description = u' '.join(args) or None
    if most_recent_clockout:
        (previous_timestamp, previous_description) = most_recent_clockout
        if timestamp < previous_timestamp:
            raise SystemExit, \
                  'error: time periods could end up overlapping'
        if opts.resume:
            description = previous_description
    db.execute(
        u'''
    insert into entry (
        sheet, start_time, description, extra
    ) values (?,?,?,?)
    ''', (sheet, timestamp, description, extra))
Exemplo n.º 11
0
def in_(db, args, extra=None, change=False):
    parser = optparse.OptionParser(usage='''usage: %prog in [NOTES...]

Start the timer for the current timesheet. Must be called before out.
Notes may be specified for this period. This is exactly equivalent to
%prog in; %prog alter''')
    parser.add_option('-s', '--switch', dest='switch', type='string',
            help='Switch to another timesheet before starting the timer.'
            )
    parser.add_option('-o', '--out', dest='out', action='store_true',
            default=False, help='Clocks out before clocking in'
            )
    parser.add_option('-a', '--at', dest='at', type='string',
            help='Set time of clock-in'
            )
    parser.add_option('-t', '--ticket', dest='ticket_number', type='string',
            default=None, help='Set ticket number'
            )
    parser.add_option('--billable', dest='billable', action='store_true',
            default=True, help='Marks entry as billable'
            )
    parser.add_option('--non-billable', dest='billable', action='store_false',
            default=True, help='Marks entry as non-billable'
            )
    cmdutil.add_user_specified_attributes(db, parser)
    opts, args = parser.parse_args(args=args)
    metadata = cmdutil.collect_user_specified_attributes(db, opts)
    metadata['billable'] = 'yes' if opts.billable else 'no'
    if opts.ticket_number:
        metadata['ticket_number'] = opts.ticket_number
    if opts.switch:
        sheet = opts.switch
        switch(db, [sheet])
    else:
        sheet = dbutil.get_current_sheet(db)
    timestamp = cmdutil.parse_date_time_or_now(opts.at)
    if opts.out:
        clock_out(db, timestamp=timestamp)
    running = dbutil.get_active_info(db, sheet)
    if running is not None:
        raise SystemExit('error: timesheet already active')
    most_recent_clockout = dbutil.get_most_recent_clockout(db, sheet)
    description = u' '.join(args) or None
    if most_recent_clockout:
        (id, start_time, prev_timestamp, prev_desc) = most_recent_clockout
        prev_meta = dbutil.get_entry_meta(db, id)
        if timestamp < prev_timestamp:
            raise SystemExit('error: time periods could end up overlapping')
        current_sheet = dbutil.get_current_sheet(db)
        if change and db.config.has_option(current_sheet, 'autocontinue'):
            if not description:
                description = prev_desc
            for p_key, p_value in prev_meta.items():
                if p_key not in metadata.keys() or not metadata[p_key]:
                    metadata[p_key] = p_value

    db.execute(u'''
    insert into entry (
        sheet, start_time, description, extra
    ) values (?,?,?,?)
    ''', (sheet, timestamp, description, extra))
    entry_id = db.cursor.lastrowid
    dbutil.update_entry_meta(db, entry_id, metadata)
Exemplo n.º 12
0
def in_(db, args, extra=None, change=False):
    parser = optparse.OptionParser(usage='''usage: %prog in [NOTES...]

Start the timer for the current timesheet. Must be called before out.
Notes may be specified for this period. This is exactly equivalent to
%prog in; %prog alter''')
    parser.add_option(
        '-s',
        '--switch',
        dest='switch',
        type='string',
        help='Switch to another timesheet before starting the timer.')
    parser.add_option('-o',
                      '--out',
                      dest='out',
                      action='store_true',
                      default=False,
                      help='Clocks out before clocking in')
    parser.add_option('-a',
                      '--at',
                      dest='at',
                      type='string',
                      help='Set time of clock-in')
    parser.add_option('-t',
                      '--ticket',
                      dest='ticket_number',
                      type='string',
                      default=None,
                      help='Set ticket number')
    parser.add_option('--billable',
                      dest='billable',
                      action='store_true',
                      default=True,
                      help='Marks entry as billable')
    parser.add_option('--non-billable',
                      dest='billable',
                      action='store_false',
                      default=True,
                      help='Marks entry as non-billable')
    cmdutil.add_user_specified_attributes(db, parser)
    opts, args = parser.parse_args(args=args)
    metadata = cmdutil.collect_user_specified_attributes(db, opts)
    metadata['billable'] = 'yes' if opts.billable else 'no'
    if opts.ticket_number:
        metadata['ticket_number'] = opts.ticket_number
    if opts.switch:
        sheet = opts.switch
        switch(db, [sheet])
    else:
        sheet = dbutil.get_current_sheet(db)
    timestamp = cmdutil.parse_date_time_or_now(opts.at)
    if opts.out:
        clock_out(db, timestamp=timestamp)
    running = dbutil.get_active_info(db, sheet)
    if running is not None:
        raise SystemExit('error: timesheet already active')
    most_recent_clockout = dbutil.get_most_recent_clockout(db, sheet)
    description = u' '.join(args) or None
    if most_recent_clockout:
        (id, start_time, prev_timestamp, prev_desc) = most_recent_clockout
        prev_meta = dbutil.get_entry_meta(db, id)
        if timestamp < prev_timestamp:
            raise SystemExit('error: time periods could end up overlapping')
        current_sheet = dbutil.get_current_sheet(db)
        if change and db.config.has_option(current_sheet, 'autocontinue'):
            if not description:
                description = prev_desc
            for p_key, p_value in prev_meta.items():
                if p_key not in metadata.keys() or not metadata[p_key]:
                    metadata[p_key] = p_value

    db.execute(
        u'''
    insert into entry (
        sheet, start_time, description, extra
    ) values (?,?,?,?)
    ''', (sheet, timestamp, description, extra))
    entry_id = db.cursor.lastrowid
    dbutil.update_entry_meta(db, entry_id, metadata)