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 

9 

10 

11def make_subparser(subparsers): 

12 return subparsers.add_parser( 

13 'persistent', 

14 help='Manage persistent selections.', 

15 description='''Manage persistent selections. 

16 

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

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

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

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

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

22--persistent option can be used. 

23 

24This command allows to list and delete persistent selections. 

25''') 

26 

27 

28def setup(parser): 

29 parser.add_argument( 

30 'action', 

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

32 help='Select action to perform.') 

33 

34 parser.add_argument( 

35 'names', 

36 nargs='*', 

37 help='Persistent selection names.') 

38 

39 

40def run(parser, args): 

41 env = environment.get_environment() 

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

43 

44 available = sorted(db.get_persistent_names()) 

45 for name in args.names: 

46 if name not in available: 

47 raise error.SquirrelError( 

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

49 

50 if args.action == 'list': 

51 if not args.names: 

52 names = available 

53 else: 

54 names = args.names 

55 

56 for name in names: 

57 print(name) 

58 

59 elif args.action == 'delete': 

60 for name in args.names: 

61 sq = base.Squirrel(persistent=name) 

62 sq.delete() 

63 

64 else: 

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