Mercurial > libervia-backend
diff sat/plugins/plugin_import.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 | 04283582966f |
children |
line wrap: on
line diff
--- a/sat/plugins/plugin_import.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_import.py Sat Apr 08 13:54:42 2023 +0200 @@ -46,7 +46,7 @@ class ImportPlugin(object): def __init__(self, host): - log.info(_("plugin Import initialization")) + log.info(_("plugin import initialization")) self.host = host def initialize(self, import_handler, name): @@ -54,13 +54,13 @@ @param import_handler(object): specialized import handler instance must have the following methods: - - importItem: import a single main item (i.e. prepare data for publishing) + - import_item: import a single main item (i.e. prepare data for publishing) - importSubitems: import sub items (i.e. items linked to main item, e.g. comments). - Must return a dict with kwargs for recursiveImport if items are to be imported recursively. + Must return a dict with kwargs for recursive_import if items are to be imported recursively. At least "items_import_data", "service" and "node" keys must be provided. if None is returned, no recursion will be done to import subitems, but import can still be done directly by the method. - - publishItem: actualy publish an item - - itemFilters: modify item according to options + - publish_item: actualy publish an item + - item_filters: modify item according to options @param name(unicode): import handler name """ assert name == name.lower().strip() @@ -71,7 +71,7 @@ import_handler.importers = {} def _import(name, location, options, pubsub_service, pubsub_node, profile): - return self._doImport( + return self._do_import( import_handler, name, location, @@ -81,40 +81,40 @@ profile, ) - def _importList(): - return self.listImporters(import_handler) + def _import_list(): + return self.list_importers(import_handler) - def _importDesc(name): + def _import_desc(name): return self.getDescription(import_handler, name) - self.host.bridge.addMethod( - name + "Import", + self.host.bridge.add_method( + name + "import", ".plugin", in_sign="ssa{ss}sss", out_sign="s", method=_import, async_=True, ) - self.host.bridge.addMethod( + self.host.bridge.add_method( name + "ImportList", ".plugin", in_sign="", out_sign="a(ss)", - method=_importList, + method=_import_list, ) - self.host.bridge.addMethod( + self.host.bridge.add_method( name + "ImportDesc", ".plugin", in_sign="s", out_sign="(ss)", - method=_importDesc, + method=_import_desc, ) - def getProgress(self, import_handler, progress_id, profile): - client = self.host.getClient(profile) + def get_progress(self, import_handler, progress_id, profile): + client = self.host.get_client(profile) return client._import[import_handler.name][progress_id] - def listImporters(self, import_handler): + def list_importers(self, import_handler): importers = list(import_handler.importers.keys()) importers.sort() return [ @@ -139,9 +139,9 @@ else: return importer.short_desc, importer.long_desc - def _doImport(self, import_handler, name, location, options, pubsub_service="", + def _do_import(self, import_handler, name, location, options, pubsub_service="", pubsub_node="", profile=C.PROF_KEY_NONE): - client = self.host.getClient(profile) + client = self.host.get_client(profile) options = {key: str(value) for key, value in options.items()} for option in import_handler.BOOL_OPTIONS: try: @@ -158,7 +158,7 @@ _("invalid json option: {option}").format(option=option) ) pubsub_service = jid.JID(pubsub_service) if pubsub_service else None - return self.doImport( + return self.do_import( client, import_handler, str(name), @@ -169,9 +169,9 @@ ) @defer.inlineCallbacks - def doImport(self, client, import_handler, name, location, options=None, + def do_import(self, client, import_handler, name, location, options=None, pubsub_service=None, pubsub_node=None,): - """Import data + """import data @param import_handler(object): instance of the import handler @param name(unicode): name of the importer @@ -221,18 +221,18 @@ "direction": "out", "type": import_handler.name.upper() + "_IMPORT", } - self.host.registerProgressCb( + self.host.register_progress_cb( progress_id, - partial(self.getProgress, import_handler), + partial(self.get_progress, import_handler), metadata, profile=client.profile, ) - self.host.bridge.progressStarted(progress_id, metadata, client.profile) + self.host.bridge.progress_started(progress_id, metadata, client.profile) session = { # session data, can be used by importers "root_service": pubsub_service, "root_node": pubsub_node, } - self.recursiveImport( + self.recursive_import( client, import_handler, items_import_data, @@ -246,7 +246,7 @@ defer.returnValue(progress_id) @defer.inlineCallbacks - def recursiveImport( + def recursive_import( self, client, import_handler, @@ -268,7 +268,7 @@ can be used by importer so store any useful data "root_service" and "root_node" are set to the main pubsub service and node of the import @param options(dict): import options - @param return_data(dict): data to return on progressFinished + @param return_data(dict): data to return on progress_finished @param service(jid.JID, None): PubSub service to use @param node(unicode, None): PubSub node to use @param depth(int): level of recursion @@ -276,14 +276,14 @@ if return_data is None: return_data = {} for idx, item_import_data in enumerate(items_import_data): - item_data = yield import_handler.importItem( + item_data = yield import_handler.import_item( client, item_import_data, session, options, return_data, service, node ) - yield import_handler.itemFilters(client, item_data, session, options) - recurse_kwargs = yield import_handler.importSubItems( + yield import_handler.item_filters(client, item_data, session, options) + recurse_kwargs = yield import_handler.import_sub_items( client, item_import_data, item_data, session, options ) - yield import_handler.publishItem(client, item_data, service, node, session) + yield import_handler.publish_item(client, item_data, service, node, session) if recurse_kwargs is not None: recurse_kwargs["client"] = client @@ -294,7 +294,7 @@ recurse_kwargs["return_data"] = return_data recurse_kwargs["depth"] = depth + 1 log.debug(_("uploading subitems")) - yield self.recursiveImport(**recurse_kwargs) + yield self.recursive_import(**recurse_kwargs) if depth == 0: client._import[import_handler.name][progress_id]["position"] = str( @@ -302,8 +302,8 @@ ) if depth == 0: - self.host.bridge.progressFinished(progress_id, return_data, client.profile) - self.host.removeProgressCb(progress_id, client.profile) + self.host.bridge.progress_finished(progress_id, return_data, client.profile) + self.host.remove_progress_cb(progress_id, client.profile) del client._import[import_handler.name][progress_id] def register(self, import_handler, name, callback, short_desc="", long_desc=""): @@ -311,10 +311,10 @@ @param name(unicode): unique importer name, should indicate the software it can import and always lowercase @param callback(callable): method to call: - the signature must be (client, location, options) (cf. [doImport]) + the signature must be (client, location, options) (cf. [do_import]) the importer must return a tuple with (items_import_data, items_count) items_import_data(iterable[dict]) data specific to specialized importer - cf. importItem docstring of specialized importer for details + cf. import_item docstring of specialized importer for details items_count (int, None) indicate the total number of items (without subitems) useful to display a progress indicator when the iterator is a generator use None if you can't guess the total number of items