Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Pass Logic Audit #2068

Closed
wants to merge 54 commits into from
Closed
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
51ad16c
#2051 Remove Unneeded object calls
Rixxan Aug 4, 2023
9f57113
#2051 Update 3 More Files
Rixxan Aug 4, 2023
b568a61
#2051 3 More Files
Rixxan Aug 4, 2023
82266d6
#2051 Another 3 Files
Rixxan Aug 4, 2023
d6efcf7
#2051 And another 3!
Rixxan Aug 4, 2023
919dbaf
#2051 Drei Gläser
Rixxan Aug 4, 2023
7711374
#2051 One... Two... FIVE
Rixxan Aug 4, 2023
ee1820c
#2051 Another three
Rixxan Aug 4, 2023
d8b1c70
#2051 Being brave... doing 5 files first pass
Rixxan Aug 4, 2023
aa4d831
#2051 And then there were five
Rixxan Aug 4, 2023
d45c690
#2051 First Pass Penultimate
Rixxan Aug 4, 2023
2f550f0
#2051 Last 4, also cleans up auth page
Rixxan Aug 4, 2023
30ccbc3
#2051 Revert Subprocess Change
Rixxan Aug 6, 2023
ab44b1f
Merge branch 'main' into enhancement/2051/logic-audit
Rixxan Aug 10, 2023
636ce7c
#2051 Inara pass
Rixxan Aug 10, 2023
5bcec55
#2051 Make MyPy Happy
Rixxan Aug 10, 2023
bb22c4c
#2051 Other Core Plugin Pass
Rixxan Aug 10, 2023
49fca9f
#2051 Other Core Plugin Pass
Rixxan Aug 10, 2023
b779f6d
Merge branch 'enhancement/2051/logic-audit' of https://github.com/Hul…
Rixxan Aug 10, 2023
4bc9871
#2051 Update Scripts, Config
Rixxan Aug 10, 2023
74eadce
#2051 Update Docstrings
Rixxan Aug 10, 2023
56f7c65
#2051 Begin Test Updates
Rixxan Aug 11, 2023
e36f3c6
#2051 Finish 6 files
Rixxan Aug 11, 2023
e1e0fff
#2051 Update Union Usage
Rixxan Aug 11, 2023
9331dd0
#2051 A few more ready for testing
Rixxan Aug 11, 2023
4877f8f
#2051 Another Set
Rixxan Aug 11, 2023
14477d1
#2051 Disable MyPy for Now
Rixxan Aug 11, 2023
5442dd1
#2051 Audit EDMC main GUI
Rixxan Aug 12, 2023
46023e3
#2051 A Bunch More Files
Rixxan Aug 12, 2023
5d3eeb4
[Minor] Flake8, eat your heart out
Rixxan Aug 12, 2023
1ba119b
#2051 Second Pass Plugins
Rixxan Aug 12, 2023
e2f4dbc
#2051 Black Run
Rixxan Aug 12, 2023
62e883e
#2051 Retire Ship.p
Rixxan Aug 15, 2023
6df71c2
#2051 Outfitting.py
Rixxan Aug 15, 2023
1af1d6f
#2051 Final First Pass + Retire More Pickles
Rixxan Aug 16, 2023
84ab112
#2051 Revert Bad Refactor
Rixxan Aug 16, 2023
5105d20
#2051 More Graceful Monitoring
Rixxan Aug 16, 2023
7382e67
Limited Rollback - Black
Rixxan Aug 16, 2023
8e01b45
#2051 Update Category Check
Rixxan Aug 17, 2023
d5108bd
#2051 ReRefactor Monitor
Rixxan Aug 23, 2023
36ce884
Merge branch 'develop' into enhancement/2051/logic-audit
Rixxan Aug 23, 2023
27eed80
#2051 Revert Overzealousness
Rixxan Aug 23, 2023
c9f8818
#2051 #1379 Respect Minimized Start
Rixxan Aug 24, 2023
90893f0
#2051 We are Borg. We are not Self.
Rixxan Aug 25, 2023
c183a1a
[Docs] Restore some inline comments
Rixxan Aug 25, 2023
d20c281
#2051 Revert Unneeded Assignment
Rixxan Aug 25, 2023
b2427ed
#2051 Update MyPy Ignores
Rixxan Aug 25, 2023
3d82a46
2051 Apparently not all of them were incorrect to Windows
Rixxan Aug 25, 2023
850965f
Update mypy-all.sh
Rixxan Sep 1, 2023
49874d7
Update mypy-all.sh
Rixxan Sep 2, 2023
dea3a95
#2051 First Pass Checks
Rixxan Sep 19, 2023
2386d01
2051 One hop this time
Rixxan Sep 19, 2023
2056199
2051 Further Reversions from Silliness
Rixxan Sep 20, 2023
324b618
[2051] Track Accepted Changes
Rixxan Nov 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# Ignore version file
.gitversion

# Ignore macOS DS_Store files
.DS_Store

# Ignore build artifacts
build
ChangeLog.html
dist.win32/
dist.*

# Ignore generated ChangeLog.html file
ChangeLog.html

# Ignore files
dump
*.bak
*.pyc
Expand All @@ -11,20 +21,36 @@ dump
*.pdb
*.msi
*.wixobj
*.zip

# Ignore Update Things
EDMarketConnector_Installer_*.exe
appcast_win_*.xml
appcast_mac_*.xml
EDMarketConnector.VisualElementsManifest.xml
*.zip
EDMC_Installer_Config.iss
EDMarketConnector.wxs
wix/components.wxs

# Ignore Visual Elements Manifest file for Windows
EDMarketConnector.VisualElementsManifest.xml

# Ignore IDE and editor configuration files
.idea
.vscode

# Ignore virtual environments
.venv/
venv/

# Ignore workspace file for Visual Studio Code
*.code-workspace

# Ignore coverage reports
htmlcov/
.ignored
.coverage
EDMarketConnector.wxs
wix/components.wxs
pylintrc
pylint.txt

# Ignore Submodule data directory
coriolis-data/
48 changes: 24 additions & 24 deletions EDMC.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#!/usr/bin/env python3
"""Command-line interface. Requires prior setup through the GUI."""

"""
EDMC.py - Command-line interface. Requires prior setup through the GUI.

Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
"""
import argparse
import json
import locale
import os
import queue
import sys
from os.path import getmtime
from pathlib import Path
from time import sleep, time
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, List, Optional, Union

# isort: off

os.environ["EDMC_NO_UI"] = "1"

# See EDMCLogging.py docs.
Expand All @@ -25,7 +28,6 @@
edmclogger.set_channels_loglevel(logging.INFO)

# isort: on

import collate
import commodity
import companion
Expand Down Expand Up @@ -60,7 +62,6 @@ def log_locale(prefix: str) -> None:


l10n.Translations.install_dummy()

SERVER_RETRY = 5 # retry pause for Companion servers [s]
EXIT_SUCCESS, EXIT_SERVER, EXIT_CREDENTIALS, EXIT_VERIFICATION, EXIT_LAGGING, EXIT_SYS_ERR, EXIT_ARGS, \
EXIT_JOURNAL_READ_ERR, EXIT_COMMANDER_UNKNOWN = range(9)
Expand All @@ -71,12 +72,11 @@ def versioncmp(versionstring) -> List:
return list(map(int, versionstring.split('.')))


def deep_get(target: dict | companion.CAPIData, *args: str, default=None) -> Any:
def deep_get(target: Union[dict, companion.CAPIData], *args: str, default=None) -> Any:
"""
Walk into a dict and return the specified deep value.

Example usage:

>>> thing = {'a': {'b': {'c': 'foo'} } }
>>> deep_get(thing, ('a', 'b', 'c'), None)
'foo'
Expand All @@ -98,9 +98,7 @@ def deep_get(target: dict | companion.CAPIData, *args: str, default=None) -> Any
res = current.get(arg)
if res is None:
return default

current = res

return current


Expand Down Expand Up @@ -155,7 +153,7 @@ def main(): # noqa: C901, CCR001
args = parser.parse_args()

if args.version:
updater = Updater(provider='internal')
updater = Updater()
newversion: Optional[EDMCVersion] = updater.check_appcast()
if newversion:
print(f'{appversion()} ({newversion.title!r} is available)')
Expand Down Expand Up @@ -204,13 +202,16 @@ def main(): # noqa: C901, CCR001
# Import and collate from JSON dump
#
# Try twice, once with the system locale and once enforcing utf-8. If the file was made on the current
# system, chances are its the current locale, and not utf-8. Otherwise if it was copied, its probably
# system, chances are it's the current locale, and not utf-8. Otherwise, if it was copied, its probably
# utf8. Either way, try the system FIRST because reading something like cp1251 in UTF-8 results in garbage
# but the reverse results in an exception.
json_file = Path(args.j)
try:
data = json.load(open(args.j))
with open(json_file) as file_handle:
data = json.load(file_handle)
except UnicodeDecodeError:
data = json.load(open(args.j, encoding='utf-8'))
with open(json_file, encoding='utf-8') as file_handle:
data = json.load(file_handle)

config.set('querytime', int(getmtime(args.j)))

Expand Down Expand Up @@ -293,8 +294,7 @@ def main(): # noqa: C901, CCR001
if capi_response.exception:
raise capi_response.exception

else:
raise ValueError(capi_response.message)
raise ValueError(capi_response.message)

logger.trace_if('capi.worker', 'Answer is not a Failure')
if not isinstance(capi_response, companion.EDMCCAPIResponse):
Expand Down Expand Up @@ -366,17 +366,17 @@ def main(): # noqa: C901, CCR001
else:
print(deep_get(data, 'lastSystem', 'name', default='Unknown'))

if (args.m or args.o or args.s or args.n or args.j):
if args.m or args.o or args.s or args.n or args.j:
if not data['commander'].get('docked'):
logger.error("Can't use -m, -o, -s, -n or -j because you're not currently docked!")
return

elif not deep_get(data, 'lastStarport', 'name'):
if not deep_get(data, 'lastStarport', 'name'):
logger.error("No data['lastStarport']['name'] from CAPI")
sys.exit(EXIT_LAGGING)

# Ignore possibly missing shipyard info
elif not (data['lastStarport'].get('commodities') or data['lastStarport'].get('modules')):
if not (data['lastStarport'].get('commodities') or data['lastStarport'].get('modules')):
logger.error("No commodities or outfitting (modules) in CAPI data")
return

Expand Down Expand Up @@ -462,14 +462,14 @@ def main(): # noqa: C901, CCR001
except Exception:
logger.exception('Failed to send data to EDDN')

except companion.ServerError:
logger.exception('Frontier CAPI Server returned an error')
sys.exit(EXIT_SERVER)

except companion.ServerConnectionError:
logger.exception('Exception while contacting server')
sys.exit(EXIT_SERVER)

except companion.ServerError:
logger.exception('Frontier CAPI Server returned an error')
sys.exit(EXIT_SERVER)

except companion.CredentialsError:
logger.error('Frontier CAPI Server: Invalid Credentials')
sys.exit(EXIT_CREDENTIALS)
Expand Down
32 changes: 12 additions & 20 deletions EDMCLogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
# See, plug.py:load_plugins()
logger = logging.getLogger(f'{appname}.{plugin_name}')
"""

import inspect
import logging
import logging.handlers
Expand All @@ -48,7 +47,7 @@
from threading import get_native_id as thread_native_id
from time import gmtime
from traceback import print_exc
from typing import TYPE_CHECKING, Tuple, cast
from typing import TYPE_CHECKING, Tuple, cast, Union

import config as config_mod
from config import appcmdname, appname, config
Expand Down Expand Up @@ -145,7 +144,7 @@ class Logger:
logging.Logger instance.
"""

def __init__(self, logger_name: str, loglevel: int | str = _default_loglevel):
def __init__(self, logger_name: str, loglevel: Union[int, str] = _default_loglevel):
"""
Set up a `logging.Logger` with our preferred configuration.

Expand Down Expand Up @@ -183,18 +182,12 @@ def __init__(self, logger_name: str, loglevel: int | str = _default_loglevel):
# rotated versions.
# This is {logger_name} so that EDMC.py logs to a different file.
logfile_rotating = pathlib.Path(tempfile.gettempdir())
logfile_rotating = logfile_rotating / f'{appname}'
logfile_rotating /= f'{appname}'
logfile_rotating.mkdir(exist_ok=True)
logfile_rotating = logfile_rotating / f'{logger_name}-debug.log'

self.logger_channel_rotating = logging.handlers.RotatingFileHandler(
logfile_rotating,
mode='a',
maxBytes=1024 * 1024, # 1MiB
backupCount=10,
encoding='utf-8',
delay=False
)
logfile_rotating /= f'{logger_name}-debug.log'

self.logger_channel_rotating = logging.handlers.RotatingFileHandler(logfile_rotating, maxBytes=1024 * 1024,
backupCount=10, encoding='utf-8')
# Yes, we always want these rotated files to be at TRACE level
self.logger_channel_rotating.setLevel(logging.TRACE) # type: ignore
self.logger_channel_rotating.setFormatter(self.logger_formatter)
Expand All @@ -216,7 +209,7 @@ def get_streamhandler(self) -> logging.Handler:
"""
return self.logger_channel

def set_channels_loglevel(self, level: int | str) -> None:
def set_channels_loglevel(self, level: Union[int, str]) -> None:
"""
Set the specified log level on the channels.

Expand All @@ -226,7 +219,7 @@ def set_channels_loglevel(self, level: int | str) -> None:
self.logger_channel.setLevel(level)
self.logger_channel_rotating.setLevel(level)

def set_console_loglevel(self, level: int | str) -> None:
def set_console_loglevel(self, level: Union[int, str]) -> None:
"""
Set the specified log level on the console channel.

Expand Down Expand Up @@ -535,13 +528,12 @@ def get_main_logger(sublogger_name: str = '') -> 'LoggerMixin':
if not os.getenv("EDMC_NO_UI"):
# GUI app being run
return cast('LoggerMixin', logging.getLogger(appname))
else:
# Must be the CLI
return cast('LoggerMixin', logging.getLogger(appcmdname))
# Must be the CLI
return cast('LoggerMixin', logging.getLogger(appcmdname))


# Singleton
loglevel: str | int = config.get_str('loglevel')
loglevel: Union[str, int] = config.get_str('loglevel')
if not loglevel:
loglevel = logging.INFO

Expand Down
Loading