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 

10headline = 'Manage persistent selections.' 

11 

12 

13def make_subparser(subparsers): 

14 return subparsers.add_parser( 

15 'persistent', 

16 help=headline, 

17 description=headline + ''' 

18 

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

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

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

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

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

24``--persistent`` option can be used. 

25 

26This command allows to list and delete persistent selections. 

27''') 

28 

29 

30def setup(parser): 

31 parser.add_argument( 

32 'action', 

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

34 help='Select action to perform.') 

35 

36 parser.add_argument( 

37 'names', 

38 nargs='*', 

39 help='Persistent selection names.') 

40 

41 

42def run(parser, args): 

43 env = environment.get_environment() 

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

45 

46 available = sorted(db.get_persistent_names()) 

47 for name in args.names: 

48 if name not in available: 

49 raise error.SquirrelError( 

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

51 

52 if args.action == 'list': 

53 if not args.names: 

54 names = available 

55 else: 

56 names = args.names 

57 

58 for name in names: 

59 print(name) 

60 

61 elif args.action == 'delete': 

62 for name in args.names: 

63 sq = base.Squirrel(persistent=name) 

64 sq.delete() 

65 

66 else: 

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