<commit_before>#!/usr/bin/env python

from datetime import datetime
from flywheel import Model, Field, Engine
from tabulate import tabulate
import sys, argparse, logging


class Approval(Model):
    __metadata__ = {
        '_name': 'concourse-approval',
        '_ordering': 'ts-index',
        'throughput': {
            'read': 1,
            'write': 1,
        }
    }
    id = Field(type=str, range_key=True)
    lockname = Field()
    pool = Field(hash_key=True)
    timestamp = Field(type=datetime, index='ts-index')
    claimed = Field(type=bool)
    need_approval = Field(type=bool, default=False)
    approved = Field(type=bool, nullable=True)
    team = Field()
    pipeline = Field()
    description = Field(type=str, nullable=True)


class CLI():
    def __init__(self, args):
        self.engine = Engine()
        self.engine.connect_to_region('eu-west-1')

        # Register our model with the engine so it can create the Dynamo table
        self.engine.register(Approval)

        # Create the dynamo table for our registered model
        self.engine.create_schema()
        self.args = args
        # Setup logging
        if args.verbose:
            loglevel = logging.DEBUG
        else:
            loglevel = logging.INFO
        logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel)

    def main(self):
        if self.args.action == 'list':
            self.list()
        elif self.args.action == 'approve':
            self.approve()
        elif self.args.action == 'reject':
            self.reject()
        else:
            logging.error('Please use a correct argument')
            exit(1)

    def approve(self):
        if 'id' not in self.args:
            logging.error('Please give an id')
            exit(1)
        approval_lock = self.engine.scan(Approval).filter(id=self.args.id).first()
        approval_lock.approved = True
        approval_lock.timestamp = datetime.utcnow()
        self.engine.save(approval_lock, overwrite=True)
        logging.info('The lock %s has been approved' % self.args.id)

    def reject(self):
        if 'id' not in self.args:
            logging.error('Please give an id')
            exit(1)
        approval_lock = self.engine.scan(Approval).filter(id=self.args.id).first()
        approval_lock.approved = False
        approval_lock.timestamp = datetime.utcnow()
        self.engine.save(approval_lock, overwrite=True)
        logging.info('The lock %s has been rejected' % self.args.id)

    def list(self):
        approval_locks = self.engine.scan(Approval).filter(claimed=True).all()
        table = []

        if approval_locks:
            headers = sorted(approval_locks[0].keys_())
        else:
            headers = None
        if approval_locks:
            for item in approval_locks:
                row = []
                for key in sorted(item.keys_()):
                    row.append(getattr(item, key))
                table.append(row)

            print(tabulate(table, headers))
        else:
            print('There is no waiting approval')

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  parser = argparse.ArgumentParser(description="Approval CLI")<commit_msg>cli: Fixing the cli approval<commit_after>#!/usr/bin/env python

from datetime import datetime
from flywheel import Model, Field, Engine
from tabulate import tabulate
import sys, argparse, logging


class Approval(Model):
    __metadata__ = {
        '_name': 'concourse-approval',
        '_ordering': 'ts-index',
        'throughput': {
            'read': 1,
            'write': 1,
        }
    }
    id = Field(type=str, range_key=True)
    lockname = Field()
    pool = Field(hash_key=True)
    timestamp = Field(type=datetime, index='ts-index')
    claimed = Field(type=bool)
    need_approval = Field(type=bool, default=False)
    approved = Field(type=bool, nullable=True)
    team = Field()
    pipeline = Field()
    description = Field(type=str, nullable=True)


class CLI():
    def __init__(self, args):
        self.engine = Engine()
        self.engine.connect_to_region('eu-west-1')

        # Register our model with the engine so it can create the Dynamo table
        self.engine.register(Approval)

        # Create the dynamo table for our registered model
        self.engine.create_schema()
        self.args = args
        # Setup logging
        if args.verbose:
            loglevel = logging.DEBUG
        else:
            loglevel = logging.INFO
        logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel)

    def main(self):
        if self.args.action == 'list':
            self.list()
        elif self.args.action == 'approve':
            self.approve()
        elif self.args.action == 'reject':
            self.reject()
        else:
            logging.error('Please use a correct argument')
            exit(1)

    def approve(self):
        if 'id' not in self.args:
            logging.error('Please give an id')
            exit(1)
        approval_lock = self.engine.scan(Approval).filter(id=self.args.id).all()
        if not approval_lock:
            logging.info('No lock with the id %s has been found' % self.args.id)
            exit(1)
        approval_lock = approval_lock[0]
        approval_lock.approved = True
        approval_lock.timestamp = datetime.utcnow()
        self.engine.save(approval_lock, overwrite=True)
        logging.info('The lock %s has been approved' % self.args.id)

    def reject(self):
        if 'id' not in self.args:
            logging.error('Please give an id')
            exit(1)
        approval_lock = self.engine.scan(Approval).filter(id=self.args.id).all()
        if not approval_lock:
            logging.info('No lock with the id %s has been found' % self.args.id)
            exit(1)
        approval_lock = approval_lock[0]
        approval_lock.approved = False
        approval_lock.timestamp = datetime.utcnow()
        self.engine.save(approval_lock, overwrite=True)
        logging.info('The lock %s has been rejected' % self.args.id)


    def list(self):
        approval_locks = self.engine.scan(Approval).filter(claimed=True).all()
        table = []

        if approval_locks:
            headers = sorted(approval_locks[0].keys_())
        else:
            headers = None
        if approval_locks:
            for item in approval_locks:
                row = []
                for key in sorted(item.keys_()):
                    row.append(getattr(item, key))
                table.append(row)

            print(tabulate(table, headers))
        else:
            print('There is no waiting approval')

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  parser = argparse.ArgumentParser(description="Approval CLI")
{'fraction_non_alphanumeric': 0.11128938156359393, 'fraction_numerical': 0.0026254375729288216, 'mean_word_length': 2.201213818860878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7667009', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1953, 'n_words': 491}