Daniel Brötzmann pushed to branch mainwindow at gajim / gajim

Commits:

2 changed files:

Changes:

  • gajim/common/preview_helpers.py
    ... ... @@ -12,6 +12,9 @@
    12 12
     # You should have received a copy of the GNU General Public License
    
    13 13
     # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
    
    14 14
     
    
    15
    +from typing import List
    
    16
    +from typing import Tuple
    
    17
    +from typing import Optional
    
    15 18
     import math
    
    16 19
     import logging
    
    17 20
     import binascii
    
    ... ... @@ -127,14 +130,14 @@ def extract_and_resize_frames(image, resize_to):
    127 130
         return frames, result
    
    128 131
     
    
    129 132
     
    
    130
    -def create_thumbnail(data, size):
    
    133
    +def create_thumbnail(data: bytes, size:int) -> Optional[bytes]:
    
    131 134
         thumbnail = create_thumbnail_with_pil(data, size)
    
    132 135
         if thumbnail is not None:
    
    133 136
             return thumbnail
    
    134 137
         return create_thumbnail_with_pixbuf(data, size)
    
    135 138
     
    
    136 139
     
    
    137
    -def create_thumbnail_with_pixbuf(data, size):
    
    140
    +def create_thumbnail_with_pixbuf(data: bytes, size: int) -> Optional[bytes]:
    
    138 141
         loader = GdkPixbuf.PixbufLoader()
    
    139 142
         try:
    
    140 143
             loader.write(data)
    
    ... ... @@ -160,7 +163,7 @@ def create_thumbnail_with_pixbuf(data, size):
    160 163
         return bytes_
    
    161 164
     
    
    162 165
     
    
    163
    -def create_thumbnail_with_pil(data, size):
    
    166
    +def create_thumbnail_with_pil(data: bytes, size: int) -> Optional[bytes]:
    
    164 167
         input_file = BytesIO(data)
    
    165 168
         output_file = BytesIO()
    
    166 169
         try:
    
    ... ... @@ -170,7 +173,7 @@ def create_thumbnail_with_pil(data, size):
    170 173
             log.warning('fallback to pixbuf')
    
    171 174
             input_file.close()
    
    172 175
             output_file.close()
    
    173
    -        return
    
    176
    +        return None
    
    174 177
     
    
    175 178
         image_width, image_height = image.size
    
    176 179
         if size > image_width and size > image_height:
    
    ... ... @@ -197,7 +200,7 @@ def create_thumbnail_with_pil(data, size):
    197 200
         return bytes_
    
    198 201
     
    
    199 202
     
    
    200
    -def get_thumbnail_size(pixbuf, size):
    
    203
    +def get_thumbnail_size(pixbuf: GdkPixbuf.Pixbuf, size: int) -> Tuple[int, int]:
    
    201 204
         # Calculates the new thumbnail size while preserving the aspect ratio
    
    202 205
         image_width = pixbuf.get_width()
    
    203 206
         image_height = pixbuf.get_height()
    
    ... ... @@ -214,7 +217,7 @@ def get_thumbnail_size(pixbuf, size):
    214 217
         return image_width, image_height
    
    215 218
     
    
    216 219
     
    
    217
    -def pixbuf_from_data(data):
    
    220
    +def pixbuf_from_data(data: bytes) -> GdkPixbuf.Pixbuf:
    
    218 221
         loader = GdkPixbuf.PixbufLoader()
    
    219 222
         try:
    
    220 223
             loader.write(data)
    
    ... ... @@ -286,7 +289,7 @@ def get_image_paths(uri, urlparts, size, orig_dir, thumb_dir):
    286 289
         return orig_path, thumb_path
    
    287 290
     
    
    288 291
     
    
    289
    -def split_geo_uri(uri):
    
    292
    +def split_geo_uri(uri: str) -> Coords:
    
    290 293
         # Example:
    
    291 294
         # geo:37.786971,-122.399677,122.3;CRS=epsg:32718;U=20;mapcolors=abc
    
    292 295
         # Assumption is all coordinates are CRS=WGS-84
    
    ... ... @@ -318,7 +321,7 @@ def split_geo_uri(uri):
    318 321
         return Coords(location=location, lat=lat, lon=lon)
    
    319 322
     
    
    320 323
     
    
    321
    -def filename_from_uri(uri):
    
    324
    +def filename_from_uri(uri: str) -> str:
    
    322 325
         urlparts = urlparse(unquote(uri))
    
    323 326
         path = Path(urlparts.path)
    
    324 327
         return path.name
    
    ... ... @@ -365,7 +368,9 @@ def get_previewable_mime_types():
    365 368
         ))
    
    366 369
     
    
    367 370
     
    
    368
    -def guess_mime_type(file_path, data=None):
    
    371
    +def guess_mime_type(file_path: str,
    
    372
    +                    data: Optional[bytes] = None
    
    373
    +                    ) -> str:
    
    369 374
         mime_type, _ = mimetypes.MimeTypes().guess_type(str(file_path))
    
    370 375
         if mime_type is None:
    
    371 376
             # Try to guess MIME type by file name
    
    ... ... @@ -374,7 +379,9 @@ def guess_mime_type(file_path, data=None):
    374 379
         return mime_type
    
    375 380
     
    
    376 381
     
    
    377
    -def guess_simple_file_type(file_path, data=None):
    
    382
    +def guess_simple_file_type(file_path: str,
    
    383
    +                           data: Optional[bytes] = None
    
    384
    +                           ) -> Tuple[Gio.Icon, str]:
    
    378 385
         mime_type = guess_mime_type(file_path, data)
    
    379 386
         icon = get_icon_for_mime_type(mime_type)
    
    380 387
         if file_path.startswith('geo:'):
    
    ... ... @@ -390,7 +397,7 @@ def guess_simple_file_type(file_path, data=None):
    390 397
         return icon, _('File')
    
    391 398
     
    
    392 399
     
    
    393
    -def get_icon_for_mime_type(mime_type):
    
    400
    +def get_icon_for_mime_type(mime_type: str) -> Gio.Icon:
    
    394 401
         if mime_type is None:
    
    395 402
             return Gio.Icon.new_for_string('mail-attachment')
    
    396 403
         return Gio.content_type_get_icon(mime_type)

  • gajim/gtk/util.py
    ... ... @@ -19,6 +19,7 @@
    19 19
     from typing import List
    
    20 20
     from typing import Tuple
    
    21 21
     from typing import Optional
    
    22
    +from typing import Union
    
    22 23
     
    
    23 24
     import sys
    
    24 25
     import logging
    
    ... ... @@ -70,8 +71,8 @@
    70 71
     class NickCompletionGenerator:
    
    71 72
         def __init__(self, self_nick: str) -> None:
    
    72 73
             self.nick = self_nick
    
    73
    -        self.sender_list = []  # type: List[str]
    
    74
    -        self.attention_list = []  # type: List[str]
    
    74
    +        self.sender_list: List[str] = []
    
    75
    +        self.attention_list: List[str] = []
    
    75 76
     
    
    76 77
         def change_nick(self, new_nick: str) -> None:
    
    77 78
             self.nick = new_nick
    
    ... ... @@ -419,7 +420,8 @@ def at_the_end(widget: Gtk.ScrolledWindow) -> bool:
    419 420
         return adj_v.get_value() == max_scroll_pos
    
    420 421
     
    
    421 422
     
    
    422
    -def get_image_button(icon_name, tooltip, toggle=False):
    
    423
    +def get_image_button(icon_name: str, tooltip: str,
    
    424
    +                     toggle: Optional[bool] = False) -> Gtk.Button:
    
    423 425
         if toggle:
    
    424 426
             button = Gtk.ToggleButton()
    
    425 427
             image = Gtk.Image.new_from_icon_name(icon_name, Gtk.IconSize.MENU)
    
    ... ... @@ -465,11 +467,11 @@ def convert_rgb_string_to_float(rgb_string: str) -> Tuple[float, float, float]:
    465 467
         return (rgba.red, rgba.green, rgba.blue)
    
    466 468
     
    
    467 469
     
    
    468
    -def rgba_to_float(rgba):
    
    470
    +def rgba_to_float(rgba: Gdk.RGBA) -> Tuple[float, float, float]:
    
    469 471
         return (rgba.red, rgba.green, rgba.blue)
    
    470 472
     
    
    471 473
     
    
    472
    -def make_rgba(color_string):
    
    474
    +def make_rgba(color_string: str) -> Gdk.RGBA:
    
    473 475
         rgba = Gdk.RGBA()
    
    474 476
         rgba.parse(color_string)
    
    475 477
         return rgba
    
    ... ... @@ -506,7 +508,7 @@ def get_metacontact_surface(icon_name, expanded, scale):
    506 508
         return state_surface
    
    507 509
     
    
    508 510
     
    
    509
    -def get_primary_accel_mod():
    
    511
    +def get_primary_accel_mod() -> Optional[Gdk.ModifierType]:
    
    510 512
         """
    
    511 513
         Returns the primary Gdk.ModifierType modifier.
    
    512 514
         cmd on osx, ctrl everywhere else.
    
    ... ... @@ -514,7 +516,7 @@ def get_primary_accel_mod():
    514 516
         return Gtk.accelerator_parse("<Primary>")[1]
    
    515 517
     
    
    516 518
     
    
    517
    -def get_hardware_key_codes(keyval):
    
    519
    +def get_hardware_key_codes(keyval: int) -> List[int]:
    
    518 520
         keymap = Gdk.Keymap.get_for_display(Gdk.Display.get_default())
    
    519 521
     
    
    520 522
         valid, key_map_keys = keymap.get_entries_for_keyval(keyval)
    
    ... ... @@ -618,7 +620,7 @@ def get_account_location_icon_name(account):
    618 620
         return None if location is None else 'applications-internet'
    
    619 621
     
    
    620 622
     
    
    621
    -def format_eta(time_):
    
    623
    +def format_eta(time_: Union[int, float]) -> str:
    
    622 624
         times = {'minutes': 0, 'seconds': 0}
    
    623 625
         time_ = int(time_)
    
    624 626
         times['seconds'] = time_ % 60
    
    ... ... @@ -629,7 +631,7 @@ def format_eta(time_):
    629 631
         return _('%s s') % times['seconds']
    
    630 632
     
    
    631 633
     
    
    632
    -def format_fingerprint(fingerprint):
    
    634
    +def format_fingerprint(fingerprint: str) -> str:
    
    633 635
         fplen = len(fingerprint)
    
    634 636
         wordsize = fplen // 8
    
    635 637
         buf = ''
    
    ... ... @@ -677,7 +679,7 @@ def do_get_preferred_width(self):
    677 679
             return minimum_width, natural_width
    
    678 680
     
    
    679 681
     
    
    680
    -def text_to_color(text):
    
    682
    +def text_to_color(text: str) -> Tuple[float, float, float]:
    
    681 683
         if app.css_config.prefer_dark:
    
    682 684
             background = (0, 0, 0)  # RGB (0, 0, 0) black
    
    683 685
         else:
    
    ... ... @@ -692,7 +694,7 @@ def get_color_for_account(account: str) -> str:
    692 694
     
    
    693 695
     
    
    694 696
     @lru_cache(maxsize=16)
    
    695
    -def get_css_show_class(show):
    
    697
    +def get_css_show_class(show: str) -> str:
    
    696 698
         if show in ('online', 'chat'):
    
    697 699
             return '.gajim-status-online'
    
    698 700
         if show == 'away':
    
    ... ... @@ -741,7 +743,7 @@ def get_pixbuf_from_data(file_data):
    741 743
         return pixbuf
    
    742 744
     
    
    743 745
     
    
    744
    -def scale_with_ratio(size, width, height):
    
    746
    +def scale_with_ratio(size: int, width: int, height: int) -> Tuple[int, int]:
    
    745 747
         if height == width:
    
    746 748
             return size, size
    
    747 749
         if height > width:
    
    ... ... @@ -752,7 +754,8 @@ def scale_with_ratio(size, width, height):
    752 754
         return size, int(size / ratio)
    
    753 755
     
    
    754 756
     
    
    755
    -def scale_pixbuf(pixbuf, size):
    
    757
    +def scale_pixbuf(pixbuf: GdkPixbuf.Pixbuf,
    
    758
    +                 size: int) -> Optional[GdkPixbuf.Pixbuf]:
    
    756 759
         width, height = scale_with_ratio(size,
    
    757 760
                                          pixbuf.get_width(),
    
    758 761
                                          pixbuf.get_height())
    
    ... ... @@ -799,7 +802,7 @@ def load_pixbuf(path, size=None):
    799 802
             return None
    
    800 803
     
    
    801 804
     
    
    802
    -def get_thumbnail_size(pixbuf, size):
    
    805
    +def get_thumbnail_size(pixbuf: GdkPixbuf.Pixbuf, size: int) -> Tuple[int, int]:
    
    803 806
         # Calculates the new thumbnail size while preserving the aspect ratio
    
    804 807
         image_width = pixbuf.get_width()
    
    805 808
         image_height = pixbuf.get_height()
    
    ... ... @@ -832,7 +835,7 @@ def _to_href(match):
    832 835
         return URL_REGEX.sub(_to_href, string)
    
    833 836
     
    
    834 837
     
    
    835
    -def get_app_windows(account):
    
    838
    +def get_app_windows(account: str) -> List[Gtk.Window]:
    
    836 839
         windows = []
    
    837 840
         for win in app.app.get_windows():
    
    838 841
             if hasattr(win, 'account'):
    
    ... ... @@ -934,7 +937,7 @@ def _on_realize(*args):
    934 937
     
    
    935 938
     
    
    936 939
     class AccountBadge(Gtk.Label):
    
    937
    -    def __init__(self, account):
    
    940
    +    def __init__(self, account: str) -> None:
    
    938 941
             Gtk.Label.__init__(self)
    
    939 942
             self.set_ellipsize(Pango.EllipsizeMode.END)
    
    940 943
             self.set_max_width_chars(12)
    
    ... ... @@ -945,7 +948,7 @@ def __init__(self, account):
    945 948
             self.refresh()
    
    946 949
             self.show()
    
    947 950
     
    
    948
    -    def refresh(self):
    
    951
    +    def refresh(self) -> None:
    
    949 952
             label = app.get_account_label(self._account)
    
    950 953
             self.set_text(label)
    
    951 954
             account_class = app.css_config.get_dynamic_class(self._account)
    
    ... ... @@ -953,7 +956,7 @@ def refresh(self):
    953 956
             self.set_tooltip_text(_('Account: %s') % label)
    
    954 957
     
    
    955 958
     
    
    956
    -def make_pango_attribute(name, start, end):
    
    959
    +def make_pango_attribute(name: str, start: int, end: int) -> Pango.Attribute:
    
    957 960
         if name == 'strong':
    
    958 961
             attr = Pango.attr_weight_new(Pango.Weight.BOLD)
    
    959 962
         if name == 'strike':