Ejemplo n.º 1
0
def index():
    form = SearchForm()

    query = request.args.get('query', '').strip()

    db = TinyDB(recipyGui.config.get('tinydb'))

    if not query:
        runs = db.all()
    else:
        # Search run outputs using the query string
        runs = db.search(
            where('outputs').any(lambda x: listsearch(query, x)) |
            where('inputs').any(lambda x: listsearch(query, x)) |
            where('script').search(query) |
            where('notes').search(query) |
            where('unique_id').search(query))
    runs = sorted(runs, key = lambda x: parse(x['date'].replace('{TinyDate}:', '')) if x['date'] is not None else x['eid'], reverse=True)

    run_ids = []
    for run in runs:
        if 'notes' in run.keys():
            run['notes'] = str(escape(run['notes']))
        run_ids.append(run.eid)

    db.close()

    return render_template('list.html', runs=runs, query=query, form=form,
                           run_ids=str(run_ids),
                           dbfile=recipyGui.config.get('tinydb'))
Ejemplo n.º 2
0
def index():
    form = SearchForm()

    query = request.args.get('query', '').strip()

    db = TinyDB(recipyGui.config.get('tinydb'))

    if not query:
        runs = db.all()
    else:
        # Search run outputs using the query string
        runs = db.search(
            where('outputs').any(lambda x: listsearch(query, x))
            | where('inputs').any(lambda x: listsearch(query, x))
            | where('script').search(query) | where('notes').search(query)
            | where('unique_id').search(query))
    runs = sorted(runs,
                  key=lambda x: parse(x['date'].replace('{TinyDate}:', ''))
                  if x['date'] is not None else x['eid'],
                  reverse=True)

    run_ids = []
    for run in runs:
        if 'notes' in run.keys():
            run['notes'] = str(escape(run['notes']))
        run_ids.append(run.eid)

    db.close()

    return render_template('list.html',
                           runs=runs,
                           query=query,
                           form=form,
                           run_ids=str(run_ids),
                           dbfile=recipyGui.config.get('tinydb'))
Ejemplo n.º 3
0
def search_database(db, query=None):
    """ Use this to perform a search of runs in the database """
    if not query:
        runs = db.all()
    else:
        # Search run outputs using the query string
        runs = db.search(
            where('outputs').any(lambda x: listsearch(query, x))
            | where('inputs').any(lambda x: listsearch(query, x))
            | where('script').search(query) | where('notes').search(query)
            | where('unique_id').search(query))
    return runs
Ejemplo n.º 4
0
def search(args):
    filename = args['<outputfile>']

    if args['--fuzzy']:
        results = db.search(
            where('outputs').any(lambda x: re.match(".+%s.+" % filename, x)))
    elif args['--regex']:
        results = db.search(
            where('outputs').any(lambda x: listsearch(filename, x)))
    elif args['--id']:
        results = db.search(where('unique_id').matches('%s.+' % filename))
        # Automatically turn on display of all results so we don't misleadingly
        # suggest that their shortened ID is unique when it isn't
        args['--all'] = True
    else:
        results = db.search(
            where('outputs').any(
                lambda x: listsearch(os.path.abspath(filename), x)))

    results = [_change_date(result) for result in results]

    # Sort the results
    results = sorted(results, key=lambda x: parse(x['date']))

    if args['--json']:
        if args['--all']:
            res_to_output = results
        else:
            res_to_output = results[-1]
        output = dumps(res_to_output, indent=2, sort_keys=True)
        print(output)
    else:
        if len(results) == 0:
            print("No results found")
        else:
            if args['--all']:
                for r in results[:-1]:
                    print(template_result(r))
                    print("-" * 40)
                print(template_result(results[-1]))
            else:
                print(template_result(results[-1]))
                if len(results) > 1:
                    print("** Previous runs creating this output have been " +
                          "found. Run with --all to show. **")

                if args['--diff']:
                    if 'diff' in results[-1]:
                        print("\n\n")
                        print(results[-1]['diff'])

    db.close()
Ejemplo n.º 5
0
def search(args):
    filename = args['<outputfile>']

    if args['--fuzzy']:
        results = db.search(where('outputs').any(
            lambda x: re.match(".+%s.+" % filename, x)))
    elif args['--regex']:
        results = db.search(where('outputs').any(
            lambda x: listsearch(filename, x)))
    elif args['--id']:
        results = db.search(where('unique_id').matches('%s.+' % filename))
        # Automatically turn on display of all results so we don't misleadingly
        # suggest that their shortened ID is unique when it isn't
        args['--all'] = True
    else:
        results = db.search(where('outputs').any(
            lambda x: listsearch(os.path.abspath(filename), x)))

    results = [_change_date(result) for result in results]

    # Sort the results
    results = sorted(results, key=lambda x: parse(x['date']))

    if args['--json']:
        if args['--all']:
            res_to_output = results
        else:
            res_to_output = results[-1]
        output = dumps(res_to_output, indent=2, sort_keys=True)
        print(output)
    else:
        if len(results) == 0:
            print("No results found")
        else:
            if args['--all']:
                for r in results[:-1]:
                    print(template_result(r))
                    print("-" * 40)
                print(template_result(results[-1]))
            else:
                print(template_result(results[-1]))
                if len(results) > 1:
                    print("** Previous runs creating this output have been " +
                          "found. Run with --all to show. **")

                if args['--diff']:
                    if 'diff' in results[-1]:
                        print("\n\n")
                        print(results[-1]['diff'])

    db.close()
Ejemplo n.º 6
0
def test_listsearch_flat_list_match():
    q = 'test'
    l = ['test', '/test/test.csv']

    for item in l:
        msg = 'listsearch({}, {}) does not return true'.format(q, item)
        yield assert_true, listsearch(q, item), msg
Ejemplo n.º 7
0
def test_listsearch_list_of_lists_no_match():
    q = 'test'
    l = [['string', 'hash-for-string'], ['/text/data.csv', 'another-hash']]

    for item in l:
        msg = 'listsearch({}, {}) does not return false'.format(q, item)
        yield assert_false, listsearch(q, item), msg
Ejemplo n.º 8
0
def test_listsearch_flat_list_no_match():
    q = 'test'
    l = ['string', '/text/data.csv']

    for item in l:
        msg = 'listsearch({}, {}) does not return false'.format(q, item)
        yield assert_false, listsearch(q, item), msg
Ejemplo n.º 9
0
def test_listsearch_hash_is_none():
    item = [u'test.csv', None]
    q = 'test'

    msg = 'listsearch fails when hash is None'

    assert listsearch(q, item), msg
Ejemplo n.º 10
0
def test_listsearch_empty_item():
    item = []
    q = 'test'

    msg = 'listsearch fails when item=[]'

    assert not listsearch(q, item), msg
Ejemplo n.º 11
0
def test_listsearch_list_of_lists_match():
    q = 'string'
    l = [['string', 'hash-for-string'], ['/text/string.csv', 'another-hash'],
         ['blabla', 'string-in-hash']]

    for item in l:
        msg = 'listsearch({}, {}) does not return true'.format(q, item)
        yield assert_true, listsearch(q, item), msg
Ejemplo n.º 12
0
def test_listsearch_flat_list_match(item):
    q = 'test'

    msg = 'listsearch({}, {}) does not return true'.format(q, item)
    assert listsearch(q, item), msg
Ejemplo n.º 13
0
def test_listsearch_list_of_lists_no_match(item):
    q = 'test'

    msg = 'listsearch({}, {}) does not return false'.format(q, item)
    assert not listsearch(q, item), msg
Ejemplo n.º 14
0
def test_listsearch_list_of_lists_match(item):
    q = 'string'

    msg = 'listsearch({}, {}) does not return true'.format(q, item)
    assert listsearch(q, item), msg