annotate frontends/src/jp/cmd_blog.py @ 1883:abb2f253188e

jp (blog): fixed syntax guessing on existing files
author Goffi <goffi@goffi.org>
date Sat, 05 Mar 2016 18:00:12 +0100
parents ed444e9aa078
children edd8dc8df1b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #! /usr/bin/python
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
3
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
6
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
11
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
16
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
19
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
20
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import base
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.i18n import _
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.constants import Const as C
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
24 from sat.tools import config
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
25 from ConfigParser import NoSectionError, NoOptionError
1834
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
26 import json
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
27 import os.path
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
28 import os
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
29 import time
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
30 import tempfile
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
31 import subprocess
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
32 import shlex
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
33 import glob
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
34 from sat.tools import common
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
35
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
36 __commands__ = ["Blog"]
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
37
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
38 # extensions to use with known syntaxes
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
39 SYNTAX_EXT = {
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
40 '': 'txt', # used when the syntax is not found
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
41 "XHTML": "xhtml",
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
42 "markdown": "md"
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
43 }
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
44
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
45 # defaut arguments used for some known editors
1873
6ec54626610c jp (blog): add to EDITOR_ARGS_MAGIC parameters for (x)emacs and nano
souliane <souliane@mailoo.org>
parents: 1872
diff changeset
46 VIM_SPLIT_ARGS = "-c 'vsplit|wincmd w|next|wincmd w'"
6ec54626610c jp (blog): add to EDITOR_ARGS_MAGIC parameters for (x)emacs and nano
souliane <souliane@mailoo.org>
parents: 1872
diff changeset
47 EMACS_SPLIT_ARGS = '--eval "(split-window-horizontally)"'
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
48 EDITOR_ARGS_MAGIC = {
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
49 'vim': VIM_SPLIT_ARGS + ' {content_file} {metadata_file}',
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
50 'gvim': VIM_SPLIT_ARGS + ' --nofork {content_file} {metadata_file}',
1873
6ec54626610c jp (blog): add to EDITOR_ARGS_MAGIC parameters for (x)emacs and nano
souliane <souliane@mailoo.org>
parents: 1872
diff changeset
51 'emacs': EMACS_SPLIT_ARGS + ' {content_file} {metadata_file}',
6ec54626610c jp (blog): add to EDITOR_ARGS_MAGIC parameters for (x)emacs and nano
souliane <souliane@mailoo.org>
parents: 1872
diff changeset
52 'xemacs': EMACS_SPLIT_ARGS + ' {content_file} {metadata_file}',
6ec54626610c jp (blog): add to EDITOR_ARGS_MAGIC parameters for (x)emacs and nano
souliane <souliane@mailoo.org>
parents: 1872
diff changeset
53 'nano': ' -F {content_file} {metadata_file}',
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
54 }
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
55
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
56 CONF_SYNTAX_EXT = 'syntax_ext_dict'
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
57 BLOG_TMP_DIR="blog"
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
58 METADATA_SUFF = '_metadata.json'
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
59 # key to remove from metadata tmp file if they exist
1871
64a40adccba4 jp (blog/edit): removed content_rich from KEY_TO_REMOVE_METADATA as it is never given by backend
Goffi <goffi@goffi.org>
parents: 1870
diff changeset
60 KEY_TO_REMOVE_METADATA = ('id','content', 'content_xhtml', 'comments_node', 'comments_service')
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
61
1834
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
62 URL_REDIRECT_PREFIX = 'url_redirect_'
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
63 INOTIFY_INSTALL = '"pip install inotify"'
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
64
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
65
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
66 class BlogCommon(object):
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
67 def getTmpDir(self, sat_conf):
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
68 """Return directory used to store temporary files
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
69
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
70 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
71 @return (str): path to the dir
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
72 """
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
73 local_dir = config.getConfig(sat_conf, '', 'local_dir', Exception)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
74 return os.path.join(local_dir, BLOG_TMP_DIR)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
75
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
76 def getCurrentFile(self, sat_conf):
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
77 # we guess the blog item currently edited by choosing
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
78 # the most recent file corresponding to temp file pattern
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
79 # in tmp_dir, excluding metadata files
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
80 tmp_dir = self.getTmpDir(sat_conf)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
81 available = [path for path in glob.glob(os.path.join(tmp_dir, 'blog_*')) if not path.endswith(METADATA_SUFF)]
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
82 if not available:
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
83 self.disp(u"Counldn't find any content draft in {path}".format(path=tmp_dir), error=True)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
84 self.host.quit(1)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
85 return max(available, key=lambda path: os.stat(path).st_mtime)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
86
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
87 def guessSyntaxFromPath(self, sat_conf, path):
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
88 """Return syntax guessed according to filename extension
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
89
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
90 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
91 @param path(str): path to the content file
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
92 @return(unicode): syntax to use
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
93 """
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
94 # we first try to guess syntax with extension
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
95 ext = os.path.splitext(path)[1][1:] # we get extension without the '.'
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
96 if ext:
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
97 for k,v in SYNTAX_EXT.iteritems():
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
98 if ext == v:
1883
abb2f253188e jp (blog): fixed syntax guessing on existing files
Goffi <goffi@goffi.org>
parents: 1882
diff changeset
99 return k
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
100
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
101 # if not found, we use current syntax
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
102 return self.host.bridge.getParamA("Syntax", "Composition", "value", self.profile)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
103
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
104
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
105 class Edit(base.CommandBase, BlogCommon):
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
106
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
107 def __init__(self, host):
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
108 super(Edit, self).__init__(host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post'))
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
109
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
110 def add_parser_options(self):
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
111 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword"))
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
112 self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"title of the item"))
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
113 self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item"))
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
114 self.parser.add_argument("--no-comment", action='store_true', help=_(u"disable comments"))
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
115
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
116 def getTmpFile(self, sat_conf, tmp_suff):
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
117 """Create a temporary file to received blog item body
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
118
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
119 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
120 @param tmp_suff (str): suffix to use for the filename
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
121 @return (tuple(file, str)): opened (w+b) file object and file path
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
122 """
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
123 tmp_dir = self.getTmpDir(sat_conf)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
124 if not os.path.exists(tmp_dir):
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
125 try:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
126 os.makedirs(tmp_dir)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
127 except OSError as e:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
128 self.disp(u"Can't create {path} directory: {reason}".format(
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
129 path=tmp_dir, reason=e), error=True)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
130 self.host.quit(1)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
131 try:
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
132 fd, path = tempfile.mkstemp(suffix=tmp_suff,
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
133 prefix=time.strftime('blog_%Y-%m-%d_%H:%M:%S_'),
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
134 dir=tmp_dir, text=True)
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
135 return os.fdopen(fd, 'w+b'), path
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
136 except OSError as e:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
137 self.disp(u"Can't create temporary file: {reason}".format(reason=e), error=True)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
138 self.host.quit(1)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
139
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
140 def buildMetadataFile(self, content_file_path, mb_data=None):
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
141 """Build a metadata file using json
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
142
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
143 The file is named after content_file_path, with extension replaced by _metadata.json
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
144 @param content_file_path(str): path to the temporary file which will contain the body
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
145 @param mb_data(dict, None): microblog metadata (for existing items)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
146 @return (tuple[dict, str]): merged metadata put originaly in metadata file
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
147 and path to temporary metadata file
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
148 """
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
149 # we first construct metadata from edited item ones and CLI argumments
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
150 # or re-use the existing one if it exists
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
151 meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
152 if os.path.exists(meta_file_path):
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
153 self.disp(u"Metadata file already exists, we re-use it")
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
154 try:
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
155 with open(meta_file_path, 'rb') as f:
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
156 mb_data = json.load(f)
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
157 except (OSError, IOError, ValueError) as e:
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
158 self.disp(u"Can't read existing metadata file at {path}, aborting: {reason}".format(
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
159 path=meta_file_path, reason=e), error=True)
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
160 self.host.quit(1)
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
161 else:
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
162 mb_data = {} if mb_data is None else mb_data.copy()
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
163
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
164 # in all cases, we want to remove unwanted keys
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
165 for key in KEY_TO_REMOVE_METADATA:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
166 try:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
167 del mb_data[key]
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
168 except KeyError:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
169 pass
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
170 # and override metadata with command-line arguments
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
171 mb_data['allow_comments'] = C.boolConst(not self.args.no_comment)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
172 if self.args.tag:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
173 common.iter2dict('tag', self.args.tag, mb_data)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
174 if self.args.title is not None:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
175 mb_data['title'] = self.args.title
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
176
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
177 # then we create the file and write metadata there, as JSON dict
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
178 # XXX: if we port jp one day on Windows, O_BINARY may need to be added here
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
179 if os.path.exists(meta_file_path):
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
180 self.disp(u"metadata file {} already exists, this should not happen! Cancelling...", error=True)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
181 with os.fdopen(os.open(meta_file_path, os.O_RDWR | os.O_CREAT ,0o600), 'w+b') as f:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
182 # we need to use an intermediate unicode buffer to write to the file unicode without escaping characters
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
183 unicode_dump = json.dumps(mb_data, ensure_ascii=False, indent=4, separators=(',', ': '), sort_keys=True)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
184 f.write(unicode_dump.encode('utf-8'))
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
185
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
186 return mb_data, meta_file_path
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
187
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
188 def edit(self, sat_conf, content_file_path, content_file_obj, mb_data=None):
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
189 """Edit the file contening the content using editor, and publish it"""
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
190 item_ori_mb_data = mb_data
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
191 # we first create metadata file
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
192 meta_ori, meta_file_path = self.buildMetadataFile(content_file_path, item_ori_mb_data)
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
193
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
194 # then we calculate hashes to check for modifications
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
195 import hashlib
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
196 content_file_obj.seek(0)
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
197 tmp_ori_hash = hashlib.sha1(content_file_obj.read()).digest()
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
198 content_file_obj.close()
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
199
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
200 # then we launch editor
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
201 editor = config.getConfig(sat_conf, 'jp', 'editor') or os.getenv('EDITOR', 'vi')
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
202 try:
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
203 # is there custom arguments in sat.conf ?
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
204 editor_args = config.getConfig(sat_conf, 'jp', 'blog_editor_args', Exception)
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
205 except (NoOptionError, NoSectionError):
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
206 # no, we check if we know the editor and have special arguments
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
207 editor_args = EDITOR_ARGS_MAGIC.get(os.path.basename(editor), '')
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
208 try:
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
209 # we split the arguments and add the known fields
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
210 # we split arguments first to avoid escaping issues in file names
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
211 args = [a.format(content_file=content_file_path, metadata_file=meta_file_path) for a in shlex.split(editor_args)]
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
212 except ValueError as e:
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
213 self.disp(u"Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=editor_args, reason=e))
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
214 args = []
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
215 if not args:
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
216 args = [content_file_path]
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
217 editor_exit = subprocess.call([editor] + args)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
218
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
219 # we send the file if edition was a success
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
220 if editor_exit != 0:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
221 self.disp(u"Editor exited with an error code, so temporary file has not be deleted, and blog item is not published.\nTou can find temporary file at {path}".format(
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
222 path=content_file_path), error=True)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
223 else:
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
224 try:
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
225 with open(content_file_path, 'rb') as f:
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
226 content = f.read()
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
227 with open(meta_file_path, 'rb') as f:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
228 mb_data = json.load(f)
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
229 except (OSError, IOError):
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
230 self.disp(u"Can read files at {content_path} and/or {meta_path}, have they been deleted?\nCancelling edition".format(
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
231 content_path=content_file_path, meta_path=meta_file_path), error=True)
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
232 self.host.quit(1)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
233 except ValueError:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
234 self.disp(u"Can't parse metadata, please check it is correct JSON format. Cancelling edition.\n" +
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
235 "You can find tmp file at {content_path} and temporary meta file at {meta_path}.".format(
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
236 content_path=content_file_path, meta_path=meta_file_path), error=True)
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
237 self.host.quit(1)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
238
1870
c25077c87b1d base (blog/edit): added a way to block publishing by adding "publish": "false" in metadata file
Goffi <goffi@goffi.org>
parents: 1868
diff changeset
239 if not C.bool(mb_data.get('publish', "true")):
c25077c87b1d base (blog/edit): added a way to block publishing by adding "publish": "false" in metadata file
Goffi <goffi@goffi.org>
parents: 1868
diff changeset
240 self.disp(u'Publication blocked by "publish" key in metadata, cancelling edition.\n\n' +
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
241 "temporary file path:\t{content_path}\nmetadata file path:\t{meta_path}".format(
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
242 content_path=content_file_path, meta_path=meta_file_path), error=True)
1870
c25077c87b1d base (blog/edit): added a way to block publishing by adding "publish": "false" in metadata file
Goffi <goffi@goffi.org>
parents: 1868
diff changeset
243 self.host.quit(0)
c25077c87b1d base (blog/edit): added a way to block publishing by adding "publish": "false" in metadata file
Goffi <goffi@goffi.org>
parents: 1868
diff changeset
244
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
245 if len(content) == 0:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
246 self.disp(u"Content is empty, cancelling the blog edition")
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
247
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
248 # time to re-check the hash
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
249 elif (tmp_ori_hash == hashlib.sha1(content).digest() and
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
250 meta_ori == mb_data):
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
251 self.disp(u"The content has not been modified, cancelling the blog edition")
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
252
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
253 else:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
254 # we can now send the blog
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
255 mb_data['content_rich'] = content.decode('utf-8')
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
256
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
257 if item_ori_mb_data is not None:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
258 mb_data['id'] = item_ori_mb_data['id']
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
259
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
260 try:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
261 self.host.bridge.mbSend('', '', mb_data, self.profile)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
262 except Exception as e:
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
263 self.disp(u"Error while sending your blog, the temporary files have been kept at {content_path} and {meta_path}: {reason}".format(
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
264 content_path=content_file_path, meta_path=meta_file_path, reason=e), error=True)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
265 self.host.quit(1)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
266
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
267 os.unlink(content_file_path)
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
268 os.unlink(meta_file_path)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
269
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
270 def start(self):
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
271 item_lower = self.args.item.lower()
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
272 sat_conf = config.parseMainConf()
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
273 # if there are user defined extension, we use them
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
274 SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {}))
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
275 current_syntax = None
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
276
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
277 if item_lower == 'current':
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
278 # use wants to continue current draft
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
279 content_file_path = self.getCurrentFile(sat_conf)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
280 content_file_obj = open(content_file_path, 'r+b')
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
281 current_syntax = self.guessSyntaxFromPath(sat_conf, content_file_path)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
282 else:
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
283 # we get current syntax to determine file extension
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
284 current_syntax = self.host.bridge.getParamA("Syntax", "Composition", "value", self.profile)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
285 # we now create a temporary file
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
286 tmp_suff = '.' + SYNTAX_EXT.get(current_syntax, SYNTAX_EXT[''])
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
287 content_file_obj, content_file_path = self.getTmpFile(sat_conf, tmp_suff)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
288
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
289 self.disp(u"Syntax used: {}".format(current_syntax), 1)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
290
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
291 if item_lower == 'new':
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
292 self.disp(u'Editing a new blog item', 2)
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
293 mb_data = None
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
294 elif item_lower == 'current':
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
295 self.disp(u'Continuing edition of current draft', 2)
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
296 mb_data = None
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
297 elif item_lower == 'last':
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
298 self.disp(u'Editing last published item', 2)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
299 try:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
300 mb_data = self.host.bridge.mbGet('', '', 1, [], {}, self.profile)[0][0]
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
301 except Exception as e:
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
302 self.disp(u"Error while retrieving last comment: {}".format(e))
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
303 self.host.quit(1)
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
304
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
305 content = mb_data['content_xhtml']
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
306 if current_syntax != 'XHTML':
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
307 content = self.host.bridge.syntaxConvert(content, 'XHTML', current_syntax, False, self.profile)
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
308 content_file_obj.write(content.encode('utf-8'))
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
309 content_file_obj.seek(0)
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
310
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
311 self.edit(sat_conf, content_file_path, content_file_obj, mb_data=mb_data)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
312
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
313
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
314 class Preview(base.CommandBase, BlogCommon):
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
315
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
316 def __init__(self, host):
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
317 super(Preview, self).__init__(host, 'preview', use_verbose=True, help=_(u'preview a blog content'))
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
318
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
319 def add_parser_options(self):
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
320 self.parser.add_argument("--inotify", type=str, choices=('auto', 'true', 'false'), default=u'auto', help=_(u"use inotify to handle preview"))
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
321 self.parser.add_argument("file", type=base.unicode_decoder, nargs='?', default=u'current', help=_(u"path to the content file"))
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
322
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
323 def showPreview(self):
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
324 # we implement showPreview here so we don't have to import webbroser and urllib
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
325 # when preview is not used
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
326 url = 'file:{}'.format(self.urllib.quote(self.preview_file_path))
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
327 self.webbrowser.open_new_tab(url)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
328
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
329 def updateContent(self):
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
330 with open(self.current_file_path, 'rb') as f:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
331 content_xhtml = f.read().decode('utf-8')
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
332 if self.syntax != 'XHTML':
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
333 # we use safe=True because we want to have a preview as close as possible to what the
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
334 # people will see
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
335 content_xhtml = self.host.bridge.syntaxConvert(content_xhtml, self.syntax, 'XHTML', True, self.profile)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
336
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
337 xhtml = (u'<html xmlns="http://www.w3.org/1999/xhtml">' +
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
338 u'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>'+
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
339 '<body>{}</body>' +
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
340 u'</html>').format(content_xhtml)
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
341
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
342 with open(self.preview_file_path, 'wb') as f:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
343 f.write(xhtml.encode('utf-8'))
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
344
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
345 def start(self):
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
346 import webbrowser
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
347 import urllib
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
348 self.webbrowser, self.urllib = webbrowser, urllib
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
349
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
350 if self.args.inotify != 'false':
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
351 try:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
352 import inotify.adapters
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
353 import inotify.constants
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
354 except ImportError:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
355 if self.args.inotify == 'auto':
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
356 inotify = None
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
357 self.disp(u'inotify module not found, deactivating feature. You can install it with {install}'.format(install=INOTIFY_INSTALL))
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
358 else:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
359 self.disp(u"inotify not found, can't activate the feature! Please install it with {install}".format(install=INOTIFY_INSTALL), error=True)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
360 self.host.quit(1)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
361 else:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
362 # we deactivate logging in inotify, which is quite annoying
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
363 try:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
364 inotify.adapters._LOGGER.setLevel(40)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
365 except AttributeError:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
366 self.disp(u"Logger doesn't exists, inotify may have chanded", error=True)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
367 else:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
368 inotify=None
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
369
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
370 sat_conf = config.parseMainConf()
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
371 SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {}))
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
372
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
373 open_cb = self.showPreview
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
374 update_cb = self.showPreview
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
375
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
376 # which file do we need to edit?
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
377 if self.args.file == 'current':
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
378 self.current_file_path = self.getCurrentFile(sat_conf)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
379 else:
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
380 self.current_file_path = os.path.abspath(self.args.file)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
381
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
382 self.syntax = self.guessSyntaxFromPath(sat_conf, self.current_file_path)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
383
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
384
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
385 # at this point the syntax is converted, we can display the preview
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
386 preview_file = tempfile.NamedTemporaryFile(suffix='.xhtml', delete=False)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
387 self.preview_file_path = preview_file.name
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
388 preview_file.close()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
389 self.updateContent()
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
390
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
391 if inotify is None:
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
392 # XXX: we don't delete file automatically because browser need it (and webbrowser.open can return before it is read)
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
393 self.disp(u'temporary file created at {}\nthis file will NOT BE DELETED AUTOMATICALLY, please delete it yourself when you have finished'.format(self.preview_file_path))
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
394 open_cb()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
395 else:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
396 open_cb()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
397 i = inotify.adapters.Inotify(block_duration_s=60) # no need for 1 s duraction, inotify drive actions here
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
398 # XXX: we only check IN_CLOSE_WRITE, but that may need to be changed depending on editor
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
399 # experience will tell the appropriate values
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
400 i.add_watch(self.current_file_path, mask=inotify.constants.IN_CLOSE_WRITE)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
401 try:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
402 for event in i.event_gen():
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
403 if event is not None:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
404 self.disp(u"Content updated", 1)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
405 self.updateContent()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
406 update_cb()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
407 finally:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
408 os.unlink(self.preview_file_path)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
409 i.remove_watch(self.current_file_path)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
410
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
411
1840
9eabf7fadfdd frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents: 1839
diff changeset
412 class Import(base.CommandAnswering):
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
413 def __init__(self, host):
1834
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
414 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1840
diff changeset
415 self.need_loop=True
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
416
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
417 def add_parser_options(self):
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
418 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
419 self.parser.add_argument('--host', type=base.unicode_decoder, help=_(u"original blog host"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
420 self.parser.add_argument('--no-images-upload', action='store_true', help=_(u"do *NOT* upload images (default: do upload images)"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
421 self.parser.add_argument('--upload-ignore-host', help=_(u"do not upload images from this host (default: upload all images)"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
422 self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate for uploads"))
1839
cdecf553e051 frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents: 1834
diff changeset
423 self.parser.add_argument('-o', '--option', action='append', nargs=2, default=[], metavar=(u'NAME', u'VALUE'),
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
424 help=_(u"importer specific options (see importer description)"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
425 self.parser.add_argument('--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE',
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
426 help=_(u"PubSub service where the items must be uploaded (default: server)"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
427 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?',
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
428 help=_(u"importer data location (see importer description), nothing to show importer description"))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
429
1834
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
430 def onProgressStarted(self, metadata):
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
431 self.disp(_(u'Blog upload started'),2)
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
432
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
433 def onProgressFinished(self, metadata):
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
434 self.disp(_(u'Blog uploaded successfully'),2)
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
435 redirections = {k[len(URL_REDIRECT_PREFIX):]:v for k,v in metadata.iteritems()
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
436 if k.startswith(URL_REDIRECT_PREFIX)}
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
437 if redirections:
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
438 conf = u'\n'.join([
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
439 u'url_redirections_profile = {}'.format(self.profile),
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
440 u"url_redirections_dict = {}".format(
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
441 # we need to add ' ' before each new line and to double each '%' for ConfigParser
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
442 u'\n '.join(json.dumps(redirections, indent=1, separators=(',',': ')).replace(u'%', u'%%').split(u'\n'))),
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
443 ])
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
444 self.disp(_(u'\nTo redirect old URLs to new ones, put the following lines in your sat.conf file, in [libervia] section:\n\n{conf}'.format(conf=conf)))
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
445
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
446 def onProgressError(self, error_msg):
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
447 self.disp(_(u'Error while uploading blog: {}').format(error_msg),error=True)
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
448
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
449 def error(self, failure):
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
450 self.disp(_("Error while trying to upload a blog: {reason}").format(reason=failure), error=True)
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
451 self.host.quit(1)
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
452
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1840
diff changeset
453 def start(self):
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
454 if self.args.location is None:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
455 for name in ('option', 'service', 'no_images_upload'):
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
456 if getattr(self.args, name):
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
457 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name))
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
458 if self.args.importer is None:
1839
cdecf553e051 frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents: 1834
diff changeset
459 print u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.blogImportList()])
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
460 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
461 try:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
462 short_desc, long_desc = self.host.bridge.blogImportDesc(self.args.importer)
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
463 except Exception as e:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
464 msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
465 print msg
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
466 self.host.quit(1)
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
467 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
468 print u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc)
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
469 self.host.quit()
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
470 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
471 # we have a location, an import is requested
1839
cdecf553e051 frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents: 1834
diff changeset
472 options = {key: value for key, value in self.args.option}
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
473 if self.args.host:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
474 options['host'] = self.args.host
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
475 if self.args.ignore_tls_errors:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
476 options['ignore_tls_errors'] = C.BOOL_TRUE
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
477 if self.args.no_images_upload:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
478 options['upload_images'] = C.BOOL_FALSE
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
479 if self.args.upload_ignore_host:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
480 self.parser.error(u"upload-ignore-host option can't be used when no-images-upload is set")
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
481 elif self.args.upload_ignore_host:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
482 options['upload_ignore_host'] = self.args.upload_ignore_host
1834
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
483 def gotId(id_):
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
484 self.progress_id = id_
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
485 self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile,
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
486 callback=gotId, errback=self.error)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
487
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
488
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
489 class Blog(base.CommandBase):
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
490 subcommands = (Edit, Preview, Import)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
491
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
492 def __init__(self, host):
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
493 super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management'))