From 378a66cf54bea5f554b996adec9ec1b06a5e136d Mon Sep 17 00:00:00 2001 From: Davee Nguyen Date: Tue, 19 Mar 2019 20:00:33 -0700 Subject: [PATCH] feat: delete Clipboard and Prefs I've decided to cut these two modules to slim down atomacos. Maybe someone can provide use case and tests, it can be readded. BREAKING CHANGE: Module was importable from atomacos and are now deleted. No alternative exists in the project. --- atomacos/Clipboard.py | 209 ------------------------------------------ atomacos/Prefs.py | 101 -------------------- atomacos/__init__.py | 2 - tests/test_prefs.py | 7 -- 4 files changed, 319 deletions(-) delete mode 100755 atomacos/Clipboard.py delete mode 100755 atomacos/Prefs.py delete mode 100644 tests/test_prefs.py diff --git a/atomacos/Clipboard.py b/atomacos/Clipboard.py deleted file mode 100755 index 4779e48..0000000 --- a/atomacos/Clipboard.py +++ /dev/null @@ -1,209 +0,0 @@ -# Copyright (c) 2010 VMware, Inc. All Rights Reserved. - -# This file is part of ATOMac. - -# ATOMac is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by the Free -# Software Foundation version 2 and no later version. - -# ATOMac is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License version 2 -# for more details. - -# You should have received a copy of the GNU General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -# St, Fifth Floor, Boston, MA 02110-1301 USA. - -import logging -import pprint -import types - -import AppKit -import Foundation - - -class Clipboard(object): - """Class to represent clipboard-related operations for text""" - - # String encoding type - utf8encoding = Foundation.NSUTF8StringEncoding - - # Class attributes to distinguish types of data: - # Reference: - # http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ - # ApplicationKit/Classes/NSPasteboard_Class/Reference/Reference.html - - # Text data type - STRING = AppKit.NSStringPboardType - # Rich-text format data type (e.g. rtf documents) - RTF = AppKit.NSRTFPboardType - # Image datatype (e.g. tiff) - IMAGE = AppKit.NSTIFFPboardType - # URL data type (not just web but file locations also) - URL = AppKit.NSURLPboardType - # Color datatype - not sure if we'll have to use this one - # Supposedly replaced in 10.6 but the pyobjc AppKit module doesn't have the - # new data type as an attribute - COLOR = AppKit.NSColorPboardType - - # You can extend this list of data types - # e.g. File copy and paste between host and guest - # Not sure if text copy and paste between host and guest falls under STRING/ - # RTF or not - # List of PboardTypes I found in AppKit: - # NSColorPboardType - # NSCreateFileContentsPboardType - # NSCreateFilenamePboardType - # NSDragPboard - # NSFileContentsPboardType - # NSFilenamesPboardType - # NSFilesPromisePboardType - # NSFindPanelSearchOptionsPboardType - # NSFindPboard - # NSFontPboard - # NSFontPboardType - # NSGeneralPboard - # NSHTMLPboardType - # NSInkTextPboardType - # NSMultipleTextSelectionPboardType - # NSPDFPboardType - # NSPICTPboardType - # NSPostScriptPboardType - # NSRTFDPboardType - # NSRTFPboardType - # NSRulerPboard - # NSRulerPboardType - # NSSoundPboardType - # NSStringPboardType - # NSTIFFPboardType - # NSTabularTextPboardType - # NSURLPboardType - # NSVCardPboardType - - @classmethod - def paste(cls): - """Get the clipboard data ('Paste'). - - Returns: Data (string) retrieved or None if empty. Exceptions from - AppKit will be handled by caller. - """ - pb = AppKit.NSPasteboard.generalPasteboard() - - # If we allow for multiple data types (e.g. a list of data types) - # we will have to add a condition to check just the first in the - # list of datatypes) - data = pb.stringForType_(cls.STRING) - return data - - @classmethod - def copy(cls, data): - """Set the clipboard data ('Copy'). - - Parameters: data to set (string) - Optional: datatype if it's not a string - Returns: True / False on successful copy, Any exception raised (like - passes the NSPasteboardCommunicationError) should be caught - by the caller. - """ - pp = pprint.PrettyPrinter() - - copy_data = "Data to copy (put in pasteboard): %s" - logging.debug(copy_data % pp.pformat(data)) - - # Clear the pasteboard first: - cleared = cls.clearAll() - if not cleared: - logging.warning("Clipboard could not clear properly") - return False - - # Prepare to write the data - # If we just use writeObjects the sequence to write to the clipboard is - # a) Call clearContents() - # b) Call writeObjects() with a list of objects to write to the - # clipboard - if not isinstance(data, types.ListType): - data = [data] - - pb = AppKit.NSPasteboard.generalPasteboard() - pb_set_ok = pb.writeObjects_(data) - - return bool(pb_set_ok) - - @classmethod - def clearContents(cls): - """Clear contents of general pasteboard. - - Future enhancement can include specifying which clipboard to clear - Returns: True on success; caller should expect to catch exceptions, - probably from AppKit (ValueError) - """ - log_msg = "Request to clear contents of pasteboard: general" - logging.debug(log_msg) - pb = AppKit.NSPasteboard.generalPasteboard() - pb.clearContents() - return True - - @classmethod - def clearProperties(cls): - """Clear properties of general pasteboard. - - Future enhancement can include specifying which clipboard's properties - to clear - Returns: True on success; caller should catch exceptions raised, - e.g. from AppKit (ValueError) - """ - log_msg = "Request to clear properties of pasteboard: general" - logging.debug(log_msg) - pb = AppKit.NSPasteboard.generalPasteboard() - pb.clearProperties() - - return True - - @classmethod - def clearAll(cls): - """Clear contents and properties of general pasteboard. - - Future enhancement can include specifying which clipboard's properties - to clear - Returns: Boolean True on success; caller should handle exceptions - """ - cls.clearContents() - cls.clearProperties() - - return True - - @classmethod - def isEmpty(cls, datatype=None): - """Method to test if the general pasteboard is empty or not with respect - to the type of object you want. - - Parameters: datatype (defaults to strings) - Returns: Boolean True (empty) / False (has contents); Raises - exception (passes any raised up) - """ - if not datatype: - datatype = AppKit.NSString - if not isinstance(datatype, types.ListType): - datatype = [datatype] - pp = pprint.PrettyPrinter() - logging.debug("Desired datatypes: %s" % pp.pformat(datatype)) - opt_dict = {} - logging.debug("Results filter is: %s" % pp.pformat(opt_dict)) - - try: - log_msg = "Request to verify pasteboard is empty" - logging.debug(log_msg) - pb = AppKit.NSPasteboard.generalPasteboard() - # canReadObjectForClasses_options_() seems to return an int (> 0 if - # True) - # Need to negate to get the sense we want (True if can not read the - # data type from the pasteboard) - its_empty = not bool( - pb.canReadObjectForClasses_options_(datatype, opt_dict) - ) - except ValueError as error: - logging.exception(error) - raise - - return bool(its_empty) diff --git a/atomacos/Prefs.py b/atomacos/Prefs.py deleted file mode 100755 index f1f62da..0000000 --- a/atomacos/Prefs.py +++ /dev/null @@ -1,101 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (c) 2011 Julián Romero. - -# This file is part of ATOMac. - -# ATOMac is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by the Free -# Software Foundation version 2 and no later version. - -# ATOMac is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License version 2 -# for more details. - -# You should have received a copy of the GNU General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -# St, Fifth Floor, Boston, MA 02110-1301 USA. - -from os import path - -from AppKit import NSDictionary, NSUserDefaults, NSWorkspace -from future import standard_library - -try: - from collections import UserDict -except ImportError: - from UserDict import UserDict - -standard_library.install_aliases() - -__all__ = ["Prefs"] - - -class Prefs(UserDict): - """NSUserDefaults proxy to read/write application preferences. - It has been conceived to prepare the preferences before a test launch the - app. Once a Prefs instance is created, it doesn't detect prefs changed - elsewhere, so for now you need to create the instance right before - reading/writing a pref. - Defaults.plist with default values is expected to exist on the app bundle. - - p = Prefs('com.example.App') - coolStuff = p['CoolStuff'] - p['CoolStuff'] = newCoolStuff - """ - - def __init__(self, bundleID, bundlePath=None, defaultsPlistName="Defaults"): - """ - bundleId: the application bundle identifier - bundlePath: the full bundle path (useful to test a Debug build) - defaultsPlistName: the name of the plist that contains default values - """ - self.__bundleID = bundleID - self.__bundlePath = bundlePath - UserDict.__init__(self) - self.__setup(defaultsPlistName) - - def __setup(self, defaultsPlistName=None): - NSUserDefaults.resetStandardUserDefaults() - prefs = NSUserDefaults.standardUserDefaults() - self.defaults = self.__defaults(defaultsPlistName) - domainData = prefs.persistentDomainForName_(self.__bundleID) - if domainData: - self.data = domainData - else: - self.data = NSDictionary.dictionary() - - def __defaults(self, plistName="Defaults"): - if self.__bundlePath is None: - self.__bundlePath = NSWorkspace.sharedWorkspace().absolutePathForAppBundleWithIdentifier_( # noqa:: B950 - self.__bundleID - ) - if self.__bundlePath: - plistPath = path.join( - self.__bundlePath, "Contents/Resources/%s.plist" % plistName - ) - plist = NSDictionary.dictionaryWithContentsOfFile_(plistPath) - if plist: - return plist - return NSDictionary.dictionary() - - def get(self, key): - return self.__getitem__(key) - - def __getitem__(self, key): - result = self.data.get(key, None) - if result is None or result == "": - if self.defaults: - result = self.defaults.get(key, None) - return result - - def set(self, key, value): - self.__setitem__(key, value) - - def __setitem__(self, key, value): - mutableData = self.data.mutableCopy() - mutableData[key] = value - self.data = mutableData - prefs = NSUserDefaults.standardUserDefaults() - prefs.setPersistentDomain_forName_(self.data, self.__bundleID) diff --git a/atomacos/__init__.py b/atomacos/__init__.py index 6d21aa3..6344324 100644 --- a/atomacos/__init__.py +++ b/atomacos/__init__.py @@ -4,8 +4,6 @@ from atomacos import a11y, errors from atomacos.AXClasses import NativeUIElement -from atomacos.Clipboard import Clipboard -from atomacos.Prefs import Prefs Error = errors.AXError ErrorAPIDisabled = errors.AXErrorAPIDisabled diff --git a/tests/test_prefs.py b/tests/test_prefs.py deleted file mode 100644 index 6e15c16..0000000 --- a/tests/test_prefs.py +++ /dev/null @@ -1,7 +0,0 @@ -from atomacos import Prefs -from future.utils import string_types - - -def test_prefs(): - fp = Prefs("com.apple.finder") - assert isinstance(fp["GoToField"], string_types)