class StdConfigItemNumber(StdConfigItemBase): def __init__(self, fmt, udm, min, max, step, *args): self._fmt = fmt self._udm = udm self._min = min self._max = max self._step = step self._val = None self._dia = None StdConfigItemBase.__init__(self, *args) def label_end_get(self, url, user_data): if self._udm: return ini.get(self._sec, self._opt) + ' ' + self._udm else: return ini.get(self._sec, self._opt) def item_selected(self, url, user_data): self._val = ini.get_float(self._sec, self._opt) self._dia = EmcDialog(style='minimal', title=self._lbl, text='') self._dia.button_add(_('Ok'), self._btn_ok_cb) self._dia.button_add(None, self._btn_plus_cb, icon='icon/plus') self._dia.button_add(None, self._btn_minus_cb, icon='icon/minus') self._dia.button_add(_('Cancel'), self._btn_canc_cb) self._dia_text_update() def _dia_text_update(self): val = (self._fmt % self._val) + ' ' + self._udm self._dia.text_set('<br><br><br><center><bigger>%s</bigger></center>' % val) def _btn_plus_cb(self, btn): self._val += self._step self._val = min(self._val, self._max) self._dia_text_update() def _btn_minus_cb(self, btn): self._val -= self._step self._val = max(self._val, self._min) self._dia_text_update() def _btn_canc_cb(self, btn): self._dia.delete() def _btn_ok_cb(self, btn): val = self._fmt % self._val ini.set(self._sec, self._opt, val) self._dia.delete() StdConfigItemBase.__done__(self)
class Test_Url(GenericItemClass): url1 = 'http://ipv4.download.thinkbroadband.com/5MB.zip' url2 = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/' \ 'resources/pdf/dummy.pdf' def __init__(self): super().__init__() self.dia = None self.dload = None def item_selected(self, url, user_data): self.dia = EmcDialog(style='progress', title=user_data, text='Press start to test a 5MB download') self.dia.button_add('Close', lambda b: self.dia.delete()) self.dia.button_add('delete()', self.delete_btn_cb) self.dia.button_add('To file (5M)', self.start_btn_cb, self.url1) self.dia.button_add('To mem (13K)', self.start_btn_cb, self.url2) def start_btn_cb(self, btn, url): self.dia.text_set('Download started...') dest = '::tmp::' if url == self.url1 else '::mem::' self.dload = EmcUrl(url, dest=dest, done_cb=self.done_cb, prog_cb=self.prog_cb, decode=None, parent=self.dia) def delete_btn_cb(self, btn): if self.dload: self.dload.delete() def done_cb(self, url, success, dest): print("DONE", success) if success and url.dest == '::mem::': size = len(dest) self.dia.text_set( 'Download successfully completed to Memory<br>' 'First bytes: {}<br>Download size: {} bytes'.format( dest[:10], size)) elif success and os.path.exists(dest): size = utils.hum_size(os.path.getsize(dest)) self.dia.text_set('Download successfully completed to<br>{}<br>' 'File size: {} '.format(dest, size)) else: self.dia.text_set("Error !!") self.dload = None def prog_cb(self, url, total, received): print("PROGRESS", url, total, received) self.dia.progress_set(received / total if total else 0)
class Test_Exe(GenericItemClass): path = os.path.dirname(__file__) infinite_path = os.path.join(path, 'infinite.py') def __init__(self): super().__init__() self.dia = None self.exe = None def item_selected(self, url, user_data): self.dia = EmcDialog(title='EmcExe test', text='Press a button to run a command') self.dia.button_add('Close', lambda b: self.dia.delete()) self.dia.button_add('delete()', lambda b: self.exe.delete()) self.dia.button_add('"wrong"', self.run_btn_cb, ('wrong', )) self.dia.button_add('"infinite.py"', self.run_btn_cb, (sys.executable, self.infinite_path)) self.dia.button_add('"ls -l"', self.run_btn_cb, ('ls', '-l')) self.dia.button_add('"ls"', self.run_btn_cb, ('ls', )) def run_btn_cb(self, btn, pars): cmd = pars[0] params = pars[1:] self.exe = EmcExe(cmd, params, grab_output=True, done_cb=self.exe_done_cb, parent=self.dia, asd1='asd_1', asd2='asd_2') self.dia.text_set('Running: "{} {}"<br><br>'.format( cmd, ' '.join(params))) def exe_done_cb(self, exit_code, output, asd1, asd2): assert asd1 == 'asd_1' assert asd2 == 'asd_2' print("OUT", exit_code, output) self.dia.text_append('Process completed<br>' 'exit_code: {}<br>' 'output: {}'.format(exit_code, repr(output)))