Skip to content

Commit

Permalink
[CODE] Add aliases attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ObaraEmmanuel committed Jan 17, 2021
1 parent a7df77a commit 10884d9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
33 changes: 11 additions & 22 deletions browser_history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,6 @@ def get_bookmarks():
return output_object


# keep everything lower-cased
browser_aliases = {
"google-chrome": browsers.Chrome,
"chromehtml": browsers.Chrome,
"chromiumhtm": browsers.Chromium,
"chromium-browser": browsers.Chromium,
"msedgehtm": browsers.Edge,
"operastable": browsers.Opera,
"opera-stable": browsers.Opera,
"operagxstable": browsers.OperaGX,
"firefoxurl": browsers.Firefox,
"bravehtml": browsers.Brave,
}


def default_browser():
"""This method gets the default browser of the current platform
Expand All @@ -112,6 +97,7 @@ def default_browser():

# ---- get default from specific platform ----

# Always try to return a lower-cased value for ease of comparison
if plat == utils.Platform.LINUX:
default = webbrowser.get().name.lower()
elif plat == utils.Platform.WINDOWS:
Expand All @@ -126,18 +112,21 @@ def default_browser():
return None

# ---- convert obtained default to something we understand ----
all_browsers = get_browsers()

b_map = {browser.__name__.lower(): browser for browser in get_browsers()}
if default in b_map:
# we are lucky and the name is exactly like we want it
return b_map[default]
# first quick pass for direct matches
for browser in all_browsers:
if default == browser.name.lower() or default in browser.aliases:
return browser

for browser in browser_aliases:
# separate pass for deeper matches
for browser in all_browsers:
# look for alias matches even if the default name has "noise"
# for instance firefox on windows returns something like
# "firefoxurl-3EEDF34567DDE" but we only need "firefoxurl"
if browser in default:
return browser_aliases[browser]
for alias in browser.aliases:
if alias in default:
return browser

# nothing was found
utils.logger.warning("Current default browser is not supported")
Expand Down
7 changes: 7 additions & 0 deletions browser_history/browsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Chromium(ChromiumBasedBrowser):
"""

name = "Chromium"
aliases = ("chromiumhtm", "chromium-browser", "chromiumhtml")

linux_path = ".config/chromium"
windows_path = "AppData/Local/chromium/User Data"
Expand All @@ -40,6 +41,7 @@ class Chrome(ChromiumBasedBrowser):
"""

name = "Chrome"
aliases = ("chromehtml", "google-chrome", "chromehtm")

windows_path = "AppData/Local/Google/Chrome/User Data"
mac_path = "Library/Application Support/Google/Chrome/"
Expand All @@ -61,6 +63,7 @@ class Firefox(Browser):
"""

name = "Firefox"
aliases = ("firefoxurl", )

windows_path = "AppData/Roaming/Mozilla/Firefox/Profiles"
linux_path = ".mozilla/firefox"
Expand Down Expand Up @@ -177,6 +180,7 @@ class Edge(ChromiumBasedBrowser):
"""

name = "Edge"
aliases = ("msedgehtm", "msedge")

windows_path = "AppData/Local/Microsoft/Edge/User Data"
mac_path = "Library/Application Support/Microsoft Edge"
Expand All @@ -195,6 +199,7 @@ class Opera(ChromiumBasedBrowser):
"""

name = "Opera"
aliases = ("operastable", "opera-stable")

linux_path = ".config/opera"
windows_path = "AppData/Roaming/Opera Software/Opera Stable"
Expand All @@ -214,6 +219,7 @@ class OperaGX(ChromiumBasedBrowser):
"""

name = "OperaGX"
aliases = ("operagxstable", "operagx-stable")

windows_path = "AppData/Roaming/Opera Software/Opera GX Stable"

Expand All @@ -233,6 +239,7 @@ class Brave(ChromiumBasedBrowser):
"""

name = "Brave"
aliases = ("bravehtml", )

linux_path = ".config/BraveSoftware/Brave-Browser"
mac_path = "Library/Application Support/BraveSoftware/Brave-Browser"
Expand Down
8 changes: 8 additions & 0 deletions browser_history/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Browser(abc.ABC):
* :py:class:`profile_support`
* :py:class:`profile_dir_prefixes`
* :py:class:`_local_tz`
* :py:class:`aliases` :A tuple containing other names for the browser in lowercase
:param plat: the current platform. A value of :py:class:`None` means the platform
will be inferred from the system.
Expand All @@ -57,6 +58,7 @@ class Browser(abc.ABC):
>>> class CustomBrowser(Browser):
... name = 'custom browser'
... aliases = ('custom-browser', 'customhtm')
... history_file = 'history_file'
... history_SQL = \"\"\"
... SELECT
Expand Down Expand Up @@ -93,6 +95,12 @@ class Browser(abc.ABC):
history_dir: Path
"""History directory."""

"""Gets possible names (lower-cased) used to refer to the browser type.
Useful for making the browser detectable as a default browser which may be
named in various forms on different platforms. Do not include :py:class:`name`
in this list"""
aliases: tuple = ()

@property
@abc.abstractmethod
def name(self) -> str:
Expand Down

0 comments on commit 10884d9

Please sign in to comment.