comparison frontends/src/jp/cmd_blog.py @ 2225:301bb52c8715

jp (blog): service and node can now be specified for edit command
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 18:00:45 +0200
parents 63d191c05ecd
children 4f389486667d
comparison
equal deleted inserted replaced
2224:87fcd4a7c7e4 2225:301bb52c8715
66 URL_REDIRECT_PREFIX = 'url_redirect_' 66 URL_REDIRECT_PREFIX = 'url_redirect_'
67 INOTIFY_INSTALL = '"pip install inotify"' 67 INOTIFY_INSTALL = '"pip install inotify"'
68 SECURE_UNLINK_MAX = 10 * 2 # we double value as there are 2 files per draft (content and metadata) 68 SECURE_UNLINK_MAX = 10 * 2 # we double value as there are 2 files per draft (content and metadata)
69 SECURE_UNLINK_DIR = ".backup" 69 SECURE_UNLINK_DIR = ".backup"
70 HEADER_ANSI = A.BOLD + A.FG_YELLOW 70 HEADER_ANSI = A.BOLD + A.FG_YELLOW
71 NS_MICROBLOG = u'urn:xmpp:microblog:0'
72 MB_KEYS = (u"id", 71 MB_KEYS = (u"id",
73 u"atom_id", 72 u"atom_id",
74 u"updated", 73 u"updated",
75 u"published", 74 u"published",
76 u"comments", # this key is used for all comments* keys 75 u"comments", # this key is used for all comments* keys
88 87
89 class BlogCommon(object): 88 class BlogCommon(object):
90 89
91 def __init__(self, host): 90 def __init__(self, host):
92 self.host = host 91 self.host = host
92
93 def addServiceNodeArgs(self):
94 self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=u'', help=_(u"PubSub node to request (default: microblog namespace)"))
95 self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'', help=_(u"JID of the PubSub service (default: request profile own blog)"))
93 96
94 def getTmpDir(self, sat_conf, sub_dir=None): 97 def getTmpDir(self, sat_conf, sub_dir=None):
95 """Return directory used to store temporary files 98 """Return directory used to store temporary files
96 99
97 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration 100 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
186 except ValueError as e: 189 except ValueError as e:
187 self.disp(u"Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=cmd_line, reason=e)) 190 self.disp(u"Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=cmd_line, reason=e))
188 return [] 191 return []
189 192
190 193
191 class Get(base.CommandBase): 194 class Get(base.CommandBase, BlogCommon):
192 TEMPLATE = u"blog/articles.html" 195 TEMPLATE = u"blog/articles.html"
193 196
194 def __init__(self, host): 197 def __init__(self, host):
195 extra_outputs = {'default': self.default_output, 198 extra_outputs = {'default': self.default_output,
196 'fancy': self.fancy_output} 199 'fancy': self.fancy_output}
197 base.CommandBase.__init__(self, host, 'get', use_verbose=True, use_output='complex', extra_outputs=extra_outputs, help=_(u'get blog item(s)')) 200 base.CommandBase.__init__(self, host, 'get', use_verbose=True, use_output='complex', extra_outputs=extra_outputs, help=_(u'get blog item(s)'))
201 BlogCommon.__init__(self, self.host)
198 self.need_loop=True 202 self.need_loop=True
199 203
200 def add_parser_options(self): 204 def add_parser_options(self):
201 self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=NS_MICROBLOG, help=_(u"PubSub node to request")) 205 self.addServiceNodeArgs()
202 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items', 206 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items',
203 help=_(u"item(s) id(s) to get (default: request all items)")) 207 help=_(u"item(s) id(s) to get (default: request all items)"))
204 self.parser.add_argument("-m", "--max", type=int, default=10, help=_(u"maximum number of items to get ({} to get all items)".format(C.NO_LIMIT))) 208 self.parser.add_argument("-m", "--max", type=int, default=10, help=_(u"maximum number of items to get ({} to get all items)".format(C.NO_LIMIT)))
205 # TODO: a key(s) argument to select keys to display 209 # TODO: a key(s) argument to select keys to display
206 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys', 210 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys',
207 help=_(u"microblog data key(s) to display (default: depend of verbosity)")) 211 help=_(u"microblog data key(s) to display (default: depend of verbosity)"))
208 self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'',
209 help=_(u"JID of the PubSub service (default: request profile own blog)"))
210 # TODO: add MAM filters 212 # TODO: add MAM filters
211 213
212 def template_data_mapping(self, data): 214 def template_data_mapping(self, data):
213 return {u'items': data_objects.BlogItems(data)} 215 return {u'items': data_objects.BlogItems(data)}
214 216
368 def __init__(self, host): 370 def __init__(self, host):
369 base.CommandBase.__init__(self, host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post')) 371 base.CommandBase.__init__(self, host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post'))
370 BlogCommon.__init__(self, self.host) 372 BlogCommon.__init__(self, self.host)
371 373
372 def add_parser_options(self): 374 def add_parser_options(self):
375 self.addServiceNodeArgs()
373 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) 376 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword"))
374 self.parser.add_argument("-P", "--preview", action="store_true", help=_(u"launch a blog preview in parallel")) 377 self.parser.add_argument("-P", "--preview", action="store_true", help=_(u"launch a blog preview in parallel"))
375 self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"title of the item")) 378 self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"title of the item"))
376 self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item")) 379 self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item"))
377 self.parser.add_argument("--no-comment", action='store_true', help=_(u"disable comments")) 380 self.parser.add_argument("--no-comment", action='store_true', help=_(u"disable comments"))
545 command = self.args.item.lower() 548 command = self.args.item.lower()
546 sat_conf = config.parseMainConf() 549 sat_conf = config.parseMainConf()
547 # if there are user defined extension, we use them 550 # if there are user defined extension, we use them
548 SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {})) 551 SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {}))
549 current_syntax = None 552 current_syntax = None
550 pubsub_service = pubsub_node = '' 553 pubsub_service = self.args.service
554 pubsub_node = self.args.node
551 pubsub_item = None 555 pubsub_item = None
552 556
553 if command not in ('new', 'last', 'current'): 557 if command not in ('new', 'last', 'current'):
554 # we have probably an URL, we try to parse it 558 # we have probably an URL, we try to parse it
555 import urlparse 559 import urlparse
571 self.host.quit(1) 575 self.host.quit(1)
572 url = links[0].get('href') 576 url = links[0].get('href')
573 parsed_url = urlparse.urlsplit(url) 577 parsed_url = urlparse.urlsplit(url)
574 578
575 if parsed_url.scheme == 'xmpp': 579 if parsed_url.scheme == 'xmpp':
580 if self.args.service or self.args.node:
581 self.parser.error(_(u"You can't use URI and --service or --node at the same time"))
582
576 self.disp(u"XMPP URI used: {}".format(url),2) 583 self.disp(u"XMPP URI used: {}".format(url),2)
577 # XXX: if we have not xmpp: URI here, we'll take the data as a file path 584 # XXX: if we have not xmpp: URI here, we'll take the data as a file path
578 pubsub_service = parsed_url.path 585 pubsub_service = parsed_url.path
579 pubsub_data = urlparse.parse_qs(parsed_url.query) 586 pubsub_data = urlparse.parse_qs(parsed_url.query)
580 try: 587 try: