Mercurial > libervia-backend
diff sat/plugins/plugin_misc_android.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 | 3910ad643e9d |
children | c23cad65ae99 |
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_android.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_misc_android.py Sat Apr 08 13:54:42 2023 +0200 @@ -143,7 +143,7 @@ notification_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) notification_intent.setAction(Intent.ACTION_MAIN) - notification_intent.addCategory(Intent.CATEGORY_LAUNCHER) + notification_intent.add_category(Intent.CATEGORY_LAUNCHER) if sat_action is not None: action_data = AndroidString(json.dumps(sat_action).encode()) log.debug(f"adding extra {INTENT_EXTRA_ACTION} ==> {action_data}") @@ -233,10 +233,10 @@ category_label=D_(PARAM_VIBRATE_CATEGORY), vibrate_param_name=PARAM_VIBRATE_NAME, vibrate_param_label=PARAM_VIBRATE_LABEL, - vibrate_options=params.makeOptions(VIBRATION_OPTS, "always"), + vibrate_options=params.make_options(VIBRATION_OPTS, "always"), ring_param_name=PARAM_RING_NAME, ring_param_label=PARAM_RING_LABEL, - ring_options=params.makeOptions(RING_OPTS, "normal"), + ring_options=params.make_options(RING_OPTS, "normal"), ) def __init__(self, host): @@ -245,7 +245,7 @@ self.host = host self._csi = host.plugins.get('XEP-0352') self._csi_timer = None - host.memory.updateParams(self.params) + host.memory.update_params(self.params) try: os.mkdir(SOCKET_DIR, 0o700) except OSError as e: @@ -268,15 +268,15 @@ raise e # we set a low priority because we want the notification to be sent after all # plugins have done their job - host.trigger.add("messageReceived", self.messageReceivedTrigger, priority=-1000) + host.trigger.add("messageReceived", self.message_received_trigger, priority=-1000) # profiles autoconnection - host.bridge.addMethod( - "profileAutoconnectGet", + host.bridge.add_method( + "profile_autoconnect_get", ".plugin", in_sign="", out_sign="s", - method=self._profileAutoconnectGet, + method=self._profile_autoconnect_get, async_=True, ) @@ -284,7 +284,7 @@ self.am = activity.getSystemService(Context.AUDIO_SERVICE) # sound notification - media_dir = Path(host.memory.getConfig("", "media_dir")) + media_dir = Path(host.memory.config_get("", "media_dir")) assert media_dir is not None notif_path = media_dir / "sounds" / "notifications" / "music-box.mp3" self.notif_player = MediaPlayer() @@ -297,20 +297,20 @@ log.info("SSL Android patch applied") # DNS fix - defer.ensureDeferred(self.updateResolver()) + defer.ensureDeferred(self.update_resolver()) # Connectivity handling self.cm = activity.getSystemService(Context.CONNECTIVITY_SERVICE) self._net_type = None - d = defer.ensureDeferred(self._checkConnectivity()) - d.addErrback(host.logErrback) + d = defer.ensureDeferred(self._check_connectivity()) + d.addErrback(host.log_errback) # XXX: we need to keep a reference to BroadcastReceiver to avoid # "XXX has no attribute 'invoke'" error (looks like the same issue as # https://github.com/kivy/pyjnius/issues/59) self.br = BroadcastReceiver( callback=lambda *args, **kwargs: reactor.callFromThread( - self.onConnectivityChange + self.on_connectivity_change ), actions=["android.net.conn.CONNECTIVITY_CHANGE"] ) @@ -326,29 +326,29 @@ previous_state = self._state self._state = new_state if new_state == STATE_RUNNING: - self._onRunning(previous_state) + self._on_running(previous_state) elif new_state == STATE_PAUSED: - self._onPaused(previous_state) + self._on_paused(previous_state) elif new_state == STATE_STOPPED: - self._onStopped(previous_state) + self._on_stopped(previous_state) @property def cagou_active(self): return self._state == STATE_RUNNING - def _onRunning(self, previous_state): + def _on_running(self, previous_state): if previous_state is not None: - self.host.bridge.bridgeReactivateSignals() - self.setActive() + self.host.bridge.bridge_reactivate_signals() + self.set_active() - def _onPaused(self, previous_state): - self.host.bridge.bridgeDeactivateSignals() - self.setInactive() + def _on_paused(self, previous_state): + self.host.bridge.bridge_deactivate_signals() + self.set_inactive() - def _onStopped(self, previous_state): - self.setInactive() + def _on_stopped(self, previous_state): + self.set_inactive() - def _notifyMessage(self, mess_data, client): + def _notify_message(self, mess_data, client): """Send notification when suitable notification is sent if: @@ -378,7 +378,7 @@ ringer_mode = self.am.getRingerMode() vibrate_mode = ringer_mode == AudioManager.RINGER_MODE_VIBRATE - ring_setting = self.host.memory.getParamA( + ring_setting = self.host.memory.param_get_a( PARAM_RING_NAME, PARAM_RING_CATEGORY, profile_key=client.profile @@ -387,7 +387,7 @@ if ring_setting != 'never' and ringer_mode == AudioManager.RINGER_MODE_NORMAL: self.notif_player.start() - vibration_setting = self.host.memory.getParamA( + vibration_setting = self.host.memory.param_get_a( PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile @@ -400,27 +400,27 @@ log.warning("Can't use vibrator: {e}".format(e=e)) return mess_data - def messageReceivedTrigger(self, client, message_elt, post_treat): + def message_received_trigger(self, client, message_elt, post_treat): if not self.cagou_active: # we only send notification is the frontend is not displayed - post_treat.addCallback(self._notifyMessage, client) + post_treat.addCallback(self._notify_message, client) return True # Profile autoconnection - def _profileAutoconnectGet(self): - return defer.ensureDeferred(self.profileAutoconnectGet()) + def _profile_autoconnect_get(self): + return defer.ensureDeferred(self.profile_autoconnect_get()) - async def _getProfilesAutoconnect(self): - autoconnect_dict = await self.host.memory.storage.getIndParamValues( + async def _get_profiles_autoconnect(self): + autoconnect_dict = await self.host.memory.storage.get_ind_param_values( category='Connection', name='autoconnect_backend', ) return [p for p, v in autoconnect_dict.items() if C.bool(v)] - async def profileAutoconnectGet(self): + async def profile_autoconnect_get(self): """Return profile to connect automatically by frontend, if any""" - profiles_autoconnect = await self._getProfilesAutoconnect() + profiles_autoconnect = await self._get_profiles_autoconnect() if not profiles_autoconnect: return None if len(profiles_autoconnect) > 1: @@ -431,56 +431,56 @@ # CSI - def _setInactive(self): + def _set_inactive(self): self._csi_timer = None - for client in self.host.getClients(C.PROF_KEY_ALL): - self._csi.setInactive(client) + for client in self.host.get_clients(C.PROF_KEY_ALL): + self._csi.set_inactive(client) - def setInactive(self): + def set_inactive(self): if self._csi is None or self._csi_timer is not None: return - self._csi_timer = reactor.callLater(CSI_DELAY, self._setInactive) + self._csi_timer = reactor.callLater(CSI_DELAY, self._set_inactive) - def setActive(self): + def set_active(self): if self._csi is None: return if self._csi_timer is not None: self._csi_timer.cancel() self._csi_timer = None - for client in self.host.getClients(C.PROF_KEY_ALL): - self._csi.setActive(client) + for client in self.host.get_clients(C.PROF_KEY_ALL): + self._csi.set_active(client) # Connectivity - async def _handleNetworkChange(self, net_type): + async def _handle_network_change(self, net_type): """Notify the clients about network changes. This way the client can disconnect/reconnect transport, or change delays """ log.debug(f"handling network change ({net_type})") if net_type == NET_TYPE_NONE: - for client in self.host.getClients(C.PROF_KEY_ALL): - client.networkDisabled() + for client in self.host.get_clients(C.PROF_KEY_ALL): + client.network_disabled() else: # DNS servers may have changed - await self.updateResolver() + await self.update_resolver() # client may be there but disabled (e.g. with stream management) - for client in self.host.getClients(C.PROF_KEY_ALL): + for client in self.host.get_clients(C.PROF_KEY_ALL): log.debug(f"enabling network for {client.profile}") - client.networkEnabled() + client.network_enabled() # profiles may have been disconnected and then purged, we try # to reconnect them in case - profiles_autoconnect = await self._getProfilesAutoconnect() + profiles_autoconnect = await self._get_profiles_autoconnect() for profile in profiles_autoconnect: - if not self.host.isConnected(profile): + if not self.host.is_connected(profile): log.info(f"{profile} is not connected, reconnecting it") try: await self.host.connect(profile) except Exception as e: log.error(f"Can't connect profile {profile}: {e}") - async def _checkConnectivity(self): + async def _check_connectivity(self): active_network = self.cm.getActiveNetworkInfo() if active_network is None: net_type = NET_TYPE_NONE @@ -506,24 +506,24 @@ log.info("network activated (type={net_type_android})" .format(net_type_android=net_type_android)) else: - log.debug("_checkConnectivity called without network change ({net_type})" + log.debug("_check_connectivity called without network change ({net_type})" .format(net_type = net_type)) - # we always call _handleNetworkChange even if there is not connectivity change + # we always call _handle_network_change even if there is not connectivity change # to be sure to reconnect when necessary - await self._handleNetworkChange(net_type) + await self._handle_network_change(net_type) - def onConnectivityChange(self): - log.debug("onConnectivityChange called") - d = defer.ensureDeferred(self._checkConnectivity()) - d.addErrback(self.host.logErrback) + def on_connectivity_change(self): + log.debug("on_connectivity_change called") + d = defer.ensureDeferred(self._check_connectivity()) + d.addErrback(self.host.log_errback) - async def updateResolver(self): + async def update_resolver(self): # There is no "/etc/resolv.conf" on Android, which confuse Twisted and makes # SRV record checking unusable. We fixe that by checking DNS server used, and # updating Twisted's resolver accordingly - dns_servers = await self.getDNSServers() + dns_servers = await self.get_dns_servers() log.info( "Patching Twisted to use Android DNS resolver ({dns_servers})".format( @@ -531,7 +531,7 @@ ) dns_client.theResolver = dns_client.createResolver(servers=dns_servers) - async def getDNSServers(self): + async def get_dns_servers(self): servers = [] if api_version < 26: