2015年8月12日 星期三

wxPython: Send event manually (v2.8.x)

#!/usr/bin/env python
# vim: tabstop=8 shiftwidth=4 softtabstop=4
# python version: 2.7.5 final, serial: 0

import sys
import wxversion
wxversion.select('2.8')
import wx


class DemoApp(wx.App):
    def OnInit(self):
        self._frame = DemoMainFrame(None, title='Send Event Demo')
        self.SetTopWindow(self._frame)
        self._frame.Show()
        return True
    # end of OnInit
# end of DemoApp


class DemoMainFrame(wx.Frame):
    def __init__(self, parent, id_=wx.ID_ANY, title='Main', \
                      pos=wx.DefaultPosition, size=wx.DefaultSize, \
                      style=wx.DEFAULT_FRAME_STYLE, name='Main'):
        super(DemoMainFrame, self).__init__(\
            parent, id_, title, pos, size, style, name)
        self.Centre(wx.BOTH)
        self._mainMenu = wx.MenuBar()
        self._fileMenu = wx.Menu()
        self._checkItem = self._fileMenu.AppendCheckItem(\
            wx.ID_ANY, 'Check Item')
        self.Bind(wx.EVT_MENU, self._onCheckItemClick, self._checkItem)
        self._mainMenu.Append(self._fileMenu, 'File(&F)')
        self.SetMenuBar(self._mainMenu)
        self._panel = wx.Panel(self, wx.ID_ANY)
        self._button = wx.Button(self._panel, wx.ID_ANY, 'Send Event')
        self._button.SetPosition((10, 10))
        self._button.Bind(wx.EVT_BUTTON, self._onButtonClick)
    # end of __init__

    def _onCheckItemClick(self, event):
        style = wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.CENTRE
        dialog = wx.MessageDialog(self, 'Check Item Clicked!', 'Check Item', style)
        dialog.ShowModal()
        if dialog:
            dialog.Destroy()
    # end of _onCheckItemClick


    def _onButtonClick(self, event):
        sendEvent(wx.wxEVT_COMMAND_MENU_SELECTED, self._checkItem.Id, \
                          self._fileMenu, self.GetEventHandler())

        self._checkItem.Check(not self._checkItem.IsChecked())
    # end of _onButtonClick

def sendEvent(eventType, targetID, eventObj, eventHandler, isSetInt=True, \
              eventClass=wx.CommandEvent):
    """Send a event manually."""
    event = eventClass(eventType, targetID)
    if isSetInt:
        event.SetInt(1)
    event.SetEventObject(eventObj)
    #wx.PostEvent(eventHandler, event)
    assert eventHandler.ProcessEvent(event), 'Send event fail!'
# end of sendEvent


def _main():
    try:
        app = DemoApp(False)
        app.SetAppName('Send Event Demo')
        app.MainLoop()
    except KeyboardInterrupt:
        sys.exit(0)
# end of _main

if __name__ == '__main__':
    _main()
# end of __main__

2015年8月3日 星期一

How To Backup / Restore Master Boot Record (MBR) in Linux

Structure of a classical generic MBR
Address Description Size
(bytes)
Hex Dec
+000h +0 Bootstrap code area 446
+1BEh +446 Partition entry #1 Partition table
(for primary partitions)
16
+1CEh +462 Partition entry #2 16
+1DEh +478 Partition entry #3 16
+1EEh +494 Partition entry #4 16
+1FEh +510 55h Boot signature[a] 2
+1FFh +511 AAh
Total size: 446 + 4×16 + 2 512

Backup / Restore Bootstrap code w/wo Partition Table

Use 446 bytes to backup / restore your /dev/s(h)dx MBR boot code.
Use 512 bytes to backup / restore your /dev/s(h)dx MBR boot code and disk partition table.

*** Use the following commands with CAUTION or it will destroy your data! ***

Clone the MBR From One Disk to Another (With Identical Sized Partitions)


Copy the MBR (first 512 bytes) from disk sda to disk sdb:
$ dd if=/dev/sda of=/dev/sdb bs=512 count=1

Clone the MBR From One Disk to Another (With different Sized Partitions)

Backup the MBR of target disk (sdb):
$ dd if=/dev/sdb of=/tmp/sdb_mbr.bak bs=512 count=1

Copy the MBR (first 446 bytes, without partition table) from disk sda to disk sdb:
$ dd if=/dev/sda of=/dev/sdb bs=446 count=1

Backup / Restore the Primary and Extended Partitions

Backup:
$ sfdisk -d /dev/sda> sda.out

Restore:
$ sfdisk /dev/sda < sda.out

Reference:

https://wiki.archlinux.org/index.php/Disk_Cloning
https://en.wikipedia.org/wiki/Master_boot_record
http://manpages.ubuntu.com/manpages/natty/man1/dd.1.html
http://manpages.ubuntu.com/manpages/hardy/man8/sfdisk.8.html