-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplib-topic.py
More file actions
134 lines (122 loc) · 5.28 KB
/
plib-topic.py
File metadata and controls
134 lines (122 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Pdf Library command line utilities
To expand the utility of the orevious library.py, need toi factor out
functions betwee the Topic database and the Paper dattabase
rgr19sep17
"""
# =============================================================================
# Version Information
# 1.x.x Initial ideas
#
#
# Dependencies
# local
# library
#
# Other
# =============================================================================
# define the user interface, leaving all the functionality to
# imported code from plibCommands
PROG_DESCRIPTION = "PDF Library topic database utility"
CMD_NAME = 'plib-topic'
VERSION_NO = '1.2.0' # increment last digit with minor changes
DATE = 5*'\t'+'rgr13may18' # see plib_Revisions.txt for notes
import argparse, sys
from os import path
# override the standard parser to add a usage line in case of an exception
class ArgumentParserRGR(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write(message + '\n')
self.print_usage()
# for the command line version a system exit here
# prevents the traceback message - not so useful when debugging
self.exit(2,'done')
# sys.exit(2)
# raise
parser = ArgumentParserRGR(
description=PROG_DESCRIPTION,
version='%(prog)s ' + VERSION_NO,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=path.basename(__file__) + DATE)
# build the parser structure by hand
subparsers = parser.add_subparsers(
help='available sub-commands are add, find and edit')
parser.add_argument('-t', '--test',
help="show actions but don't change the database",
action='store_true')
parser.add_argument('-V', '--Verbose',
help='a way to get more help', action='store_true')
from plibCommands import doShowPaths
parser.add_argument('-p', '--paths',
help='show paths to topic and paper databases', action='store_true') #default=doShowPaths)
# use sub-parsers for each of the sub commands
from plibCommands import doAdd # the add command
# 2 required positional parameters
parser_add = subparsers.add_parser('add',
help='add a category to the database')
parser_add.set_defaults(func=doAdd)
parser_add.add_argument('cat', type=str, help='the category to be added')
parser_add.add_argument('key', type=str, help='key words for the category')
parser_add.add_argument('desc', type=str, nargs='*',
help='text for the decriptoin of this category in the database')
# add --see-also as an optiona item
from plibCommands import doFind
parser_find = subparsers.add_parser('find',
help='find a target string in the database - default=description')
parser_find.set_defaults(func=doFind)
# 1 required positional parameter
parser_find.add_argument('target', type=str, help='the item to search for')
# optional switches to control the search
parser_find.add_argument('-i', '--ignore', action='store_true',
help='make search case sensitive')
parser_find.add_argument('-w', '--words', action='store_true',
help='search for whole words only')
parser_find.add_argument('-c', '--cat', action='store_true',
help='include the category field in the search')
parser_find.add_argument('-C', '--Category', action='store_true',
help='search the category field only')
parser_find.add_argument('-d', '--desc', action='store_true',
help='include the description field in the search')
parser_find.add_argument('-D', '--Description', action='store_true',
help='search the description field only')
parser_find.add_argument('-k', '--key', action='store_true',
help='include the key words field in the search')
parser_find.add_argument('-K', '--Key-word', action='store_true',
help='search the key words field only')
from plibCommands import doEdit
parser_edit = subparsers.add_parser('edit',
help='edit an exisitng item in the database')
parser_edit.add_argument('turl', type=str,
help='the T-URL to edit, use del to delete the last entry added')
parser_edit.add_argument('cmd', type=str, nargs='*',
help='edit commands "new text to replace all exisitng"\r\n\
"int text" to replace the existing[int] item')
parser_edit.add_argument('-u', '--url', action='store_true',
help='replace the existing url with the remaining cmd ... string')
parser_edit.set_defaults(func=doEdit)
def test(cmds):
args = parser.parse_args(cmds.split())
if args.Verbose:
print 'args:', args
args.func(args)
return args
if __name__=='__main__':
import logging
from sys import stdout
from time import strftime
logger = logging.getLogger(__name__)
logging.basicConfig(stream=stdout)
logger.level = logging.INFO
logger.debug('\tstarting: '+path.basename(__file__)+'\t==='
+ strftime('%a-%d %H:%M') + ' ===')
if len(sys.argv)>1 and sys.argv[1]=='-p':
# cannot see how to handle this as optional in parse_args()
doShowPaths()
sys.exit(0)
args = parser.parse_args() # this will show help for -h
if args.Verbose:
print 'args:', args
# run the utility
args.func(args)