<commit_before>import os
import sys
import logging
from pprint import pprint as pp

from prf.request import Request
from prf.utils.utils import TODAY

from argparse import ArgumentParser

log = logging
log.basicConfig(level=logging.DEBUG)

ACTIONS = ['ls', 'snapshot', 'restore', 'current', ]

class Script(object):
    def __init__(self, argv):
        parser = ArgumentParser(description=__doc__)
        parser.add_argument('--host', default='localhost')
        parser.add_argument('--port', default=9200)
        parser.add_argument('-r', '--repo', default='es2.s3')
        parser.add_argument('-s', '--snapname')
        parser.add_argument('--date-pref', action='store_true')
        parser.add_argument('-i', '--indices')
        parser.add_argument('--rename_to')
        parser.add_argument('-a', '--action', required=True, choices=ACTIONS)
        parser.add_argument('--silent', action='store_true')

        self.args = parser.parse_args()
        self.api = Request('http://%s:%s' % (self.args.host, self.args.port))

        if (self.args.action in ['snapshot', 'restore']) and not (self.args.snapname or self.args.indices):
            parser.error('snapname and/or indices missing')

        self.silent = self.args.silent or self.args.action in ['ls', 'current']

    def js(self, resp):
        pp(resp.json())

    def request(self, method, **kw):
        print('URL:', self.api.base_url)
        pp(kw)

        if not self.silent:
            if input('Hit ENTER to run  ').upper() == '':
                self.js(getattr(self.api, method)(**kw))
            else:
                print('Canceled')
        else:
            self.js(getattr(self.api, method)(**kw))

    def snapshot(self):
        snapname = self.args.snapname
        if self.args.date_pref:
            snapname = '%s_%s' % (TODAY(), self.args.snapname)

        params = dict(
            path = '_snapshot/%s/%s' % (self.args.repo, snapname),
            data = dict(
                indices=self.args.indices
            )
        )
        self.request('post', **params)

    def restore(self):
        params = dict(
            path = '_snapshot/%s/%s/_restore' % (self.args.repo, self.args.snapname),
            data = dict(
                indices=self.args.indices,
                rename_pattern=self.args.indices,
                rename_replacement=self.args.rename_to or '%s_%s' % (self.args.snapname, self.args.indices),
                include_aliases='false'
            )
        )
        self.request('post', **params)
<commit_msg>* small fix<commit_after>import os
import sys
import logging
from pprint import pprint as pp

from prf.request import Request
from prf.utils.utils import TODAY

from argparse import ArgumentParser

log = logging
log.basicConfig(level=logging.DEBUG)

ACTIONS = ['ls', 'snapshot', 'restore', 'current', ]

class Script(object):
    def __init__(self, argv):
        parser = ArgumentParser(description=__doc__)
        parser.add_argument('--host', default='localhost')
        parser.add_argument('--port', default=9200)
        parser.add_argument('-r', '--repo', default='es2.s3')
        parser.add_argument('-s', '--snapname')
        parser.add_argument('--date-pref', action='store_true')
        parser.add_argument('-i', '--indices')
        parser.add_argument('--rename_to')
        parser.add_argument('-a', '--action', required=True, choices=ACTIONS)
        parser.add_argument('--silent', action='store_true')

        self.args = parser.parse_args()
        self.api = Request('http://%s:%s' % (self.args.host, self.args.port))

        if (self.args.action in ['snapshot', 'restore']) and not (self.args.snapname or self.args.indices):
            parser.error('snapname and/or indices missing')

        self.silent = self.args.silent or self.args.action in ['ls', 'current']

    def js(self, resp):
        pp(resp.json())

    def request(self, method, **kw):
        print('URL:', self.api.base_url)
        pp(kw)

        if not self.silent:
            if input('Hit ENTER to run  ').upper() == '':
                self.js(getattr(self.api, method)(**kw))
            else:
                print('Canceled')
        else:
            self.js(getattr(self.api, method)(**kw))

    def snapshot(self):
        snapname = self.args.snapname
        if self.args.date_pref:
            snapname = '%s%s' % (TODAY(), self.args.snapname)

        params = dict(
            path = '_snapshot/%s/%s' % (self.args.repo, snapname),
            data = dict(
                indices=self.args.indices
            )
        )
        self.request('post', **params)

    def restore(self):
        params = dict(
            path = '_snapshot/%s/%s/_restore' % (self.args.repo, self.args.snapname),
            data = dict(
                indices=self.args.indices,
                rename_pattern=self.args.indices,
                rename_replacement=self.args.rename_to or '%s_%s' % (self.args.snapname, self.args.indices),
                include_aliases='false'
            )
        )
        self.request('post', **params)

{'fraction_non_alphanumeric': 0.15174178311356032, 'fraction_numerical': 0.002361739814997048, 'mean_word_length': 2.27237604636188, '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': '6228016', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1537, 'n_words': 310}