Mercurial > libervia-backend
annotate libervia/cli/common.py @ 4097:0f6fd28fde0d
component file sharing: fix method name case:
`render_OPTIONS` is a method inherited from Twisted, name has been put to lowercase
following massive refactoring, resulting in the method not being called anymore. This
patch fixes it.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jun 2023 15:45:20 +0200 |
parents | 47401850dec6 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
4 # Libervia CLI |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
3040 | 20 import json |
21 import os | |
22 import os.path | |
23 import time | |
24 import tempfile | |
25 import asyncio | |
26 import shlex | |
3121 | 27 import re |
3040 | 28 from pathlib import Path |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
29 from libervia.cli.constants import Const as C |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
30 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
31 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
32 from libervia.backend.tools.common import regex |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
33 from libervia.backend.tools.common.ansi import ANSI as A |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.tools.common import uri as xmpp_uri |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.tools import config |
3028 | 36 from configparser import NoSectionError, NoOptionError |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
37 from collections import namedtuple |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
3676
fc24e611c9aa
jp (common): add neovim (`nvim`) default arguments
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
39 # default arguments used for some known editors (editing with metadata) |
2523
21d43eab3fb9
jp (common): assure nosplitright in default commands for edition with vim
Goffi <goffi@goffi.org>
parents:
2493
diff
changeset
|
40 VIM_SPLIT_ARGS = "-c 'set nospr|vsplit|wincmd w|next|wincmd w'" |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 EMACS_SPLIT_ARGS = '--eval "(split-window-horizontally)"' |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 EDITOR_ARGS_MAGIC = { |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
43 "vim": VIM_SPLIT_ARGS + " {content_file} {metadata_file}", |
3676
fc24e611c9aa
jp (common): add neovim (`nvim`) default arguments
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
44 "nvim": VIM_SPLIT_ARGS + " {content_file} {metadata_file}", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
45 "gvim": VIM_SPLIT_ARGS + " --nofork {content_file} {metadata_file}", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
46 "emacs": EMACS_SPLIT_ARGS + " {content_file} {metadata_file}", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
47 "xemacs": EMACS_SPLIT_ARGS + " {content_file} {metadata_file}", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
48 "nano": " -F {content_file} {metadata_file}", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
49 } |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 SECURE_UNLINK_MAX = 10 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 SECURE_UNLINK_DIR = ".backup" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
53 METADATA_SUFF = "_metadata.json" |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
3667
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
56 def format_time(timestamp): |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
57 """Return formatted date for timestamp |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
58 |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
59 @param timestamp(str,int,float): unix timestamp |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
60 @return (unicode): formatted date |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
61 """ |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
62 fmt = "%d/%m/%Y %H:%M:%S %Z" |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
63 return time.strftime(fmt, time.localtime(float(timestamp))) |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
64 |
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3626
diff
changeset
|
65 |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
66 def ansi_ljust(s, width): |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
67 """ljust method handling ANSI escape codes""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
68 cleaned = regex.ansi_remove(s) |
3028 | 69 return s + " " * (width - len(cleaned)) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
70 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
71 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
72 def ansi_center(s, width): |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
73 """ljust method handling ANSI escape codes""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
74 cleaned = regex.ansi_remove(s) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
75 diff = width - len(cleaned) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
76 half = diff / 2 |
3028 | 77 return half * " " + s + (half + diff % 2) * " " |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
78 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
79 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
80 def ansi_rjust(s, width): |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
81 """ljust method handling ANSI escape codes""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
82 cleaned = regex.ansi_remove(s) |
3028 | 83 return " " * (width - len(cleaned)) + s |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
84 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
85 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
86 def get_tmp_dir(sat_conf, cat_dir, sub_dir=None): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 """Return directory used to store temporary files |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
3040 | 90 @param cat_dir(str): directory of the category (e.g. "blog") |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 @param sub_dir(str): sub directory where data need to be put |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 profile can be used here, or special directory name |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
93 sub_dir will be escaped to be usable in path (use regex.path_unescape to find |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 initial str) |
3040 | 95 @return (Path): path to the dir |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
97 local_dir = config.config_get(sat_conf, "", "local_dir", Exception) |
3040 | 98 path_elts = [local_dir, cat_dir] |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 if sub_dir is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
100 path_elts.append(regex.path_escape(sub_dir)) |
3040 | 101 return Path(*path_elts) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
104 def parse_args(host, cmd_line, **format_kw): |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
105 """Parse command arguments |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
106 |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
107 @param cmd_line(unicode): command line as found in sat.conf |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
108 @param format_kw: keywords used for formating |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
109 @return (list(unicode)): list of arguments to pass to subprocess function |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
110 """ |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
111 try: |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
112 # we split the arguments and add the known fields |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
113 # we split arguments first to avoid escaping issues in file names |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
114 return [a.format(**format_kw) for a in shlex.split(cmd_line)] |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
115 except ValueError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
116 host.disp( |
3028 | 117 "Couldn't parse editor cmd [{cmd}]: {reason}".format(cmd=cmd_line, reason=e) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
118 ) |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
119 return [] |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
120 |
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
121 |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 class BaseEdit(object): |
3028 | 123 """base class for editing commands |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 This class allows to edit file for PubSub or something else. |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 It works with temporary files in SàT local_dir, in a "cat_dir" subdir |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 """ |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
129 def __init__(self, host, cat_dir, use_metadata=False): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 """ |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 @param cat_dir(unicode): directory to use for drafts |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 this will be a sub-directory of SàT's local_dir |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 @param use_metadata(bool): True is edition need a second file for metadata |
3040 | 135 most of signature change with use_metadata with an additional metadata |
136 argument. | |
137 This is done to raise error if a command needs metadata but forget the flag, | |
138 and vice versa | |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 """ |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 self.host = host |
3040 | 141 self.cat_dir = cat_dir |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 self.use_metadata = use_metadata |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
144 def secure_unlink(self, path): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 """Unlink given path after keeping it for a while |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 This method is used to prevent accidental deletion of a draft |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 If there are more file in SECURE_UNLINK_DIR than SECURE_UNLINK_MAX, |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 older file are deleted |
3040 | 150 @param path(Path, str): file to unlink |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 """ |
3040 | 152 path = Path(path).resolve() |
153 if not path.is_file: | |
3028 | 154 raise OSError("path must link to a regular file") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
155 if path.parent != get_tmp_dir(self.sat_conf, self.cat_dir): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
156 self.disp( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
157 f"File {path} is not in SàT temporary hierarchy, we do not remove " f"it", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
158 2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
159 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 return |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
161 # we have 2 files per draft with use_metadata, so we double max |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 unlink_max = SECURE_UNLINK_MAX * 2 if self.use_metadata else SECURE_UNLINK_MAX |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
163 backup_dir = get_tmp_dir(self.sat_conf, self.cat_dir, SECURE_UNLINK_DIR) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 if not os.path.exists(backup_dir): |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 os.makedirs(backup_dir) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 filename = os.path.basename(path) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 backup_path = os.path.join(backup_dir, filename) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 # we move file to backup dir |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
169 self.host.disp( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
170 "Backuping file {src} to {dst}".format(src=path, dst=backup_path), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
171 1, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
172 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 os.rename(path, backup_path) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 # and if we exceeded the limit, we remove older file |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 backup_files = [os.path.join(backup_dir, f) for f in os.listdir(backup_dir)] |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 if len(backup_files) > unlink_max: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 backup_files.sort(key=lambda path: os.stat(path).st_mtime) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
178 for path in backup_files[: len(backup_files) - unlink_max]: |
3028 | 179 self.host.disp("Purging backup file {}".format(path), 2) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 os.unlink(path) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
182 async def run_editor( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
183 self, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
184 editor_args_opt, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
185 content_file_path, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
186 content_file_obj, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
187 meta_file_path=None, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
188 meta_ori=None, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
189 ): |
3040 | 190 """Run editor to edit content and metadata |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
192 @param editor_args_opt(unicode): option in [cli] section in configuration for |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 specific args |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 @param content_file_path(str): path to the content file |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 @param content_file_obj(file): opened file instance |
3040 | 196 @param meta_file_path(str, Path, None): metadata file path |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 if None metadata will not be used |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 @param meta_ori(dict, None): original cotent of metadata |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 can't be used if use_metadata is False |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 """ |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 if not self.use_metadata: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 assert meta_file_path is None |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 assert meta_ori is None |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
205 # we calculate hashes to check for modifications |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 import hashlib |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
207 |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 content_file_obj.seek(0) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 tmp_ori_hash = hashlib.sha1(content_file_obj.read()).digest() |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 content_file_obj.close() |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 # we prepare arguments |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
213 editor = config.config_get(self.sat_conf, C.CONFIG_SECTION, "editor") or os.getenv( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
214 "EDITOR", "vi" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
215 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 try: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 # is there custom arguments in sat.conf ? |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
218 editor_args = config.config_get( |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
219 self.sat_conf, C.CONFIG_SECTION, editor_args_opt, Exception |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
220 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 except (NoOptionError, NoSectionError): |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 # no, we check if we know the editor and have special arguments |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
223 if self.use_metadata: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
224 editor_args = EDITOR_ARGS_MAGIC.get(os.path.basename(editor), "") |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
225 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
226 editor_args = "" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
227 parse_kwargs = {"content_file": content_file_path} |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 if self.use_metadata: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
229 parse_kwargs["metadata_file"] = meta_file_path |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
230 args = parse_args(self.host, editor_args, **parse_kwargs) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 if not args: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 args = [content_file_path] |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
234 # actual editing |
3040 | 235 editor_process = await asyncio.create_subprocess_exec( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
236 editor, *[str(a) for a in args] |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
237 ) |
3040 | 238 editor_exit = await editor_process.wait() |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 # edition will now be checked, and data will be sent if it was a success |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 if editor_exit != 0: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
242 self.disp( |
3040 | 243 f"Editor exited with an error code, so temporary file has not be " |
244 f"deleted, and item is not published.\nYou can find temporary file " | |
245 f"at {content_file_path}", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
246 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
247 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 else: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 # main content |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 try: |
3040 | 251 with content_file_path.open("rb") as f: |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 content = f.read() |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 except (OSError, IOError): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
254 self.disp( |
3040 | 255 f"Can read file at {content_file_path}, have it been deleted?\n" |
256 f"Cancelling edition", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
257 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
258 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 self.host.quit(C.EXIT_NOT_FOUND) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
261 # metadata |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 if self.use_metadata: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 try: |
3040 | 264 with meta_file_path.open("rb") as f: |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 metadata = json.load(f) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 except (OSError, IOError): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
267 self.disp( |
3040 | 268 f"Can read file at {meta_file_path}, have it been deleted?\n" |
269 f"Cancelling edition", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
270 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
271 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 self.host.quit(C.EXIT_NOT_FOUND) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 except ValueError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
274 self.disp( |
3040 | 275 f"Can't parse metadata, please check it is correct JSON format. " |
276 f"Cancelling edition.\nYou can find tmp file at " | |
277 f"{content_file_path} and temporary meta file at " | |
278 f"{meta_file_path}.", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
279 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
280 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 self.host.quit(C.EXIT_DATA_ERROR) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 |
2905
d9491cb81726
jp (common): `publish` is now a boolean
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
283 if self.use_metadata and not metadata.get("publish", True): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
284 self.disp( |
3040 | 285 f'Publication blocked by "publish" key in metadata, cancelling ' |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
286 f"edition.\n\ntemporary file path:\t{content_file_path}\nmetadata " |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
287 f"file path:\t{meta_file_path}", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
288 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
289 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 self.host.quit() |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 if len(content) == 0: |
3028 | 293 self.disp("Content is empty, cancelling the edition") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
294 if content_file_path.parent != get_tmp_dir(self.sat_conf, self.cat_dir): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
295 self.disp( |
3028 | 296 "File are not in SàT temporary hierarchy, we do not remove them", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
297 2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
298 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 self.host.quit() |
3040 | 300 self.disp(f"Deletion of {content_file_path}", 2) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 os.unlink(content_file_path) |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
302 if self.use_metadata: |
3040 | 303 self.disp(f"Deletion of {meta_file_path}".format(meta_file_path), 2) |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
304 os.unlink(meta_file_path) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 self.host.quit() |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
307 # time to re-check the hash |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
308 elif tmp_ori_hash == hashlib.sha1(content).digest() and ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
309 not self.use_metadata or meta_ori == metadata |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
310 ): |
3028 | 311 self.disp("The content has not been modified, cancelling the edition") |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 self.host.quit() |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 else: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 # we can now send the item |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
316 content = content.decode("utf-8-sig") # we use utf-8-sig to avoid BOM |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 try: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 if self.use_metadata: |
3040 | 319 await self.publish(content, metadata) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 else: |
3040 | 321 await self.publish(content) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 except Exception as e: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 if self.use_metadata: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
324 self.disp( |
3040 | 325 f"Error while sending your item, the temporary files have " |
326 f"been kept at {content_file_path} and {meta_file_path}: " | |
327 f"{e}", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
328 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
329 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
331 self.disp( |
3040 | 332 f"Error while sending your item, the temporary file has been " |
333 f"kept at {content_file_path}: {e}", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
334 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
335 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 self.host.quit(1) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
338 self.secure_unlink(content_file_path) |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
339 if self.use_metadata: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
340 self.secure_unlink(meta_file_path) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 |
3040 | 342 async def publish(self, content): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 # if metadata is needed, publish will be called with it last argument |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 raise NotImplementedError |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
346 def get_tmp_file(self): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 """Create a temporary file |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 |
3040 | 349 @return (tuple(file, Path)): opened (w+b) file object and file path |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
351 suff = "." + self.get_tmp_suff() |
3040 | 352 cat_dir_str = self.cat_dir |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
353 tmp_dir = get_tmp_dir(self.sat_conf, self.cat_dir, self.profile) |
3040 | 354 if not tmp_dir.exists(): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 try: |
3040 | 356 tmp_dir.mkdir(parents=True) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 except OSError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
358 self.disp( |
3040 | 359 f"Can't create {tmp_dir} directory: {e}", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
360 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
361 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 self.host.quit(1) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
364 fd, path = tempfile.mkstemp( |
3040 | 365 suffix=suff, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
366 prefix=time.strftime(cat_dir_str + "_%Y-%m-%d_%H:%M:%S_"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
367 dir=tmp_dir, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
368 text=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
369 ) |
3040 | 370 return os.fdopen(fd, "w+b"), Path(path) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 except OSError as e: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
372 self.disp(f"Can't create temporary file: {e}", error=True) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 self.host.quit(1) |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
375 def get_current_file(self, profile): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 """Get most recently edited file |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 @param profile(unicode): profile linked to the draft |
3040 | 379 @return(Path): full path of current file |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 """ |
2273
5f0dbf42aa9c
jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents:
2270
diff
changeset
|
381 # we guess the item currently edited by choosing |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 # the most recent file corresponding to temp file pattern |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 # in tmp_dir, excluding metadata files |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
384 tmp_dir = get_tmp_dir(self.sat_conf, self.cat_dir, profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
385 available = [ |
3040 | 386 p |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
387 for p in tmp_dir.glob(f"{self.cat_dir}_*") |
3040 | 388 if not p.match(f"*{METADATA_SUFF}") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
389 ] |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
390 if not available: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
391 self.disp( |
3040 | 392 f"Could not find any content draft in {tmp_dir}", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
393 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
394 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 self.host.quit(1) |
3040 | 396 return max(available, key=lambda p: p.stat().st_mtime) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
398 async def get_item_data(self, service, node, item): |
2280
4bc9a2c2d6c9
jp (pubsub, common): fixed last item edition (keep item id instead of creating a new one)
Goffi <goffi@goffi.org>
parents:
2279
diff
changeset
|
399 """return formatted content, metadata (or not if use_metadata is false), and item id""" |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 raise NotImplementedError |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
402 def get_tmp_suff(self): |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 """return suffix used for content file""" |
3028 | 404 return "xml" |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
406 async def get_item_path(self): |
3040 | 407 """Retrieve item path (i.e. service and node) from item argument |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
408 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 This method is obviously only useful for edition of PubSub based features |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 """ |
2532 | 411 service = self.args.service |
412 node = self.args.node | |
413 item = self.args.item | |
414 last_item = self.args.last_item | |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
415 |
2532 | 416 if self.args.current: |
417 # user wants to continue current draft | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
418 content_file_path = self.get_current_file(self.profile) |
3028 | 419 self.disp("Continuing edition of current draft", 2) |
3040 | 420 content_file_obj = content_file_path.open("r+b") |
2532 | 421 # we seek at the end of file in case of an item already exist |
422 # this will write content of the existing item at the end of the draft. | |
423 # This way no data should be lost. | |
424 content_file_obj.seek(0, os.SEEK_END) | |
425 elif self.args.draft_path: | |
426 # there is an existing draft that we use | |
3040 | 427 content_file_path = self.args.draft_path.expanduser() |
428 content_file_obj = content_file_path.open("r+b") | |
2532 | 429 # we seek at the end for the same reason as above |
430 content_file_obj.seek(0, os.SEEK_END) | |
431 else: | |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
432 # we need a temporary file |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
433 content_file_obj, content_file_path = self.get_tmp_file() |
2532 | 434 |
435 if item or last_item: | |
3028 | 436 self.disp("Editing requested published item", 2) |
2532 | 437 try: |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
438 if self.use_metadata: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
439 content, metadata, item = await self.get_item_data(service, node, item) |
2532 | 440 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
441 content, item = await self.get_item_data(service, node, item) |
2532 | 442 except Exception as e: |
443 # FIXME: ugly but we have not good may to check errors in bridge | |
3028 | 444 if "item-not-found" in str(e): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
445 # item doesn't exist, we create a new one with requested id |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
446 metadata = None |
2532 | 447 if last_item: |
3028 | 448 self.disp(_("no item found at all, we create a new one"), 2) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
449 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
450 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
451 _( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
452 'item "{item}" not found, we create a new item with' |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
453 "this id" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
454 ).format(item=item), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
455 2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
456 ) |
2532 | 457 content_file_obj.seek(0) |
458 else: | |
3040 | 459 self.disp(f"Error while retrieving item: {e}") |
2532 | 460 self.host.quit(C.EXIT_ERROR) |
461 else: | |
462 # item exists, we write content | |
463 if content_file_obj.tell() != 0: | |
464 # we already have a draft, | |
465 # we copy item content after it and add an indicator | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
466 content_file_obj.write("\n*****\n") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
467 content_file_obj.write(content.encode("utf-8")) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
468 content_file_obj.seek(0) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
469 self.disp(_('item "{item}" found, we edit it').format(item=item), 2) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
470 else: |
3028 | 471 self.disp("Editing a new item", 2) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
472 if self.use_metadata: |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 metadata = None |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
474 |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 if self.use_metadata: |
2532 | 476 return service, node, item, content_file_path, content_file_obj, metadata |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
477 else: |
2532 | 478 return service, node, item, content_file_path, content_file_obj |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
479 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
480 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
481 class Table(object): |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
482 def __init__(self, host, data, headers=None, filters=None, use_buffer=False): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
483 """ |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
484 @param data(iterable[list]): table data |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
485 all lines must have the same number of columns |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
486 @param headers(iterable[unicode], None): names/titles of the columns |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
487 if not None, must have same number of columns as data |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
488 @param filters(iterable[(callable, unicode)], None): values filters |
2610
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
489 the callable will get 2 arguments: |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
490 - current column value |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
491 - RowData with all columns values |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
492 if may also only use 1 argument, which will then be current col value. |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
493 the callable must return a string |
3121 | 494 if it's unicode, it will be used with .format and must countain u'{}' which |
495 will be replaced with the string. | |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
496 if not None, must have same number of columns as data |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
497 @param use_buffer(bool): if True, bufferise output instead of printing it directly |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
498 """ |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
499 self.host = host |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
500 self._buffer = [] if use_buffer else None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
501 # headers are columns names/titles, can be None |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
502 self.headers = headers |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
503 # sizes fof columns without headers, |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
504 # headers may be larger |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
505 self.sizes = [] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
506 # rows countains one list per row with columns values |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
507 self.rows = [] |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
508 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
509 size = None |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
510 if headers: |
3121 | 511 # we use a namedtuple to make the value easily accessible from filters |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
512 headers_safe = [re.sub(r"[^a-zA-Z_]", "_", h) for h in headers] |
3121 | 513 row_cls = namedtuple("RowData", headers_safe) |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
514 else: |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
515 row_cls = tuple |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
516 |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
517 for row_data in data: |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
518 new_row = [] |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
519 row_data_list = list(row_data) |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
520 for idx, value in enumerate(row_data_list): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
521 if filters is not None and filters[idx] is not None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
522 filter_ = filters[idx] |
3028 | 523 if isinstance(filter_, str): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
524 col_value = filter_.format(value) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
525 else: |
2610
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
526 try: |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
527 col_value = filter_(value, row_cls(*row_data_list)) |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
528 except TypeError: |
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
529 col_value = filter_(value) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
530 # we count size without ANSI code as they will change length of the |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
531 # string when it's mostly style/color changes. |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
532 col_size = len(regex.ansi_remove(col_value)) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
533 else: |
3028 | 534 col_value = str(value) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
535 col_size = len(col_value) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
536 new_row.append(col_value) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
537 if size is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
538 self.sizes.append(col_size) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
539 else: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
540 self.sizes[idx] = max(self.sizes[idx], col_size) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
541 if size is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
542 size = len(new_row) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
543 if headers is not None and len(headers) != size: |
3028 | 544 raise exceptions.DataError("headers size is not coherent with rows") |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
545 else: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
546 if len(new_row) != size: |
3028 | 547 raise exceptions.DataError("rows size is not coherent") |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
548 self.rows.append(new_row) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
549 |
2344
78ffb9ce57a4
jp (common): fixed empty table display when headers are specified
Goffi <goffi@goffi.org>
parents:
2334
diff
changeset
|
550 if not data and headers is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
551 # the table is empty, we print headers at their lenght |
2344
78ffb9ce57a4
jp (common): fixed empty table display when headers are specified
Goffi <goffi@goffi.org>
parents:
2334
diff
changeset
|
552 self.sizes = [len(h) for h in headers] |
78ffb9ce57a4
jp (common): fixed empty table display when headers are specified
Goffi <goffi@goffi.org>
parents:
2334
diff
changeset
|
553 |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
554 @property |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
555 def string(self): |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
556 if self._buffer is None: |
3028 | 557 raise exceptions.InternalError("buffer must be used to get a string") |
558 return "\n".join(self._buffer) | |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
559 |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
560 @staticmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
561 def read_dict_values(data, keys, defaults=None): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
562 if defaults is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
563 defaults = {} |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
564 for key in keys: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
565 try: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
566 yield data[key] |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
567 except KeyError as e: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
568 default = defaults.get(key) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
569 if default is not None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
570 yield default |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
571 else: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
572 raise e |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
573 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
574 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
575 def from_list_dict( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
576 cls, host, data, keys=None, headers=None, filters=None, defaults=None |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
577 ): |
3121 | 578 """Create a table from a list of dictionaries |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
579 |
3121 | 580 each dictionary is a row of the table, keys being columns names. |
581 the whole data will be read and kept into memory, to be printed | |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
582 @param data(list[dict[unicode, unicode]]): data to create the table from |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
583 @param keys(iterable[unicode], None): keys to get |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
584 if None, all keys will be used |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
585 @param headers(iterable[unicode], None): name of the columns |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
586 names must be in same order as keys |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
587 @param filters(dict[unicode, (callable,unicode)), None): filter to use on values |
2610
c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
588 keys correspond to keys to filter, and value is the same as for Table.__init__ |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
589 @param defaults(dict[unicode, unicode]): default value to use |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
590 if None, an exception will be raised if not value is found |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
591 """ |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
592 if keys is None and headers is not None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
593 # FIXME: keys are not needed with OrderedDict, |
3028 | 594 raise exceptions.DataError("You must specify keys order to used headers") |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
595 if keys is None: |
3028 | 596 keys = list(data[0].keys()) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
597 if headers is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
598 headers = keys |
3121 | 599 if filters is None: |
600 filters = {} | |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
601 filters = [filters.get(k) for k in keys] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
602 return cls( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
603 host, (cls.read_dict_values(d, keys, defaults) for d in data), headers, filters |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
604 ) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
605 |
3028 | 606 def _headers(self, head_sep, headers, sizes, alignment="left", style=None): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
607 """Render headers |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
608 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
609 @param head_sep(unicode): sequence to use as separator |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
610 @param alignment(unicode): how to align, can be left, center or right |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
611 @param style(unicode, iterable[unicode], None): ANSI escape sequences to apply |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
612 @param headers(list[unicode]): headers to show |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
613 @param sizes(list[int]): sizes of columns |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
614 """ |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
615 rendered_headers = [] |
3028 | 616 if isinstance(style, str): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
617 style = [style] |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
618 for idx, header in enumerate(headers): |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
619 size = sizes[idx] |
3028 | 620 if alignment == "left": |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
621 rendered = header[:size].ljust(size) |
3028 | 622 elif alignment == "center": |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
623 rendered = header[:size].center(size) |
3028 | 624 elif alignment == "right": |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
625 rendered = header[:size].rjust(size) |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
626 else: |
3028 | 627 raise exceptions.InternalError("bad alignment argument") |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
628 if style: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
629 args = style + [rendered] |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
630 rendered = A.color(*args) |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
631 rendered_headers.append(rendered) |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
632 return head_sep.join(rendered_headers) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
633 |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
634 def _disp(self, data): |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
635 """output data (can be either bufferised or printed)""" |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
636 if self._buffer is not None: |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
637 self._buffer.append(data) |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
638 else: |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
639 self.host.disp(data) |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
640 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
641 def display( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
642 self, |
3028 | 643 head_alignment="left", |
644 columns_alignment="left", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
645 head_style=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
646 show_header=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
647 show_borders=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
648 hide_cols=None, |
3028 | 649 col_sep=" │ ", |
650 top_left="┌", | |
651 top="─", | |
652 top_sep="─┬─", | |
653 top_right="┐", | |
654 left="│", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
655 right=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
656 head_sep=None, |
3028 | 657 head_line="┄", |
658 head_line_left="├", | |
659 head_line_sep="┄┼┄", | |
660 head_line_right="┤", | |
661 bottom_left="└", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
662 bottom=None, |
3028 | 663 bottom_sep="─┴─", |
664 bottom_right="┘", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
665 ): |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
666 """Print the table |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
667 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
668 @param show_header(bool): True if header need no be shown |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
669 @param show_borders(bool): True if borders need no be shown |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
670 @param hide_cols(None, iterable(unicode)): columns which should not be displayed |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
671 @param head_alignment(unicode): how to align headers, can be left, center or right |
3121 | 672 @param columns_alignment(unicode): how to align columns, can be left, center or |
673 right | |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
674 @param col_sep(unicode): separator betweens columns |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
675 @param head_line(unicode): character to use to make line under head |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
676 @param disp(callable, None): method to use to display the table |
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
677 None to use self.host.disp |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
678 """ |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
679 if not self.sizes: |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
680 # the table is empty |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
681 return |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
682 col_sep_size = len(regex.ansi_remove(col_sep)) |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
683 |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
684 # if we have columns to hide, we remove them from headers and size |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
685 if not hide_cols: |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
686 headers = self.headers |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
687 sizes = self.sizes |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
688 else: |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
689 headers = list(self.headers) |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
690 sizes = self.sizes[:] |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
691 ignore_idx = [headers.index(to_hide) for to_hide in hide_cols] |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
692 for to_hide in hide_cols: |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
693 hide_idx = headers.index(to_hide) |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
694 del headers[hide_idx] |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
695 del sizes[hide_idx] |
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
696 |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
697 if right is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
698 right = left |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
699 if top_sep is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
700 top_sep = col_sep_size * top |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
701 if head_sep is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
702 head_sep = col_sep |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
703 if bottom is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
704 bottom = top |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
705 if bottom_sep is None: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
706 bottom_sep = col_sep_size * bottom |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
707 if not show_borders: |
3028 | 708 left = right = head_line_left = head_line_right = "" |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
709 # top border |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
710 if show_borders: |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
711 self._disp( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
712 top_left + top_sep.join([top * size for size in sizes]) + top_right |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
713 ) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
714 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
715 # headers |
3121 | 716 if show_header and self.headers is not None: |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
717 self._disp( |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
718 left |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
719 + self._headers(head_sep, headers, sizes, head_alignment, head_style) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
720 + right |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
721 ) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
722 # header line |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
723 self._disp( |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
724 head_line_left |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
725 + head_line_sep.join([head_line * size for size in sizes]) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
726 + head_line_right |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
727 ) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
728 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
729 # content |
3028 | 730 if columns_alignment == "left": |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
731 alignment = lambda idx, s: ansi_ljust(s, sizes[idx]) |
3028 | 732 elif columns_alignment == "center": |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
733 alignment = lambda idx, s: ansi_center(s, sizes[idx]) |
3028 | 734 elif columns_alignment == "right": |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
735 alignment = lambda idx, s: ansi_rjust(s, sizes[idx]) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
736 else: |
3028 | 737 raise exceptions.InternalError("bad columns alignment argument") |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
738 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
739 for row in self.rows: |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
740 if hide_cols: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
741 row = [v for idx, v in enumerate(row) if idx not in ignore_idx] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
742 self._disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
743 left |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
744 + col_sep.join([alignment(idx, c) for idx, c in enumerate(row)]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
745 + right |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
746 ) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
747 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
748 if show_borders: |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
749 # bottom border |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
750 self._disp( |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
751 bottom_left |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
752 + bottom_sep.join([bottom * size for size in sizes]) |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
753 + bottom_right |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2610
diff
changeset
|
754 ) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
755 # we return self so string can be used after display (table.display().string) |
2345
c57bc0fe17d9
jp (common): added use_buffer argument in Table:
Goffi <goffi@goffi.org>
parents:
2344
diff
changeset
|
756 return self |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
757 |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
758 def display_blank(self, **kwargs): |
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
759 """Display table without visible borders""" |
3028 | 760 kwargs_ = {"col_sep": " ", "head_line_sep": " ", "show_borders": False} |
2298
276e546b7619
jp (common): new ansi_ljust, ansi_rjust and ansi_center command + table:
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
761 kwargs_.update(kwargs) |
2493
984792a451bc
jp (common/table): a column can be hidden + fix for empty tables
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
762 return self.display(**kwargs_) |
2551
b27165bf160c
jp (merge-request/set): if service and node are not specified, URIFinder is now used + ask confirmation before publishing
Goffi <goffi@goffi.org>
parents:
2532
diff
changeset
|
763 |
b27165bf160c
jp (merge-request/set): if service and node are not specified, URIFinder is now used + ask confirmation before publishing
Goffi <goffi@goffi.org>
parents:
2532
diff
changeset
|
764 |
3040 | 765 async def fill_well_known_uri(command, path, key, meta_map=None): |
766 """Look for URIs in well-known location and fill appropriate args if suitable | |
767 | |
768 @param command(CommandBase): command instance | |
769 args of this instance will be updated with found values | |
770 @param path(unicode): absolute path to use as a starting point to look for URIs | |
771 @param key(unicode): key to look for | |
772 @param meta_map(dict, None): if not None, map metadata to arg name | |
773 key is metadata used attribute name | |
774 value is name to actually use, or None to ignore | |
775 use empty dict to only retrieve URI | |
776 possible keys are currently: | |
777 - labels | |
778 """ | |
779 args = command.args | |
780 if args.service or args.node: | |
781 # we only look for URIs if a service and a node are not already specified | |
782 return | |
783 | |
784 host = command.host | |
2551
b27165bf160c
jp (merge-request/set): if service and node are not specified, URIFinder is now used + ask confirmation before publishing
Goffi <goffi@goffi.org>
parents:
2532
diff
changeset
|
785 |
3040 | 786 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
787 uris_data = await host.bridge.uri_find(path, [key]) |
3040 | 788 except Exception as e: |
789 host.disp(f"can't find {key} URI: {e}", error=True) | |
790 host.quit(C.EXIT_BRIDGE_ERRBACK) | |
2551
b27165bf160c
jp (merge-request/set): if service and node are not specified, URIFinder is now used + ask confirmation before publishing
Goffi <goffi@goffi.org>
parents:
2532
diff
changeset
|
791 |
3040 | 792 try: |
793 uri_data = uris_data[key] | |
794 except KeyError: | |
795 host.disp( | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
796 _( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
797 "No {key} URI specified for this project, please specify service and " |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
798 "node" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
799 ).format(key=key), |
3040 | 800 error=True, |
801 ) | |
802 host.quit(C.EXIT_NOT_FOUND) | |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
803 |
3040 | 804 uri = uri_data["uri"] |
805 | |
806 # set extra metadata if they are specified | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
807 for data_key in ["labels"]: |
3040 | 808 new_values_json = uri_data.get(data_key) |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
809 if uri_data is not None: |
3040 | 810 if meta_map is None: |
811 dest = data_key | |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
812 else: |
3040 | 813 dest = meta_map.get(data_key) |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
814 if dest is None: |
3040 | 815 continue |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
816 |
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
817 try: |
3040 | 818 values = getattr(args, data_key) |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
819 except AttributeError: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
820 raise exceptions.InternalError(f"there is no {data_key!r} arguments") |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
821 else: |
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
822 if values is None: |
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
823 values = [] |
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
824 values.extend(json.loads(new_values_json)) |
3040 | 825 setattr(args, dest, values) |
2558
501b0f827f63
jp (merge-request,common): fixed URIFinder when metadata are not needed:
Goffi <goffi@goffi.org>
parents:
2554
diff
changeset
|
826 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3681
diff
changeset
|
827 parsed_uri = xmpp_uri.parse_xmpp_uri(uri) |
3040 | 828 try: |
829 args.service = parsed_uri["path"] | |
830 args.node = parsed_uri["node"] | |
831 except KeyError: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
832 host.disp(_("Invalid URI found: {uri}").format(uri=uri), error=True) |
3040 | 833 host.quit(C.EXIT_DATA_ERROR) |