From b8e5b6d00b40cfe94efd0bb4fce7a74b971abb0f Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Fri, 14 Oct 2016 10:44:28 +0300 Subject: Added screensavers --- README.md | 3 ++- machammer/functions.py | 27 +++++++++++++++++++++++++-- machammer/processes.py | 14 ++++++++++++++ machammer/screensaver.py | 18 ++++++++++++++++++ tests.py | 15 ++++++++++++++- 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100755 machammer/processes.py create mode 100644 machammer/screensaver.py diff --git a/README.md b/README.md index 58ad589..301d009 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ myprinter = ('LaserWriter 8500', 'LaserWriter8500.ppd', 'lw.example.com',) printers.add_printer(myprinter) # Install Java. mount_and_install takes 2 arguments - the path to the disk image and the path to installation package on the mounted volume -mh.mount_and_install(os.path.join(APP_ROOT, 'jre-8u101-macosx-x64.dmg'), 'Java 8 Update 101.app/Contents/Resources/JavaAppletPlugin.pkg') +mh.mount_and_install(os.path.join(APP_ROOT, 'jre-8u101-macosx-x64.dmg'), + 'Java 8 Update 101.app/Contents/Resources/JavaAppletPlugin.pkg') # Install Microsoft Office. mount_image returns the path to the new mountpoint p = mh.mount_image('/Volumes/MyShare/Installation/Office2016.dmg') diff --git a/machammer/functions.py b/machammer/functions.py index d797b32..0a7b853 100644 --- a/machammer/functions.py +++ b/machammer/functions.py @@ -13,6 +13,16 @@ from system_profiler import SystemProfile SERVICEDIR = '/Library/Services' +def get_plist(path): + """Return plist dict regardless of format. + + """ + plist = subprocess.check_output(['/usr/bin/plutil', + '-convert', 'xml1', + path, '-o', '-']) + return plistlib.readPlistFromString(plist) + + def call(*args): """Shorthand for subprocess.call. @@ -21,6 +31,16 @@ def call(*args): subprocess.call(args) +def check_output(*args): + """Shortcut for subprocess.check_output.""" + result = subprocess.check_output(args).strip() + + if len(result) < 1: + result = None + + return result + + def rmdir(path): """Shortcut for deleting a directory.""" call('rm', '-r', path) @@ -62,11 +82,14 @@ def exec_jar(path, user): def osascript(s): - subprocess.call(['/usr/bin/osascript', '-e', s]) + try: + return check_output('/usr/bin/osascript', '-e', s) + except Exception as e: + raise Exception('The AppleScript returned an error: %s' % e) def tell_app(app, s): - osascript('tell application "%s" to %s' % (app, s)) + return osascript('tell application "%s" to %s' % (app, s)) def quit_app(app): diff --git a/machammer/processes.py b/machammer/processes.py new file mode 100755 index 0000000..71c2936 --- /dev/null +++ b/machammer/processes.py @@ -0,0 +1,14 @@ +from .functions import osascript + + +def pidof(name): + pass + +def is_running(name): + pass + +def quit(name): + pass + +def kill(name): + pass diff --git a/machammer/screensaver.py b/machammer/screensaver.py new file mode 100644 index 0000000..0910214 --- /dev/null +++ b/machammer/screensaver.py @@ -0,0 +1,18 @@ +from .functions import tell_app + + + +def get(): + return tell_app('System Events', 'get name of current screen saver') + + +def set(name): + return tell_app('System Events', 'set current screen saver to (get screen saver named "%s")' % name) + + +def start(): + return tell_app('System Events', 'start current screen saver') + + +def stop(): + return tell_app('System Events', 'stop current screen saver') diff --git a/tests.py b/tests.py index fcce82a..bec8125 100755 --- a/tests.py +++ b/tests.py @@ -6,7 +6,7 @@ import subprocess from unittest import main, skip, TestCase import machammer.functions as mh -from machammer import system_profiler +from machammer import system_profiler, screensaver class SystemProfilerTestCase(TestCase): @@ -73,6 +73,19 @@ class FunctionsTestCase(TestCase): self.assertEquals(p, '/Volumes/Adobe Flash Player Installer') +class ScreenSaverTestCase(TestCase): + def test_set_invalid(self): + with self.assertRaises(Exception): + screensaver.set('Blalala') + + def test_set_flurry(self): + self.assertEquals(screensaver.set('Flurry'), None) + + + def test_get(self): + self.assertEquals(screensaver.get(), 'Flurry') + + if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) main() -- cgit v1.2.3