Fix Python deprecations (#2262)

* Add missing import 'types'

The commit 6950c04614d0bd1fc9c61b79ec7e2be0d16abdc4 did the following,
but missed to import the 'types' module:

-    module = imp.new_module(name)
+    module = types.ModuleType(name)

* Remove usage of 'imp' module - removed in Python 3.12

* Remove usage of deprecated method locale.getdefaultlocale()

Fixes the following deprecation warning:

powerline/lib/encoding.py:49: DeprecationWarning: 'locale.getdefaultlocale' is deprecated and slated for removal in Python 3.15. Use setlocale(), getencoding() and getlocale() instead.
  or locale.getdefaultlocale()[1]

* Fix invalid escape sequences

Specifically:

    powerline/bindings/config.py:179: SyntaxWarning: invalid escape sequence '\$'
    powerline/lint/__init__.py:208: SyntaxWarning: invalid escape sequence '\w'
    powerline/lint/__init__.py:226: SyntaxWarning: invalid escape sequence '\w'
    powerline/lint/__init__.py:44: SyntaxWarning: invalid escape sequence '\w'
    powerline/lint/spec.py:24: SyntaxWarning: invalid escape sequence '\w'
    powerline/lint/spec.py:588: SyntaxWarning: invalid escape sequence '\w'
    powerline/segments/common/mail.py:36: SyntaxWarning: invalid escape sequence '\d'
This commit is contained in:
Carl Smedstad 2024-08-29 13:00:48 +02:00 committed by GitHub
parent 6b36ba8ae9
commit 8484023b7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 15 additions and 14 deletions

View File

@ -46,12 +46,12 @@ def get_preferred_output_encoding():
if hasattr(locale, 'LC_MESSAGES'):
return (
locale.getlocale(locale.LC_MESSAGES)[1]
or locale.getdefaultlocale()[1]
or locale.getlocale()[1]
or 'ascii'
)
return (
locale.getdefaultlocale()[1]
locale.getlocale()[1]
or 'ascii'
)
@ -66,12 +66,12 @@ def get_preferred_input_encoding():
if hasattr(locale, 'LC_MESSAGES'):
return (
locale.getlocale(locale.LC_MESSAGES)[1]
or locale.getdefaultlocale()[1]
or locale.getlocale()[1]
or 'latin1'
)
return (
locale.getdefaultlocale()[1]
locale.getlocale()[1]
or 'latin1'
)
@ -86,7 +86,7 @@ def get_preferred_arguments_encoding():
a problem.
'''
return (
locale.getdefaultlocale()[1]
locale.getlocale()[1]
or 'latin1'
)

View File

@ -41,7 +41,7 @@ def generate_json_config_loader(lhadproblem):
return load_json_config
function_name_re = '^(\w+\.)*[a-zA-Z_]\w*$'
function_name_re = r'^(\w+\.)*[a-zA-Z_]\w*$'
divider_spec = Spec().printable().len(
@ -205,7 +205,7 @@ vim_colorscheme_spec = (Spec(
mode_translations_value_spec(),
).optional().context_message('Error while loading mode translations (key {key})'),
).context_message('Error while loading vim colorscheme'))
shell_mode_spec = Spec().re('^(?:[\w\-]+|\.safe)$').copy
shell_mode_spec = Spec().re(r'^(?:[\w\-]+|\.safe)$').copy
shell_colorscheme_spec = (Spec(
name=name_spec(),
groups=groups_spec(),
@ -223,7 +223,7 @@ args_spec = Spec(
segment_module_spec = Spec().type(unicode).func(check_segment_module).optional().copy
exinclude_spec = Spec().re(function_name_re).func(check_exinclude_function).copy
segment_spec_base = Spec(
name=Spec().re('^[a-zA-Z_]\w*$').optional(),
name=Spec().re(r'^[a-zA-Z_]\w*$').optional(),
function=Spec().re(function_name_re).func(check_segment_function).optional(),
exclude_modes=Spec().list(vim_mode_spec()).optional(),
include_modes=Spec().list(vim_mode_spec()).optional(),

View File

@ -34,7 +34,7 @@ class Spec(object):
.. note::
Methods that create the specifications return ``self``, so calls to them
may be chained: ``Spec().type(unicode).re('^\w+$')``. This does not
may be chained: ``Spec().type(unicode).re('^\\w+$')``. This does not
apply to functions that *apply* specification like :py:meth`Spec.match`.
.. note::
@ -585,7 +585,7 @@ class Spec(object):
msg_func
or (lambda value: 'String "{0}" is not an alphanumeric/underscore colon-separated identifier'.format(value))
)
return self.re('^\w+(?::\w+)?$', msg_func)
return self.re(r'^\w+(?::\w+)?$', msg_func)
def oneof(self, collection, msg_func=None):
'''Describe value that is equal to one of the value in the collection

View File

@ -45,7 +45,7 @@ class EmailIMAPSegment(KwThreadedSegment):
mail.login(key.username, key.password)
rc, message = mail.status(key.folder, '(UNSEEN)')
unread_str = message[0].decode('utf-8')
unread_count = int(re.search('UNSEEN (\d+)', unread_str).group(1))
unread_count = int(re.search(r'UNSEEN (\d+)', unread_str).group(1))
return unread_count
@staticmethod

View File

@ -1,6 +1,7 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import types
import sys
@ -47,7 +48,7 @@ def urllib_read(query_url):
elif query_url.startswith('https://freegeoip.app/json/'):
return '{"ip":"82.145.55.16","country_code":"DE","country_name":"Germany","region_code":"NI","region_name":"Lower Saxony","city":"Meppen","zip_code":"49716","time_zone":"Europe/Berlin","latitude":52.6833,"longitude":7.3167,"metro_code":0}'
elif query_url.startswith('http://geoip.nekudo.com/api/'):
return '{"city":"Meppen","country":{"name":"Germany", "code":"DE"},"location":{"accuracy_radius":100,"latitude":52.6833,"longitude":7.3167,"time_zone":"Europe\/Berlin"},"ip":"82.145.55.16"}'
return r'{"city":"Meppen","country":{"name":"Germany", "code":"DE"},"location":{"accuracy_radius":100,"latitude":52.6833,"longitude":7.3167,"time_zone":"Europe\/Berlin"},"ip":"82.145.55.16"}'
elif query_url.startswith('http://query.yahooapis.com/v1/public/'):
if 'Meppen' in query_url or '52.6833' in query_url:
return r'{"query":{"count":1,"created":"2016-05-13T19:43:18Z","lang":"en-US","results":{"channel":{"units":{"distance":"mi","pressure":"in","speed":"mph","temperature":"C"},"title":"Yahoo! Weather - Meppen, NI, DE","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-674836/","description":"Yahoo! Weather for Meppen, NI, DE","language":"en-us","lastBuildDate":"Fri, 13 May 2016 09:43 PM CEST","ttl":"60","location":{"city":"Meppen","country":"Germany","region":" NI"},"wind":{"chill":"55","direction":"350","speed":"25"},"atmosphere":{"humidity":"57","pressure":"1004.0","rising":"0","visibility":"16.1"},"astronomy":{"sunrise":"5:35 am","sunset":"9:21 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Meppen, NI, DE at 08:00 PM CEST","lat":"52.68993","long":"7.29115","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-674836/","pubDate":"Fri, 13 May 2016 08:00 PM CEST","condition":{"code":"23","date":"Fri, 13 May 2016 08:00 PM CEST","temp":"14","text":"Breezy"},"forecast":[{"code":"30","date":"13 May 2016","day":"Fri","high":"71","low":"48","text":"Partly Cloudy"},{"code":"28","date":"14 May 2016","day":"Sat","high":"54","low":"44","text":"Mostly Cloudy"},{"code":"11","date":"15 May 2016","day":"Sun","high":"55","low":"43","text":"Showers"},{"code":"28","date":"16 May 2016","day":"Mon","high":"54","low":"42","text":"Mostly Cloudy"},{"code":"28","date":"17 May 2016","day":"Tue","high":"57","low":"43","text":"Mostly Cloudy"},{"code":"12","date":"18 May 2016","day":"Wed","high":"62","low":"45","text":"Rain"},{"code":"28","date":"19 May 2016","day":"Thu","high":"63","low":"48","text":"Mostly Cloudy"},{"code":"28","date":"20 May 2016","day":"Fri","high":"67","low":"50","text":"Mostly Cloudy"},{"code":"30","date":"21 May 2016","day":"Sat","high":"71","low":"50","text":"Partly Cloudy"},{"code":"30","date":"22 May 2016","day":"Sun","high":"74","low":"54","text":"Partly Cloudy"}],"description":"<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/23.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Breezy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Fri - Partly Cloudy. High: 71Low: 48\n<BR /> Sat - Mostly Cloudy. High: 54Low: 44\n<BR /> Sun - Showers. High: 55Low: 43\n<BR /> Mon - Mostly Cloudy. High: 54Low: 42\n<BR /> Tue - Mostly Cloudy. High: 57Low: 43\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-674836/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>","guid":{"isPermaLink":"false"}}}}}}'

View File

@ -111,7 +111,7 @@ class TestVimConfig(TestCase):
class TestConfig(TestCase):
def test_tmux(self):
from powerline.segments import common
from imp import reload
from importlib import reload
reload(common)
from powerline.shell import ShellPowerline
with replace_attr(common, 'urllib_read', urllib_read):
@ -165,7 +165,7 @@ class TestConfig(TestCase):
def test_wm(self):
from powerline.segments import common
from imp import reload
from importlib import reload
reload(common)
from powerline import Powerline
with replace_attr(wthr, 'urllib_read', urllib_read):