Skip to content

Commit

Permalink
Use dictionaries to get constant values for different OS
Browse files Browse the repository at this point in the history
  • Loading branch information
albertosottile committed May 14, 2019
1 parent 4ff359a commit ff6bb74
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
50 changes: 33 additions & 17 deletions syncplay/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# coding:utf8
# code needed to get customized constants for different OS
import sys

OS_WINDOWS = "win"
OS_LINUX = "linux"
OS_MACOS = "darwin"
OS_BSD = "freebsd"
OS_DRAGONFLY = "dragonfly"
OS_DEFAULT = "default"

def getValueForOS(constantDict):
if sys.platform.startswith(OS_WINDOWS):
return constantDict[OS_WINDOWS] if OS_WINDOWS in constantDict else constantDict[OS_DEFAULT]
if sys.platform.startswith(OS_LINUX):
return constantDict[OS_LINUX] if OS_LINUX in constantDict else constantDict[OS_DEFAULT]
if sys.platform.startswith(OS_MACOS):
return constantDict[OS_MACOS] if OS_MACOS in constantDict else constantDict[OS_DEFAULT]
if OS_BSD in sys.platform or sys.platform.startswith(OS_DRAGONFLY):
return constantDict[OS_BSD] if OS_BSD in constantDict else constantDict[OS_DEFAULT]

# You might want to change these
DEFAULT_PORT = 8999
OSD_DURATION = 3.0
Expand Down Expand Up @@ -62,9 +82,10 @@
PLAYLIST_MAX_ITEMS = 250
MAXIMUM_TAB_WIDTH = 350
TAB_PADDING = 30
DEFAULT_WINDOWS_MONOSPACE_FONT = "Consolas"
DEFAULT_OSX_MONOSPACE_FONT = "Menlo"
FALLBACK_MONOSPACE_FONT = "Monospace"
MONOSPACE_FONT = getValueForOS({
OS_DEFAULT: "Monospace",
OS_MACOS: "Menlo",
OS_WINDOWS: "Consolas"})
DEFAULT_CHAT_FONT_SIZE = 24
DEFAULT_CHAT_INPUT_FONT_COLOR = "#FFFF00"
DEFAULT_CHAT_OUTPUT_FONT_COLOR = "#FFFF00"
Expand Down Expand Up @@ -175,8 +196,12 @@
STYLE_SUBLABEL = "QCheckBox, QLabel {{ margin-left: 6px; padding-left: 16px; background:url('{}') left no-repeat }}" # Graphic path
STYLE_ERRORLABEL = "QLabel { color : black; border-style: outset; border-width: 2px; border-radius: 7px; border-color: red; padding: 2px; background: #FFAAAA; }"
STYLE_SUCCESSLABEL = "QLabel { color : black; border-style: outset; border-width: 2px; border-radius: 7px; border-color: green; padding: 2px; background: #AAFFAA; }"
STYLE_READY_PUSHBUTTON = "QPushButton { text-align: left; padding: 10px 5px 10px 5px;}"
STYLE_AUTO_PLAY_PUSHBUTTON = "QPushButton { text-align: left; padding: 5px 5px 5px 5px; }"
STYLE_READY_PUSHBUTTON = getValueForOS({
OS_DEFAULT: "QPushButton { text-align: left; padding: 10px 5px 10px 5px;}",
OS_MACOS: "QPushButton { text-align: left; padding: 10px 5px 10px 15px; margin: 0px 3px 0px 2px}"})
STYLE_AUTO_PLAY_PUSHBUTTON = getValueForOS({
OS_DEFAULT: "QPushButton { text-align: left; padding: 5px 5px 5px 5px; }",
OS_MACOS: "QPushButton { text-align: left; padding: 10px 5px 10px 15px; margin: 0px 0px 0px -4px}"})
STYLE_NOTIFICATIONBOX = "Username { color: #367AA9; font-weight:bold; }"
STYLE_CONTACT_INFO = "<span style=\"color: grey\"><strong><small>{}</span><br /><br />" # Contact info message
STYLE_USER_MESSAGE = "<span style=\"{}\">&lt;{}&gt;</span> {}"
Expand All @@ -187,10 +212,6 @@
STYLE_NOTCONTROLLER_COLOR = 'grey'
STYLE_UNTRUSTEDITEM_COLOR = 'purple'

# Style constants for macOS
STYLE_READY_PUSHBUTTON_MACOS = "QPushButton { text-align: left; padding: 10px 5px 10px 15px; margin: 0px 3px 0px 2px}"
STYLE_AUTO_PLAY_PUSHBUTTON_MACOS = "QPushButton { text-align: left; padding: 10px 5px 10px 15px; margin: 0px 0px 0px -4px}"

TLS_CERT_ROTATION_MAX_RETRIES = 10

USERLIST_GUI_USERNAME_OFFSET = 21 # Pixels
Expand Down Expand Up @@ -221,8 +242,9 @@
MPV_SYNCPLAYINTF_LANGUAGE_TO_SEND = ["mpv-key-tab-hint", "mpv-key-hint", "alphakey-mode-warning-first-line", "alphakey-mode-warning-second-line"]
VLC_SLAVE_ARGS = ['--extraintf=luaintf', '--lua-intf=syncplay', '--no-quiet', '--no-input-fast-seek',
'--play-and-pause', '--start-time=0']
VLC_SLAVE_MACOS_ARGS = ['--verbose=2', '--no-file-logging']
VLC_SLAVE_NONMACOS_ARGS = ['--no-one-instance', '--no-one-instance-when-started-from-file']
VLC_SLAVE_EXTRA_ARGS = getValueForOS({
OS_DEFAULT: ['--no-one-instance', '--no-one-instance-when-started-from-file'],
OS_MACOS: ['--verbose=2', '--no-file-logging']})
MPV_SUPERSEDE_IF_DUPLICATE_COMMANDS = ["no-osd set time-pos ", "loadfile "]
MPV_REMOVE_BOTH_IF_DUPLICATE_COMMANDS = ["cycle pause"]
MPLAYER_ANSWER_REGEX = "^ANS_([a-zA-Z_-]+)=(.+)$|^(Exiting)\.\.\. \((.+)\)$"
Expand Down Expand Up @@ -279,9 +301,3 @@
TRUSTABLE_WEB_PROTOCOLS = ["http://www.", "https://www.", "http://", "https://"]

PRIVATE_FILE_FIELDS = ["path"]

OS_WINDOWS = "win"
OS_LINUX = "linux"
OS_MACOS = "darwin"
OS_BSD = "freebsd"
OS_DRAGONFLY = "dragonfly"
5 changes: 1 addition & 4 deletions syncplay/players/vlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class VlcPlayer(BasePlayer):

RE_ANSWER = re.compile(constants.VLC_ANSWER_REGEX)
SLAVE_ARGS = constants.VLC_SLAVE_ARGS
if isMacOS():
SLAVE_ARGS.extend(constants.VLC_SLAVE_MACOS_ARGS)
else:
SLAVE_ARGS.extend(constants.VLC_SLAVE_NONMACOS_ARGS)
SLAVE_ARGS.extend(constants.VLC_SLAVE_EXTRA_ARGS)
vlcport = random.randrange(constants.VLC_MIN_PORT, constants.VLC_MAX_PORT) if (constants.VLC_MIN_PORT < constants.VLC_MAX_PORT) else constants.VLC_MIN_PORT

def __init__(self, client, playerPath, filePath, args):
Expand Down
8 changes: 2 additions & 6 deletions syncplay/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,10 +1426,7 @@ def addBottomLayout(self, window):
window.readyPushButton.setAutoExclusive(False)
window.readyPushButton.toggled.connect(self.changeReadyState)
window.readyPushButton.setFont(readyFont)
if isMacOS():
window.readyPushButton.setStyleSheet(constants.STYLE_READY_PUSHBUTTON_MACOS)
else:
window.readyPushButton.setStyleSheet(constants.STYLE_READY_PUSHBUTTON)
window.readyPushButton.setStyleSheet(constants.STYLE_READY_PUSHBUTTON)
window.readyPushButton.setToolTip(getMessage("ready-tooltip"))
window.listLayout.addWidget(window.readyPushButton, Qt.AlignRight)
if isMacOS(): window.listLayout.setContentsMargins(0, 0, 0, 10)
Expand All @@ -1451,12 +1448,11 @@ def addBottomLayout(self, window):
window.autoplayFrame.setMinimumWidth(window.listFrame.sizeHint().width())
window.autoplayLayout.setSpacing(15)
window.autoplayLayout.setContentsMargins(0, 8, 3, 3)
window.autoplayPushButton.setStyleSheet(constants.STYLE_AUTO_PLAY_PUSHBUTTON_MACOS)
window.autoplayPushButton.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
else:
window.autoplayLayout.setContentsMargins(0, 0, 0, 0)
window.autoplayPushButton.setStyleSheet(constants.STYLE_AUTO_PLAY_PUSHBUTTON)
window.autoplayPushButton.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
window.autoplayPushButton.setStyleSheet(constants.STYLE_AUTO_PLAY_PUSHBUTTON)
window.autoplayPushButton.setToolTip(getMessage("autoplay-tooltip"))
window.autoplayLabel = QtWidgets.QLabel(getMessage("autoplay-minimum-label"))
window.autoplayLabel.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
Expand Down
7 changes: 1 addition & 6 deletions syncplay/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,7 @@ def getResourcesPath():


def getDefaultMonospaceFont():
if platform.system() == "Windows":
return constants.DEFAULT_WINDOWS_MONOSPACE_FONT
elif platform.system() == "Darwin":
return constants.DEFAULT_OSX_MONOSPACE_FONT
else:
return constants.FALLBACK_MONOSPACE_FONT
return constants.MONOSPACE_FONT


def limitedPowerset(s, minLength):
Expand Down

0 comments on commit ff6bb74

Please sign in to comment.