Skip to content

Commit

Permalink
Rename SSL to TLS where possible, simplify ifs and some linter fixes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rn3j authored Jan 22, 2025
1 parent 0ce2141 commit 100f49c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
79 changes: 39 additions & 40 deletions src/tauon/t_modules/t_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,21 +348,20 @@

def get_cert_path() -> str:
if pyinstaller_mode:
return os.path.join(sys._MEIPASS, 'certifi', 'cacert.pem')
return os.path.join(sys._MEIPASS, "certifi", "cacert.pem")
# Running as script
return certifi.where()

def setup_ssl() -> ssl.SSLContext:
def setup_tls() -> ssl.SSLContext:
"""TLS setup (needed for frozen installs)"""
# Set the SSL certificate path environment variable
cert_path = get_cert_path()
logging.debug(f"Found TLS cert file at: {cert_path}")
os.environ['SSL_CERT_FILE'] = cert_path
os.environ['REQUESTS_CA_BUNDLE'] = cert_path
os.environ["SSL_CERT_FILE"] = cert_path
os.environ["REQUESTS_CA_BUNDLE"] = cert_path

# Create default TLS context
ssl_context = ssl.create_default_context(cafile=get_cert_path())
return ssl_context
return ssl.create_default_context(cafile=get_cert_path())

def whicher(target: str) -> bool | str | None:
"""Detect and launch programs outside of flatpak sandbox"""
Expand Down Expand Up @@ -495,7 +494,7 @@ def pl_gen(

Creates a default playlist when called without parameters
"""
if playlist_ids == None:
if playlist_ids is None:
playlist_ids = []

notify_change()
Expand Down Expand Up @@ -1055,7 +1054,7 @@ def set_rating(self, index: int, value: int, write: bool = False) -> None:
assert value <= 10
assert value >= 0

if tr.file_ext == "OGG" or tr.file_ext == "OPUS":
if tr.file_ext in ("OGG", "OPUS"):
tag = mutagen.oggvorbis.OggVorbis(tr.fullpath)
if value == 0:
if "FMPS_RATING" in tag:
Expand Down Expand Up @@ -2843,10 +2842,10 @@ def tag_scan(nt: TrackClass) -> TrackClass | None:
nt.length = audio.info.length
nt.size = os.path.getsize(nt.fullpath)
audio = mutagen.File(nt.fullpath)
if audio.tags and type(audio.tags) == mutagen.wave._WaveID3:
if audio.tags and type(audio.tags) is mutagen.wave._WaveID3:
use_id3(audio.tags, nt)

elif nt.file_ext == "OPUS" or nt.file_ext == "OGG" or nt.file_ext == "OGA":
elif nt.file_ext in ("OPUS", "OGG", "OGA"):

#logging.info("get opus")
with Opus(nt.fullpath) as audio:
Expand Down Expand Up @@ -2920,7 +2919,7 @@ def tag_scan(nt: TrackClass) -> TrackClass | None:
nt.comment = audio.comment
nt.misc = audio.misc

elif nt.file_ext == "WV" or nt.file_ext == "TTA":
elif nt.file_ext in ("WV", "TTA"):

with Ape(nt.fullpath) as audio:
audio.read()
Expand Down Expand Up @@ -3246,7 +3245,7 @@ def __init__(self):
self.gst_devices = [] # Display names
self.gst_outputs = {} # Display name : (sink, device)

#TODO(Martin) : Fix this by moving the class to root of the module
#TODO(Martin): Fix this by moving the class to root of the module
self.mpris: Gnome.main.MPRIS | None = None
self.tray_update = None
self.eq = [0] * 2 # not used
Expand Down Expand Up @@ -6771,7 +6770,7 @@ def worker_render(self):
if search_over.active:
while QuickThumbnail.queue:
img = QuickThumbnail.queue.pop(0)
response = urllib.request.urlopen(img.url, context=ssl_context)
response = urllib.request.urlopen(img.url, context=tls_context)
source_image = io.BytesIO(response.read())
img.read_and_thumbnail(source_image, img.size, img.size)
source_image.close()
Expand Down Expand Up @@ -7048,7 +7047,7 @@ def __init__(self):
self.desktop: str | None = desktop
self.device = socket.gethostname()

#TODO(Martin) : Fix this by moving the class to root of the module
#TODO(Martin): Fix this by moving the class to root of the module
self.cachement: player4.Cachement | None = None
self.dummy_event: SDL_Event = SDL_Event()
self.translate = _
Expand Down Expand Up @@ -7111,7 +7110,7 @@ def __init__(self):
self.remote_limited = True
self.enable_librespot = shutil.which("librespot")

#TODO(Martin) : Fix this by moving the class to root of the module
#TODO(Martin): Fix this by moving the class to root of the module
self.spotc: player4.LibreSpot | None = None
self.librespot_p = None
self.MenuItem = MenuItem
Expand All @@ -7124,7 +7123,7 @@ def __init__(self):
self.chrome: Chrome | None = None
self.chrome_menu: Menu | None = None

self.ssl_context = ssl_context
self.tls_context = tls_context

def start_remote(self) -> None:

Expand Down Expand Up @@ -9301,7 +9300,7 @@ def draw(
if click is False:
click = inp.mouse_click
if mouse_down:
gui.update = 2 # TODO, more elegant fix
gui.update = 2 # TODO: more elegant fix

rect = (x - 3, y - 2, width - 3, 21 * gui.scale)
select_rect = (x - 20 * gui.scale, y - 2, width + 20 * gui.scale, 21 * gui.scale)
Expand Down Expand Up @@ -10457,7 +10456,7 @@ def get_source_raw(self, offset: int, sources: list[tuple[int, str]] | int, trac
elif track.file_ext == "JELY":
source_image = jellyfin.get_cover(track)
else:
response = urllib.request.urlopen(get_network_thumbnail_url(track), context=ssl_context)
response = urllib.request.urlopen(get_network_thumbnail_url(track), context=tls_context)
source_image = io.BytesIO(response.read())
if source_image:
with Path(cached_path).open("wb") as file:
Expand Down Expand Up @@ -10560,7 +10559,7 @@ def get_background(self, track: TrackClass) -> BytesIO | BufferedReader | None:

artlink = r.json()["artistbackground"][0]["url"]

response = urllib.request.urlopen(artlink, context=ssl_context)
response = urllib.request.urlopen(artlink, context=tls_context)
info = response.info()

assert info.get_content_maintype() == "image"
Expand Down Expand Up @@ -13113,7 +13112,7 @@ def add_album_to_queue_fc(ref):
if pctl.playing_ready():
p = pctl.playing_object().parent_folder_path

# fixme for network tracks
# TODO: fixme for network tracks

for i, item in enumerate(pctl.force_queue):

Expand Down Expand Up @@ -13622,7 +13621,7 @@ def download_art1(tr):
artlink = r.json()["albums"][album_id]["albumcover"][0]["url"]
id = r.json()["albums"][album_id]["albumcover"][0]["id"]

response = urllib.request.urlopen(artlink, context=ssl_context)
response = urllib.request.urlopen(artlink, context=tls_context)
info = response.info()

t = io.BytesIO()
Expand Down Expand Up @@ -27836,12 +27835,12 @@ def render(self):
if status:
x += ddt.text((x, y), text, bg, 311)
# x += ddt.get_text_w(text, 11)
# TODO list listenieng clients
# TODO: list listening clients
elif transcode_list:
bg = colours.status_info_text
# if key_ctrl_down and key_c_press:
# del transcode_list[1:]
# gui.tc_cancel = True
# del transcode_list[1:]
# gui.tc_cancel = True
if right_click and coll([x, y, 280 * gui.scale, 18 * gui.scale]):
cancel_menu.activate(position=(x + 20 * gui.scale, y + 23 * gui.scale))

Expand All @@ -27855,9 +27854,9 @@ def render(self):
# c3 = [130, 130, 130, 255]
#
# if colours.lm:
# c1 = [100, 100, 100, 255]
# c2 = [130, 130, 130, 255]
# c3 = [180, 180, 180, 255]
# c1 = [100, 100, 100, 255]
# c2 = [130, 130, 130, 255]
# c3 = [180, 180, 180, 255]

c1 = [40, 40, 40, 255]
c2 = [100, 59, 200, 200]
Expand Down Expand Up @@ -32077,7 +32076,7 @@ def search_radio_browser2(self, param):
req = urllib.request.Request(uri)
req.add_header("User-Agent", t_agent)
req.add_header("Content-Type", "application/json")
response = urllib.request.urlopen(req, context=ssl_context)
response = urllib.request.urlopen(req, context=tls_context)
data = response.read()
data = json.loads(data.decode())
self.parse_data(data)
Expand Down Expand Up @@ -33235,7 +33234,7 @@ def save_discogs_artist_thumb(artist, filepath):
else:
url = images[0]["uri"]

response = urllib.request.urlopen(url, context=ssl_context)
response = urllib.request.urlopen(url, context=tls_context)
im = Image.open(response)

width, height = im.size
Expand Down Expand Up @@ -33266,7 +33265,7 @@ def save_fanart_artist_thumb(mbid, filepath, preview=False):
if preview:
thumblink = thumblink.replace("/fanart/music", "/preview/music")

response = urllib.request.urlopen(thumblink, timeout=10, context=ssl_context)
response = urllib.request.urlopen(thumblink, timeout=10, context=tls_context)
info = response.info()

t = io.BytesIO()
Expand Down Expand Up @@ -37462,7 +37461,7 @@ def pulse(self):

def download_img(link: str, target_folder: str, track: TrackClass) -> None:
try:
response = urllib.request.urlopen(link, context=ssl_context)
response = urllib.request.urlopen(link, context=tls_context)
info = response.info()
if info.get_content_maintype() == "image":
if info.get_content_subtype() == "jpeg":
Expand Down Expand Up @@ -38788,7 +38787,7 @@ def drop_file(target: str):
config.contents = fc.FcConfigGetCurrent()
fc.FcConfigAppFontAddDir(config.value, font_folder.encode())

ssl_context = setup_ssl()
tls_context = setup_tls()

# Detect what desktop environment we are in to enable specific features
desktop = os.environ.get("XDG_CURRENT_DESKTOP")
Expand Down Expand Up @@ -41477,7 +41476,7 @@ def dev_mode_disable_save_state() -> None:
last_fm_icon = asset_loader(scaled_asset_directory, loaded_asset_dc, "as.png", True)
lastfm_icon = MenuIcon(last_fm_icon)

if gui.scale == 2 or gui.scale == 1.25:
if gui.scale in (1.25, 2):
lastfm_icon.xoff = 0
else:
lastfm_icon.xoff = -1
Expand Down Expand Up @@ -42499,7 +42498,7 @@ def dev_mode_disable_save_state() -> None:

if power < 500:
time.sleep(0.03)
if (pctl.playing_state == 0 or pctl.playing_state == 2) and not load_orders and gui.update == 0 \
if (pctl.playing_state in (0, 2)) and not load_orders and gui.update == 0 \
and not tauon.gall_ren.queue and not transcode_list and not gui.frame_callback_list:
pass
else:
Expand Down Expand Up @@ -43392,7 +43391,7 @@ def dev_mode_disable_save_state() -> None:

if gui.mode == 4:
launch.render()
elif gui.mode == 1 or gui.mode == 2:
elif gui.mode in (1, 2):

ddt.text_background_colour = colours.playlist_panel_background

Expand Down Expand Up @@ -46619,7 +46618,7 @@ def dev_mode_disable_save_state() -> None:
if gui.vis == 1:

if prefs.backend == 2 or True:
if pctl.playing_state == 1 or pctl.playing_state == 3:
if pctl.playing_state in (1, 3):
# gui.level_update = True
while tauon.level_train and tauon.level_train[0][0] < time.time():

Expand Down Expand Up @@ -46655,12 +46654,12 @@ def dev_mode_disable_save_state() -> None:
if pctl.playing_time < 1:
gui.delay_frame(0.032)

if pctl.playing_state == 1 or pctl.playing_state == 3:
if pctl.playing_state in (1, 3):
t = gui.level_decay_timer.hit()
decay = 14 * t
gui.level_peak[1] -= decay
gui.level_peak[0] -= decay
elif pctl.playing_state == 0 or pctl.playing_state == 2:
elif pctl.playing_state in (0, 2):
gui.level_update = True
time.sleep(0.016)
t = gui.level_decay_timer.hit()
Expand Down Expand Up @@ -46805,11 +46804,11 @@ def dev_mode_disable_save_state() -> None:
# Misc things to update every tick

# Update d-bus metadata on Linux
if (pctl.playing_state == 1 or pctl.playing_state == 3) and pctl.mpris is not None:
if (pctl.playing_state in (1, 3)) and pctl.mpris is not None:
pctl.mpris.update_progress()

# GUI time ticker update
if (pctl.playing_state == 1 or pctl.playing_state == 3) and gui.lowered is False:
if (pctl.playing_state in (1, 3)) and gui.lowered is False:
if int(pctl.playing_time) != int(pctl.last_playing_time):
pctl.last_playing_time = pctl.playing_time
bottom_bar1.seek_time = pctl.playing_time
Expand Down
2 changes: 1 addition & 1 deletion src/tauon/t_modules/t_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class InterceptedHTTPResponse:
r.add_header("Icy-MetaData", "1")
r.add_header("User-Agent", self.tauon.t_agent)
logging.info("Open URL.....")
r = urllib.request.urlopen(r, timeout=20, context=self.tauon.ssl_context)
r = urllib.request.urlopen(r, timeout=20, context=self.tauon.tls_context)
logging.info("URL opened.")

except Exception as e:
Expand Down

0 comments on commit 100f49c

Please sign in to comment.