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

Python 2/3 compat fixes, and first demo apps with bundled python3.5 #32

Merged
merged 14 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion Demos/SimpleApp/SimpleApp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from AppKit import NSObject
from PyObjCTools import AppHelper
import vanilla
Expand All @@ -18,7 +19,7 @@ def __init__(self):
self.w.open()

def buttonCallback(self, sender):
print "You pressed the button!"
print("You pressed the button!", flush=True)


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions Demos/TinyTextEditor/TinyTextEditor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from AppKit import NSDocument
from PyObjCTools import AppHelper
from tinyTextEditorDocumentWindow import TinyTextEditorDocumentWindow
from io import open


class TinyTextEditorDocument(NSDocument):
Expand All @@ -13,7 +14,7 @@ def init(self):

def readFromFile_ofType_(self, path, tp):
# refer to the NSDocument reference for information about this method
f = open(path, 'rb')
f = open(path, 'r', encoding='utf-8')
text = f.read()
f.close()
self.vanillaWindowController.setText(text)
Expand All @@ -22,7 +23,7 @@ def readFromFile_ofType_(self, path, tp):
def writeWithBackupToFile_ofType_saveOperation_(self, fileName, fileType, operation):
# refer to the NSDocument reference for information about this method
text = self.vanillaWindowController.getText()
f = open(fileName, 'wb')
f = open(fileName, 'w', encoding='utf-8')
f.write(text)
f.close()
return True
Expand Down
2 changes: 1 addition & 1 deletion Documentation/source/objects/FloatingWindow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ FloatingWindow
self.w.open()

def windowMoved(sender):
print "window moved!", sender
print("window moved!", sender)

WindowBindDemo()

Expand Down
2 changes: 1 addition & 1 deletion Documentation/source/objects/Sheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Sheet
self.w.open()

def windowMoved(sender):
print "window moved!", sender
print("window moved!", sender)

WindowBindDemo()

Expand Down
2 changes: 1 addition & 1 deletion Documentation/source/objects/Window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Window
self.w.open()

def windowMoved(self, sender):
print "window moved!", sender
print("window moved!", sender)

WindowBindDemo()

Expand Down
62 changes: 31 additions & 31 deletions Lib/vanilla/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
from vanillaBase import VanillaBaseObject, VanillaBaseControl, VanillaError
from vanillaBox import Box, HorizontalLine, VerticalLine
from vanillaBrowser import ObjectBrowser
from vanillaButton import Button, SquareButton, ImageButton, HelpButton
from vanillaCheckBox import CheckBox
from vanillaColorWell import ColorWell
from vanillaComboBox import ComboBox
from vanillaDatePicker import DatePicker
from vanillaDrawer import Drawer
from vanillaEditText import EditText, SecureEditText
from vanillaGradientButton import GradientButton
from vanillaGroup import Group
from vanillaImageView import ImageView
from vanillaLevelIndicator import LevelIndicator, LevelIndicatorListCell
from vanillaList import List, CheckBoxListCell, SliderListCell, PopUpButtonListCell, ImageListCell, SegmentedButtonListCell
from vanillaPathControl import PathControl
from vanillaPopUpButton import PopUpButton, ActionButton
from vanillaProgressBar import ProgressBar
from vanillaProgressSpinner import ProgressSpinner
from vanillaRadioGroup import RadioGroup
from vanillaScrollView import ScrollView
from vanillaSearchBox import SearchBox
from vanillaSegmentedButton import SegmentedButton
from vanillaSlider import Slider
from vanillaSplitView2 import SplitView2
from vanillaTabs import Tabs
from vanillaTextBox import TextBox
from vanillaTextEditor import TextEditor
from vanillaWindows import Window, FloatingWindow, HUDFloatingWindow, Sheet
from vanilla.vanillaBase import VanillaBaseObject, VanillaBaseControl, VanillaError
from vanilla.vanillaBox import Box, HorizontalLine, VerticalLine
from vanilla.vanillaBrowser import ObjectBrowser
from vanilla.vanillaButton import Button, SquareButton, ImageButton, HelpButton
from vanilla.vanillaCheckBox import CheckBox
from vanilla.vanillaColorWell import ColorWell
from vanilla.vanillaComboBox import ComboBox
from vanilla.vanillaDatePicker import DatePicker
from vanilla.vanillaDrawer import Drawer
from vanilla.vanillaEditText import EditText, SecureEditText
from vanilla.vanillaGradientButton import GradientButton
from vanilla.vanillaGroup import Group
from vanilla.vanillaImageView import ImageView
from vanilla.vanillaLevelIndicator import LevelIndicator, LevelIndicatorListCell
from vanilla.vanillaList import List, CheckBoxListCell, SliderListCell, PopUpButtonListCell, ImageListCell, SegmentedButtonListCell
from vanilla.vanillaPathControl import PathControl
from vanilla.vanillaPopUpButton import PopUpButton, ActionButton
from vanilla.vanillaProgressBar import ProgressBar
from vanilla.vanillaProgressSpinner import ProgressSpinner
from vanilla.vanillaRadioGroup import RadioGroup
from vanilla.vanillaScrollView import ScrollView
from vanilla.vanillaSearchBox import SearchBox
from vanilla.vanillaSegmentedButton import SegmentedButton
from vanilla.vanillaSlider import Slider
from vanilla.vanillaSplitView2 import SplitView2
from vanilla.vanillaTabs import Tabs
from vanilla.vanillaTextBox import TextBox
from vanilla.vanillaTextEditor import TextEditor
from vanilla.vanillaWindows import Window, FloatingWindow, HUDFloatingWindow, Sheet

__all__ = [
"VanillaBaseObject", "VanillaBaseControl", "VanillaError",
Expand Down Expand Up @@ -64,7 +64,7 @@

# OS 10.7 objects
try:
from vanillaPopover import Popover
from vanilla.vanillaPopover import Popover
__all__.append("Popover")
except (ImportError, NameError):
pass
Expand All @@ -76,7 +76,7 @@ def __init__(self, *args, **kwargs):
raise VanillaError("SplitView is not available because the RBSplitView framework cannot be found. Refer to the Vanilla documentation for details.")

try:
from vanillaSplitView import SplitView
from vanilla.vanillaSplitView import SplitView
from vanilla.externalFrameworks import RBSplitView
except (ImportError, ValueError):
SplitView = _NoRBSplitView
Expand Down
1 change: 1 addition & 0 deletions Lib/vanilla/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initWithMessageText_informativeText_alertStyle_buttonTitlesValues_window_res
alert.beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(parentWindow, self, "alertDidEnd:returnCode:contextInfo:", 0)
return self

@objc.python_method
def _translateValue(self, code):
if code == NSAlertFirstButtonReturn:
value = 1
Expand Down
2 changes: 2 additions & 0 deletions Lib/vanilla/nsSubclasses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import objc
import weakref

from vanilla.py23 import basestring


class _VanillaMethods:

Expand Down
26 changes: 26 additions & 0 deletions Lib/vanilla/py23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
__all__ = ['basestring', 'unicode', 'long']

try:
basestring = basestring
except NameError:
basestring = str

try:
unicode = unicode
except NameError:
unicode = str

try:
long = long
except NameError:
long = int

try:
range = xrange
except NameError:
range = range

try:
unichr = unichr
except NameError:
unichr = chr
31 changes: 19 additions & 12 deletions Lib/vanilla/test/testAll.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from __future__ import print_function
import time
import os
import sys
from AppKit import *
import vanilla
reload(vanilla)
try:
reload(vanilla)
except NameError:
# the built-in 'reload' was moved to importlib with Python 3.4
from importlib import reload
reload(vanilla)
from vanilla.py23 import range
from vanilla import *

import objc
Expand All @@ -14,36 +21,36 @@

sizeStyles = ["regular", "small", "mini"]

listOptions = sys.modules.keys()
sortedListOptions = list(listOptions)
sortedListOptions.sort()
listOptions = list(sys.modules.keys())
sortedListOptions = sorted(listOptions)


class BaseTest(object):

def drawGrid(self):
w, h = self.w.getPosSize()[2:]
increment = 10
for i in xrange(int(w/increment)):
for i in range(int(w/increment)):
if i == 0:
continue
attrName = "vline%d" % i
line = VerticalLine((increment*i, 0, 1, h))
setattr(self.w, attrName, line)
for i in xrange(int(h/increment)):
for i in range(int(h/increment)):
if i == 0:
continue
attrName = "hline%d" % i
line = HorizontalLine((0, increment*i, w, 1))
setattr(self.w, attrName, line)

def basicCallback(self, sender):
print sender
print(sender)

def titleCallback(self, sender):
print sender, sender.getTitle()
print(sender, sender.getTitle())

def getCallback(self, sender):
print sender, sender.get()
print(sender, sender.get())


class WindowTest(BaseTest):
Expand Down Expand Up @@ -395,8 +402,8 @@ def drawRect_(self, rect):
width, height = self.frame()[1]
w = width / 5
h = height / 5
for xI in xrange(5):
for yI in xrange(5):
for xI in range(5):
for yI in range(5):
x = xI * w
y = height - (yI * h) - h
r = ((x, y), (w, h))
Expand Down Expand Up @@ -498,7 +505,7 @@ def startProgress(self, sender):
self.w.spinner1.start()
self.w.spinner2.start()
self.w.bar2.start()
for i in xrange(10):
for i in range(10):
self.w.bar1.increment(10)
time.sleep(.1)
time.sleep(.5)
Expand Down
2 changes: 1 addition & 1 deletion Lib/vanilla/vanillaBase.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import platform
from AppKit import *
from distutils.version import StrictVersion
from nsSubclasses import getNSSubclass
from vanilla.nsSubclasses import getNSSubclass

osVersionCurrent = StrictVersion(platform.mac_ver()[0])
osVersion10_12 = StrictVersion("10.12")
Expand Down
2 changes: 1 addition & 1 deletion Lib/vanilla/vanillaBox.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from AppKit import *
from vanillaBase import VanillaBaseObject, _breakCycles, osVersionCurrent, osVersion10_10
from vanilla.vanillaBase import VanillaBaseObject, _breakCycles, osVersionCurrent, osVersion10_10


class Box(VanillaBaseObject):
Expand Down
23 changes: 13 additions & 10 deletions Lib/vanilla/vanillaBrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
I beleive that demo was written by Just van Rossum.
"""

import objc
import AppKit
from operator import getitem, setitem

import inspect

from vanilla.py23 import unicode, long, range
from vanilla.vanillaBase import VanillaBaseObject
from vanilla.nsSubclasses import getNSSubclass

Expand Down Expand Up @@ -162,7 +164,7 @@ def getArguments(obj):
and leave 'self' out.
"""
try:
arguments = apply(inspect.formatargspec, inspect.getargspec(obj))
arguments = inspect.formatargspec(*inspect.getargspec(obj))
except TypeError:
arguments = ""
return arguments.replace("self, ", "").replace("self", "")
Expand Down Expand Up @@ -205,14 +207,13 @@ def __init__(self, name, obj, parent, setvalue, ignoreAppKit=True):
self.getters = dict()
self.setters = dict()
if isinstance(obj, dict):
self.children = obj.keys()
self.children.sort()
self.children = sorted(obj.keys())
self._setGetters(self.children, getitem)
self._setSetters(self.children, setitem)
elif obj is None or isinstance(obj, SIMPLE_TYPES):
pass
elif isinstance(obj, (list, tuple, set)):
self.children = range(len(obj))
self.children = list(range(len(obj)))
self._setGetters(self.children, getitem)
self._setSetters(self.children, setitem)
elif isinstance(obj, property):
Expand All @@ -224,16 +225,15 @@ def __init__(self, name, obj, parent, setvalue, ignoreAppKit=True):
else:
try:
l = list(obj)
self.children = range(len(l))
self.children = list(range(len(l)))
self._setGetters(self.children, getitem)
self._setSetters(self.children, setitem)
except:
pass

try:
d = dict(obj)
self.children = d.keys()
self.children.sort()
self.children = sorted(d.keys())
self._setGetters(self.children, getitem)
self._setSetters(self.children, setitem)
except:
Expand All @@ -250,20 +250,23 @@ def __init__(self, name, obj, parent, setvalue, ignoreAppKit=True):
self.children = [child for child in self.children if not (isinstance(child, (str, unicode)) and hasattr(AppKit, child))]

self._childRefs = {}


@objc.python_method
def _setSetters(self, names, callback):
for name in names:
self.setters[name] = callback


@objc.python_method
def _setGetters(self, names, callback):
for name in names:
self.getters[name] = callback

def isExpandable(self):
return bool(self.children)

@objc.python_method
def getChild(self, child):
if self._childRefs.has_key(child):
if child in self._childRefs:
return self._childRefs[child]

name = self.children[child]
Expand Down
Loading