cli.py (6819B)
1 # -*- coding: utf-8 -*- 2 """Command line interface module""" 3 import click 4 5 from . import __version__ 6 from .nncli import Nncli 7 8 # pylint: disable=unnecessary-pass 9 10 class StdinFlag(click.ParamType): 11 """StdinFlag Click Parameter Type""" 12 name = "stdin_flag" 13 14 def convert(self, value, param, ctx): 15 if value == '-': 16 return True 17 return self.fail('%s is not a valid stdin_flag') 18 19 STDIN_FLAG = StdinFlag() 20 21 @click.command() 22 @click.pass_obj 23 def rm_category(ctx_obj): 24 """Remove note category.""" 25 nncli = ctx_obj['nncli'] 26 key = ctx_obj['key'] 27 nncli.cli_note_category_rm(key) 28 29 @click.command() 30 @click.argument('category', required=True) 31 @click.pass_obj 32 def set_category(ctx_obj, category): 33 """Set the note category.""" 34 nncli = ctx_obj['nncli'] 35 key = ctx_obj['key'] 36 nncli.cli_note_category_set(key, category) 37 38 @click.command(short_help="Print the note category.") 39 @click.pass_obj 40 def get_category(ctx_obj): 41 """Print the category for the given note on stdout.""" 42 nncli = ctx_obj['nncli'] 43 key = ctx_obj['key'] 44 category = nncli.cli_note_category_get(key) 45 if category: 46 print(category) 47 48 @click.group() 49 @click.option( 50 '-k', 51 '--key', 52 required=True, 53 type=click.INT, 54 help="Specify the note key." 55 ) 56 @click.pass_context 57 def cat(ctx, key): 58 """Operate on the note category.""" 59 nncli = ctx.obj 60 ctx.obj = {} 61 ctx.obj['nncli'] = nncli 62 ctx.obj['key'] = key 63 64 cat.add_command(get_category, 'get') 65 cat.add_command(set_category, 'set') 66 cat.add_command(rm_category, 'rm') 67 68 @click.command() 69 @click.option( 70 '-k', 71 '--key', 72 required=True, 73 type=click.INT, 74 help="Specify the note key.") 75 @click.pass_obj 76 def favorite(nncli, key): 77 """Mark as note as a favorite.""" 78 nncli.cli_note_favorite(key, True) 79 80 @click.command() 81 @click.option( 82 '-k', 83 '--key', 84 required=True, 85 type=click.INT, 86 help="Specify the note key." 87 ) 88 @click.pass_obj 89 def unfavorite(nncli, key): 90 """Remove favorite flag from a note.""" 91 nncli.cli_note_favorite(key, False) 92 93 @click.command(short_help="Print JSON-formatted note to stdout.") 94 @click.option('-k', '--key', type=click.INT, help="Specify the note key.") 95 @click.option( 96 '-r', 97 '--regex', 98 is_flag=True, 99 help="Treat search term(s) as regular expressions." 100 ) 101 @click.argument('search_terms', nargs=-1) 102 @click.pass_obj 103 def export(nncli, key, regex, search_terms): 104 """ 105 Print JSON-formatted note to stdout. If a key is specified, then regex 106 and search_terms are ignored. 107 """ 108 if key: 109 nncli.cli_note_export(key) 110 else: 111 nncli.cli_export_notes(regex, ' '.join(search_terms)) 112 113 @click.command(short_help="Print note contents to stdout.") 114 @click.option('-k', '--key', type=click.INT, help="Specify the note key.") 115 @click.option( 116 '-r', 117 '--regex', 118 is_flag=True, 119 help="Treat search term(s) as regular expressions." 120 ) 121 @click.argument('search_terms', nargs=-1) 122 @click.pass_obj 123 def dump(nncli, key, regex, search_terms): 124 """ 125 Print note contents to stdout. If a key is specified, then regex 126 and search_terms are ignored. 127 """ 128 if key: 129 nncli.cli_note_dump(key) 130 else: 131 nncli.cli_dump_notes(regex, ' '.join(search_terms)) 132 133 @click.command(short_help="List notes.") 134 @click.option( 135 '-r', 136 '--regex', 137 is_flag=True, 138 help="Treat search term(s) as regular expressions." 139 ) 140 @click.argument('search_terms', nargs=-1) 141 @click.pass_obj 142 def list_notes(nncli, regex, search_terms): 143 """ 144 List notes, optionally providing search terms to narrow the 145 results. 146 """ 147 nncli.cli_list_notes(regex, ' '.join(search_terms)) 148 149 @click.command(short_help="Sync notes to server.") 150 def sync(): 151 """ 152 Perform a full, bi-directional sync of your notes between the 153 server and the local cache. 154 """ 155 pass 156 157 @click.command() 158 @click.option( 159 '-k', 160 '--key', 161 required=True, 162 type=click.INT, 163 help="Specify the note key." 164 ) 165 @click.pass_obj 166 def delete(nncli, key): 167 """Delete an existing note.""" 168 nncli.cli_note_delete(key, True) 169 170 @click.command(short_help="Edit an existing note.") 171 @click.option( 172 '-k', 173 '--key', 174 required=True, 175 type=click.INT, 176 help="Specify the note key." 177 ) 178 @click.argument('from_stdin', metavar='[-]', type=STDIN_FLAG) 179 @click.pass_obj 180 def edit(nncli, key, from_stdin): 181 """ 182 Edit an existing note. If - is specified, the note contents are 183 read from stdin, otherwise the editor will open. 184 """ 185 nncli.cli_note_edit(from_stdin, key) 186 187 @click.command(short_help="Import a JSON note.") 188 @click.argument('from_stdin', metavar='[-]', type=STDIN_FLAG) 189 @click.pass_obj 190 def json_import(nncli, from_stdin): 191 """ 192 Import a JSON-formatted note file into your account. The expected 193 JSON format is the same format used internally by nncli. If - is 194 specified, the note is read from stdin, otherwise the editor will 195 open. 196 """ 197 nncli.cli_note_import(from_stdin) 198 199 @click.command(short_help="Add a new note.") 200 @click.option('-t', '--title', help="Specify the title of note for create.") 201 @click.argument('from_stdin', metavar='[-]', type=STDIN_FLAG, required=False) 202 @click.pass_obj 203 def create(nncli, title, from_stdin): 204 """ 205 Create a new note, either opening the editor or, if - is specified, 206 reading from stdin. 207 """ 208 nncli.cli_note_create(from_stdin, title) 209 210 @click.group(invoke_without_command=True) 211 @click.option( 212 '-n', 213 '--nosync', 214 is_flag=True, 215 help="Don't perform a server sync." 216 ) 217 @click.option('-v', '--verbose', is_flag=True, help="Print verbose output.") 218 @click.option( 219 '-c', 220 '--config', 221 type=click.Path(exists=True), 222 help="Specify the config file to read from." 223 ) 224 @click.option('-k', '--key', type=click.INT, help="Specify the note key.") 225 @click.version_option(version=__version__, message='%(prog)s %(version)s') 226 @click.pass_context 227 def main(ctx, nosync, verbose, config, key): 228 """ 229 Run the NextClound Note Command Line Interface. No COMMAND means 230 to open the console GUI. 231 """ 232 ctx.obj = Nncli(not nosync, verbose, config) 233 if ctx.invoked_subcommand is None: 234 ctx.obj.gui(key) 235 elif not nosync: 236 ctx.obj.ndb.sync_notes() 237 238 main.add_command(create) 239 main.add_command(edit) 240 main.add_command(delete) 241 main.add_command(sync) 242 main.add_command(json_import, name='import') 243 main.add_command(list_notes, name='list') 244 main.add_command(dump) 245 main.add_command(export) 246 main.add_command(favorite) 247 main.add_command(unfavorite) 248 main.add_command(cat)