1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6from __future__ import absolute_import, print_function 

7 

8from pyrocko.squirrel import error, environment, database, base 

9from .. import common 

10 

11 

12def setup(subparsers): 

13 p = common.add_parser( 

14 subparsers, 'persistent', 

15 help='Manage persistent selections.', 

16 description='''Manage persistent selections. 

17 

18Usually, the contents of files given to Squirrel are made available within the 

19application through a runtime selection which is discarded again when the 

20application quits. Getting the cached meta-data into the runtime selection can 

21be a bottleneck for application startup with large datasets. To speed up 

22startup of Squirrel-based applications, persistent selections created with the 

23--persistent option can be used. 

24 

25This command allows to list and delete persistent selections. 

26''') 

27 

28 p.add_argument( 

29 'action', 

30 choices=['list', 'delete'], 

31 help='Select action to perform.') 

32 

33 p.add_argument( 

34 'names', 

35 nargs='*', 

36 help='Persistent selection names.') 

37 

38 return p 

39 

40 

41def call(parser, args): 

42 env = environment.get_environment() 

43 db = database.get_database(env.expand_path(env.database_path)) 

44 

45 available = sorted(db.get_persistent_names()) 

46 for name in args.names: 

47 if name not in available: 

48 raise error.SquirrelError( 

49 'No such persistent selection: %s' % name) 

50 

51 if args.action == 'list': 

52 if not args.names: 

53 names = available 

54 else: 

55 names = args.names 

56 

57 for name in names: 

58 print(name) 

59 

60 elif args.action == 'delete': 

61 for name in args.names: 

62 sq = base.Squirrel(persistent=name) 

63 sq.delete() 

64 

65 else: 

66 raise error.SquirrelError('Invalid action: %s' % args.action)