Skip to content

Commit

Permalink
Disable buffering on macOS app
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdGrub1384 committed Feb 9, 2019
1 parent da9ee71 commit 9534b73
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 254 deletions.
3 changes: 0 additions & 3 deletions Pyto Mac/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// MARK: - Application delegate

func applicationDidFinishLaunching(_ aNotification: Notification) {

NotificationCenter.default.addObserver(EditorViewController.self, selector: #selector(EditorViewController.print(_:)), name: .init(rawValue: "DidReceiveOutput"), object: nil)
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

// MARK: - Menu delegate
Expand Down
19 changes: 3 additions & 16 deletions Pyto Mac/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@ class EditorViewController: NSViewController, SyntaxTextViewDelegate, NSTextView
/// Console content.
@objc var console = ""

/// Prints text when notification is called.
@objc static func print(_ notification: Notification) {
if let str = notification.object as? String {

DispatchQueue.main.async {
for window in NSApp.windows {
if let editor = window.contentViewController as? EditorViewController {
editor.consoleTextView.string += str
editor.console += str
}
}
}
}
}

/// Toggles stop and play button.
@objc static func toggleStopButton() {
DispatchQueue.main.async {
Expand Down Expand Up @@ -198,7 +183,9 @@ class EditorViewController: NSViewController, SyntaxTextViewDelegate, NSTextView
} else if replacementString == "\n", let data = (prompt+"\n").data(using: .utf8) {
console += prompt+"\n"
prompt = ""
Python.shared.inputPipe.fileHandleForWriting.write(data)
if Python.shared.isScriptRunning {
Python.shared.inputPipe.fileHandleForWriting.write(data)
}
} else {
prompt += replacementString ?? ""
}
Expand Down
10 changes: 8 additions & 2 deletions Pyto Mac/Startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
__builtins__.macOS = "macOS"
__builtins__.__platform__ = __builtins__.macOS

from importlib.machinery import SourceFileLoader
__builtins__.widget = "widget"
__builtins__.app = "app"
__builtins__.__host__ = app

# MARK: - Run script

SourceFileLoader("main", "%@").load_module()
from console import run_script

run_script("%@")


87 changes: 85 additions & 2 deletions Pyto Widget/ConsoleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,91 @@ fileprivate var isPythonSetup = false
textView.text = ""
ConsoleViewController.startScript = true

if !Python.shared.isScriptRunning {
Python.shared.runScript(at: Bundle.main.url(forResource: "main", withExtension: "py")!)
let main = """
__builtins__.iOS = "iOS"
__builtins__.macOS = "macOS"
__builtins__.__platform__ = __builtins__.iOS
__builtins__.widget = "widget"
__builtins__.app = "app"
__builtins__.__host__ = widget
import traceback
try:
import importlib.util
from time import sleep
import sys
from outputredirector import *
from extensionsimporter import *
import pyto
import io
except Exception as e:
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
# MARK: - Create selector without class
__builtins__.Selector = pyto.PySelector.makeSelector
__builtins__.Target = pyto.SelectorTarget.shared
# MARK: - Output
def read(text):
pyto.ConsoleViewController.visible.print(text)
standardOutput = Reader(read)
standardOutput._buffer = io.BufferedWriter(standardOutput)
standardError = Reader(read)
standardError._buffer = io.BufferedWriter(standardError)
sys.stdout = standardOutput
sys.stderr = standardError
# MARK: - Modules
sys.meta_path.append(NumpyImporter())
sys.meta_path.append(MatplotlibImporter())
# MARK: - Run script
directory = pyto.ConsoleViewController.sharedDirectoryPath
if not directory+"/modules" in sys.path:
sys.path.append(directory+"/modules")
def run():
pyto.ConsoleViewController.startScript = False
try:
spec = importlib.util.spec_from_file_location("__main__", directory+"/main.py")
script = importlib.util.module_from_spec(spec)
spec.loader.exec_module(script)
except Exception as e:
print(e.__class__.__name__, e)
while not pyto.ConsoleViewController.startScript:
sleep(0.5)
run()
run()
"""

if !Python.shared.isScriptRunning, let newScriptURL = FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).first?.appendingPathComponent("main.py") {

do {
if FileManager.default.fileExists(atPath: newScriptURL.path) {
try FileManager.default.removeItem(at: newScriptURL)
}
if !FileManager.default.createFile(atPath: newScriptURL.path, contents: main.data(using: .utf8), attributes: nil) {
throw NSError(domain: "pyto.widget", code: 1, userInfo: [NSLocalizedDescriptionKey : "Error creating startup script."])
}
Python.shared.runScript(at: newScriptURL)
} catch {
print(error.localizedDescription)
}
}

completionHandler(NCUpdateResult.newData)
Expand Down
2 changes: 1 addition & 1 deletion Pyto Widget/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>9.0</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>5</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
Expand Down
69 changes: 0 additions & 69 deletions Pyto Widget/main.py

This file was deleted.

Loading

0 comments on commit 9534b73

Please sign in to comment.