Best Python code snippet using uiautomator
ipy_synchronize_with.py
Source:ipy_synchronize_with.py
1from IPython.core import ipapi2ip = ipapi.get()3import win32api4import win32ui5import win32console6import dde7import os8import scitedirector9# test to write.10def set_hook(synchronize_with_editor):11 """Set the synchronize with editor hook with a callable object.12 13 The callable object will be called with the following arguments when14 IPython wants to synchronize with you favorite editor:15 16 - ip: a running IPython instance.17 18 - filename: the path of the file the editor is supposed to display.19 20 - lineno : the line number of the line the editor is supposed to21 highlight.22 23 - columnno : the column number of the character the editor is supposed24 to highlight.25 """26 ip.set_hook("synchronize_with_editor", synchronize_with_editor)27def find_filename(filename):28 """Return the filename to synchronize with based on """29 filename = os.path.splitext(filename)30 if filename[1] == ".pyc":31 filename = (filename[0], ".py")32 filename = "".join(filename)33 if not os.path.isabs(filename):34 filename = os.path.join(os.getcwdu(), filename)35 if os.path.isfile(filename):36 return filename37 return ""38def run_command(path, command, arguments, asynchronous = True):39 """Run a shell command and return the exit code of the command"""40 # This is a thin wrapper around os.system that:41 # - Let you run command asynchronously.42 # - Accept spaces in command path.43 # - Dont throw exception if the command don't exist.44 line = ''45 if asynchronous:46 line += 'start '47 48 try:49 line += win32api.GetShortPathName(os.path.join(path, command) + ".exe") + " "50 except:51 print 'could not find: "%s"' % (os.path.join(path, command) + ".exe")52 return -153 54 line += arguments55 r = os.system(line)56 return r57def sleep(milliseconds):58 """Wait some milliseconds."""59 # This is used to make sure the editor did its job before we reset the focus on the console.60 win32api.Sleep(milliseconds)61def restore_console_focus():62 """Restore the focus to the IPython console."""63 h = win32console.GetConsoleWindow()64 console_window = win32ui.CreateWindowFromHandle(h)65 console_window.SetForegroundWindow()66 67# This is the most simple example of hook:68class GVimHook:69 def __init__(self, path, wakeup_duration):70 self.path = path71 self.wakeup_duration = wakeup_duration72 def __call__(self, ip, filename, lineno, columnno):73 filename = find_filename(filename)74 if not filename:75 return76 run_command(self.path, 'gvim', '--remote-silent +%d "%s"' % (lineno, filename))77 sleep(self.wakeup_duration)78 restore_console_focus()79def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100):80 synchronize_with_editor = GVimHook(path, wakeup_duration)81 set_hook(synchronize_with_editor)82class EmacsHook:83 def __init__(self, path, wakeup_duration, start_duration):84 self.path = path85 self.wakeup_duration = wakeup_duration86 self.start_duration = start_duration87 def __call__(self, ip, filename, lineno, columnno):88 filename = find_filename(filename)89 if not filename:90 return91 r = run_command(self.path, "emacsclient", '-n +%d:%d "%s" 2>nul' % (lineno, columnno, filename), False)92 if r != 0:93 run_command(self.path, 'runemacs', '--quick -f server-start +%d:%d "%s"' % (lineno, columnno, filename))94 sleep(self.start_duration)95 else:96 sleep(self.wakeup_duration)97 restore_console_focus()98def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000):99 synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration)100 set_hook(synchronize_with_editor)101class SciteHook:102 def __init__(self, path, wakeup_duration, start_duration):103 self.path = path104 self.wakeup_duration = wakeup_duration105 self.start_duration = start_duration106 def __call__(self, ip, filename, lineno, columnno):107 filename = find_filename(filename)108 if not filename:109 return110 scites = scitedirector.findWindows()111 if not scites:112 run_command(self.path, "scite", '"-open:%s" -goto:%d' % (filename.replace("\\", "/"), lineno))113 sleep(self.start_duration)114 restore_console_focus()115 else:116 scite = scites[0]117 scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/"))118 scitedirector.sendCommand(scite, "goto:%d" % lineno)119def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500):120 synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration)121 set_hook(synchronize_with_editor)122class NodePadPlusPlusHook:123 def __init__(self, path, wakeup_duration):124 self.path = path125 self.wakeup_duration = wakeup_duration126 def __call__(self, ip, filename, lineno, columnno):127 filename = find_filename(filename)128 if not filename:129 return130 run_command(self.path, "notepad++", '"%s" -n%d' % (filename, lineno))131 sleep(self.wakeup_duration)132 restore_console_focus()133def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100):134 synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration)135 set_hook(synchronize_with_editor)136class PsPadHook:137 def __init__(self, path, wakeup_duration):138 self.path = path139 self.wakeup_duration = wakeup_duration140 def __call__(self, ip, filename, lineno, columnno):141 filename = find_filename(filename)142 if not filename:143 return144 run_command(self.path, "pspad", '"%s" -%d' % (filename, lineno))145 sleep(self.wakeup_duration)146 restore_console_focus()147def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100):148 synchronize_with_editor = PsPadHook(path, wakeup_duration)149 set_hook(synchronize_with_editor)150# This is an example of DDE hook:151class UltraEditHook:152 def __init__(self, path, wakeup_duration, start_duration):153 self.path = path154 self.wakeup_duration = wakeup_duration155 self.start_duration = start_duration156 def __call__(self, ip, filename, lineno, columnno):157 filename = find_filename(filename)158 if not filename:159 return160 server = dde.CreateServer()161 server.Create("myddeserver")162 conversation = dde.CreateConversation(server)163 try:164 conversation.ConnectTo("uedit32", "System")165 conversation.Exec(r'[open("%s/%d"])' % (filename, lineno))166 167 sleep(self.wakeup_duration)168 except:169 run_command(self.path, 'uedit32', '"%s/%d"' % (filename, lineno))170 171 sleep(self.start_duration)172 server.Shutdown()173 restore_console_focus()174def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000):175 synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration)176 set_hook(synchronize_with_editor)...
wakeup.js
Source:wakeup.js
1var util2 = require('util2');2var Emitter = require('emitter');3var Settings = require('settings');4var simply = require('ui/simply');5var Wakeup = function() {6 this.init();7};8Wakeup.prototype.cleanupGracePeriod = 60 * 5;9util2.copy(Emitter.prototype, Wakeup.prototype);10Wakeup.prototype.init = function() {11 this._setRequests = [];12 this._launchCallbacks = [];13 this._loadData();14 this._cleanup();15};16Wakeup.prototype._loadData = function() {17 this.state = Settings._loadData(null, 'wakeup', true) || {};18 this.state.wakeups = this.state.wakeups || {};19};20Wakeup.prototype._saveData = function() {21 Settings._saveData(null, 'wakeup', this.state);22};23Wakeup.prototype._cleanup = function() {24 var id;25 var ids = [];26 for (id in this.state.wakeups) {27 ids.push(id);28 }29 var cleanupTime = Date.now() / 1000 - Wakeup.cleanupGracePeriod;30 var deleted = false;31 for (var i = 0, ii = ids.length; i < ii; ++i) {32 id = ids[i];33 var wakeup = this.state.wakeups[id];34 if (wakeup.params.time < cleanupTime) {35 deleted = true;36 delete this.state.wakeups[id];37 }38 }39 if (deleted) {40 this._saveData();41 }42};43Wakeup.prototype.get = function(id) {44 var wakeup = this.state.wakeups[id];45 if (wakeup) {46 return {47 id: wakeup.id,48 cookie: wakeup.cookie,49 data: wakeup.data,50 time: wakeup.params.time,51 notifyIfMissed: !!wakeup.params.notifyIfMissed,52 };53 }54};55Wakeup.prototype.each = function(callback) {56 var i = 0;57 for (var id in this.state.wakeups) {58 if (callback(this.get(id), i++) === false) {59 break;60 }61 }62};63Wakeup.prototype.schedule = function(opt, callback) {64 if (typeof opt !== 'object' || opt instanceof Date) {65 opt = { time: opt };66 }67 var cookie = opt.cookie || 0;68 this._setRequests.push({69 params: opt,70 data: opt.data,71 callback: callback,72 });73 this.launch(function() {74 simply.impl.wakeupSet(opt.time, cookie, opt.notifyIfMissed);75 });76};77Wakeup.prototype.cancel = function(id) {78 if (id === 'all') {79 this.state.wakeups = {};80 } else {81 delete this.state.wakeups[id];82 }83 simply.impl.wakeupCancel(id);84};85Wakeup.prototype.launch = function(callback) {86 if (this._launchEvent) {87 callback(this._launchEvent);88 } else {89 this._launchCallbacks.push(callback);90 }91};92Wakeup.prototype._makeBaseEvent = function(id, cookie) {93 var wakeup = this.state.wakeups[id];94 var e = {95 id: id,96 cookie: cookie,97 };98 if (wakeup) {99 e.data = wakeup.data;100 }101 return e;102};103Wakeup.prototype._makeWakeupEvent = function(id, cookie) {104 var e;105 if (id !== undefined) {106 e = this._makeBaseEvent(id, cookie);107 e.wakeup = true;108 } else {109 e = { wakeup: false };110 }111 return e;112};113Wakeup.prototype._setWakeup = function(id, wakeup) {114 this.state.wakeups[id] = wakeup;115 this._saveData();116};117Wakeup.prototype._removeWakeup = function(id) {118 if (id in this.state.wakeups) {119 delete this.state.wakeups[id];120 this._saveData();121 }122};123Wakeup.prototype.emitSetResult = function(id, cookie) {124 var req = this._setRequests.shift();125 if (!req) {126 return;127 }128 var e;129 if (typeof id === 'number') {130 this._setWakeup(id, {131 id: id,132 cookie: cookie,133 data: req.data,134 params: req.params,135 });136 e = this._makeBaseEvent(id, cookie);137 e.failed = false;138 } else {139 e = {140 error: id,141 failed: true,142 cookie: cookie,143 data: req.data,144 };145 }146 return req.callback(e);147};148Wakeup.prototype.emitWakeup = function(id, cookie) {149 var e = this._makeWakeupEvent(id, cookie);150 if (!this._launchEvent) {151 e.launch = true;152 if (this._emitWakeupLaunch(e) === false) {153 return false;154 }155 } else {156 e.launch = false;157 }158 if (e.wakeup) {159 this._removeWakeup(id);160 if (this.emit('wakeup', e) === false) {161 return false;162 }163 }164};165Wakeup.prototype._emitWakeupLaunch = function(e) {166 this._launchEvent = e;167 var callbacks = this._launchCallbacks;168 this._launchCallbacks = [];169 for (var i = 0, ii = callbacks.length; i < ii; ++i) {170 if (callbacks[i](e) === false) {171 return false;172 }173 }174};...
StbHardware.py
Source:StbHardware.py
1from time import time, localtime, gmtime2from os import path3from fcntl import ioctl4from struct import pack, unpack5from Components.config import config6from boxbranding import getBoxType, getBrandOEM7def getFPVersion():8 ret = None9 try:10 if getBrandOEM() == "blackbox":11 file = open("/proc/stb/info/micomver", "r")12 ret = file.readline().strip()13 file.close()14 elif getBoxType() in ('dm7080','dm820','dm520','dm525','dm900','dm920'):15 ret = open("/proc/stb/fp/version", "r").read()16 else:17 ret = long(open("/proc/stb/fp/version", "r").read())18 except IOError:19 try:20 fp = open("/dev/dbox/fp0")21 ret = ioctl(fp.fileno(),0)22 except IOError:23 print "getFPVersion failed!"24 return ret25def setFPWakeuptime(wutime):26 try:27 f = open("/proc/stb/fp/wakeup_time", "w")28 f.write(str(wutime))29 f.close()30 except IOError:31 try:32 fp = open("/dev/dbox/fp0")33 ioctl(fp.fileno(), 6, pack('L', wutime)) # set wake up34 fp.close()35 except IOError:36 print "setFPWakeupTime failed!"37def setRTCoffset():38 forsleep = (localtime(time()).tm_hour-gmtime(time()).tm_hour)*360039 print "[RTC] set RTC offset to %s sec." % (forsleep)40 try:41 open("/proc/stb/fp/rtc_offset", "w").write(str(forsleep))42 except IOError:43 print "setRTCoffset failed!"44def setRTCtime(wutime):45 if path.exists("/proc/stb/fp/rtc_offset"):46 setRTCoffset()47 try:48 f = open("/proc/stb/fp/rtc", "w")49 f.write(str(wutime))50 f.close()51 except IOError:52 try:53 fp = open("/dev/dbox/fp0")54 ioctl(fp.fileno(), 0x101, pack('L', wutime)) # set wake up55 fp.close()56 except IOError:57 print "setRTCtime failed!"58def getFPWakeuptime():59 ret = 060 try:61 f = open("/proc/stb/fp/wakeup_time", "r")62 ret = long(f.read())63 f.close()64 except IOError:65 try:66 fp = open("/dev/dbox/fp0")67 ret = unpack('L', ioctl(fp.fileno(), 5, ' '))[0] # get wakeuptime68 fp.close()69 except IOError:70 print "getFPWakeupTime failed!"71 return ret72wasTimerWakeup = None73def getFPWasTimerWakeup(check = False):74 global wasTimerWakeup75 isError = False76 if wasTimerWakeup is not None:77 if check:78 return wasTimerWakeup, isError79 return wasTimerWakeup80 wasTimerWakeup = False81 try:82 f = open("/proc/stb/fp/was_timer_wakeup", "r")83 file = f.read()84 f.close()85 wasTimerWakeup = int(file) and True or False86 f = open("/tmp/was_timer_wakeup.txt", "w")87 file = f.write(str(wasTimerWakeup))88 f.close()89 except:90 try:91 fp = open("/dev/dbox/fp0")92 wasTimerWakeup = unpack('B', ioctl(fp.fileno(), 9, ' '))[0] and True or False93 fp.close()94 except IOError:95 print "wasTimerWakeup failed!"96 isError = True97 if wasTimerWakeup:98 # clear hardware status99 clearFPWasTimerWakeup()100 if check:101 return wasTimerWakeup, isError102 return wasTimerWakeup103def clearFPWasTimerWakeup():104 try:105 f = open("/proc/stb/fp/was_timer_wakeup", "w")106 f.write('0')107 f.close()108 except:109 try:110 fp = open("/dev/dbox/fp0")111 ioctl(fp.fileno(), 10)112 fp.close()113 except IOError:...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!