Skip to content

Commit

Permalink
EDCD#2051 Finish 6 files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rixxan committed Aug 11, 2023
1 parent 56f7c65 commit e36f3c6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
9 changes: 7 additions & 2 deletions commodity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""Export various CSV formats."""
# -*- coding: utf-8 -*-
"""
commodity.py - Export various CSV formats
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
"""

import time
from os.path import join
Expand Down
6 changes: 5 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""
All constants for the application.
constants.py - Constants for the app
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
This file should contain all constants that the application uses, but that
means migrating the existing ones, so for now it's only the ones that we've
Expand Down
6 changes: 5 additions & 1 deletion edmc_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""
Static data.
edmc_data.py - Static App Data
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
For easy reference any variable should be prefixed with the name of the file it
was either in originally, or where the primary code utilising it is.
Expand Down
14 changes: 11 additions & 3 deletions journal_lock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
"""Implements locking of Journal directory."""
"""
journal_lock.py - Locking of the Journal Directory
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
"""
from __future__ import annotations

import pathlib
import sys
Expand All @@ -7,7 +14,6 @@
from os import getpid as os_getpid
from tkinter import ttk
from typing import TYPE_CHECKING, Callable, Optional

from config import config
from EDMCLogging import get_main_logger

Expand All @@ -33,6 +39,8 @@ class JournalLock:

def __init__(self) -> None:
"""Initialise where the journal directory and lock file are."""
self.retry_popup = None
self.journal_dir_lockfile = None
self.journal_dir: str | None = config.get_str('journaldir') or config.default_journal_dir
self.journal_dir_path: Optional[pathlib.Path] = None
self.set_path_from_journaldir()
Expand Down Expand Up @@ -178,7 +186,7 @@ def release_lock(self) -> bool:
else:
unlocked = True

# Close the file whether or not the unlocking succeeded.
# Close the file whether the unlocking succeeded.
if hasattr(self, 'journal_dir_lockfile'):
self.journal_dir_lockfile.close()

Expand Down
17 changes: 11 additions & 6 deletions shipyard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Export list of ships as CSV."""
import csv
"""
constants.py - Export Ships as CSV
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
"""
import csv
import companion
from edmc_data import ship_name_map

Expand All @@ -17,9 +22,9 @@ def export(data: companion.CAPIData, filename: str) -> None:
assert data['lastStarport'].get('name')
assert data['lastStarport'].get('ships')

with open(filename, 'w', newline='') as f:
c = csv.writer(f)
c.writerow(('System', 'Station', 'Ship', 'FDevID', 'Date'))
with open(filename, 'w', newline='') as csv_file:
csv_line = csv.writer(csv_file)
csv_line.writerow(('System', 'Station', 'Ship', 'FDevID', 'Date'))

for (name, fdevid) in [
(
Expand All @@ -29,7 +34,7 @@ def export(data: companion.CAPIData, filename: str) -> None:
(data['lastStarport']['ships'].get('shipyard_list') or {}).values()
) + data['lastStarport']['ships'].get('unavailable_list')
]:
c.writerow((
csv_line.writerow((
data['lastSystem']['name'], data['lastStarport']['name'],
name, fdevid, data['timestamp']
))
33 changes: 19 additions & 14 deletions timeout_session.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""A requests.session with a TimeoutAdapter."""
import requests
"""
timeout_session.py - requests session with timeout adapter
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
"""
from typing import Optional, Any
from requests import PreparedRequest, Response, Session
from requests.adapters import HTTPAdapter

from config import user_agent

REQUEST_TIMEOUT = 10 # reasonable timeout that all HTTP requests should use
Expand All @@ -17,29 +23,28 @@ def __init__(self, timeout: int, *args, **kwargs):

super().__init__(*args, **kwargs)

def send(self, *args, **kwargs) -> requests.Response:
def send(self, request: PreparedRequest, *args, **kwargs: Any) -> Response:
"""Send, but with a timeout always set."""
if kwargs["timeout"] is None:
kwargs["timeout"] = self.default_timeout

return super().send(*args, **kwargs)
return super().send(request, *args, **kwargs)


def new_session(
timeout: int = REQUEST_TIMEOUT, session: requests.Session | None = None
) -> requests.Session:
timeout: int = REQUEST_TIMEOUT, session: Optional[Session] = None
) -> Session:

"""
Create a new requests.Session and override the default HTTPAdapter with a TimeoutAdapter.
:param timeout: the timeout to set the TimeoutAdapter to, defaults to REQUEST_TIMEOUT
:param session: the Session object to attach the Adapter to, defaults to a new session
:return: The created Session
"""
if session is None:
session = requests.Session()
with Session() as session:
session.headers['User-Agent'] = user_agent

adapter = TimeoutAdapter(timeout)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
adapter = TimeoutAdapter(timeout)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session

0 comments on commit e36f3c6

Please sign in to comment.