Mercurial > libervia-backend
diff sat_frontends/jp/cmd_blog.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 4941cd102f93 |
children | 4b842c1fb686 |
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_blog.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat_frontends/jp/cmd_blog.py Sat Apr 08 13:54:42 2023 +0200 @@ -92,7 +92,7 @@ ALLOWER_ATTACH_MD_KEY = ("desc", "media_type", "external") -async def guessSyntaxFromPath(host, sat_conf, path): +async def guess_syntax_from_path(host, sat_conf, path): """Return syntax guessed according to filename extension @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration @@ -107,7 +107,7 @@ return k # if not found, we use current syntax - return await host.bridge.getParamA("Syntax", "Composition", "value", host.profile) + return await host.bridge.param_get_a("Syntax", "Composition", "value", host.profile) class BlogPublishCommon: @@ -121,13 +121,13 @@ """ if self.args.syntax is None: self.default_syntax_used = True - return await self.host.bridge.getParamA( + return await self.host.bridge.param_get_a( "Syntax", "Composition", "value", self.profile ) else: self.default_syntax_used = False try: - syntax = await self.host.bridge.syntaxGet(self.args.syntax) + syntax = await self.host.bridge.syntax_get(self.args.syntax) self.current_syntax = self.args.syntax = syntax except Exception as e: if e.classname == "NotFound": @@ -215,7 +215,7 @@ elif self.current_syntax == SYNTAX_XHTML: mb_data["content_xhtml"] = content else: - mb_data["content_xhtml"] = await self.host.bridge.syntaxConvert( + mb_data["content_xhtml"] = await self.host.bridge.syntax_convert( content, self.current_syntax, SYNTAX_XHTML, False, self.profile ) @@ -311,7 +311,7 @@ await self.set_mb_data_content(content, mb_data) try: - item_id = await self.host.bridge.mbSend( + item_id = await self.host.bridge.mb_send( self.args.service, self.args.node, data_format.serialise(mb_data), @@ -381,7 +381,7 @@ return common.format_time(item["published"]) def format_url(self, item, keys): - return uri.buildXMPPUri( + return uri.build_xmpp_uri( "pubsub", subtype="microblog", path=self.metadata["service"], @@ -509,12 +509,12 @@ async def start(self): try: mb_data = data_format.deserialise( - await self.host.bridge.mbGet( + await self.host.bridge.mb_get( self.args.service, self.args.node, self.args.max, self.args.items, - self.getPubsubExtra(), + self.get_pubsub_extra(), self.profile, ) ) @@ -556,7 +556,7 @@ help=_('add "publish: False" to metadata'), ) - def buildMetadataFile(self, content_file_path, mb_data=None): + def build_metadata_file(self, content_file_path, mb_data=None): """Build a metadata file using json The file is named after content_file_path, with extension replaced by @@ -620,7 +620,7 @@ async def edit(self, content_file_path, content_file_obj, mb_data=None): """Edit the file contening the content using editor, and publish it""" # we first create metadata file - meta_ori, meta_file_path = self.buildMetadataFile(content_file_path, mb_data) + meta_ori, meta_file_path = self.build_metadata_file(content_file_path, mb_data) coroutines = [] @@ -646,7 +646,7 @@ # we launch editor coroutines.append( - self.runEditor( + self.run_editor( "blog_editor_args", content_file_path, content_file_obj, @@ -665,20 +665,20 @@ mb_data = data_format.serialise(mb_data) - await self.host.bridge.mbSend( + await self.host.bridge.mb_send( self.pubsub_service, self.pubsub_node, mb_data, self.profile ) self.disp("Blog item published") - def getTmpSuff(self): + def get_tmp_suff(self): # we get current syntax to determine file extension return SYNTAX_EXT.get(self.current_syntax, SYNTAX_EXT[""]) - async def getItemData(self, service, node, item): + async def get_item_data(self, service, node, item): items = [item] if item else [] mb_data = data_format.deserialise( - await self.host.bridge.mbGet( + await self.host.bridge.mb_get( service, node, 1, items, data_format.serialise({}), self.profile ) ) @@ -689,12 +689,12 @@ except KeyError: content = item["content"] if content: - content = await self.host.bridge.syntaxConvert( + content = await self.host.bridge.syntax_convert( content, "text", SYNTAX_XHTML, False, self.profile ) if content and self.current_syntax != SYNTAX_XHTML: - content = await self.host.bridge.syntaxConvert( + content = await self.host.bridge.syntax_convert( content, SYNTAX_XHTML, self.current_syntax, False, self.profile ) @@ -716,7 +716,7 @@ async def start(self): # if there are user defined extension, we use them SYNTAX_EXT.update( - config.getConfig(self.sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {}) + config.config_get(self.sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {}) ) self.current_syntax = await self.get_current_syntax() @@ -727,7 +727,7 @@ content_file_path, content_file_obj, mb_data, - ) = await self.getItemPath() + ) = await self.get_item_path() await self.edit(content_file_path, content_file_obj, mb_data=mb_data) self.host.quit() @@ -749,7 +749,7 @@ async def start(self): try: - await self.host.bridge.mbRename( + await self.host.bridge.mb_rename( self.args.service, self.args.node, self.args.item, @@ -779,7 +779,7 @@ async def start(self): try: - repeat_id = await self.host.bridge.mbRepeat( + repeat_id = await self.host.bridge.mb_repeat( self.args.service, self.args.node, self.args.item, @@ -821,13 +821,13 @@ help=_("path to the content file"), ) - async def showPreview(self): - # we implement showPreview here so we don't have to import webbrowser and urllib + async def show_preview(self): + # we implement show_preview here so we don't have to import webbrowser and urllib # when preview is not used url = "file:{}".format(self.urllib.parse.quote(self.preview_file_path)) self.webbrowser.open_new_tab(url) - async def _launchPreviewExt(self, cmd_line, opt_name): + async def _launch_preview_ext(self, cmd_line, opt_name): url = "file:{}".format(self.urllib.parse.quote(self.preview_file_path)) args = common.parse_args( self.host, cmd_line, url=url, preview_file=self.preview_file_path @@ -840,19 +840,19 @@ self.host.quit(1) subprocess.Popen(args) - async def openPreviewExt(self): - await self._launchPreviewExt(self.open_cb_cmd, "blog_preview_open_cmd") + async def open_preview_ext(self): + await self._launch_preview_ext(self.open_cb_cmd, "blog_preview_open_cmd") - async def updatePreviewExt(self): - await self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd") + async def update_preview_ext(self): + await self._launch_preview_ext(self.update_cb_cmd, "blog_preview_update_cmd") - async def updateContent(self): + async def update_content(self): with self.content_file_path.open("rb") as f: content = f.read().decode("utf-8-sig") if content and self.syntax != SYNTAX_XHTML: # we use safe=True because we want to have a preview as close as possible # to what the people will see - content = await self.host.bridge.syntaxConvert( + content = await self.host.bridge.syntax_convert( content, self.syntax, SYNTAX_XHTML, True, self.profile ) @@ -896,30 +896,30 @@ sat_conf = self.sat_conf SYNTAX_EXT.update( - config.getConfig(sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {}) + config.config_get(sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {}) ) try: - self.open_cb_cmd = config.getConfig( + self.open_cb_cmd = config.config_get( sat_conf, C.CONFIG_SECTION, "blog_preview_open_cmd", Exception ) except (NoOptionError, NoSectionError): self.open_cb_cmd = None - open_cb = self.showPreview + open_cb = self.show_preview else: - open_cb = self.openPreviewExt + open_cb = self.open_preview_ext - self.update_cb_cmd = config.getConfig( + self.update_cb_cmd = config.config_get( sat_conf, C.CONFIG_SECTION, "blog_preview_update_cmd", self.open_cb_cmd ) if self.update_cb_cmd is None: - update_cb = self.showPreview + update_cb = self.show_preview else: - update_cb = self.updatePreviewExt + update_cb = self.update_preview_ext # which file do we need to edit? if self.args.file == "current": - self.content_file_path = self.getCurrentFile(self.profile) + self.content_file_path = self.get_current_file(self.profile) else: try: self.content_file_path = Path(self.args.file).resolve(strict=True) @@ -927,7 +927,7 @@ self.disp(_('File "{file}" doesn\'t exist!').format(file=self.args.file)) self.host.quit(C.EXIT_NOT_FOUND) - self.syntax = await guessSyntaxFromPath( + self.syntax = await guess_syntax_from_path( self.host, sat_conf, self.content_file_path ) @@ -935,7 +935,7 @@ preview_file = tempfile.NamedTemporaryFile(suffix=".xhtml", delete=False) self.preview_file_path = preview_file.name preview_file.close() - await self.updateContent() + await self.update_content() if aionotify is None: # XXX: we don't delete file automatically because browser needs it @@ -989,7 +989,7 @@ # as a workaround, we do a little rest and try again await asyncio.sleep(1) await watcher.setup(loop) - await self.updateContent() + await self.update_content() await update_cb() except FileNotFoundError: self.disp("The file seems to have been deleted.", error=True) @@ -1007,7 +1007,7 @@ class Import(base.CommandBase): def __init__(self, host): - super(Import, self).__init__( + super().__init__( host, "import", use_pubsub=True, @@ -1054,10 +1054,10 @@ ), ) - async def onProgressStarted(self, metadata): + async def on_progress_started(self, metadata): self.disp(_("Blog upload started"), 2) - async def onProgressFinished(self, metadata): + async def on_progress_finished(self, metadata): self.disp(_("Blog uploaded successfully"), 2) redirections = { k[len(URL_REDIRECT_PREFIX) :]: v @@ -1085,7 +1085,7 @@ ).format(conf=conf) ) - async def onProgressError(self, error_msg): + async def on_progress_error(self, error_msg): self.disp( _("Error while uploading blog: {error_msg}").format(error_msg=error_msg), error=True, @@ -1163,12 +1163,12 @@ def __init__(self, host): super().__init__(host) - self.overridePubsubFlags({C.SERVICE, C.SINGLE_ITEM}) + self.override_pubsub_flags({C.SERVICE, C.SINGLE_ITEM}) async def start(self): if not self.args.node: - namespaces = await self.host.bridge.namespacesGet() + namespaces = await self.host.bridge.namespaces_get() try: ns_microblog = namespaces["microblog"] except KeyError: @@ -1183,11 +1183,11 @@ def __init__(self, host): super().__init__(host) - self.overridePubsubFlags({C.SERVICE, C.SINGLE_ITEM}) + self.override_pubsub_flags({C.SERVICE, C.SINGLE_ITEM}) async def start(self): if not self.args.node: - namespaces = await self.host.bridge.namespacesGet() + namespaces = await self.host.bridge.namespaces_get() try: ns_microblog = namespaces["microblog"] except KeyError: