How to use transfer_ownership method in hypothesis

Best Python code snippet using hypothesis

clipboardmenu.py

Source:clipboardmenu.py Github

copy

Full Screen

1# Copyright (C) 2007, One Laptop Per Child2#3# This program is free software: you can redistribute it and/or modify4# it under the terms of the GNU General Public License as published by5# the Free Software Foundation, either version 3 of the License, or6# (at your option) any later version.7#8# This program is distributed in the hope that it will be useful,9# but WITHOUT ANY WARRANTY; without even the implied warranty of10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11# GNU General Public License for more details.12#13# You should have received a copy of the GNU General Public License14# along with this program. If not, see <http://www.gnu.org/licenses/>.15from gettext import gettext as _16import tempfile17import urlparse18import os19import logging20from gi.repository import Gio21from gi.repository import Gtk22from sugar3.graphics.palette import Palette23from sugar3.graphics.menuitem import MenuItem24from sugar3.graphics.icon import Icon25from sugar3.graphics import style26from sugar3.datastore import datastore27from sugar3 import mime28from sugar3 import env29from sugar3 import profile30from sugar3.activity.i18n import pgettext31from jarabe.frame import clipboard32from jarabe.journal import misc33from jarabe.model import bundleregistry34class ClipboardMenu(Palette):35 def __init__(self, cb_object):36 Palette.__init__(self, text_maxlen=100)37 self._cb_object = cb_object38 self.set_group_id('frame')39 cb_service = clipboard.get_instance()40 cb_service.connect('object-state-changed',41 self._object_state_changed_cb)42 self._remove_item = MenuItem(pgettext('Clipboard', 'Remove'),43 'list-remove')44 self._remove_item.connect('activate', self._remove_item_activate_cb)45 self.menu.append(self._remove_item)46 self._remove_item.show()47 self._open_item = MenuItem(_('Open'), 'zoom-activity')48 self._open_item.connect('activate', self._open_item_activate_cb)49 self.menu.append(self._open_item)50 self._open_item.show()51 self._journal_item = MenuItem(_('Keep'))52 color = profile.get_color()53 icon = Icon(icon_name='document-save',54 pixel_size=style.SMALL_ICON_SIZE,55 xo_color=color)56 self._journal_item.set_image(icon)57 self._journal_item.connect('activate', self._journal_item_activate_cb)58 self.menu.append(self._journal_item)59 self._journal_item.show()60 self._update()61 def _update_open_submenu(self):62 activities = self._get_activities()63 logging.debug('_update_open_submenu: %r', activities)64 child = self._open_item.get_child()65 if activities is None or len(activities) <= 1:66 child.set_text(_('Open'))67 if self._open_item.get_submenu() is not None:68 self._open_item.set_submenu(None)69 return70 child.set_text(_('Open with'))71 submenu = self._open_item.get_submenu()72 if submenu is None:73 submenu = Gtk.Menu()74 self._open_item.set_submenu(submenu)75 submenu.show()76 else:77 for item in submenu.get_children():78 submenu.remove(item)79 for service_name in activities:80 registry = bundleregistry.get_registry()81 activity_info = registry.get_bundle(service_name)82 if not activity_info:83 logging.warning('Activity %s is unknown.', service_name)84 item = Gtk.MenuItem(activity_info.get_name())85 item.connect('activate', self._open_submenu_item_activate_cb,86 service_name)87 submenu.append(item)88 item.show()89 def _update_items_visibility(self):90 activities = self._get_activities()91 installable = self._cb_object.is_bundle()92 percent = self._cb_object.get_percent()93 if percent == 100 and (activities or installable):94 self._remove_item.props.sensitive = True95 self._open_item.props.sensitive = True96 self._journal_item.props.sensitive = True97 elif percent == 100 and (not activities and not installable):98 self._remove_item.props.sensitive = True99 self._open_item.props.sensitive = False100 self._journal_item.props.sensitive = True101 else:102 self._remove_item.props.sensitive = True103 self._open_item.props.sensitive = False104 self._journal_item.props.sensitive = False105 def _get_activities(self):106 mime_type = self._cb_object.get_mime_type()107 if not mime_type:108 return ''109 registry = bundleregistry.get_registry()110 activities = registry.get_activities_for_type(mime_type)111 if activities:112 return [info.get_bundle_id() for info in activities]113 else:114 return ''115 def _object_state_changed_cb(self, cb_service, cb_object):116 if cb_object != self._cb_object:117 return118 self._update()119 def _update(self):120 self.props.primary_text = self._cb_object.get_name()121 preview = self._cb_object.get_preview()122 if preview:123 self.props.secondary_text = preview124 self._update_items_visibility()125 self._update_open_submenu()126 def _open_item_activate_cb(self, menu_item):127 logging.debug('_open_item_activate_cb')128 percent = self._cb_object.get_percent()129 if percent < 100 or menu_item.get_submenu() is not None:130 return131 jobject = self._copy_to_journal()132 misc.resume(jobject.metadata, self._get_activities()[0])133 jobject.destroy()134 def _open_submenu_item_activate_cb(self, menu_item, service_name):135 logging.debug('_open_submenu_item_activate_cb')136 percent = self._cb_object.get_percent()137 if percent < 100:138 return139 jobject = self._copy_to_journal()140 misc.resume(jobject.metadata, service_name)141 jobject.destroy()142 def _remove_item_activate_cb(self, menu_item):143 cb_service = clipboard.get_instance()144 cb_service.delete_object(self._cb_object.get_id())145 def _journal_item_activate_cb(self, menu_item):146 logging.debug('_journal_item_activate_cb')147 jobject = self._copy_to_journal()148 jobject.destroy()149 def _write_to_temp_file(self, data):150 tmp_dir = os.path.join(env.get_profile_path(), 'data')151 f, file_path = tempfile.mkstemp(dir=tmp_dir)152 try:153 os.write(f, data)154 finally:155 os.close(f)156 return file_path157 def _copy_to_journal(self):158 formats = self._cb_object.get_formats().keys()159 most_significant_mime_type = mime.choose_most_significant(formats)160 format_ = self._cb_object.get_formats()[most_significant_mime_type]161 transfer_ownership = False162 if most_significant_mime_type == 'text/uri-list':163 uri = format_.get_data()164 if uri.startswith('file://'):165 parsed_url = urlparse.urlparse(uri)166 file_path = parsed_url.path # pylint: disable=E1101167 transfer_ownership = False168 mime_type = mime.get_for_file(file_path)169 else:170 file_path = self._write_to_temp_file(format_.get_data())171 transfer_ownership = True172 mime_type = 'text/uri-list'173 else:174 if format_.is_on_disk():175 parsed_url = urlparse.urlparse(format_.get_data())176 file_path = parsed_url.path # pylint: disable=E1101177 transfer_ownership = False178 mime_type = mime.get_for_file(file_path)179 else:180 file_path = self._write_to_temp_file(format_.get_data())181 transfer_ownership = True182 sniffed_mime_type = mime.get_for_file(file_path)183 if sniffed_mime_type == 'application/octet-stream':184 mime_type = most_significant_mime_type185 else:186 mime_type = sniffed_mime_type187 jobject = datastore.create()188 jobject.metadata['title'] = self._cb_object.get_name()189 jobject.metadata['keep'] = '0'190 jobject.metadata['buddies'] = ''191 jobject.metadata['preview'] = ''192 settings = Gio.Settings('org.sugarlabs.user')193 color = settings.get_string('color')194 jobject.metadata['icon-color'] = color195 jobject.metadata['mime_type'] = mime_type196 jobject.file_path = file_path197 datastore.write(jobject, transfer_ownership=transfer_ownership)...

Full Screen

Full Screen

pyobjecttype.py

Source:pyobjecttype.py Github

copy

Full Screen

1# docstrings not neede here (the type handler doubleerfaces are fully2# documented in base.py) pylint: disable-msg=C01113from base import ReturnValue, Parameter, \4 ReverseWrapperBase, ForwardWrapperBase5class PyObjectParam(Parameter):6 DIRECTIONS = [Parameter.DIRECTION_IN]7 CTYPES = ['PyObject*']8 def __init__(self, ctype, name, transfer_ownership, is_const=False):9 """10 :param ctype: C type, normally 'PyObject*'11 :param name: parameter name12 :param transfer_ownership: this parameter transfer the ownership of13 the pointed-to object to the called14 function15 """16 super(PyObjectParam, self).__init__(17 ctype, name, direction=Parameter.DIRECTION_IN, is_const=is_const)18 self.transfer_ownership = transfer_ownership19 def convert_c_to_python(self, wrapper):20 assert isinstance(wrapper, ReverseWrapperBase)21 if self.transfer_ownership:22 wrapper.build_params.add_parameter('N', [self.value])23 else:24 wrapper.build_params.add_parameter('O', [self.value])25 def convert_python_to_c(self, wrapper):26 assert isinstance(wrapper, ForwardWrapperBase)27 name = wrapper.declarations.declare_variable(self.ctype_no_const, self.name)28 wrapper.parse_params.add_parameter('O', ['&'+name], self.name)29 wrapper.call_params.append(name)30 if self.transfer_ownership:31 wrapper.before_call.write_code("Py_INCREF((PyObject*) %s);" % name)32class PyObjectReturnValue(ReturnValue):33 CTYPES = ['PyObject*']34 def __init__(self, ctype, caller_owns_return, is_const=False):35 """36 :param ctype: C type, normally 'MyClass*'37 :param caller_owns_return: if true, ownership of the object pointer38 is transferred to the caller39 """40 super(PyObjectReturnValue, self).__init__(ctype, is_const)41 self.caller_owns_return = caller_owns_return42 def get_c_error_return(self):43 return "return NULL;"44 45 def convert_python_to_c(self, wrapper):46 wrapper.parse_params.add_parameter("O", ["&"+self.value], prepend=True)47 if self.caller_owns_return:48 wrapper.after_call.write_code("Py_INCREF((PyObject*) %s);" % self.value)49 def convert_c_to_python(self, wrapper):50 wrapper.build_params.add_parameter(51 (self.caller_owns_return and "N" or "O"),...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful