def __init__(self, **kwargs): super(Detail, self).__init__(**kwargs) self.popup = Popup(title='Warning', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading()
def __init__(self, **kwargs): super(QrGuide, self).__init__(**kwargs) self.popup = Popup(title='Error', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading()
def __init__(self, **kwargs): super(BookingCode, self).__init__(**kwargs) self.popup = Popup(title='Error', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading() keyboard = Keyboard() self.keyboard = keyboard self.add_widget(keyboard)
class Detail(Screen): file_name = StringProperty() email = StringProperty() detail_back_screen = StringProperty() page_count = NumericProperty(0) page_max = NumericProperty(0) unit_price = NumericProperty(0.5) preview_area = ObjectProperty() def __init__(self, **kwargs): super(Detail, self).__init__(**kwargs) self.popup = Popup(title='Warning', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading() def on_enter(self): self._clear_preview() app = App.get_running_app() file_info = app.file_info self.counter.num = 1 if 'name' in file_info: self.file_name = file_info['name'] if 'email' in file_info: self.email = file_info['email'] self.detail_back_screen = app.detail_back_screen self._get_printer_state() self._init_file(file_info) def _get_printer_state(self): mac_address = App.get_running_app().mac_address try: res = App.get_running_app().rest_get( 'cms/printer/getbymac?address=' + mac_address) if res['errcode'] != 0: self._show_warning('Printer disconnected') Clock.schedule_once(self._go_home, 3) return data = res['data'] self.printer_id = data['id'] self.paper_max = min(data['paperSurplus'], data['inkSurplus']) self.unit_price = data['unitPrice'] except Exception as e: Logger.exception(e) self._go_home() def _go_home(self, *args, **kwargs): self.manager.current = 'home' def _clear_preview(self): if self.preview_area: self.preview_area.clear_widgets() if Path('tmp/firstPage.jpg').exists(): os.remove('tmp/firstPage.jpg') def preview(self): app = App.get_running_app() file_fmt = app.file_info['name'].split('.')[-1].lower() # not_preview = True # not_preview = not_preview and (file_fmt not in ['jpg', 'png', 'pdf', 'jpeg']) # if not_preview and 'type' in app.file_info: # not_preview = not_preview and ('image' not in app.file_info['type']) # if not_preview: # self._show_warning('Only .jpg, .png and .pdf files can be previewd') # return path = 'tmp/download.data' # if the file is not images, transfer the first page to jpg file and save to tmp folder to preview if not file_fmt in ['jpg', 'png', 'jpeg']: with tempfile.TemporaryDirectory() as path1: images_from_path = convert_from_path(path, dpi=50, output_folder=path1, last_page=1, fmt='jpg') first_page = images_from_path[0] first_page.save('tmp/firstPage.jpg') image = PreviewImage(source='tmp/firstPage.jpg') image.reload() self.preview_area.add_widget(image) else: image = PreviewImage(source=path) image.reload() self.preview_area.add_widget(image) def on_leave(self): self.preview_area.clear_widgets() def _show_warning(self, msg): Logger.info(msg) self.loading.dismiss() self.popup.content.text = msg self.popup.open() def _init_file(self, file_info): file_fmt = file_info['name'].split('.')[-1].lower() if file_fmt in ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx']: self.loading.open() Clock.schedule_once(partial(self._convert_file, file_info), .5) else: self._set_page_count(file_info) def _go_back(self, later): self.manager.current = self.detail_back_screen def _convert_file(self, file_info, later): cwd = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir, 'tmp')) subprocess.Popen('soffice --headless --convert-to pdf download.data', shell=True, cwd=cwd).wait() if not Path('tmp/download.pdf').exists(): self._show_warning( 'Unable to process the file,\nplease contact our customer support.' ) Clock.schedule_once(self._go_back, 3) return move('tmp/download.pdf', 'tmp/download.data') self.loading.dismiss() self._set_page_count(file_info) def _set_page_count(self, file_info): file_type = file_info['name'].split('.')[-1].lower() file_path = 'tmp/download.data' if file_type in ['jpg', 'png', 'jpeg']: self.page_count = 1 else: self.page_count = self.pdf2page_count(file_path) Logger.info('Page: ' + str(self.page_count)) def pdf2page_count(self, path): try: pdfFileObj = open(path, 'rb') pdfReader = PyPDF3.PdfFileReader(pdfFileObj, strict=False) except Exception as e: self._show_warning( 'Unable to process the file,\nplease contact our customer support.' ) Clock.schedule_once(self._go_back, 3) return 1 return pdfReader.numPages def confirm(self): total_page_count = self.page_count * self.counter.num if total_page_count > self.paper_max: self._out_of_limit() return self.loading.open() Clock.schedule_once(self._request, .5) def _request(self, *args, **kwargs): total_page_count = self.page_count * self.counter.num total_price = self.unit_price * total_page_count order_id = self._create_order(total_page_count, total_price) Logger.info('order_id: ' + str(order_id)) app = App.get_running_app() app.total_price = total_price Logger.info('total price is ' + str(total_price)) if order_id: app.order_id = order_id app.num_copies = self.counter.num self.loading.dismiss() self.manager.current = 'payselector' def _create_order(self, total_page_count, total_price): data = { "id": self.printer_id, "paper": total_page_count, "money": total_price } file_info = App.get_running_app().file_info keys = ['bookNumber', 'userId'] for key in keys: if key in file_info: data[key] = file_info[key] try: res = App.get_running_app().rest_post('order/submit', data) errcode = res['errcode'] if res['errcode'] == 31010: self._out_of_limit() return False elif res['errcode'] != 0: self._show_warning(str(errcode)) return False return res['data']['orderId'] except Exception as e: Logger.exception(e) self._show_warning('Submit Error') return False def _out_of_limit(self): self.loading.dismiss() self.manager.current = 'error'
class BookingCode(Screen): code = StringProperty('') clicked = BooleanProperty(False) def __init__(self, **kwargs): super(BookingCode, self).__init__(**kwargs) self.popup = Popup(title='Error', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading() keyboard = Keyboard() self.keyboard = keyboard self.add_widget(keyboard) def on_pre_enter(self, *args): self.code = '' self.clicked = False App.get_running_app().file_info = None App.get_running_app().detail_back_screen = 'bookingcode' def submit(self): if not self.code: self._show_error('Invalid booking number') return if self.clicked: return self.clicked = True self.loading.open() Clock.schedule_once(self._request, .5) def _request(self, *args, **kwargs): file_info = self._get_file_info() if not file_info: return App.get_running_app().file_info = file_info file_url = file_info['fileUrl'] if App.get_running_app().fetchFile(file_url): self.manager.current = 'detail' self.loading.dismiss() else: self._show_error('Network error, please try again later.') def _get_file_info(self): try: res = App.get_running_app().rest_get('file/booknumber?bookNumber=' + self.code) if res['errcode'] != 0: self._show_error('Invalid booking number') else: return res['data'] except Exception as e: Logger.exception(e) self._show_error('Network error, please try again later.') def _show_error(self, msg): Logger.info(msg) self.loading.dismiss() self.popup.content.text = msg self.popup.open() self.clicked = False def _on_type(self, text): self.code += text def _on_delete(self): self.code = self.code[0:-1] def _on_ok(self): self.submit()
class QrGuide(Screen): code = StringProperty() isRequesting = BooleanProperty(False) def __init__(self, **kwargs): super(QrGuide, self).__init__(**kwargs) self.popup = Popup(title='Error', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading() def on_pre_enter(self): self.code = '' self.isRequesting = False App.get_running_app().file_info = None App.get_running_app().detail_back_screen = 'qrguide' self._keyboard_init() def on_leave(self): self._keyboard_closed() def _keyboard_init(self): self._keyboard = Window.request_keyboard(self._callback, self, 'number') self._keyboard.bind(on_key_down=self._on_keyboard_down) def _callback(self): Logger.info('keyboard want to close') def _keyboard_closed(self): Logger.info('My keyboard have been closed!') self._keyboard.unbind(on_key_down=self._on_keyboard_down) self._keyboard = None def _on_keyboard_down(self, keyboard, keycode, text, modifiers): if self.isRequesting: Logger.info('requesting, please wait') return False print(keycode, text, modifiers) if keycode[0] >= 48 and keycode[0] <= 57: self.code += text # if keycode[0] == 13: self.submit() # Return True to accept the key. Otherwise, it will be used by # the system. return True def submit(self): self.isRequesting = True if not self.code: self._show_error( 'Invalid QR Code\nYou can try again by using booking number instead.' ) return self.loading.open() Clock.schedule_once(self._request, .5) def _request(self, *args, **kwargs): file_info = self._get_file_info() if not file_info: return Logger.info(file_info) App.get_running_app().file_info = file_info file_url = file_info['fileUrl'] if App.get_running_app().fetchFile(file_url): self.manager.current = 'detail' self.loading.dismiss() else: self._show_error('Network error, please try again later.') def _get_file_info(self): try: res = App.get_running_app().rest_get( 'file/booknumber?bookNumber=' + self.code) if res['errcode'] != 0: self._show_error( 'Invalid QR Code\nYou can try again by using booking number instead.' ) else: return res['data'] except Exception as e: Logger.info("Network timeout, retry in 3s") self._show_error('Network error, please try again later.') def _show_error(self, msg): Logger.info(msg) self.loading.dismiss() self.popup.content.text = msg self.popup.open() self.isRequesting = False self.code = ''
import sys from pygame.locals import * from config.settings import * from src.bullet import Bullet from src.enemy import SmallEnemy, MidEnemy, BigEnemy from src.plane import Hero from src.loading import Loading bg_size = 480, 852 screen = pygame.display.set_mode(bg_size) pygame.display.set_caption("飞机大战") hero = Hero(bg_size) loading = Loading(bg_size) def main(): pygame.mixer.music.play(-1) running = True is_pause = False switch_image = False delay = 60 score = 0 font = pygame.font.SysFont("arial", 16) enemies = pygame.sprite.Group() add_enemies(SmallEnemy(bg_size), enemies, 6) add_enemies(MidEnemy(bg_size), enemies, 3) add_enemies(BigEnemy(bg_size), enemies, 1)
class Pay(Screen): qrcode_url = StringProperty('') count_down_num = NumericProperty(60) go_home_schedule = ObjectProperty() count_down_schedule = ObjectProperty() order_state_schedule = ObjectProperty() pay_channel_name = '' pay_channel_logo = '' qrcode = False def __init__(self, **kwargs): super(Pay, self).__init__(**kwargs) self.popup = Popup(title='Warning', content=Label(text=''), size_hint=(None, None), size=(400, 400)) self.loading = Loading() self.homemodal = HomeModal() def on_enter(self): self.qrcode_url = 'assets/loading.gif' self.count_down_num = 60 self.go_home_schedule = Clock.schedule_once(self._show_gohome_modal, 180) Window.bind(on_touch_down=self._listen_screen_touch) pay_channel = App.get_running_app().pay_channel self.pay_channel_name = PAY_CHANNEL_NAMES[pay_channel] self.pay_channel_logo = PAY_CHANNEL_LOGOS[pay_channel] self.pay_channel_label.text = PAY_CHANNEL_NAMES[pay_channel] self.pay_channel_image.source = PAY_CHANNEL_LOGOS[pay_channel] Clock.schedule_once(self._delay_init, .5) def _delay_init(self, time): if self._init_qrcode(): self.count_down_schedule = Clock.schedule_once(self._count_down, 1) self.order_state_schedule = Clock.schedule_interval( self._listen_order_state, 5) def on_pre_leave(self): Window.unbind(on_touch_down=self._listen_screen_touch) if self.go_home_schedule: self.go_home_schedule.cancel() if self.count_down_schedule: self.count_down_schedule.cancel() if self.order_state_schedule: self.order_state_schedule.cancel() def _print(self): # self.popup.dismiss() self.loading.open() app = App.get_running_app() path = 'tmp/download.data' process = subprocess.Popen('lp -n ' + str(app.num_copies) + ' ' + path, shell=True, stdout=subprocess.PIPE) out, err = process.communicate() if err: Logger.info('print error, ' + str(err)) self._show_warning( 'Printing Error Occurred,Please Contact Service Center') else: job_id = self._get_job_id(out) Logger.info('job id is ' + job_id) Clock.schedule_once(partial(self._listen_printer_job, job_id)) def _listen_printer_job(self, time, job_id): if job_id: queue = subprocess.Popen('lpstat -o', shell=True, stdout=subprocess.PIPE).communicate()[0] if str(job_id) in str(queue): Clock.schedule_once(partial(self._listen_printer_job, job_id), 1) return self.loading.dismiss() self.manager.current = 'complete' def _get_job_id(self, out): reg = re.compile('.*is\s(.*)\s\(.*') try: job_id = reg.match(str(out)).group(1) return job_id except Exception as e: Logger.exception('get print job id failed, ' + str(e)) return None def _listen_order_state(self, time): app = App.get_running_app() try: api = 'order/pay/status?id=' + app.order_id if 'userId' in app.file_info: api += ('&userId=' + app.file_info['userId']) res = App.get_running_app().rest_get(api) errcode = res['errcode'] if res['errcode'] != 0: self._show_warning(str(errcode)) return False status = res['data']['status'] if status == 1: self._pay_success() elif status == 2: self._pay_error() except Exception as e: Logger.exception(e) def _pay_success(self): self.count_down_schedule.cancel() self.order_state_schedule.cancel() self._print() def _pay_error(self): self.count_down_schedule.cancel() self.order_state_schedule.cancel() self._show_warning('Payment canceled.') Clock.schedule_once(self._go_home, 2) def _go_home(self, time): self.manager.current = 'home' def _init_qrcode(self): Clock.schedule_once(self._get_qrcode, 1) return True def _count_down(self, time): if self.count_down_num == 0 and self._init_qrcode(): self.count_down_num = 60 self.count_down_schedule = Clock.schedule_once(self._count_down, 1) return self.count_down_num -= 1 self.count_down_schedule = Clock.schedule_once(self._count_down, 1) def _get_qrcode(self, time): app = App.get_running_app() data = {"id": app.order_id, "type": app.pay_channel} if 'userId' in app.file_info: data['userId'] = app.file_info['userId'] try: res = App.get_running_app().rest_post('order/pay/qrcode', data) errcode = res['errcode'] if res['errcode'] != 0: self._show_warning(str(errcode)) return False self.qrcode_url = res['data']['url'] self.qrcode_url = self.qrcode_url.replace('https', 'http', 1) return True except Exception as e: Logger.exception(e) Logger.info('Network timeout, retrying in 3s') Clock.schedule_once(self._get_qrcode, 3) return False def _listen_screen_touch(self, instance, event): if self.go_home_schedule: self.go_home_schedule.cancel() self.go_home_schedule = Clock.schedule_once(self._show_gohome_modal, 180) def _show_gohome_modal(self, *args, **kwargs): self.homemodal.open() def _show_warning(self, msg): Logger.info(msg) self.loading.dismiss() self.popup.auto_dismiss = True self.popup.title = 'Warning' self.popup.content.text = msg self.popup.open() def _show_success(self, msg): Logger.info(msg) self.loading.dismiss() self.popup.auto_dismiss = False self.popup.title = 'Success' self.popup.content.text = msg self.popup.open()