I have the following PyObjC script:
from Foundation import NSObject
import QTKit
error = None
capture_session = QTKit.QTCaptureSession.alloc().init()
print 'capture_session', capture_session
device = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
print 'device', device, type(device)
success = device.open_(error)
print 'device open success', success, error
if not success:
raise Exception(error)
capture_device_input = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(device)
print 'capture_device_input', capture_device_input, capture_device_input.device()
success = capture_session.addInput_error_(capture_device_input, error)
print 'session add input success', success, error
if not success:
raise Exception(error)
capture_decompressed_video_output = QTKit.QTCaptureDecompressedVideoOutput.alloc().init()
print 'capture_decompressed_video_output', capture_decompressed_video_output
class Delegate(NSObject):
def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput, videoFrame, sampleBuffer, connection):
print videoFrame, sampleBuffer, connection
delegate = Delegate.alloc().init()
print 'delegate', delegate
capture_decompressed_video_output.setDelegate_(delegate)
print 'output delegate:', capture_decompressed_video_output.delegate()
success = capture_session.addOutput_error_(capture_decompressed_video_output, error)
print 'capture session add output success', success, error
if not success:
raise Exception(error)
print 'about to run session', capture_session, 'with inputs', capture_session.inputs(), 'and outputs', capture_session.outputs()
capture_session.startRunning()
print 'capture session is running?', capture_session.isRunning()
import time
time.sleep(10)
The program reports no errors, but iSight's green light is never activated and the delegate's frame capture callback is never called. Here's the output I get:
$ python prueba.py
capture_session <QTCaptureSession: 0x1006c16f0>
device Built-in iSight <objective-c class QTCaptureDALDevice at 0x7fff70366aa8>
device open success (True, None) None
capture_device_input <QTCaptureDeviceInput: 0x1002ae010> Built-in iSight
session add input success (True, None) None
capture_decompressed_video_output <QTCaptureDecompressedVideoOutput: 0x104239f10>
delegate <Delegate: 0x10423af50>
output delegate: <Delegate: 0x10423af50>
capture session add output success (True, None) None
about to run session <QTCaptureSession: 0x1006c16f0> with inputs (
"<QTCaptureDeviceInput: 0x1002ae010>"
) and outputs (
"<QTCaptureDecompressedVideoOutput: 0x104239f10>"
)
capture session is running? True
PS: Please don't answer I should try PySight, I have but it won't work because Xcode can't compile CocoaSequenceGrabber in 64bit.
Your problem here is that you don't have an event loop. If you want to do this as a standalone script, you'll have to figure out how to create one. The PyObjC XCode templates automatically set that up for you with:
from PyObjCTools import AppHelper
AppHelper.runEventLoop()
Trying to insert that at the top of your script, however, shows that something inside AppHelper (probably NSApplicationMain) expects a plist file to extract the main class from. You can get that by creating a setup.py file and using py2app, something like this example from a PyObjc talk:
from distutils.core import setup
import py2app
plist = dict(
NSPrincipalClass='SillyBalls',
)
setup(
plugin=['SillyBalls.py'],
data_files=['English.lproj'],
options=dict(py2app=dict(
extension='.saver',
plist=plist,
)),
)
You should give a try to motmot's camiface library from Andrew Straw. It also works with firewire cameras, but it works also with the isight, which is what you are looking for.
From the tutorial:
import motmot.cam_iface.cam_iface_ctypes as cam_iface
import numpy as np
mode_num = 0
device_num = 0
num_buffers = 32
cam = cam_iface.Camera(device_num,num_buffers,mode_num)
cam.start_camera()
frame = np.asarray(cam.grab_next_frame_blocking())
print 'grabbed frame with shape %s'%(frame.shape,)
Related
We have a python program that is designed to open an Excel template document, run refreshall() twice (data and pivots), then saveas() a new filename in an output directory.
When I log in to the server and run the program, everything works as required - both refreshall() steps update and the new file is saved with the updated data. This is the case not matter how the Visible flag is set (True or False).
When we set it as a step in an MS SQL job, the output file is created, but the updates are not done. I've been all over Stack Overflow and the internet in general, and have found no answers to solve this.
Using the combrowse.py script to look at the COM objects, we can see that excel is in the "Running Objects" list when run logged in, but it is NOT in the list when it is running via the MS SQL job (reduced to a single step in the job).
I looked at stdout to see if that might be an issue. My theory is that there is no console in which to operate when run via the job, and so Excel does not start (and the refreshall() cannot run). I found that the stdout when run locally is a UTF-8 encoding and when run via the job is a cp1252 encoding. I couldn't get anything more useful than that.
Code snippet:
from AE import logging, encrypt
import os, sys, shutil, datetime, gc
import time
import win32com.client
script_name = 'PerSta'
log_id = logging.Start_script(script_name)
try:
logging.Log_script_message(id = log_id, message = 'Opening excel')
ExcelConn = win32com.client.DispatchEx("Excel.Application")
logging.Log_script_message(id = log_id, message = 'ExcelConn is:')
logging.Log_script_message(id = log_id, message = repr(ExcelConn))
logging.Log_script_message(id = log_id, message = 'Opening {}'.format(script_name))
PS = ExcelConn.Workbooks.Open(datadict.get('path') + datadict.get('filename'))
ExcelConn.Interactive = False
ExcelConn.Visible = False
ExcelConn.DisplayAlerts = False
ExcelConn.EnableEvents = False
logging.Log_script_message(id = log_id, message = 'Refreshing excel first time')
PS.RefreshAll()
ExcelConn.CalculateUntilAsyncQueriesDone()
time.sleep(pause_for_refresh)
logging.Log_script_message(id = log_id, message = 'Refreshing excel second time')
PS.RefreshAll() #Refresh again to update any pivots
ExcelConn.CalculateUntilAsyncQueriesDone()
time.sleep(pause_for_refresh)
logging.Log_script_message(id = log_id, message = 'Saving workbook')
PS.SaveAs(Report)
time.sleep(pause_for_refresh)
logging.Log_script_message(id = log_id, message = 'Closing workbook')
PS.Close(SaveChanges = True)
time.sleep(pause_for_refresh)
PS = None
logging.Log_script_message(id = log_id, message = 'Closing filehandle')
ExcelConn.Quit()
ExcelConn = None
except:
logging.Log_script_message(id = log_id, message = 'Refreshed failed, closing filehandle')
PS.Close(SaveChanges = False)
PS = None
ExcelConn.Quit()
ExcelConn = None
I believe the issue lies in not having a screen for Excel to do its work, but I have not been able to prove that. We get NO ERRORS at all, either way it is run. I would expect that there would be an error in the job scenario, since it doesn't do what it says that it is, but that is not the case.
Any help would be much appreciated!!
--MIKE--
Edit: the Interactive, Visible, DisplayAlerts, and EnableEvents was put in as testing to see if we could use those to fix the issue. They did not work, no matter how they were set, but left them in in case they came up in discussion.
I'm trying to modify the simple python script provided with my Pipsta Printer so that instead of printing a single line of text, it prints out a list of things.
I know there are probably better ways to do this, but using the script below, could somebody please tell me what changes I need to make around the "txt = " part of the script, so that I can print out a list of items and not just one item?
Thanks.
# BasicPrint.py
# Copyright (c) 2014 Able Systems Limited. All rights reserved.
'''This simple code example is provided as-is, and is for demonstration
purposes only. Able Systems takes no responsibility for any system
implementations based on this code.
This very simple python script establishes USB communication with the Pipsta
printer sends a simple text string to the printer.
Copyright (c) 2014 Able Systems Limited. All rights reserved.
'''
import argparse
import platform
import sys
import time
import usb.core
import usb.util
FEED_PAST_CUTTER = b'\n' * 5
USB_BUSY = 66
# NOTE: The following section establishes communication to the Pipsta printer
# via USB. YOU DO NOT NEED TO UNDERSTAND THIS SECTION TO PROGRESS WITH THE
# TUTORIALS! ALTERING THIS SECTION IN ANY WAY CAN CAUSE A FAILURE TO COMMUNICATE
# WITH THE PIPSTA. If you are interested in learning about what is happening
# herein, please look at the following references:
#
# PyUSB: http://sourceforge.net/apps/trac/pyusb/
# ...which is a wrapper for...
# LibUSB: http://www.libusb.org/
#
# For full help on PyUSB, at the IDLE prompt, type:
# >>> import usb
# >>> help(usb)
# 'Deeper' help can be trawled by (e.g.):
# >>> help(usb.core)
#
# or at the Linux prompt, type:
# pydoc usb
# pydoc usb.core
PIPSTA_USB_VENDOR_ID = 0x0483
PIPSTA_USB_PRODUCT_ID = 0xA053
def parse_arguments():
'''Parse the arguments passed to the script looking for a font file name
and a text string to print. If either are mssing defaults are used.
'''
txt = 'Hello World from Pipsta!'
parser = argparse.ArgumentParser()
parser.add_argument('text', help='the text to print',
nargs='*', default=txt.split())
args = parser.parse_args()
return ' '.join(args.text)
def main():
"""The main loop of the application. Wrapping the code in a function
prevents it being executed when various tools import the code.
"""
if platform.system() != 'Linux':
sys.exit('This script has only been written for Linux')
# Find the Pipsta's specific Vendor ID and Product ID
dev = usb.core.find(idVendor=PIPSTA_USB_VENDOR_ID,
idProduct=PIPSTA_USB_PRODUCT_ID)
if dev is None: # if no such device is connected...
raise IOError('Printer not found') # ...report error
try:
# Linux requires USB devices to be reset before configuring, may not be
# required on other operating systems.
dev.reset()
# Initialisation. Passing no arguments sets the configuration to the
# currently active configuration.
dev.set_configuration()
except usb.core.USBError as ex:
raise IOError('Failed to configure the printer', ex)
# The following steps get an 'Endpoint instance'. It uses
# PyUSB's versatile find_descriptor functionality to claim
# the interface and get a handle to the endpoint
# An introduction to this (forming the basis of the code below)
# can be found at:
cfg = dev.get_active_configuration() # Get a handle to the active interface
interface_number = cfg[(0, 0)].bInterfaceNumber
# added to silence Linux complaint about unclaimed interface, it should be
# release automatically
usb.util.claim_interface(dev, interface_number)
alternate_setting = usb.control.get_interface(dev, interface_number)
interface = usb.util.find_descriptor(
cfg, bInterfaceNumber=interface_number,
bAlternateSetting=alternate_setting)
usb_endpoint = usb.util.find_descriptor(
interface,
custom_match=lambda e:
usb.util.endpoint_direction(e.bEndpointAddress) ==
usb.util.ENDPOINT_OUT
)
if usb_endpoint is None: # check we have a real endpoint handle
raise IOError("Could not find an endpoint to print to")
# Now that the USB endpoint is open, we can start to send data to the
# printer.
# The following opens the text_file, by using the 'with' statemnent there is
# no need to close the text_file manually. This method ensures that the
# close is called in all situation (including unhandled exceptions).
txt = parse_arguments()
usb_endpoint.write(b'\x1b!\x00')
# Print a char at a time and check the printers buffer isn't full
for x in txt:
usb_endpoint.write(x) # write all the data to the USB OUT endpoint
res = dev.ctrl_transfer(0xC0, 0x0E, 0x020E, 0, 2)
while res[0] == USB_BUSY:
time.sleep(0.01)
res = dev.ctrl_transfer(0xC0, 0x0E, 0x020E, 0, 2)
usb_endpoint.write(FEED_PAST_CUTTER)
usb.util.dispose_resources(dev)
# Ensure that BasicPrint is ran in a stand-alone fashion (as intended) and not
# imported as a module. Prevents accidental execution of code.
if __name__ == '__main__':
main()
Pipsta uses linux line-feed ('\n', 0x0A, 10 in decimal) to mark a new line. For a quick test change BasicPrint.py as follows:
#txt = parse_arguments()
txt = 'Recipt:\n========\n1. food 1 - 100$\n2. drink 1 - 200$\n\n\n\n\n'
usb_endpoint.write(b'\x1b!\x00')
for x in txt:
usb_endpoint.write(x)
res = dev.ctrl_transfer(0xC0, 0x0E, 0x020E, 0, 2)
while res[0] == USB_BUSY:
time.sleep(0.01)
res = dev.ctrl_transfer(0xC0, 0x0E, 0x020E, 0, 2)
I commented out parameter parsing and injected a test string with multi line content.
I use custom tags for my AWS instances. I am trying to list all the instances (running and stopped) in CSV file. Not being a programmer, I searched and copied/pasted codes and came with a script which runs well. But I have noticed that if one tag is missing then the script throws an error and stops right there. If the tag is created but is empty, then the script prints a blank space, but if the tag is not created at all then the script just stops. For example if the tag Owner is missing then it throws an error KeyError: 'Owner' and stops there. My script is pasted below. Can somebody let me know what changes I need to make so that if the tag does not exist, the script prints out N/A instead of stopping.
#!/usr/bin/env python
import boto.ec2
from boto.ec2 import EC2Connection
csv_file = open('/home/sbasnet/Scripts/Instances/instances_east.csv','w+')
def process_instance_list(connection):
map(build_instance_list,connection.get_all_instances())
def build_instance_list(reservation):
map(write_instances,reservation.instances)
def write_instances(instance):
if (instance.platform == 'windows'):
platform = 'Windows'
else:
platform = 'Linux'
csv_file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(instance.id,instance.private_ip_address,instance.tags['Classification'],instance.tags['FQDN'],instance.tags['Owner'],instance.tags['RunType'],instance.instance_type,instance.subnet_id,instance.key_name,platform,instance.placement))
csv_file.flush()
if __name__=="__main__":
connection = EC2Connection(aws_access_key_id='ACCESS',aws_secret_access_key='SECRET')
process_instance_list(connection)
csv_file.close()
TIA
sbasnet
Maybe something like this will do?
Warning: untested
#!/usr/bin/env python
import boto.ec2
from boto.ec2 import EC2Connection
csv_file = open('/home/sbasnet/Scripts/Instances/instances_east.csv','w+')
def process_instance_list(connection):
map(build_instance_list,connection.get_all_instances())
def build_instance_list(reservation):
map(write_instances,reservation.instances)
def get_tag(tags, tag_name):
if tag_name in tags.keys():
return tags[tag_name]
else:
return "N/A"
def write_instances(instance):
if instance.platform == 'windows':
platform = 'Windows'
else:
platform = 'Linux'
csv_file.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(instance.id,
instance.private_ip_address,
get_tag(instance.tags, "Classification"),
get_tag(instance.tags, "FQDN"),
get_tag(instance.tags, "Owner"),
get_tag(instance.tags, "RunType"),
instance.instance_type,
instance.subnet_id,
instance.key_name,
platform,
instance.placement))
csv_file.flush()
if __name__=="__main__":
connection = EC2Connection(aws_access_key_id='ACCESS',aws_secret_access_key='SECRET')
process_instance_list(connection)
csv_file.close()
I am getting both the currently active window title and exe filepath with the code below
hwnd = win32gui.GetForegroundWindow()
_, pid = win32process.GetWindowThreadProcessId(hwnd)
if hwnd != 0 or pid != 0:
try:
hndl = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, 0, pid)
self.newExe = win32process.GetModuleFileNameEx(hndl, 0)
self.newWindowTitle = win32gui.GetWindowText(hwnd)
except:
self.newExe = ''
self.newWindowTitle = ''
the issue is that although it often is, the window title is not always the application name (the name the users understand as the main part of an application) and this is what I need. for example from calc.exe get Calculator withiout relying on the window title.
the purpose is to create a script that will log in an xml comparative use of any software on a computer
Is this possible?
Most Windows applications store information such as this inside their resource tables. There are API calls that can be used to extract this.
The following extracts the file description from a given application:
import win32api
def getFileDescription(windows_exe):
try:
language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
stringFileInfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
description = win32api.GetFileVersionInfo(windows_exe, stringFileInfo)
except:
description = "unknown"
return description
print(getFileDescription(r"C:\Program Files\Internet Explorer\iexplore.exe"))
The output is:
Internet Explorer
You could therefore pass the result of your call to win32process.GetModuleFileNameEx() to this function.
I've searched in Internet for a while, but I haven't found out anything useful...
I want to do something as simple as redirect the page to the listing page (folder) after save/create an AT content type.
I already know I have to use validate_integrity.cpy and write my redirect's logic there, but the file isn't run...
So far this is an example of my validate_integrity.cpy:
## Script (Python) "validate_integrity"
##title=Validate Integrity
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=
##
from Products.Archetypes import PloneMessageFactory as _
from Products.Archetypes.utils import addStatusMessage
request = context.REQUEST
errors = {}
errors = context.validate(REQUEST=request, errors=errors, data=1, metadata=0)
import pdb; pdb.set_trace()
if errors:
message = _(u'Please correct the indicated errors.')
addStatusMessage(request, message, type='error')
return state.set(status='failure', errors=errors)
else:
message = _(u'Changes saved.')
stat = 'created'
# Redirection after saving edition forms
redirects = {'Multifile': context.aq_parent.absolute_url_path() + '/multifile'}
import pdb; pdb.set_trace()
if context.portal_type in redirects:
redirect = 'redirect_to:string:${portal_url}' + redirects[context.portal_type]
state.setNextAction(redirect)
else:
stat = 'success'
addStatusMessage(request, message)
return state.set(status=stat)
RESOLUTION
I just needed to write the following upgrade step:
from Acquisition import aq_inner, aq_parent
from Products.CMFCore.utils import getToolByName
def upgrade(tool):
portal = aq_parent(aq_inner(tool))
setup = portal.portal_setup
setup.runImportStepFromProfile('profile-my.addon:default', 'skins')
Useful info about upgrade steps here
Your *.metadata file might be missing or have an action that routes to a different location than you expect: http://docs.plone.org/old-reference-manuals/forms/using_cmfformcontroller.html
The default metadata for content_edit lives in Products/Archetypes/skins/archetypes/content_edit.cpy.metadata:
...
[actions]
action.success = traverse_to:string:validate_integrity
action.success_add_reference = redirect_to:python:object.REQUEST['last_referer']
action.failure = traverse_to_action:string:edit
action.next_schemata = traverse_to_action:string:edit
Is your action button value "success"?