Mercurial > libervia-backend
comparison frontends/src/jp/common.py @ 2351:3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 06 Sep 2017 07:39:10 +0200 |
parents | c57bc0fe17d9 |
children | 8b37a62336c3 |
comparison
equal
deleted
inserted
replaced
2350:388226e9c3ff | 2351:3c0a3fae1862 |
---|---|
134 u"""base class for editing commands | 134 u"""base class for editing commands |
135 | 135 |
136 This class allows to edit file for PubSub or something else. | 136 This class allows to edit file for PubSub or something else. |
137 It works with temporary files in SàT local_dir, in a "cat_dir" subdir | 137 It works with temporary files in SàT local_dir, in a "cat_dir" subdir |
138 """ | 138 """ |
139 # use_items(bool): True if items are used, will then add item related options | |
140 use_items=True | |
139 | 141 |
140 def __init__(self, host, cat_dir, use_metadata=False): | 142 def __init__(self, host, cat_dir, use_metadata=False): |
141 """ | 143 """ |
142 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration | 144 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
143 @param cat_dir(unicode): directory to use for drafts | 145 @param cat_dir(unicode): directory to use for drafts |
150 self.sat_conf = config.parseMainConf() | 152 self.sat_conf = config.parseMainConf() |
151 self.cat_dir_str = cat_dir.encode('utf-8') | 153 self.cat_dir_str = cat_dir.encode('utf-8') |
152 self.use_metadata = use_metadata | 154 self.use_metadata = use_metadata |
153 | 155 |
154 def add_parser_options(self): | 156 def add_parser_options(self): |
155 group = self.parser.add_mutually_exclusive_group() | 157 if self.use_items: |
156 group.add_argument("--force-item", action='store_true', help=_(u"don't use magic and take item argument as an actual item")) | 158 group = self.parser.add_mutually_exclusive_group() |
157 group.add_argument("--last-item", action='store_true', help=_(u"take last item instead of creating a new one if no item id is found")) | 159 group.add_argument("--force-item", action='store_true', help=_(u"don't use magic and take item argument as an actual item")) |
160 group.add_argument("--last-item", action='store_true', help=_(u"take last item instead of creating a new one if no item id is found")) | |
158 | 161 |
159 def secureUnlink(self, path): | 162 def secureUnlink(self, path): |
160 """Unlink given path after keeping it for a while | 163 """Unlink given path after keeping it for a while |
161 | 164 |
162 This method is used to prevent accidental deletion of a draft | 165 This method is used to prevent accidental deletion of a draft |
309 | 312 |
310 def publish(self, content): | 313 def publish(self, content): |
311 # if metadata is needed, publish will be called with it last argument | 314 # if metadata is needed, publish will be called with it last argument |
312 raise NotImplementedError | 315 raise NotImplementedError |
313 | 316 |
314 def getTmpFile(self, suff): | 317 def getTmpFile(self): |
315 """Create a temporary file | 318 """Create a temporary file |
316 | 319 |
317 @param suff (str): suffix to use for the filename | 320 @param suff (str): suffix to use for the filename |
318 @return (tuple(file, str)): opened (w+b) file object and file path | 321 @return (tuple(file, str)): opened (w+b) file object and file path |
319 """ | 322 """ |
323 suff = '.' + self.getTmpSuff() | |
320 cat_dir_str = self.cat_dir_str | 324 cat_dir_str = self.cat_dir_str |
321 tmp_dir = getTmpDir(self.sat_conf, self.cat_dir_str, self.profile.encode('utf-8')) | 325 tmp_dir = getTmpDir(self.sat_conf, self.cat_dir_str, self.profile.encode('utf-8')) |
322 if not os.path.exists(tmp_dir): | 326 if not os.path.exists(tmp_dir): |
323 try: | 327 try: |
324 os.makedirs(tmp_dir) | 328 os.makedirs(tmp_dir) |
441 elif command != 'last': | 445 elif command != 'last': |
442 self.parser.error(_(u"--last-item can't be used with a specified item")) | 446 self.parser.error(_(u"--last-item can't be used with a specified item")) |
443 | 447 |
444 if not force_item and command in ('new', 'last', 'edit'): | 448 if not force_item and command in ('new', 'last', 'edit'): |
445 # we need a temporary file | 449 # we need a temporary file |
446 tmp_suff = '.' + self.getTmpSuff() | 450 content_file_obj, content_file_path = self.getTmpFile() |
447 content_file_obj, content_file_path = self.getTmpFile(tmp_suff) | |
448 if command == 'new': | 451 if command == 'new': |
449 self.disp(u'Editing a new item', 2) | 452 self.disp(u'Editing a new item', 2) |
450 if self.use_metadata: | 453 if self.use_metadata: |
451 metadata = None | 454 metadata = None |
452 elif command in ('last', 'edit'): | 455 elif command in ('last', 'edit'): |
473 # there is an existing draft that we use | 476 # there is an existing draft that we use |
474 content_file_path = os.path.expanduser(self.args.item) | 477 content_file_path = os.path.expanduser(self.args.item) |
475 content_file_obj = open(content_file_path, 'r+b') | 478 content_file_obj = open(content_file_path, 'r+b') |
476 else: | 479 else: |
477 # last chance, it should be an item | 480 # last chance, it should be an item |
478 tmp_suff = '.' + self.getTmpSuff() | 481 content_file_obj, content_file_path = self.getTmpFile() |
479 content_file_obj, content_file_path = self.getTmpFile(tmp_suff) | |
480 pubsub_item = self.args.item | 482 pubsub_item = self.args.item |
481 | 483 |
482 try: | 484 try: |
483 # we try to get existing item | 485 # we try to get existing item |
484 if self.use_metadata: | 486 if self.use_metadata: |