Mercurial > libervia-backend
annotate frontends/src/jp/cmd_blog.py @ 2157:b4a515e36631
jp (blog): added blog/get command:
- blog/get will retrieve and parse blog items on specified service/node and can request only one or more specific items according to ids of max value
- it takes profit of outputs and the new extra_outputs: a default output display microblogs as key/value, a fancy one display it in a more confortable way for reading it the terminal
- for default output, keys can be filtered using --key argument
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Feb 2017 01:02:33 +0100 |
parents | 5fbe09b9b568 |
children | 970a348d3fe9 |
rev | line source |
---|---|
1960 | 1 #!/usr/bin/env python2 |
1827
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 _ |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
23 from sat_frontends.jp.constants import Const as C |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
24 from sat.tools.common.ansi import ANSI as A |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
25 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
|
26 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
|
27 import json |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
28 import sys |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
29 import os.path |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
30 import os |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
31 import time |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
32 import tempfile |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
33 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
|
34 import shlex |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
35 import glob |
1919
d3354c80bd1f
core (tools): moved common to a separate package, and put data method in a data_format module
Goffi <goffi@goffi.org>
parents:
1912
diff
changeset
|
36 from sat.tools.common import data_format |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
37 from sat.tools.common import regex |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
38 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
39 __commands__ = ["Blog"] |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
40 |
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
|
41 # 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
|
42 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
|
43 '': '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
|
44 "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
|
45 "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
|
46 } |
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
|
47 |
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 # 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
|
49 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
|
50 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
|
51 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
|
52 '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
|
53 '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
|
54 '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
|
55 '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
|
56 '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
|
57 } |
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
|
58 |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
59 CONF_SYNTAX_EXT = 'syntax_ext_dict' |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
60 BLOG_TMP_DIR="blog" |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
61 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
|
62 # key to remove from metadata tmp file if they exist |
1912
c38bcc0343b6
jp (blog): added "updated" to keys to keys to remove from tmp metadata
Goffi <goffi@goffi.org>
parents:
1906
diff
changeset
|
63 KEY_TO_REMOVE_METADATA = ('id','content', 'content_xhtml', 'comments_node', 'comments_service', 'updated') |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
64 |
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
|
65 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
|
66 INOTIFY_INSTALL = '"pip install inotify"' |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
67 SECURE_UNLINK_MAX = 10 * 2 # we double value as there are 2 files per draft (content and metadata) |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
68 SECURE_UNLINK_DIR = ".backup" |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
69 HEADER_ANSI = A.BOLD + A.FG_YELLOW |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
70 NS_MICROBLOG = u'urn:xmpp:microblog:0' |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
71 MB_KEYS = (u"id", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
72 u"atom_id", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
73 u"updated", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
74 u"published", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
75 u"comments", # this key is used for all comments* keys |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
76 u"tags", # this key is used for all tag* keys |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
77 u"author", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
78 u"author_jid", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
79 u"author_email", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
80 u"author_jid_verified", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
81 u"content", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
82 u"content_xhtml", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
83 u"title", |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
84 u"title_xhtml") |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
85 OUTPUT_OPT_NO_HEADER = u'no-header' |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
87 |
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
|
88 class BlogCommon(object): |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
89 |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
90 def __init__(self, host): |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
91 self.host = host |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
92 |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
93 def getTmpDir(self, sat_conf, sub_dir=None): |
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
|
94 """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
|
95 |
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 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
97 @param sub_dir(str): sub directory where data need to be put |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
98 profile can be used here, or special directory name |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
99 sub_dir will be escaped to be usable in path (use regex.pathUnescape to find |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
100 initial str) |
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
|
101 @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
|
102 """ |
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
|
103 local_dir = config.getConfig(sat_conf, '', 'local_dir', Exception) |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
104 path = [local_dir, BLOG_TMP_DIR] |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
105 if sub_dir is not None: |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
106 path.append(regex.pathEscape(sub_dir)) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
107 return os.path.join(*path) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
108 |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
109 def getCurrentFile(self, sat_conf, profile): |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
110 """Get most recently edited file |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
111 |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
112 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
113 @param profile(unicode): profile linked to the blog draft |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
114 @return(str): full path of current file |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
115 """ |
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
|
116 # 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
|
117 # 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
|
118 # in tmp_dir, excluding metadata files |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
119 tmp_dir = self.getTmpDir(sat_conf, profile.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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
126 def secureUnlink(self, sat_conf, path): |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
127 """Unlink given path after keeping it for a while |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
128 |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
129 This method is used to prevent accidental deletion of a blog draft |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
130 If there are more file in SECURE_UNLINK_DIR than SECURE_UNLINK_MAX, |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
131 older file are deleted |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
132 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
133 @param path(str): file to unlink |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
134 """ |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
135 if not os.path.isfile(path): |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
136 raise OSError(u"path must link to a regular file") |
1926
55a7328fafb6
jp (blog/edit): do not remove files if they are not in the temporary dir hierarchy
Goffi <goffi@goffi.org>
parents:
1925
diff
changeset
|
137 if not path.startswith(self.getTmpDir(sat_conf)): |
55a7328fafb6
jp (blog/edit): do not remove files if they are not in the temporary dir hierarchy
Goffi <goffi@goffi.org>
parents:
1925
diff
changeset
|
138 self.disp(u"File {} is not in blog temporary hierarchy, we do not remove it".format(path.decode('utf-8')), 2) |
55a7328fafb6
jp (blog/edit): do not remove files if they are not in the temporary dir hierarchy
Goffi <goffi@goffi.org>
parents:
1925
diff
changeset
|
139 return |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
140 backup_dir = self.getTmpDir(sat_conf, SECURE_UNLINK_DIR) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
141 if not os.path.exists(backup_dir): |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
142 os.makedirs(backup_dir) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
143 filename = os.path.basename(path) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
144 backup_path = os.path.join(backup_dir, filename) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
145 # we move file to backup dir |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
146 self.host.disp(u"Backuping file {src} to {dst}".format( |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
147 src=path.decode('utf-8'), dst=backup_path.decode('utf-8')), 1) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
148 os.rename(path, backup_path) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
149 # and if we exceeded the limit, we remove older file |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
150 backup_files = [os.path.join(backup_dir, f) for f in os.listdir(backup_dir)] |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
151 if len(backup_files) > SECURE_UNLINK_MAX: |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
152 backup_files.sort(key=lambda path: os.stat(path).st_mtime) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
153 for path in backup_files[:len(backup_files) - SECURE_UNLINK_MAX]: |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
154 self.host.disp(u"Purging backup file {}".format(path.decode('utf-8')), 2) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
155 os.unlink(path) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
156 |
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
|
157 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
|
158 """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
|
159 |
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
|
160 @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
|
161 @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
|
162 @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
|
163 """ |
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
|
164 # 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
|
165 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
|
166 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
|
167 for k,v in SYNTAX_EXT.iteritems(): |
1968
eca59bc4e6c6
jp (blog): fixed syntax detection when extension is .txt
Goffi <goffi@goffi.org>
parents:
1929
diff
changeset
|
168 if k and ext == v: |
1883
abb2f253188e
jp (blog): fixed syntax guessing on existing files
Goffi <goffi@goffi.org>
parents:
1882
diff
changeset
|
169 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
|
170 |
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
|
171 # 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
|
172 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
|
173 |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
174 def parse_args(self, cmd_line, **format_kw): |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
175 """Parse command arguments |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
176 |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
177 @param cmd_line(unicode): command line as found in sat.conf |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
178 @param format_kw: keywords used for formmating |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
179 @return (list(unicode)): list of arguments to pass to subprocess function |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
180 """ |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
181 try: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
182 # we split the arguments and add the known fields |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
183 # we split arguments first to avoid escaping issues in file names |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
184 return [a.format(**format_kw) for a in shlex.split(cmd_line)] |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
185 except ValueError as e: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
186 self.disp(u"Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=cmd_line, reason=e)) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
187 return [] |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
188 |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
189 |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
190 class Get(base.CommandBase): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
191 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
192 def __init__(self, host): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
193 extra_outputs = {'default': self.default_output, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
194 'fancy': self.fancy_output} |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
195 base.CommandBase.__init__(self, host, 'get', use_verbose=True, use_output='complex', extra_outputs=extra_outputs, help=_(u'get blog item(s)')) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
196 self.need_loop=True |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
197 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
198 def add_parser_options(self): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
199 self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=NS_MICROBLOG, help=_(u"PubSub node to request")) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
200 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items', |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
201 help=_(u"item(s) id(s) to get (default: request all items)")) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
202 self.parser.add_argument("-m", "--max", type=int, default=10, help=_(u"maximum number of items to get ({} to get all items)".format(C.NO_LIMIT))) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
203 # TODO: a key(s) argument to select keys to display |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
204 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys', |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
205 help=_(u"microblog data key(s) to display (default: depend of verbosity)")) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
206 self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'', |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
207 help=_(u"JID of the PubSub service (default: request profile own blog)")) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
208 # TODO: add MAM filters |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
209 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
210 def format_comments(self, item, keys): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
211 comments_data = data_format.dict2iterdict(u'comments', item, (u'node', u'service'), pop=True) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
212 lines = [] |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
213 for data in comments_data: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
214 lines.append(data[u'comments']) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
215 for k in (u'node', u'service'): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
216 if OUTPUT_OPT_NO_HEADER in self.args.output_opts: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
217 header = u'' |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
218 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
219 header = HEADER_ANSI + k + u': ' + A.RESET |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
220 lines.append(header + data[k]) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
221 return u'\n'.join(lines) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
222 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
223 def format_tags(self, item, keys): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
224 tags = data_format.dict2iter('tag', item, pop=True) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
225 return u', '.join(tags) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
226 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
227 def get_keys(self): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
228 """return keys to display according to verbosity or explicit key request""" |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
229 verbosity = self.args.verbose |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
230 if self.args.keys: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
231 if not set(MB_KEYS).issuperset(self.args.keys): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
232 self.disp(u"following keys are invalid: {invalid}.\n" |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
233 u"Valid keys are: {valid}.".format( |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
234 invalid = u', '.join(set(self.args.keys).difference(MB_KEYS)), |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
235 valid = u', '.join(sorted(MB_KEYS))), |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
236 error=True) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
237 self.host.quit(C.EXIT_BAD_ARG) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
238 return self.args.keys |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
239 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
240 if verbosity == 0: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
241 return (u'title', u'content') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
242 elif verbosity == 1: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
243 return (u"title", u"tags", u"author", u"author_jid", u"author_email", u"author_jid_verified", u"content") |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
244 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
245 return MB_KEYS |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
246 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
247 def default_output(self, data): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
248 """simple key/value output""" |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
249 items, metadata = data |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
250 keys = self.get_keys() |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
251 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
252 # k_cb use format_[key] methods for complex formattings |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
253 k_cb = {} |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
254 for k in keys: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
255 try: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
256 callback = getattr(self, "format_" + k) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
257 except AttributeError: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
258 pass |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
259 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
260 k_cb[k] = callback |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
261 for idx, item in enumerate(items): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
262 for k in keys: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
263 if k not in item and k not in k_cb: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
264 continue |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
265 if OUTPUT_OPT_NO_HEADER in self.args.output_opts: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
266 header = '' |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
267 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
268 header = u"{k_fmt}{key}:{k_fmt_e} {sep}".format( |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
269 k_fmt = HEADER_ANSI, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
270 key = k, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
271 k_fmt_e = A.RESET, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
272 sep = u'\n' if 'content' in k else u'') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
273 value = k_cb[k](item, keys) if k in k_cb else item[k] |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
274 print header + value |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
275 # we want a separation line after each item but the last one |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
276 if idx < len(items)-1: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
277 print(u'') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
278 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
279 def fancy_output(self, data): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
280 """display blog is a nice to read way |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
281 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
282 this output doesn't use keys filter |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
283 """ |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
284 # thanks to http://stackoverflow.com/a/943921 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
285 rows, columns = map(int, os.popen('stty size', 'r').read().split()) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
286 items, metadata = data |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
287 sep = A.color(A.FG_BLUE, columns * u'▬') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
288 if items: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
289 print(u'\n' + sep + '\n') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
290 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
291 for idx, item in enumerate(items): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
292 title = item.get(u'title') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
293 tags = list(data_format.dict2iter('tag', item, pop=True)) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
294 content = item.get(u'content') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
295 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
296 if title: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
297 print(A.color(A.BOLD, A.FG_CYAN, item[u'title'])) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
298 if tags: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
299 print(A.color(A.FG_YELLOW, u', '.join(tags))) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
300 if (title or tags) and content: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
301 print("") |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
302 if content: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
303 print content |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
304 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
305 print(u'\n' + sep + '\n') |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
306 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
307 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
308 def mbGetCb(self, mb_result): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
309 self.output(mb_result) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
310 self.host.quit(C.EXIT_OK) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
311 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
312 def mbGetEb(self, failure_): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
313 self.disp(u"can't get blog items: {reason}".format( |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
314 reason=failure_), error=True) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
315 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
316 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
317 def start(self): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
318 self.host.bridge.mbGet( |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
319 self.args.service, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
320 self.args.node, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
321 self.args.max, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
322 self.args.items, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
323 {}, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
324 self.profile, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
325 callback=self.mbGetCb, |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
326 errback=self.mbGetEb) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
327 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
328 |
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
|
329 class Edit(base.CommandBase, BlogCommon): |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
330 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
331 def __init__(self, host): |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
332 base.CommandBase.__init__(self, host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post')) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
333 BlogCommon.__init__(self, self.host) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
334 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
335 def add_parser_options(self): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
336 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
337 self.parser.add_argument("-P", "--preview", action="store_true", help=_(u"launch a blog preview in parallel")) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
338 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
|
339 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
|
340 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
|
341 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
342 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
|
343 """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
|
344 |
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
|
345 @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
|
346 @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
|
347 @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
|
348 """ |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
349 tmp_dir = self.getTmpDir(sat_conf, self.profile.encode('utf-8')) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
350 if not os.path.exists(tmp_dir): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
351 try: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
352 os.makedirs(tmp_dir) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
353 except OSError as e: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
354 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
|
355 path=tmp_dir, reason=e), error=True) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
356 self.host.quit(1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
357 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
|
358 fd, path = tempfile.mkstemp(suffix=tmp_suff, |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
359 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
|
360 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
|
361 return os.fdopen(fd, 'w+b'), path |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
362 except OSError as e: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
363 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
|
364 self.host.quit(1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
365 |
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
|
366 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
|
367 """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
|
368 |
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
|
369 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
|
370 @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
|
371 @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
|
372 @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
|
373 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
|
374 """ |
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
|
375 # 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
|
376 # 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
|
377 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
|
378 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
|
379 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
|
380 try: |
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
381 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
|
382 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
|
383 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
|
384 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
|
385 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
|
386 self.host.quit(1) |
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
387 else: |
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
388 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
|
389 |
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
390 # 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
|
391 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
|
392 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
|
393 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
|
394 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
|
395 pass |
1876
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
396 # 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
|
397 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
|
398 if self.args.tag: |
1923
e28c6c4dffa5
jp (blog): avoid conflict error when command line tags are already present in metadata
Goffi <goffi@goffi.org>
parents:
1921
diff
changeset
|
399 data_format.iter2dict('tag', self.args.tag, mb_data, check_conflict=False) |
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
|
400 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
|
401 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
|
402 |
1876
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
403 # 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
|
404 # XXX: if we port jp one day on Windows, O_BINARY may need to be added here |
1927
03f780b60d5d
jp (blog/edit): added forgotten O_TRUNC while opening metadata file
Goffi <goffi@goffi.org>
parents:
1926
diff
changeset
|
405 with os.fdopen(os.open(meta_file_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC,0o600), 'w+b') 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
|
406 # 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
|
407 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
|
408 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
|
409 |
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
|
410 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
|
411 |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
412 def edit(self, sat_conf, content_file_path, content_file_obj, |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
413 pubsub_service, pubsub_node, mb_data=None): |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
414 """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
|
415 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
|
416 # 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
|
417 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
|
418 |
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
|
419 # then we calculate hashes to check for modifications |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
420 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
|
421 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
|
422 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
|
423 content_file_obj.close() |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
424 |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
425 # do we need a preview ? |
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
426 if self.args.preview: |
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
427 self.disp(u"Preview requested, launching it", 1) |
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
428 # we redirect outputs to /dev/null to avoid console pollution in editor |
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
429 # if user wants to see messages, (s)he can call "blog preview" directly |
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
430 DEVNULL = open(os.devnull, 'wb') |
1894
116a55865364
frontends (jp / blog): pass the profile to subprocess when using option --preview
souliane <souliane@mailoo.org>
parents:
1893
diff
changeset
|
431 subprocess.Popen([sys.argv[0], "blog", "preview", "--inotify", "true", "-p", self.profile, content_file_path], stdout=DEVNULL, stderr=subprocess.STDOUT) |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
432 |
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
|
433 # then we launch editor |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
434 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
|
435 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
|
436 # 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
|
437 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
|
438 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
|
439 # 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
|
440 editor_args = EDITOR_ARGS_MAGIC.get(os.path.basename(editor), '') |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
441 args = self.parse_args(editor_args, content_file=content_file_path, metadata_file=meta_file_path) |
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
|
442 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
|
443 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
|
444 editor_exit = subprocess.call([editor] + args) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
445 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
446 # we send the file if edition was a success |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
447 if editor_exit != 0: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
448 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
|
449 path=content_file_path), error=True) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 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
|
456 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
|
457 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
|
458 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
|
459 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
|
460 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
|
461 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
|
462 "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
|
463 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
|
464 self.host.quit(1) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
465 |
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
|
466 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
|
467 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
|
468 "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
|
469 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
|
470 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
|
471 |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
472 if len(content) == 0: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
473 self.disp(u"Content is empty, cancelling the blog edition") |
1926
55a7328fafb6
jp (blog/edit): do not remove files if they are not in the temporary dir hierarchy
Goffi <goffi@goffi.org>
parents:
1925
diff
changeset
|
474 if not content_file_path.startswith(self.getTmpDir(sat_conf)): |
55a7328fafb6
jp (blog/edit): do not remove files if they are not in the temporary dir hierarchy
Goffi <goffi@goffi.org>
parents:
1925
diff
changeset
|
475 self.disp(u"File are not in blog temporary hierarchy, we do not remove it", 2) |
55a7328fafb6
jp (blog/edit): do not remove files if they are not in the temporary dir hierarchy
Goffi <goffi@goffi.org>
parents:
1925
diff
changeset
|
476 self.host.quit() |
1924
70b1a29e1338
jp (blog): force deletion (without backup) when content is empty
Goffi <goffi@goffi.org>
parents:
1923
diff
changeset
|
477 self.disp(u"Deletion of {}".format(content_file_path.decode('utf-8')), 2) |
70b1a29e1338
jp (blog): force deletion (without backup) when content is empty
Goffi <goffi@goffi.org>
parents:
1923
diff
changeset
|
478 os.unlink(content_file_path) |
70b1a29e1338
jp (blog): force deletion (without backup) when content is empty
Goffi <goffi@goffi.org>
parents:
1923
diff
changeset
|
479 self.disp(u"Deletion of {}".format(meta_file_path.decode('utf-8')), 2) |
70b1a29e1338
jp (blog): force deletion (without backup) when content is empty
Goffi <goffi@goffi.org>
parents:
1923
diff
changeset
|
480 os.unlink(meta_file_path) |
70b1a29e1338
jp (blog): force deletion (without backup) when content is empty
Goffi <goffi@goffi.org>
parents:
1923
diff
changeset
|
481 self.host.quit() |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
482 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
483 # 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
|
484 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
|
485 meta_ori == mb_data): |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
486 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
|
487 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
488 else: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
489 # we can now send the blog |
1929
b603a3d516d3
jp (blog/edit): use utf-8-sig to decode content file to avoid BOM being propaged to content
Goffi <goffi@goffi.org>
parents:
1927
diff
changeset
|
490 mb_data['content_rich'] = content.decode('utf-8-sig') # we use utf-8-sig to avoid BOM |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
491 |
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
|
492 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
|
493 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
|
494 |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
495 try: |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
496 self.host.bridge.mbSend(pubsub_service, pubsub_node, mb_data, self.profile) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
497 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
|
498 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
|
499 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
|
500 self.host.quit(1) |
1888
7b9cdde29d8b
jp (blog/edit): added a confirmation message when blog item has been successfuly published
Goffi <goffi@goffi.org>
parents:
1887
diff
changeset
|
501 else: |
7b9cdde29d8b
jp (blog/edit): added a confirmation message when blog item has been successfuly published
Goffi <goffi@goffi.org>
parents:
1887
diff
changeset
|
502 self.disp(u"Blog item published") |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
503 |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
504 self.secureUnlink(sat_conf, content_file_path) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
505 self.secureUnlink(sat_conf, meta_file_path) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
506 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
507 def start(self): |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
508 command = self.args.item.lower() |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
509 sat_conf = config.parseMainConf() |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
510 # if there are user defined extension, we use them |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
511 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
|
512 current_syntax = None |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
513 pubsub_service = pubsub_node = '' |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
514 pubsub_item = None |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
515 |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
516 if command not in ('new', 'last', 'current'): |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
517 # we have probably an URL, we try to parse it |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
518 import urlparse |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
519 url = self.args.item |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
520 parsed_url = urlparse.urlsplit(url) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
521 if parsed_url.scheme.startswith('http'): |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
522 self.disp(u"{} URL found, trying to find associated xmpp: URI".format(parsed_url.scheme.upper()),1) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
523 # HTTP URL, we try to find xmpp: links |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
524 try: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
525 from lxml import etree |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
526 except ImportError: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
527 self.disp(u"lxml module must be installed to use http(s) scheme, please install it with \"pip install lxml\"", error=True) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
528 self.host.quit(1) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
529 parser = etree.HTMLParser() |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
530 root = etree.parse(url, parser) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
531 links = root.xpath("//link[@rel='alternate' and starts-with(@href, 'xmpp:')]") |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
532 if not links: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
533 self.disp(u'Could not find alternate "xmpp:" URI, can\'t find associated XMPP PubSub node/item', error=True) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
534 self.host.quit(1) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
535 url = links[0].get('href') |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
536 parsed_url = urlparse.urlsplit(url) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
537 |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
538 if parsed_url.scheme == 'xmpp': |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
539 self.disp(u"XMPP URI used: {}".format(url),2) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
540 # XXX: if we have not xmpp: URI here, we'll take the data as a file path |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
541 pubsub_service = parsed_url.path |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
542 pubsub_data = urlparse.parse_qs(parsed_url.query) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
543 try: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
544 pubsub_node = pubsub_data['node'][0] |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
545 except KeyError: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
546 self.disp(u'No node found in xmpp: URI, can\'t retrieve item', error=True) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
547 self.host.quit(1) |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
548 pubsub_item = pubsub_data.get('item',[None])[0] |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
549 if pubsub_item is not None: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
550 command = 'edit' # XXX: edit command is only used internaly, it similar to last, but with the item given in the URL |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
551 else: |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
552 command = 'new' |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
553 |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
554 if command in ('new', 'last', 'edit'): |
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
|
555 # 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
|
556 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
|
557 # 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
|
558 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
|
559 content_file_obj, content_file_path = self.getTmpFile(sat_conf, tmp_suff) |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
560 if command == 'new': |
1905
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
561 self.disp(u'Editing a new blog item', 2) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
562 mb_data = None |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
563 elif command in ('last', 'edit'): |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
564 self.disp(u'Editing requested published item', 2) |
1905
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
565 try: |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
566 items_ids = [pubsub_item] if pubsub_item is not None else [] |
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
567 mb_data = self.host.bridge.mbGet(pubsub_service, pubsub_node, 1, items_ids, {}, self.profile)[0][0] |
1905
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
568 except Exception as e: |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
569 self.disp(u"Error while retrieving last item: {}".format(e)) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
570 self.host.quit(1) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
571 |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
572 content = mb_data['content_xhtml'] |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
573 if content and current_syntax != 'XHTML': |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
574 content = self.host.bridge.syntaxConvert(content, 'XHTML', current_syntax, False, self.profile) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
575 content_file_obj.write(content.encode('utf-8')) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
576 content_file_obj.seek(0) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
577 else: |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
578 mb_data = None |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
579 if command == 'current': |
1905
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
580 # use wants to continue current draft |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
581 content_file_path = self.getCurrentFile(sat_conf, self.profile) |
1905
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
582 self.disp(u'Continuing edition of current draft', 2) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
583 else: |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
584 # we consider the item as a file path |
1905
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
585 content_file_path = os.path.expanduser(self.args.item) |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
586 content_file_obj = open(content_file_path, 'r+b') |
e0bfdd379e8d
jp (blog/edit): a draft file can be directly specified
Goffi <goffi@goffi.org>
parents:
1904
diff
changeset
|
587 current_syntax = self.guessSyntaxFromPath(sat_conf, content_file_path) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
588 |
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
|
589 self.disp(u"Syntax used: {}".format(current_syntax), 1) |
1925
53b51866747f
jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents:
1924
diff
changeset
|
590 self.edit(sat_conf, content_file_path, content_file_obj, pubsub_service, pubsub_node, 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
|
591 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
592 |
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
|
593 class Preview(base.CommandBase, BlogCommon): |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
594 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
595 def __init__(self, host): |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
596 base.CommandBase.__init__(self, host, 'preview', use_verbose=True, help=_(u'preview a blog content')) |
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
597 BlogCommon.__init__(self, self.host) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
598 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
599 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
|
600 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
|
601 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
|
602 |
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
|
603 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
|
604 # 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
|
605 # 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
|
606 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
|
607 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
|
608 |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
609 def _launchPreviewExt(self, cmd_line, opt_name): |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
610 url = 'file:{}'.format(self.urllib.quote(self.preview_file_path)) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
611 args = self.parse_args(cmd_line, url=url, preview_file=self.preview_file_path) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
612 if not args: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
613 self.disp(u"Couln't find command in \"{name}\", abording".format(name=opt_name), error=True) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
614 self.host.quit(1) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
615 subprocess.Popen(args) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
616 |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
617 def openPreviewExt(self): |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
618 self._launchPreviewExt(self.open_cb_cmd, "blog_preview_open_cmd") |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
619 |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
620 def updatePreviewExt(self): |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
621 self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd") |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
622 |
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
|
623 def updateContent(self): |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
624 with open(self.content_file_path, 'rb') as f: |
1929
b603a3d516d3
jp (blog/edit): use utf-8-sig to decode content file to avoid BOM being propaged to content
Goffi <goffi@goffi.org>
parents:
1927
diff
changeset
|
625 content = f.read().decode('utf-8-sig') |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
626 if content and self.syntax != '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
|
627 # 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
|
628 # people will see |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
629 content = self.host.bridge.syntaxConvert(content, self.syntax, 'XHTML', True, self.profile) |
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
|
630 |
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
|
631 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
|
632 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
|
633 '<body>{}</body>' + |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
634 u'</html>').format(content) |
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
|
635 |
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
|
636 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
|
637 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
|
638 |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
639 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
|
640 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
|
641 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
|
642 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
|
643 |
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
|
644 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
|
645 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
|
646 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
|
647 import inotify.constants |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
648 from inotify.calls import InotifyError |
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
|
649 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
|
650 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
|
651 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
|
652 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
|
653 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
|
654 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
|
655 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
|
656 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
|
657 # 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 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
|
663 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
|
664 |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
665 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
|
666 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
|
667 |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
668 try: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
669 self.open_cb_cmd = config.getConfig(sat_conf, 'jp', "blog_preview_open_cmd", Exception) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
670 except (NoOptionError, NoSectionError): |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
671 self.open_cb_cmd = None |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
672 open_cb = self.showPreview |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
673 else: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
674 open_cb = self.openPreviewExt |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
675 |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
676 self.update_cb_cmd = config.getConfig(sat_conf, 'jp', "blog_preview_update_cmd", self.open_cb_cmd) |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
677 if self.update_cb_cmd is None: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
678 update_cb = self.showPreview |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
679 else: |
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
680 update_cb = self.updatePreviewExt |
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
|
681 |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
682 # which file do we need to edit? |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
683 if self.args.file == 'current': |
1921
b111f6589da4
jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents:
1919
diff
changeset
|
684 self.content_file_path = self.getCurrentFile(sat_conf, self.profile) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
685 else: |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
686 self.content_file_path = os.path.abspath(self.args.file) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
687 |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
688 self.syntax = self.guessSyntaxFromPath(sat_conf, self.content_file_path) |
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
|
689 |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
690 |
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
|
691 # 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
|
692 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
|
693 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
|
694 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
|
695 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
|
696 |
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
|
697 if inotify is None: |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
698 # 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
|
699 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
|
700 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
|
701 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
|
702 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
|
703 i = inotify.adapters.Inotify(block_duration_s=60) # no need for 1 s duraction, inotify drive actions here |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
704 |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
705 def add_watch(): |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
706 i.add_watch(self.content_file_path, mask=inotify.constants.IN_CLOSE_WRITE | |
1893
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
707 inotify.constants.IN_DELETE_SELF | |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
708 inotify.constants.IN_MOVE_SELF) |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
709 add_watch() |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
710 |
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
|
711 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
|
712 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
|
713 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
|
714 self.disp(u"Content updated", 1) |
1893
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
715 if {"IN_DELETE_SELF", "IN_MOVE_SELF"}.intersection(event[1]): |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
716 self.disp(u"{} event catched, changing the watch".format(", ".join(event[1])), 2) |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
717 try: |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
718 add_watch() |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
719 except InotifyError: |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
720 # if the new file is not here yet we can have an error |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
721 # as a workaround, we do a little rest |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
722 time.sleep(1) |
4f252b72b193
jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents:
1888
diff
changeset
|
723 add_watch() |
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
|
724 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
|
725 update_cb() |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
726 except InotifyError: |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
727 self.disp(u"Can't catch inotify events, as the file been deleted?", error=True) |
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
|
728 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
|
729 os.unlink(self.preview_file_path) |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
730 try: |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
731 i.remove_watch(self.content_file_path) |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
732 except InotifyError: |
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
733 pass |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
734 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
735 |
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
|
736 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
|
737 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
|
738 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
|
739 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
|
740 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
741 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
|
742 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
|
743 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
|
744 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
|
745 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
|
746 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
|
747 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
|
748 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
|
749 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
|
750 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
|
751 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
|
752 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
|
753 |
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
|
754 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
|
755 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
|
756 |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
757 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
|
758 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
|
759 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
|
760 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
|
761 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
|
762 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
|
763 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
|
764 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
|
765 # 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
|
766 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
|
767 ]) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
768 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
|
769 |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
770 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
|
771 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
|
772 |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
773 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
|
774 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
|
775 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
|
776 |
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
|
777 def start(self): |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
778 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
|
779 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
|
780 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
|
781 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
|
782 if self.args.importer is None: |
1839
cdecf553e051
frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents:
1834
diff
changeset
|
783 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
|
784 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
785 try: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
786 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
|
787 except Exception as e: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
788 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
|
789 print msg |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
790 self.host.quit(1) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
791 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
792 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
|
793 self.host.quit() |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
794 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
795 # 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
|
796 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
|
797 if self.args.host: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
798 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
|
799 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
|
800 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
|
801 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
|
802 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
|
803 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
|
804 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
|
805 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
|
806 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
|
807 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
|
808 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
|
809 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
|
810 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
|
811 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
812 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
813 class Blog(base.CommandBase): |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
814 subcommands = (Get, Edit, Preview, Import) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
815 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
816 def __init__(self, host): |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
817 super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management')) |