How to use add_component method in Kiwi

Best Python code snippet using Kiwi_python

screen_player.py

Source:screen_player.py Github

copy

Full Screen

...43 """44 def __init__(self, screen_rect):45 Screen.__init__(self, screen_rect)46 # Screen navigation buttons47 self.add_component(ScreenNavigation('screen_nav', self.screen, 'btn_playlist'))48 # Player specific buttons49 self.add_component(ButtonIcon('btn_play', self.screen, ICO_PLAY, SCREEN_WIDTH - 51, 45))50 self.add_component(ButtonIcon('btn_stop', self.screen, ICO_STOP, SCREEN_WIDTH - 51, 85))51 self.add_component(ButtonIcon('btn_prev', self.screen, ICO_PREVIOUS, SCREEN_WIDTH - 51, 125))52 self.add_component(ButtonIcon('btn_next', self.screen, ICO_NEXT, SCREEN_WIDTH - 51, 165))53 self.add_component(ButtonIcon('btn_volume', self.screen, ICO_VOLUME, SCREEN_WIDTH - 51, 205))54 # Player specific labels55 self.add_component(LabelText('lbl_track_title', self.screen, 55, 5, SCREEN_WIDTH - 130, 18))56 self.add_component(LabelText('lbl_track_artist', self.screen, 55, 23, SCREEN_WIDTH - 130, 18))57 self.add_component(LabelText('lbl_time', self.screen, SCREEN_WIDTH - 67, 5, 67, 18))58 self.add_component(LabelText('lbl_volume', self.screen, SCREEN_WIDTH - 70, 23, 70, 18))59 # Splits labels from playlist60 self.add_component(Rectangle('rct_split', self.screen, 55, 43, 208, 1))61 # Playlist62 self.add_component(Playlist(self.screen))63 self.components['list_playing'].active_item_index = mpd.playlist_current_playing_index_get()64 def show(self):65 """ Displays the screen. """66 self.components['screen_nav'].radio_mode_set(mpd.radio_mode_get())67 now_playing = mpd.now_playing68 self.components['lbl_time'].text_set(now_playing.time_current + '/' + now_playing.time_total)69 self.components['lbl_volume'].text_set('Vol: ' + str(mpd.volume) + '%')70 if mpd.player_control_get() == 'play':71 self.components['btn_play'].set_image_file(ICO_PAUSE)72 else:73 self.components['btn_play'].set_image_file(ICO_PLAY)74 self.components['btn_play'].draw()75 self.components['lbl_track_title'].text_set(now_playing.title)76 self.components['lbl_track_artist'].text_set(now_playing.artist)77 super(ScreenPlaylist, self).show() # Draw screen78 self.components['list_playing'].show_playlist()79 self.components['list_playing'].show_item_active() # Makes sure currently playing playlist item is on screen80 def update(self):81 now_playing = mpd.now_playing82 self.components['screen_nav'].radio_mode_set(mpd.radio_mode_get())83 while True:84 try:85 event = mpd.events.popleft()86 if event == 'volume':87 self.components['lbl_volume'].text_set('Vol: ' + str(mpd.volume) + '%')88 elif event == 'playing_index':89 self.components['list_playing'].show_playlist()90 elif event == 'time_elapsed' or event == 'playing_time_total':91 self.components['lbl_time'].text_set(now_playing.time_current + '/' + now_playing.time_total)92 elif event == 'playing_file':93 self.components['lbl_track_title'].text_set(now_playing.title)94 self.components['lbl_track_artist'].text_set(now_playing.artist)95 elif event == 'state':96 state = mpd.player_control_get()97 if self.components['btn_play'].image_file != ICO_PAUSE and state == 'play':98 self.components['btn_play'].draw(ICO_PAUSE)99 elif self.components['btn_play'].image_file == ICO_PAUSE and state != 'play':100 self.components['btn_play'].draw(ICO_PLAY)101 except IndexError:102 break103 def on_click(self, x, y):104 """105 :param x: The horizontal click position.106 :param y: The vertical click position.107 :return: Possibly returns a screen index number to switch to.108 """109 tag_name = super(ScreenPlaylist, self).on_click(x, y)110 if tag_name == 'btn_player':111 return 0112 elif tag_name == 'btn_playlist':113 return 1114 elif tag_name == 'btn_library':115 return 2116 elif tag_name == 'btn_directory':117 return 3118 elif tag_name == 'btn_radio':119 return 4120 elif tag_name == 'btn_settings':121 setting_screen = ScreenSettings(self.screen)122 setting_screen.show()123 self.show()124 elif tag_name == 'btn_play':125 if mpd.player_control_get() == 'play':126 mpd.player_control_set('pause')127 self.components['btn_play'].set_image_file(ICO_PLAY)128 else:129 mpd.player_control_set('play')130 self.components['btn_play'].set_image_file(ICO_PAUSE)131 self.components['btn_play'].draw()132 elif tag_name == 'btn_stop':133 self.components['btn_play'].set_image_file(ICO_PLAY)134 mpd.player_control_set('stop')135 elif tag_name == 'btn_prev':136 mpd.player_control_set('previous')137 elif tag_name == 'btn_next':138 mpd.player_control_set('next')139 elif tag_name == 'btn_volume':140 screen_volume = ScreenVolume(self.screen)141 screen_volume.show()142 self.show()143 elif tag_name == 'list_playing':144 selected_index = self.components['list_playing'].item_selected_index145 if selected_index >= 0:146 mpd.play_playlist_item(selected_index + 1)147 self.components['list_playing'].active_item_index = selected_index148 self.components['list_playing'].draw()149class ScreenPlaying(Screen):150 """ Screen cover art151 :param screen_rect: The display's rectangle where the screen is drawn on.152 """153 def __init__(self, screen_rect):154 Screen.__init__(self, screen_rect)155 # Screen navigation buttons156 self.add_component(ScreenNavigation('screen_nav', self.screen, 'btn_player'))157 # Player specific buttons158 self.add_component(ButtonIcon('btn_play', self.screen, ICO_PLAY, SCREEN_WIDTH - 51, 5))159 self.add_component(ButtonIcon('btn_stop', self.screen, ICO_STOP, SCREEN_WIDTH - 51, 45))160 self.add_component(ButtonIcon('btn_prev', self.screen, ICO_PREVIOUS, SCREEN_WIDTH - 51, 85))161 self.add_component(ButtonIcon('btn_next', self.screen, ICO_NEXT, SCREEN_WIDTH - 51, 125))162 self.add_component(ButtonIcon('btn_volume', self.screen, ICO_VOLUME, SCREEN_WIDTH - 51, 165))163 # Cover art164 self.add_component(Picture('pic_cover_art', self.screen, 79, 40, 162, 162, mpd.get_cover_art()))165 # Player specific labels166 self.add_component(LabelText('lbl_track_artist', self.screen, 54, 3, 215, 18))167 self.components['lbl_track_artist'].set_alignment(HOR_MID, VERT_MID)168 self.add_component(LabelText('lbl_track_album', self.screen, 54, 19, 215, 18))169 self.components['lbl_track_album'].set_alignment(HOR_MID, VERT_MID)170 self.add_component(LabelText('lbl_track_title', self.screen, 55, SCREEN_HEIGHT - 27, 212, 18))171 self.components['lbl_track_title'].set_alignment(HOR_MID, VERT_MID)172 self.add_component(LabelText('lbl_time_current', self.screen, SCREEN_WIDTH - 51, 205, 48, 18))173 self.components['lbl_time_current'].set_alignment(HOR_MID, VERT_MID)174 self.add_component(LabelText('lbl_time_total', self.screen, SCREEN_WIDTH - 51, 221, 48, 18))175 self.components['lbl_time_total'].set_alignment(HOR_MID, VERT_MID)176 self.add_component(Slider2('slide_time', self.screen, 55, SCREEN_HEIGHT - 35, 212, 3))177 def show(self):178 """ Displays the screen. """179 self.components['screen_nav'].radio_mode_set(mpd.radio_mode_get())180 self.components['lbl_time_current'].text_set(mpd.now_playing.time_current)181 self.components['lbl_time_total'].text_set(mpd.now_playing.time_total)182 if mpd.player_control_get() == 'play':183 self.components['btn_play'].set_image_file(ICO_PAUSE)184 else:185 self.components['btn_play'].set_image_file(ICO_PLAY)186 self.components['btn_play'].draw()187 self.components['lbl_track_title'].text_set(mpd.now_playing.title)188 self.components['lbl_track_artist'].text_set(mpd.now_playing.artist)189 self.components['lbl_track_album'].text_set(mpd.now_playing.album)190 if mpd.radio_mode_get():191 self.components['lbl_track_artist'].visible = False192 self.components['lbl_track_album'].position_set(54, 3, 215, 39)193 self.components['pic_cover_art'].picture_set(COVER_ART_RADIO)194 else:195 self.components['lbl_track_artist'].visible = True196 self.components['lbl_track_artist'].text_set(mpd.now_playing.artist)197 self.components['lbl_track_album'].position_set(54, 19, 215, 18)198 self.components['pic_cover_art'].picture_set(mpd.now_playing.cover_art_get())199 super(ScreenPlaying, self).show() # Draw screen200 def update(self):201 while True:202 try:203 event = mpd.events.popleft()204 self.components['screen_nav'].radio_mode_set(mpd.radio_mode_get())205 playing = mpd.now_playing206 if event == 'time_elapsed':207 self.components['lbl_time_current'].text_set(playing.time_current)208 self.components['slide_time'].draw(playing.time_percentage)209 elif event == 'playing_file':210 self.components['lbl_track_title'].text_set(playing.title)211 if mpd.radio_mode_get():212 self.components['lbl_track_artist'].visible = False213 self.components['lbl_track_album'].position_set(54, 3, 215, 39)214 self.components['pic_cover_art'].picture_set(COVER_ART_RADIO)215 else:216 self.components['lbl_track_artist'].visible = True217 self.components['lbl_track_artist'].text_set(playing.artist)218 self.components['lbl_track_album'].position_set(54, 19, 215, 18)219 self.components['pic_cover_art'].picture_set(mpd.now_playing.cover_art_get())220 self.components['lbl_track_album'].text_set(playing.album)221 self.components['lbl_time_total'].text_set(playing.time_total)222 elif event == 'state':223 if self.components['btn_play'].image_file != ICO_PAUSE and mpd.player_control_get() == 'play':224 self.components['btn_play'].draw(ICO_PAUSE)225 elif self.components['btn_play'].image_file == ICO_PAUSE and mpd.player_control_get() != 'play':226 self.components['btn_play'].draw(ICO_PLAY)227 except IndexError:228 break229 def on_click(self, x, y):230 tag_name = super(ScreenPlaying, self).on_click(x, y)231 if tag_name == 'btn_player':232 return 0233 elif tag_name == 'btn_playlist':234 return 1235 elif tag_name == 'btn_library':236 return 2237 elif tag_name == 'btn_directory':238 return 3239 elif tag_name == 'btn_radio':240 return 4241 elif tag_name == 'btn_settings':242 setting_screen = ScreenSettings(self.screen)243 setting_screen.show()244 self.show()245 elif tag_name == 'btn_play':246 if mpd.player_control_get() == 'play':247 mpd.player_control_set('pause')248 self.components['btn_play'].set_image_file(ICO_PLAY)249 else:250 mpd.player_control_set('play')251 self.components['btn_play'].set_image_file(ICO_PAUSE)252 self.components['btn_play'].draw()253 elif tag_name == 'btn_stop':254 self.components['btn_play'].set_image_file(ICO_PLAY)255 mpd.player_control_set('stop')256 elif tag_name == 'btn_prev':257 mpd.player_control_set('previous')258 elif tag_name == 'btn_next':259 mpd.player_control_set('next')260 elif tag_name == 'btn_volume':261 screen_volume = ScreenVolume(self.screen)262 screen_volume.show()263 self.show()264class ScreenVolume(ScreenModal):265 """ Screen setting volume266 :param screen_rect: The display's rectangle where the screen is drawn on.267 """268 def __init__(self, screen_rect):269 ScreenModal.__init__(self, screen_rect, "Volume")270 self.window_x = 15271 self.window_y = 52272 self.window_width -= 2 * self.window_x273 self.window_height -= 2 * self.window_y274 self.outline_shown = True275 self.title_color = FIFTIES_GREEN276 self.outline_color = FIFTIES_GREEN277 self.add_component(ButtonIcon('btn_mute', screen_rect, ICO_VOLUME_MUTE, self.window_x + 5, self.window_y + 25))278 self.components['btn_mute'].x_pos = self.window_x + self.window_width / 2 - self.components[279 'btn_mute'].width / 2280 self.add_component(281 ButtonIcon('btn_volume_down', self.screen, ICO_VOLUME_DOWN, self.window_x + 5, self.window_y + 25))282 self.add_component(283 ButtonIcon('btn_volume_up', self.screen, ICO_VOLUME_UP, self.window_width - 40, self.window_y + 25))284 self.add_component(285 Slider('slide_volume', self.screen, self.window_x + 8, self.window_y + 63, self.window_width - 18, 30))286 self.components['slide_volume'].progress_percentage_set(mpd.volume)287 self.add_component(288 ButtonText('btn_back', self.screen, self.window_x + self.window_width / 2 - 23, self.window_y + 98, 46, 32,289 "Back"))290 self.components['btn_back'].button_color = FIFTIES_TEAL291 def on_click(self, x, y):292 tag_name = super(ScreenModal, self).on_click(x, y)293 if tag_name == 'btn_mute':294 mpd.volume_mute_switch()295 self.components['slide_volume'].progress_percentage_set(mpd.volume)296 elif tag_name == 'btn_volume_down':297 mpd.volume_set_relative(-10)298 self.components['slide_volume'].progress_percentage_set(mpd.volume)299 elif tag_name == 'btn_volume_up':300 mpd.volume_set_relative(10)301 self.components['slide_volume'].progress_percentage_set(mpd.volume)...

Full Screen

Full Screen

screen_settings.py

Source:screen_settings.py Github

copy

Full Screen

...25 ScreenModal.__init__(self, screen_rect, "Settings")26 button_left = self.window_x + 1027 button_width = self.window_width - 2 * button_left28 label = "Quit Pi-Jukebox"29 self.add_component(ButtonText('btn_quit', self.screen, button_left, 30, button_width, 32, label))30 label = "Playback options"31 self.add_component(ButtonText('btn_playback', self.screen, button_left, 72, button_width, 32, label))32 label = "MPD related settings"33 self.add_component(ButtonText('btn_mpd', self.screen, button_left, 114, button_width, 32, label))34 label = "System info"35 self.add_component(ButtonText('btn_system_info', self.screen, button_left, 156, button_width, 32, label))36 label = "Back"37 self.add_component(ButtonText('btn_return', self.screen, button_left, 198, button_width, 32, label))38 def on_click(self, x, y):39 tag_name = super(ScreenSettings, self).on_click(x, y)40 if tag_name == 'btn_playback':41 screen_playback_options = ScreenSettingsPlayback(self.screen)42 screen_playback_options.show()43 self.show()44 elif tag_name == 'btn_quit':45 screen_quit = ScreenSettingsQuit(self.screen)46 screen_quit.show()47 self.show()48 elif tag_name == 'btn_mpd':49 screen_mpd = ScreenSettingsMPD(self.screen)50 screen_mpd.show()51 self.show()52 elif tag_name == 'btn_system_info':53 screen_system_info = ScreenSystemInfo(self.screen)54 screen_system_info.show()55 self.show()56 elif tag_name == 'btn_return':57 self.close()58class ScreenSettingsQuit(ScreenModal):59 """ Screen for quitting pi-jukebox.60 :param screen_rect: The display's rectangle where the screen is drawn on.61 """62 def __init__(self, screen_rect):63 ScreenModal.__init__(self, screen_rect, "Quit")64 self.window_x = 7065 self.window_y = 2566 self.window_width -= 2 * self.window_x67 self.window_height -= 2 * self.window_y68 self.outline_shown = True69 self.add_component(ButtonText('btn_quit', screen_rect, self.window_x + 10, self.window_y + 30, 160, 32, "Quit"))70 self.add_component(71 ButtonText('btn_shutdown', screen_rect, self.window_x + 10, self.window_y + 70, 160, 32, "Shutdown Pi"))72 self.add_component(73 ButtonText('btn_reboot', screen_rect, self.window_x + 10, self.window_y + 110, 160, 32, "Reboot Pi"))74 self.add_component(75 ButtonText('btn_cancel', screen_rect, self.window_x + 10, self.window_y + 150, 160, 32, "Cancel"))76 def on_click(self, x, y):77 tag_name = super(ScreenModal, self).on_click(x, y)78 if tag_name == 'btn_quit':79 mpd.disconnect()80 print ("Bye!")81 sys.exit()82 elif tag_name == 'btn_shutdown':83 if RUN_ON_RASPBERRY_PI:84 pygame.display.quit()85 os.system("sudo shutdown -h now")86 else:87 sys.exit()88 elif tag_name == 'btn_reboot':89 if RUN_ON_RASPBERRY_PI:90 pygame.display.quit()91 os.system("sudo shutdown -r now")92 else:93 sys.exit()94 elif tag_name == 'btn_cancel':95 self.close()96class ScreenSettingsPlayback(ScreenModal):97 """ Screen for settings playback options98 :param screen_rect: The display's rectangle where the screen is drawn on.99 """100 def __init__(self, screen_rect):101 ScreenModal.__init__(self, screen_rect, "Playback settings")102 self.add_component(LabelText('lbl_shuffle', screen_rect, 10, 30, 40, 20, "Shuffle"))103 self.add_component(Switch('switch_shuffle', screen_rect, 60, 23))104 self.add_component(LabelText('lbl_repeat', screen_rect, 120, 30, 40, 20, "Repeat"))105 self.add_component(Switch('switch_repeat', screen_rect, 170, 23))106 self.add_component(LabelText('lbl_single', screen_rect, 230, 30, 40, 20, "Single"))107 self.add_component(Switch('switch_single', screen_rect, 280, 23))108 self.add_component(LabelText('lbl_consume', screen_rect, 10, 65, 110, 20, "Consume playlist"))109 self.add_component(Switch('switch_consume', screen_rect, 125, 58))110 self.add_component(111 ButtonText('btn_rescan', self.screen, 10, 108, self.window_width - 20, 32, "Re-scan library"))112 self.add_component(ButtonText('btn_update', self.screen, 10, 150, self.window_width - 20, 32, "Update library"))113 self.add_component(ButtonText('btn_return', screen_rect, 10, 192, self.window_width - 20, 32, "Back"))114 self.__initialize()115 def __initialize(self):116 """ Sets the screen controls according to current mpd configuration.117 """118 for key, value in self.components.items():119 if key == 'switch_shuffle':120 value.set_on(mpd.random)121 elif key == 'switch_repeat':122 value.set_on(mpd.repeat)123 elif key == 'switch_single':124 value.set_on(mpd.single)125 elif key == 'switch_consume':126 value.set_on(mpd.consume)127 def on_click(self, x, y):128 tag_name = super(ScreenModal, self).on_click(x, y)129 if tag_name == 'switch_shuffle':130 mpd.random_switch()131 elif tag_name == 'switch_repeat':132 mpd.repeat_switch()133 elif tag_name == 'switch_single':134 mpd.single_switch()135 elif tag_name == 'switch_consume':136 mpd.consume_switch()137 elif tag_name == 'btn_rescan':138 mpd.library_rescan()139 elif tag_name == 'btn_update':140 mpd.library_update()141 elif tag_name == 'btn_return':142 self.close()143class ScreenSettingsMPD(ScreenModal):144 """ Screen for settings playback options145 :param screen_rect: The display's rectangle where the screen is drawn on.146 """147 def __init__(self, screen_rect):148 ScreenModal.__init__(self, screen_rect, "MPD settings")149 button_left = self.window_x + 10150 button_width = self.window_width - 2 * button_left151 label = "Change host: " + config_file.setting_get('MPD Settings', 'Host')152 self.add_component(ButtonText('btn_host', self.screen, button_left, 30, button_width, 32, label))153 label = "Change port: " + str(config_file.setting_get('MPD Settings', 'port'))154 self.add_component(ButtonText('btn_port', self.screen, button_left, 72, button_width, 32, label))155 self.add_component(156 ButtonText('btn_music_dir', self.screen, button_left, 114, button_width, 32, "Change music directory"))157 label = "Back"158 self.add_component(ButtonText('btn_back', self.screen, button_left, 198, button_width, 32, label))159 def on_click(self, x, y):160 tag_name = super(ScreenModal, self).on_click(x, y)161 setting_label = ""162 setting_value = None163 if tag_name == 'btn_back':164 self.close()165 return166 elif tag_name == 'btn_host':167 setting_label = "Set mpd host"168 self.keyboard_setting(setting_label, 'MPD Settings', 'Host')169 mpd.disconnect()170 mpd.host = config_file.setting_get('MPD Settings', 'Host')171 mpd.connect()172 elif tag_name == 'btn_port':173 setting_label = "Set mpd server port"174 self.keyboard_setting(setting_label, 'MPD Settings', 'port')175 mpd.disconnect()176 mpd.host = int(config_file.setting_get('MPD Settings', 'port'))177 mpd.connect()178 elif tag_name == 'btn_music_dir':179 setting_label = "Set music directory"180 self.keyboard_setting(setting_label, 'MPD Settings', 'music directory')181 mpd.music_directory = config_file.setting_get('MPD Settings', 'music directory')182 self.update()183 self.show()184 def keyboard_setting(self, caption, section, key, value=""):185 setting_value = config_file.setting_get(section, key)186 keyboard = Keyboard(self.screen, caption)187 if setting_value is None:188 keyboard.text = value189 else:190 keyboard.text = setting_value191 keyboard.title_color = FIFTIES_ORANGE192 new_value = keyboard.show() # Get entered search text193 config_file.setting_set(section, key, new_value)194 def update(self):195 label = "Change host: " + config_file.setting_get('MPD Settings', 'Host')196 self.components['btn_host'].draw(label)197 label = "Change port: " + str(config_file.setting_get('MPD Settings', 'port'))198 self.components['btn_port'].draw(label)199class ScreenSystemInfo(ScreenModal):200 """ Screen for settings playback options201 :param screen_rect: The display's rectangle where the screen is drawn on.202 """203 def __init__(self, screen_rect):204 ScreenModal.__init__(self, screen_rect, "System info")205 button_left = self.window_x + 10206 button_width = self.window_width - 2 * button_left207 label = "Back"208 self.add_component(ButtonText('btn_back', self.screen, button_left, 198, button_width, 32, label))209 info = mpd.mpd_client.stats()210 self.add_component(LabelText('lbl_database', self.screen, button_left, 30, 100, 18, "Music database"))211 self.components['lbl_database'].font_color = FIFTIES_TEAL212 artist_count = "Artists: " + "{:,}".format(int(info['artists']))213 self.add_component(LabelText('lbl_artist_count', self.screen, button_left, 48, 100, 18, artist_count))214 album_count = "Albums: " + "{:,}".format(int(info['albums']))215 self.add_component(LabelText('lbl_album_count', self.screen, button_left + 100, 48, 100, 18, album_count))216 song_count = "Songs: " + "{:,}".format(int(info['songs']))217 self.add_component(LabelText('lbl_song_count', self.screen, button_left + 210, 48, 100, 18, song_count))218 play_time = "Total time: " + self.make_time_string(int(info['db_playtime']))219 self.add_component(LabelText('lbl_play_time', self.screen, button_left, 66, 300, 18, play_time))220 self.add_component(LabelText('lbl_system', self.screen, button_left, 90, 100, 18, "Server"))221 self.components['lbl_system'].font_color = FIFTIES_TEAL222 self.add_component(223 LabelText('lbl_host_name', self.screen, button_left, 108, 1500, 18, "Host name: " + socket.gethostname()))224 try:225 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)226 s.connect(('google.com', 0))227 ip_address = s.getsockname()[0]228 self.add_component(229 LabelText('lbl_ip_address', self.screen, button_left, 126, 1500, 18, "IP address: " + ip_address))230 except Exception:231 pass232 def on_click(self, x, y):233 tag_name = super(ScreenModal, self).on_click(x, y)234 if tag_name == 'btn_back':235 self.close()236 return237 def make_time_string(self, seconds):238 days = int(seconds / 86400)239 hours = int((seconds - (days * 86400)) / 3600)240 minutes = int((seconds - (days * 86400) - (hours * 3600)) / 60)241 seconds_left = int(round(seconds - (days * 86400) - (hours * 3600) - (minutes * 60), 0))242 time_string = ""...

Full Screen

Full Screen

generate_ics.py

Source:generate_ics.py Github

copy

Full Screen

...10 tz_standard = TimezoneStandard()11 tz_standard.add("DTSTART", datetime.datetime(1970, 1, 1))12 tz_standard.add("TZOFFSETFROM", datetime.timedelta(hours=8))13 tz_standard.add("TZOFFSETTO", datetime.timedelta(hours=8))14 tz.add_component(tz_standard)15 return tz16def _create_event(event_name, start, end, descrip=None):17 # 创建事件/日程18 event = Event()19 event.add("SUMMARY", event_name)20 event.add("DTSTART", start)21 event.add("DTEND", end)22 # 创建时间23 event.add("DTSTAMP", start)24 if(descrip):25 event.add("DESCRIPTION", descrip)26 # UID保证唯一27 event["UID"] = str(uuid.uuid4())28 return event29def _cast_date(v: Any) -> datetime.date:30 if isinstance(v, datetime.date):31 return v32 if isinstance(v, str):33 return datetime.date.fromisoformat(v)34 raise NotImplementedError("can not convert to date: %s" % v)35def _iter_date_ranges(days: Sequence[dict]) -> Iterator[Tuple[dict, dict]]:36 if len(days) == 0:37 return38 if len(days) == 1:39 yield days[0], days[0]40 return41 fr, to = days[0], days[0]42 for cur in days[1:]:43 if (_cast_date(cur["date"]) - _cast_date(to["date"])).days == 1 and cur[44 "isOffDay"45 ] == to["isOffDay"]:46 to = cur47 else:48 yield fr, to49 fr, to = cur, cur50 yield fr, to51def generate_ics(days: Sequence[dict], filename: Text) -> None:52 """Generate ics from days."""53 cal = Calendar()54 cal.add("X-WR-CALNAME", "中国法定节假日")55 cal.add("X-WR-CALDESC", "中国法定节假日数据,自动每日抓取国务院公告。")56 cal.add("VERSION", "2.0")57 cal.add("METHOD", "PUBLISH")58 cal.add("CLASS", "PUBLIC")59 cal.add_component(_create_timezone())60 days = sorted(days, key=lambda x: x["date"])61 for fr, to in _iter_date_ranges(days):62 start = _cast_date(fr["date"])63 end = _cast_date(to["date"]) + datetime.timedelta(days=1)64 name = fr["name"] + "假期"65 if not fr["isOffDay"]:66 name = "上班(补" + name + ")"67 cal.add_component(_create_event(name, start, end))68 with open(filename, "wb") as f:69 f.write(cal.to_ical())70def generate_main_ics(days: Sequence[dict], filename: Text, nowyear) -> None:71 """Generate ics from days."""72 # url = 'https://calendars.icloud.com/holidays/cn_zh.ics'73 # cal = Calendar.from_ical(requests.get(url).text)74 cal = Calendar()75 cal.add("X-WR-CALNAME", "中国节日")76 cal.add("X-WR-CALDESC", "中国常用节日,并抓取国务院公告获取法定假期数据。")77 cal.add("VERSION", "2.0")78 cal.add("METHOD", "PUBLISH")79 cal.add("CLASS", "PUBLIC")80 cal.add_component(_create_timezone())81 for year in range(nowyear-3, nowyear + 2):82 name = "元旦"83 start = _cast_date("%d-01-01" % year)84 end = start85 cal.add_component(_create_event(name, start, end))86 name = "情人节"87 start = _cast_date("%d-02-14" % year)88 end = start89 cal.add_component(_create_event(name, start, end))90 name = "妇女节"91 start = _cast_date("%d-03-08" % year)92 end = start93 cal.add_component(_create_event(name, start, end))94 name = "植树节"95 start = _cast_date("%d-03-12" % year)96 end = start97 cal.add_component(_create_event(name, start, end))98 name = "愚人节"99 start = _cast_date("%d-04-01" % year)100 end = start101 cal.add_component(_create_event(name, start, end))102 name = "世界地球日"103 start = _cast_date("%d-04-22" % year)104 end = start105 cal.add_component(_create_event(name, start, end))106 name = "劳动节"107 start = _cast_date("%d-05-01" % year)108 end = start109 cal.add_component(_create_event(name, start, end))110 name = "青年节"111 start = _cast_date("%d-05-04" % year)112 end = start113 descrip = "五四运动%d周年纪念日" % (year - 1919)114 cal.add_component(_create_event(name, start, end, descrip))115 name = "母亲节"116 start = _cast_date("%d-05-01" % year)+datetime.timedelta(days=13-_cast_date("%d-05-01" % year).weekday())117 end = start118 cal.add_component(_create_event(name, start, end))119 name = "儿童节"120 start = _cast_date("%d-06-01" % year)121 end = start122 cal.add_component(_create_event(name, start, end))123 name = "父亲节"124 start = _cast_date("%d-06-01" % year)+datetime.timedelta(days=20-_cast_date("%d-06-01" % year).weekday())125 end = start126 cal.add_component(_create_event(name, start, end))127 name = "建党节"128 start = _cast_date("%d-07-01" % year)129 end = start130 cal.add_component(_create_event(name, start, end))131 name = "建军节"132 start = _cast_date("%d-08-01" % year)133 end = start134 cal.add_component(_create_event(name, start, end))135 name = "抗日战争胜利纪念日"136 start = _cast_date("%d-09-03" % year)137 end = start138 descrip = "抗日战争胜利%d周年" % (year - 1945)139 cal.add_component(_create_event(name, start, end, descrip))140 name = "教师节"141 start = _cast_date("%d-09-10" % year)142 end = start143 cal.add_component(_create_event(name, start, end))144 name = "国耻日"145 start = _cast_date("%d-09-18" % year)146 end = start147 descrip = "九·一八事变 1931年9月18日"148 cal.add_component(_create_event(name, start, end, descrip))149 name = "国庆节"150 start = _cast_date("%d-10-01" % year)151 end = start152 descrip = "建国%d周年" % (year - 1949)153 cal.add_component(_create_event(name, start, end, descrip))154 name = "辛亥革命纪念日"155 start = _cast_date("%d-10-10" % year)156 end = start157 descrip = "辛亥革命%d周年" % (year-1911)158 cal.add_component(_create_event(name, start, end, descrip))159 name= "一二·九运动纪念日"160 start = _cast_date("%d-12-09" % year)161 end = start162 descrip = "一二·九运动%d周年" % (year-1935)163 cal.add_component(_create_event(name, start, end, descrip))164 name = "南京大屠杀纪念日"165 start = _cast_date("%d-12-13" % year)166 end = start167 descrip = "南京大屠杀%d周年" % (year-1937)168 cal.add_component(_create_event(name, start, end, descrip))169 name = "平安夜"170 start = _cast_date("%d-12-24" % year)171 end = start172 cal.add_component(_create_event(name, start, end))173 name = "圣诞节"174 start = _cast_date("%d-12-25" % year)175 end = start176 cal.add_component(_create_event(name, start, end))177 name = "春节"178 start = _cast_date(ZhDate(year, 1, 1).to_datetime().date())179 end = start180 cal.add_component(_create_event(name, start, end))181 name = "元宵节"182 start = _cast_date(ZhDate(year, 1, 15).to_datetime().date())183 end = start184 cal.add_component(_create_event(name, start, end))185 name = "龙抬头"186 start = _cast_date(ZhDate(year, 2, 2).to_datetime().date())187 end = start188 cal.add_component(_create_event(name, start, end))189 name = "上巳节"190 start = _cast_date(ZhDate(year, 3, 3).to_datetime().date())191 end = start192 cal.add_component(_create_event(name, start, end))193 name = "端午节"194 start = _cast_date(ZhDate(year, 5, 5).to_datetime().date())195 end = start196 cal.add_component(_create_event(name, start, end))197 name = "七夕节"198 start = _cast_date(ZhDate(year, 7, 7).to_datetime().date())199 end = start200 cal.add_component(_create_event(name, start, end))201 name = "中元节"202 start = _cast_date(ZhDate(year, 7, 15).to_datetime().date())203 end = start204 cal.add_component(_create_event(name, start, end))205 name = "中秋节"206 start = _cast_date(ZhDate(year, 8, 15).to_datetime().date())207 end = start208 cal.add_component(_create_event(name, start, end))209 name = "重阳节"210 start = _cast_date(ZhDate(year, 9, 9).to_datetime().date())211 end = start212 cal.add_component(_create_event(name, start, end))213 name = "下元节"214 start = _cast_date(ZhDate(year, 10, 10).to_datetime().date())215 end = start216 cal.add_component(_create_event(name, start, end))217 name = "腊八节"218 if (ZhDate(year-1, 12, 8).to_datetime().year == year):219 start = _cast_date(ZhDate(year-1, 12, 8).to_datetime().date())220 end = start221 cal.add_component(_create_event(name, start, end))222 elif (ZhDate(year, 12, 8).to_datetime().year == year):223 start = _cast_date(ZhDate(year, 12, 8).to_datetime().date())224 end = start225 cal.add_component(_create_event(name, start, end))226 name = "除夕"227 start = _cast_date(228 ZhDate(year, 1, 1).to_datetime().date()-datetime.timedelta(days=1))229 end = start230 cal.add_component(_create_event(name, start, end))231 days = sorted(days, key=lambda x: x["date"])232 for fr, to in _iter_date_ranges(days):233 start = _cast_date(fr["date"])234 end = _cast_date(to["date"]) + datetime.timedelta(days=1)235 name = fr["name"] + "假期"236 if not fr["isOffDay"]:237 name = "上班(补" + name + ")"238 cal.add_component(_create_event(name, start, end))239 with open(filename, "wb") as f:...

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 Kiwi 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