<commit_before># Date Time Example
# Copyright (C) 2009 Yit Choong (http://code.google.com/u/yitchoong/)
# Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net>

from SimplePanel import SimplePanel
from pyjamas import Factory
from VerticalPanel import  VerticalPanel
from HorizontalPanel import HorizontalPanel
from PopupPanel import  PopupPanel
from Grid import Grid
from Composite import Composite
from Label import Label
from Hyperlink import Hyperlink
from HyperlinkImage import HyperlinkImage
from HTML import HTML
from FocusPanel import FocusPanel
from TextBox import TextBox
from Image import Image
from pyjamas.ui import HasAlignment
from pyjamas import DOM

import time

class Calendar(FocusPanel):
    def __init__(self, **kwargs):
        FocusPanel.__init__(self, **kwargs)
        self.monthsOfYear = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
        self.daysOfWeek = ['S','M','T','W','T','F','S']
        yr,mth,day = time.strftime("%Y-%m-%d").split("-")
        self.todayYear = int(yr)
        self.todayMonth = int(mth)  # change to offset 0 as per javascript
        self.todayDay = int(day)

        self.currentMonth = self.todayMonth
        self.currentYear = self.todayYear
        self.currentDay = self.todayDay

        self.selectedDateListeners = []

        self.defaultGrid = None # used later

        return

    def addSelectedDateListener(self,listener):
        self.selectedDateListeners.append(listener)

    def removeSelectedDateListener(self,listener):
        self.selectedDateListeners.remove(listener)

    def isLeapYear(self,year):
        if (year % 4 == 0 and year % 100 != 0) or ( year % 400 == 0):
            return True
        else:
            return False

    def getDaysInMonth(self,mth,year):
        days = 0
        if (mth == 1 or mth == 3 or mth == 5 or mth == 7 or mth == 8 or mth == 10 or mth == 12): # in (1,3,5,7,8,10,12):
            days=31
        elif mth == 4 or mth == 6 or mth == 8 or mth == 11: #in (4,6,8,11):
            days = 30
        elif (mth==2 and self.isLeapYear(year)):
            days = 29
        else:
            days = 28
        return days

    def setPosition(self, left, top):
        element = self.getElement()
        DOM.setStyleAttribute(element, "left", "%dpx" % left)
        DOM.setStyleAttribute(element, "top", "%dpx" % top)

    def show(self, left, top):
        if left < 0:
            left = 0
        if top < 0:
            top = 0
        self.setPosition(left,top)
        self.drawCurrent()
        self.setVisible(True)

    def drawCurrent(self):
        yr,mth,day = time.strftime("%Y-%m-%d").split("-")
        self.draw( int(mth), int(yr) )

    def draw(self, month , year):
        tod = time.localtime()
        mm = tod.tm_mon
        yy = tod.tm_year
        # has today changed and thus changed month? cater to rare case where widget in created on last day of month &
        # page left till next day
        hasChangeMonth = False
        if yy <> self.todayYear or mm <> self.todayMonth:
            hasChangeMonth = True
            self.todayYear = yy
            self.todayMonth = mm

        # check to see if we have drawn the full widget before
        if self.defaultGrid is None:
            self.drawFull(month,year)
        else:
            # ok means we are re-drawing, but first check if it is the same as the defaultGrid, if yes, just use it
            if not hasChangeMonth and month == self.todayMonth and year == self.todayYear:
                self.middlePanel.setWidget(self.defaultGrid)
                self.currentMonth = self.todayMonth
                self.currentYear = self.todayYear
            else:
                # we have to redraw the grid -- bah
                g = self.drawGrid(month,year)

                if hasChangeMonth:
                    self.defaultGrid = grid # reset the default grid as we have changed months

            #
            # what about the title panel?
            #
            self.titlePanel.setWidget(HTML("" + self.monthsOfYear[month-1] + " " + str(year) + "" ) )
            self.setVisible(True)

        return

    def drawFull(self, month, year):
        # should be called only once when we draw the calendar for the first time
        #
        self.vp = VerticalPanel()
        self.vp.setSpacing(2)
        self.vp.addStyleName("calendarbox calendar-module calendar")
        self.setWidget(self.vp)
        self.setVisible(False)
        #
        mth = int(month)
        yr = int(year)

        tp = HorizontalPanel()
        tp.addStyleName("calendar-top-panel")
        tp.setSpacing(5)

        h1 = Hyperlink('<<')
        h1.addClickListener( getattr(self,'onPreviousYear') )
        h2 = Hyperlink('<')
        h2.addClickListener( getattr(self,'onPreviousMonth') )
        h4 = Hyperlink('>')
        h4.addClickListener( getattr(self,'onNextMonth') )
        h5 = Hyperlink('>>')
        h5.addClickListener( getattr(self,'onNextYear') )

        tp.add(h1)
        tp.add(h2)

        # titlePanel can be changed, whenever we draw, so keep the reference

        self.titlePanel = SimplePanel()
        self.titlePanel.setWidget(HTML("" + self.monthsOfYear[mth-1] + " " + str(yr) + "" ) )
        self.titlePanel.setStyleName("calendar-center")

        tp.add( self.titlePanel )
        tp.add(h4)
        tp.add(h5)
        tvp = VerticalPanel()
        tvp.setSpacing(10)
        tvp.add(tp)

        self.vp.add(tvp)

        # done with top panel

        self.middlePanel = SimplePanel()
        grid = self.drawGrid(mth,yr)
        self.middlePanel.setWidget(grid)
        self.vp.add(self.middlePanel)
        self.defaultGrid = grid
        #
        # some links & handlers
        #
        bh1 = Hyperlink('Yesterday')
        bh1.addClickListener( getattr(self,'onYesterday') )
        bh2 = Hyperlink('Today')
        bh2.addClickListener( getattr(self,'onToday') )
        bh3 = Hyperlink('Tomorrow')
        bh3.addClickListener( getattr(self,'onTomorrow') )
        bh4 = Hyperlink('Cancel')
        bh4.addClickListener( getattr(self,'onCancel') )
        #
        # add code to test another way of doing the layout
        #
        b = HorizontalPanel()
        b.add(bh1)
        b.add(bh2)
        b.add(bh3)
        b.addStyleName("calendar-shortcuts")
        self.vp.add(b)
        b2 = SimplePanel()
        b2.add(bh4)
        b2.addStyleName("calendar-cancel")
        self.vp.add(b2)

        self.setVisible(True)
        return

    def drawGrid(self,month,year):
        # draw the grid in the middle of the calendar

        daysInMonth = self.getDaysInMonth(month, year)
        secs = time.mktime( (year,month,1,0,0,0,0,0,-1) ) # first day of the month & year
        struct = time.localtime(secs)
        startPos = (struct.tm_wday + 1 ) % 7 # 0 - sunday for our needs instead 0 = monday in tm_wday
        slots = startPos + daysInMonth - 1
        rows = int(slots/7) + 1
        grid = Grid(rows+1, 7) # extra row for the days in the week
        grid.setWidth("100%")
        grid.addTableListener(self)
        self.middlePanel.setWidget(grid)
        #
        # put some content into the grid cells
        #
        for i in range(7):
            grid.setText(0, i, self.daysOfWeek[i] )
            grid.cellFormatter.addStyleName(0,i,"calendar-header")
        #
        # draw cells which are empty first
        #
        day =0
        pos = 0
        while pos < startPos:
            grid.setText(1, pos , " ")
            grid.cellFormatter.setStyleAttr(1,pos,"background","#f3f3f3")
            grid.cellFormatter.addStyleName(1,pos,"calendar-blank-cell")
            pos += 1
        # now for days of the month
        row = 1
        day = 1
        col = startPos
        while day <= daysInMonth:
            if pos % 7 == 0 and day <> 1:
                row += 1
            col = pos % 7
            grid.setText(row,col, str(day) )
            if self.currentYear == self.todayYear and self.currentMonth == self.todayMonth and day == self.todayDay:
                grid.cellFormatter.addStyleName(row,col,"calendar-cell-today")
            else:
                grid.cellFormatter.addStyleName(row,col,"calendar-day-cell")
            day += 1
            pos += 1
        #
        # now blank lines on the last row
        #
        col += 1
        while col < 7:
            grid.setText(row,col," ")
            grid.cellFormatter.setStyleAttr(row,col,"background","#f3f3f3")
            grid.cellFormatter.addStyleName(row,col,"calendar-blank-cell")
            col += 1

        return grid

    def onCellClicked(self, grid, row, col):
        if row == 0:
            return
        text = grid.getText(row, col)
        if text == "":
            return
        selectedDay = int(text)
        # well if anyone is listening to the listener, fire that event
        for listener in self.selectedDateListeners:
            if hasattr(listener, "onDateSelected"):
                listener.onDateSelected(self.currentYear, self.currentMonth,
                                        selectedDay)
            else:
                listener(self.currentYear, self.currentMonth, selectedDay)
        self.setVisible(False)

    def onPreviousYear(self,event):
        self.drawPreviousYear()

    def onPreviousMonth(self,event):
        self.drawPreviousMonth()

    def onNextMonth(self,event):
        self.drawNextMonth()

    def onNextYear(self,event):
        self.drawNextYear()

    def onDate(self, event, yy, mm, dd):
        for listener in self.selectedDateListeners:
            if hasattr(listener, "onDateSelected"):
                listener.onDateSelected(yy,mm,dd)
            else:
                listener(yy,mm,dd)
        self.setVisible(False)

    def onYesterday(self,event):
        yesterday = time.localtime(time.time() - 3600 * 24)
        mm = yesterday.tm_mon
        dd = yesterday.tm_mday
        yy = yesterday.tm_year
        self.onDate(event, yy, mm, dd)

    def onToday(self,event):
        tod = time.localtime()
        mm = tod.tm_mon
        dd = tod.tm_mday
        yy = tod.tm_year
        self.onDate(event, yy, mm, dd)

    def onTomorrow(self,event):
        tom = time.localtime(time.time() + 3600 * 24)
        mm = tom.tm_mon
        dd = tom.tm_mday
        yy = tom.tm_year
        self.onDate(event, yy, mm, dd)

    def onCancel(self,event):
        self.setVisible(False)

    def drawCurrent(self):
        yr,mth,day = time.strftime("%Y-%m-%d").split("-")
        self.draw( int(mth), int(yr) )

    def drawDate(self, month, year ):
        # if year == self.currentYear and month == self.currentYear():
            # self.drawCurrent()
        self.currentMonth = month
        self.currentYear = year
        self.draw(self.currentMonth, self.currentYear)

    def drawPreviousMonth(self):
        if int(self.currentMonth) == 1:
            self.currentMonth = 12
            self.currentYear = int(self.currentYear) - 1
        else:
            self.currentMonth = int(self.currentMonth) - 1
        self.draw(self.currentMonth, self.currentYear)

    def drawNextMonth(self):
        if int(self.currentMonth) == 12:
            self.currentMonth = 1
            self.currentYear = int(self.currentYear) + 1
        else:
            self.currentMonth = int(self.currentMonth) + 1
        self.draw(self.currentMonth, self.currentYear)

    def drawPreviousYear(self):
        self.currentYear = int(self.currentYear) - 1
        self.draw(self.currentMonth, self.currentYear)

    def drawNextYear(self):
        self.currentYear = int(self.currentYear) + 1
        self.draw(self.currentMonth, self.currentYear)

class DateField(Composite):

    def __init__(self,format='%d-%m-%Y'):
        self.format = format
        self.tbox = TextBox()
        self.tbox.setVisibleLength(10)
        # assume valid sep is - / . or nothing
        if format.find('-') >= 0:
            self.sep = '-'
        elif format.find('/') >= 0:
            self.sep = '/'
        elif format.find('.') >= 0:
            self.sep = '.'
        else:
            self.sep = ''
        # self.sep = format[2] # is this too presumptious?
        self.calendar = Calendar()
        img = Image("icon_calendar.gif")
        img.addStyleName("calendar-img")
        self.calendarLink = HyperlinkImage(img)
        self.todayLink = Hyperlink('Today')
        self.todayLink.addStyleName("calendar-today-link")
        #
        # lay it out
        #
        hp = HorizontalPanel()
        hp.setSpacing(2)
        vp = VerticalPanel()
        hp.add(self.tbox)
        vp.add(self.calendarLink)
        vp.add(self.todayLink)
        #vp.add(self.calendar)
        hp.add(vp)

        Composite.__init__(self)
        self.initWidget(hp)
        #
        # done with layout, so now set up some listeners
        #
        self.tbox.addFocusListener(self) # hook to onLostFocus
        self.calendar.addSelectedDateListener(getattr(self,"onDateSelected"))
        self.todayLink.addClickListener(getattr(self,"onTodayClicked"))
        self.calendarLink.addClickListener(getattr(self,"onShowCalendar"))

    def getTextBox(self):
        return self.tbox

    def getCalendar(self):
        return self.calendar

    def setID(self,id):
        self.tbox.setID(id)

    def onDateSelected(self, yyyy, mm, dd):
        secs = time.mktime((int(yyyy),int(mm),int(dd),0,0,0,0,0,-1))
        d = time.strftime(self.format,time.localtime(secs))
        self.tbox.setText(d)

    def onLostFocus(self, sender):
        #
        text = self.tbox.getText().strip()
        # if blank - leave it alone
        if text and len(text) == 8:
            # ok what format do we have? assume ddmmyyyy --> dd-mm-yyyy
            self.tbox.setText( text[0:2] + self.sep + text[2:4] + self.sep + text[4:8] )

    def onFocus(self, sender):
        pass

    def onTodayClicked(self, event):
        today = time.strftime(self.format)
        self.tbox.setText( today )

    def onShowCalendar(self, sender):
        p = CalendarPopup(self.calendar)
        x = self.tbox.getAbsoluteLeft() + 10
        y = self.tbox.getAbsoluteTop() + 10
        p.setPopupPosition(x,y)
        p.show()

Factory.registerClass('pyjamas.ui.Calendar', Calendar)

class CalendarPopup(PopupPanel):
    def __init__(self, c):
        PopupPanel.__init__(self, True)
        p = SimplePanel()
        p.add(c)
        c.show(10,10)
        p.setWidth("100%")
        self.setWidget(p)

Factory.registerClass('pyjamas.ui.CalendarPopup', CalendarPopup)
<commit_msg>Changed some internals of Calendar/DateField, so they are easier to extend and to change image sources/month names/day names<commit_after># Date Time Example
# Copyright (C) 2009 Yit Choong (http://code.google.com/u/yitchoong/)
# Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net>

from SimplePanel import SimplePanel
from pyjamas import Factory
from VerticalPanel import  VerticalPanel
from HorizontalPanel import HorizontalPanel
from PopupPanel import  PopupPanel
from Grid import Grid
from Composite import Composite
from Label import Label
from Hyperlink import Hyperlink
from HyperlinkImage import HyperlinkImage
from HTML import HTML
from FocusPanel import FocusPanel
from TextBox import TextBox
from Image import Image
from pyjamas.ui import HasAlignment
from pyjamas import DOM

import time

class Calendar(FocusPanel):
    monthsOfYear = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    daysOfWeek = ['S','M','T','W','T','F','S']
    today = 'Today'
    tomorrow = 'Tomorrow'
    yesterday = 'Yesterday'
    cancel = 'Cancel'

    def __init__(self, **kwargs):
        FocusPanel.__init__(self, **kwargs)
        yr,mth,day = time.strftime("%Y-%m-%d").split("-")
        self.todayYear = int(yr)
        self.todayMonth = int(mth)  # change to offset 0 as per javascript
        self.todayDay = int(day)

        self.currentMonth = self.todayMonth
        self.currentYear = self.todayYear
        self.currentDay = self.todayDay

        self.selectedDateListeners = []

        self.defaultGrid = None # used later

        return

    def getMonthsOfYear(self):
        return self.monthsOfYear

    def getDaysOfWeek(self):
        return self.daysOfWeek

    def addSelectedDateListener(self,listener):
        self.selectedDateListeners.append(listener)

    def removeSelectedDateListener(self,listener):
        self.selectedDateListeners.remove(listener)

    def isLeapYear(self,year):
        if (year % 4 == 0 and year % 100 != 0) or ( year % 400 == 0):
            return True
        else:
            return False

    def getDaysInMonth(self,mth,year):
        days = 0
        if (mth == 1 or mth == 3 or mth == 5 or mth == 7 or mth == 8 or mth == 10 or mth == 12): # in (1,3,5,7,8,10,12):
            days=31
        elif mth == 4 or mth == 6 or mth == 8 or mth == 11: #in (4,6,8,11):
            days = 30
        elif (mth==2 and self.isLeapYear(year)):
            days = 29
        else:
            days = 28
        return days

    def setPosition(self, left, top):
        element = self.getElement()
        DOM.setStyleAttribute(element, "left", "%dpx" % left)
        DOM.setStyleAttribute(element, "top", "%dpx" % top)

    def show(self, left, top):
        if left < 0:
            left = 0
        if top < 0:
            top = 0
        self.setPosition(left,top)
        self.drawCurrent()
        self.setVisible(True)

    def drawCurrent(self):
        yr,mth,day = time.strftime("%Y-%m-%d").split("-")
        self.draw( int(mth), int(yr) )

    def draw(self, month , year):
        tod = time.localtime()
        mm = tod.tm_mon
        yy = tod.tm_year
        # has today changed and thus changed month? cater to rare case where widget in created on last day of month &
        # page left till next day
        hasChangeMonth = False
        if yy <> self.todayYear or mm <> self.todayMonth:
            hasChangeMonth = True
            self.todayYear = yy
            self.todayMonth = mm

        # check to see if we have drawn the full widget before
        if self.defaultGrid is None:
            self.drawFull(month,year)
        else:
            # ok means we are re-drawing, but first check if it is the same as the defaultGrid, if yes, just use it
            if not hasChangeMonth and month == self.todayMonth and year == self.todayYear:
                self.middlePanel.setWidget(self.defaultGrid)
                self.currentMonth = self.todayMonth
                self.currentYear = self.todayYear
            else:
                # we have to redraw the grid -- bah
                g = self.drawGrid(month,year)

                if hasChangeMonth:
                    self.defaultGrid = grid # reset the default grid as we have changed months

            #
            # what about the title panel?
            #
            self.titlePanel.setWidget(HTML("" + self.getMonthsOfYear()[month-1] + " " + str(year) + "" ) )
            self.setVisible(True)

        return

    def drawFull(self, month, year):
        # should be called only once when we draw the calendar for the first time
        #
        self.vp = VerticalPanel()
        self.vp.setSpacing(2)
        self.vp.addStyleName("calendarbox calendar-module calendar")
        self.setWidget(self.vp)
        self.setVisible(False)
        #
        mth = int(month)
        yr = int(year)

        tp = HorizontalPanel()
        tp.addStyleName("calendar-top-panel")
        tp.setSpacing(5)

        h1 = Hyperlink('<<')
        h1.addClickListener( getattr(self,'onPreviousYear') )
        h2 = Hyperlink('<')
        h2.addClickListener( getattr(self,'onPreviousMonth') )
        h4 = Hyperlink('>')
        h4.addClickListener( getattr(self,'onNextMonth') )
        h5 = Hyperlink('>>')
        h5.addClickListener( getattr(self,'onNextYear') )

        tp.add(h1)
        tp.add(h2)

        # titlePanel can be changed, whenever we draw, so keep the reference

        self.titlePanel = SimplePanel()
        self.titlePanel.setWidget(HTML("" + self.getMonthsOfYear()[mth-1] + " " + str(yr) + "" ) )
        self.titlePanel.setStyleName("calendar-center")

        tp.add( self.titlePanel )
        tp.add(h4)
        tp.add(h5)
        tvp = VerticalPanel()
        tvp.setSpacing(10)
        tvp.add(tp)

        self.vp.add(tvp)

        # done with top panel

        self.middlePanel = SimplePanel()
        grid = self.drawGrid(mth,yr)
        self.middlePanel.setWidget(grid)
        self.vp.add(self.middlePanel)
        self.defaultGrid = grid
        #
        # some links & handlers
        #
        bh1 = Hyperlink(self.yesterday)
        bh1.addClickListener( getattr(self,'onYesterday') )
        bh2 = Hyperlink(self.today)
        bh2.addClickListener( getattr(self,'onToday') )
        bh3 = Hyperlink(self.tomorrow)
        bh3.addClickListener( getattr(self,'onTomorrow') )
        bh4 = Hyperlink(self.cancel)
        bh4.addClickListener( getattr(self,'onCancel') )
        #
        # add code to test another way of doing the layout
        #
        b = HorizontalPanel()
        b.add(bh1)
        b.add(bh2)
        b.add(bh3)
        b.addStyleName("calendar-shortcuts")
        self.vp.add(b)
        b2 = SimplePanel()
        b2.add(bh4)
        b2.addStyleName("calendar-cancel")
        self.vp.add(b2)

        self.setVisible(True)
        return

    def drawGrid(self,month,year):
        # draw the grid in the middle of the calendar

        daysInMonth = self.getDaysInMonth(month, year)
        secs = time.mktime( (year,month,1,0,0,0,0,0,-1) ) # first day of the month & year
        struct = time.localtime(secs)
        startPos = (struct.tm_wday + 1 ) % 7 # 0 - sunday for our needs instead 0 = monday in tm_wday
        slots = startPos + daysInMonth - 1
        rows = int(slots/7) + 1
        grid = Grid(rows+1, 7) # extra row for the days in the week
        grid.setWidth("100%")
        grid.addTableListener(self)
        self.middlePanel.setWidget(grid)
        #
        # put some content into the grid cells
        #
        for i in range(7):
            grid.setText(0, i, self.getDaysOfWeek()[i] )
            grid.cellFormatter.addStyleName(0,i,"calendar-header")
        #
        # draw cells which are empty first
        #
        day =0
        pos = 0
        while pos < startPos:
            grid.setText(1, pos , " ")
            grid.cellFormatter.setStyleAttr(1,pos,"background","#f3f3f3")
            grid.cellFormatter.addStyleName(1,pos,"calendar-blank-cell")
            pos += 1
        # now for days of the month
        row = 1
        day = 1
        col = startPos
        while day <= daysInMonth:
            if pos % 7 == 0 and day <> 1:
                row += 1
            col = pos % 7
            grid.setText(row,col, str(day) )
            if self.currentYear == self.todayYear and self.currentMonth == self.todayMonth and day == self.todayDay:
                grid.cellFormatter.addStyleName(row,col,"calendar-cell-today")
            else:
                grid.cellFormatter.addStyleName(row,col,"calendar-day-cell")
            day += 1
            pos += 1
        #
        # now blank lines on the last row
        #
        col += 1
        while col < 7:
            grid.setText(row,col," ")
            grid.cellFormatter.setStyleAttr(row,col,"background","#f3f3f3")
            grid.cellFormatter.addStyleName(row,col,"calendar-blank-cell")
            col += 1

        return grid

    def onCellClicked(self, grid, row, col):
        if row == 0:
            return
        text = grid.getText(row, col)
        if text == "":
            return
        selectedDay = int(text)
        # well if anyone is listening to the listener, fire that event
        for listener in self.selectedDateListeners:
            if hasattr(listener, "onDateSelected"):
                listener.onDateSelected(self.currentYear, self.currentMonth,
                                        selectedDay)
            else:
                listener(self.currentYear, self.currentMonth, selectedDay)
        self.setVisible(False)

    def onPreviousYear(self,event):
        self.drawPreviousYear()

    def onPreviousMonth(self,event):
        self.drawPreviousMonth()

    def onNextMonth(self,event):
        self.drawNextMonth()

    def onNextYear(self,event):
        self.drawNextYear()

    def onDate(self, event, yy, mm, dd):
        for listener in self.selectedDateListeners:
            if hasattr(listener, "onDateSelected"):
                listener.onDateSelected(yy,mm,dd)
            else:
                listener(yy,mm,dd)
        self.setVisible(False)

    def onYesterday(self,event):
        yesterday = time.localtime(time.time() - 3600 * 24)
        mm = yesterday.tm_mon
        dd = yesterday.tm_mday
        yy = yesterday.tm_year
        self.onDate(event, yy, mm, dd)

    def onToday(self,event):
        tod = time.localtime()
        mm = tod.tm_mon
        dd = tod.tm_mday
        yy = tod.tm_year
        self.onDate(event, yy, mm, dd)

    def onTomorrow(self,event):
        tom = time.localtime(time.time() + 3600 * 24)
        mm = tom.tm_mon
        dd = tom.tm_mday
        yy = tom.tm_year
        self.onDate(event, yy, mm, dd)

    def onCancel(self,event):
        self.setVisible(False)

    def drawCurrent(self):
        yr,mth,day = time.strftime("%Y-%m-%d").split("-")
        self.draw( int(mth), int(yr) )

    def drawDate(self, month, year ):
        # if year == self.currentYear and month == self.currentYear():
            # self.drawCurrent()
        self.currentMonth = month
        self.currentYear = year
        self.draw(self.currentMonth, self.currentYear)

    def drawPreviousMonth(self):
        if int(self.currentMonth) == 1:
            self.currentMonth = 12
            self.currentYear = int(self.currentYear) - 1
        else:
            self.currentMonth = int(self.currentMonth) - 1
        self.draw(self.currentMonth, self.currentYear)

    def drawNextMonth(self):
        if int(self.currentMonth) == 12:
            self.currentMonth = 1
            self.currentYear = int(self.currentYear) + 1
        else:
            self.currentMonth = int(self.currentMonth) + 1
        self.draw(self.currentMonth, self.currentYear)

    def drawPreviousYear(self):
        self.currentYear = int(self.currentYear) - 1
        self.draw(self.currentMonth, self.currentYear)

    def drawNextYear(self):
        self.currentYear = int(self.currentYear) + 1
        self.draw(self.currentMonth, self.currentYear)

class DateField(Composite):

    icon_img = "icon_calendar.gif"
    icon_style = "calendar-img"
    today_text = "Today"
    today_style = "calendar-today-link"

    def __init__(self,format='%d-%m-%Y'):
        self.format = format
        self.tbox = TextBox()
        self.tbox.setVisibleLength(10)
        # assume valid sep is - / . or nothing
        if format.find('-') >= 0:
            self.sep = '-'
        elif format.find('/') >= 0:
            self.sep = '/'
        elif format.find('.') >= 0:
            self.sep = '.'
        else:
            self.sep = ''
        # self.sep = format[2] # is this too presumptious?
        self.calendar = Calendar()
        self.img = Image(self.icon_img)
        self.img.addStyleName(self.icon_style)
        self.calendarLink = HyperlinkImage(self.img)
        self.todayLink = Hyperlink(today_text)
        self.todayLink.addStyleName(today_style)
        #
        # lay it out
        #
        hp = HorizontalPanel()
        hp.setSpacing(2)
        vp = VerticalPanel()
        hp.add(self.tbox)
        vp.add(self.calendarLink)
        vp.add(self.todayLink)
        #vp.add(self.calendar)
        hp.add(vp)

        Composite.__init__(self)
        self.initWidget(hp)
        #
        # done with layout, so now set up some listeners
        #
        self.tbox.addFocusListener(self) # hook to onLostFocus
        self.calendar.addSelectedDateListener(getattr(self,"onDateSelected"))
        self.todayLink.addClickListener(getattr(self,"onTodayClicked"))
        self.calendarLink.addClickListener(getattr(self,"onShowCalendar"))

    def getTextBox(self):
        return self.tbox

    def getCalendar(self):
        return self.calendar

    def setID(self,id):
        self.tbox.setID(id)

    def onDateSelected(self, yyyy, mm, dd):
        secs = time.mktime((int(yyyy),int(mm),int(dd),0,0,0,0,0,-1))
        d = time.strftime(self.format,time.localtime(secs))
        self.tbox.setText(d)

    def onLostFocus(self, sender):
        #
        text = self.tbox.getText().strip()
        # if blank - leave it alone
        if text and len(text) == 8:
            # ok what format do we have? assume ddmmyyyy --> dd-mm-yyyy
            self.tbox.setText( text[0:2] + self.sep + text[2:4] + self.sep + text[4:8] )

    def onFocus(self, sender):
        pass

    def onTodayClicked(self, event):
        today = time.strftime(self.format)
        self.tbox.setText( today )

    def onShowCalendar(self, sender):
        p = CalendarPopup(self.calendar)
        x = self.tbox.getAbsoluteLeft() + 10
        y = self.tbox.getAbsoluteTop() + 10
        p.setPopupPosition(x,y)
        p.show()

Factory.registerClass('pyjamas.ui.Calendar', Calendar)

class CalendarPopup(PopupPanel):
    def __init__(self, c):
        PopupPanel.__init__(self, True)
        p = SimplePanel()
        p.add(c)
        c.show(10,10)
        p.setWidth("100%")
        self.setWidget(p)

Factory.registerClass('pyjamas.ui.CalendarPopup', CalendarPopup)
<|endoftext|>


<commit_before>#!/usr/bin/env python

#
# file: generateChipDtsi.py
#
# author: Copyright (C) 2017 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import argparse
import ast
import collections
import csv
import datetime
import jinja2
import os

#
# Tries to parse string into proper type
#
# param [in] string is the string which will be parsed
#
# return string parsed into proper type
#

def parseString(string):
	try:
		return ast.literal_eval(string)
	except ValueError:
		return string

#
# Handles single row read from CSV file
#
# param [in] jinjaEnvironment is the jinja environment
# param [in] outputPath is the output path for generated file
# param [in] header is the header of CSV row
# param [in] row is the row read from CSV file
#

def handleRow(jinjaEnvironment, outputPath, header, row):
	singles = collections.OrderedDict()
	nodes = collections.OrderedDict()
	for index, element in enumerate(header):
		if element[0] == '' or row[index] == '':
			continue
		if element[1] == '':	# single element
			singles[element[0]] = parseString(row[index])
		else:	# node
			if element[0] not in nodes:
				nodes[element[0]] = collections.OrderedDict()
			if element[1] != 'dtsiTemplate':
				nodes[element[0]][element[1]] = parseString(row[index])
			else:
				nodes[element[0]][element[1]] = jinjaEnvironment.get_template(row[index])

	with open(os.path.join(outputPath, singles['name'] + '.dtsi'), 'w') as dtsiFile:
		jinjaTemplate = jinjaEnvironment.get_template(singles['dtsiTemplate'])
		dtsiFile.write(jinjaTemplate.render(nodes = nodes, **singles))

########################################################################################################################
# main
########################################################################################################################

parser = argparse.ArgumentParser()
parser.add_argument('csvFile', help = 'input CSV file')
parser.add_argument('outputPath', help = 'output path')
arguments = parser.parse_args()

with open(arguments.csvFile, newline = '') as csvFile:

	jinjaEnvironment = jinja2.Environment(trim_blocks = True, lstrip_blocks = True, keep_trailing_newline = True,
			loader = jinja2.FileSystemLoader('.'))
	jinjaEnvironment.globals['year'] = datetime.date.today().year

	csvReader = csv.reader(csvFile)
	header = list(zip(next(csvReader), next(csvReader)))
	for row in csvReader:
		handleRow(jinjaEnvironment, arguments.outputPath, header, row)
<commit_msg>Don't use 'newline' keyword argument for open() in generateChipDtsi.py<commit_after>#!/usr/bin/env python

#
# file: generateChipDtsi.py
#
# author: Copyright (C) 2017 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import argparse
import ast
import collections
import csv
import datetime
import jinja2
import os

#
# Tries to parse string into proper type
#
# param [in] string is the string which will be parsed
#
# return string parsed into proper type
#

def parseString(string):
	try:
		return ast.literal_eval(string)
	except ValueError:
		return string

#
# Handles single row read from CSV file
#
# param [in] jinjaEnvironment is the jinja environment
# param [in] outputPath is the output path for generated file
# param [in] header is the header of CSV row
# param [in] row is the row read from CSV file
#

def handleRow(jinjaEnvironment, outputPath, header, row):
	singles = collections.OrderedDict()
	nodes = collections.OrderedDict()
	for index, element in enumerate(header):
		if element[0] == '' or row[index] == '':
			continue
		if element[1] == '':	# single element
			singles[element[0]] = parseString(row[index])
		else:	# node
			if element[0] not in nodes:
				nodes[element[0]] = collections.OrderedDict()
			if element[1] != 'dtsiTemplate':
				nodes[element[0]][element[1]] = parseString(row[index])
			else:
				nodes[element[0]][element[1]] = jinjaEnvironment.get_template(row[index])

	with open(os.path.join(outputPath, singles['name'] + '.dtsi'), 'w') as dtsiFile:
		jinjaTemplate = jinjaEnvironment.get_template(singles['dtsiTemplate'])
		dtsiFile.write(jinjaTemplate.render(nodes = nodes, **singles))

########################################################################################################################
# main
########################################################################################################################

parser = argparse.ArgumentParser()
parser.add_argument('csvFile', help = 'input CSV file')
parser.add_argument('outputPath', help = 'output path')
arguments = parser.parse_args()

with open(arguments.csvFile) as csvFile:

	jinjaEnvironment = jinja2.Environment(trim_blocks = True, lstrip_blocks = True, keep_trailing_newline = True,
			loader = jinja2.FileSystemLoader('.'))
	jinjaEnvironment.globals['year'] = datetime.date.today().year

	csvReader = csv.reader(csvFile)
	header = list(zip(next(csvReader), next(csvReader)))
	for row in csvReader:
		handleRow(jinjaEnvironment, arguments.outputPath, header, row)
<|endoftext|>


<commit_before>"""This is the docstring for the example.py module.  Modules names should
have short, all-lowercase names.  The module name may have underscores if
this improves readability.
Every module should have a docstring at the very top of the file.  The
module's docstring may extend over multiple lines.  If your docstring does
extend over multiple lines, the closing three quotation marks must be on
a line by itself, preferably preceded by a blank line."""

__copyright__ = "tba"
__license__ = "tba"
__author__ = "tba"

# work around set path
import sys
sys.path.append('/home/dozeumbuw/Dokumente/ZNES/open_eGo/Coding/eTraGo/eTraGo/etrago')

import numpy as np
np.random.seed()
from egopowerflow.tools.tools import oedb_session
from egopowerflow.tools.io import NetworkScenario
import time
from egopowerflow.tools.plot import (plot_line_loading, plot_stacked_gen,
                                     add_coordinates, curtailment, gen_dist,
                                     storage_distribution)
from extras.utilities import load_shedding, data_manipulation_sh, results_to_csv
from cluster.networkclustering import busmap_from_psql, cluster_on_extra_high_voltage

args = {'network_clustering':False,
        'db': 'oedb2', # db session   ## oedb2
        'gridversion':None, #None for model_draft or Version number (e.g. v0.2.10) for grid schema
        'method': 'lopf', # lopf or pf
        'start_h': 2301,
        'end_h' : 2302,
        'scn_name': 'SH Status Quo',
        'ormcls_prefix': 'EgoGridPfHv', #if gridversion:'version-number' then 'EgoPfHv', if gridversion:None then 'EgoGridPfHv'
        'outfile': '/path', # state if and where you want to save pyomo's lp file
        'results': '/path', # state if and where you want to save results as csv
        'solver': 'gurobi', #glpk, cplex or gurobi
        'branch_capacity_factor': 1, #to globally extend or lower branch capacities
        'storage_extendable':True,
        'load_shedding':True,
        'generator_noise':False}

def etrago(args):
    session = oedb_session(args['db'])

    # additional arguments cfgpath, version, prefix
    scenario = NetworkScenario(session,
                               version=args['gridversion'],
                               prefix=args['ormcls_prefix'],
                               method=args['method'],
                               start_h=args['start_h'],
                               end_h=args['end_h'],
                               scn_name=args['scn_name'])

    network = scenario.build_network()

    # add coordinates
    network = add_coordinates(network)

    if args['branch_capacity_factor']:
        network.lines.s_nom = network.lines.s_nom*args['branch_capacity_factor']
        network.transformers.s_nom = network.transformers.s_nom*args['branch_capacity_factor']

    if args['generator_noise']:
        # add random noise to all generators with marginal_cost of 0.
        network.generators.marginal_cost[ network.generators.marginal_cost
         == 0] = abs(np.random.normal(0,0.00001,sum(network.generators.marginal_cost == 0)))

    if args['storage_extendable']:
        # set virtual storages to be extendable
        network.storage_units.p_nom_extendable = True
        # set virtual storage costs with regards to snapshot length
        network.storage_units.capital_cost = (network.storage_units.capital_cost /
        (8760//(args['end_h']-args['start_h']+1)))

    # for SH scenario run do data preperation:
    if args['scn_name'] == 'SH Status Quo':
        data_manipulation_sh(network)

    #load shedding in order to hunt infeasibilities
    if args['load_shedding']:
    	load_shedding(network)

    # network clustering
    if args['network_clustering']:
        network.generators.control="PV"
        busmap = busmap_from_psql(network, session, scn_name=args['scn_name'])
        network = cluster_on_extra_high_voltage(network, busmap, with_time=True)

    # start powerflow calculations
    x = time.time()
    network.lopf(scenario.timeindex, solver_name=args['solver'])
    y = time.time()
    z = (y - x) / 60 # z is time for lopf in minutes

    return

<<<<<<< HEAD
test = etrago(args)
=======
# start powerflow calculations
if args['method'] == 'lopf':
    x = time.time()
    network.lopf(scenario.timeindex, solver_name=args['solver'])
    y = time.time()
    z = (y - x) / 60 # z is time for lopf in minutes
>>>>>>> 887dba8def8a4218aa6a6be2b0497d80263532e3

# write results
network.model.write(args['outfile'], io_options={'symbolic_solver_labels':
                                                     True})
#results_to_csv(network, args['results'])

# plots

# make a line loading plot
#plot_line_loading(network)

# plot stacked sum of nominal power for each generator type and timestep
#plot_stacked_gen(network, resolution="MW")

# plot to show extendable storages
#storage_distribution(network)

# close session
#session.close()
<commit_msg>changed test to network<commit_after>"""This is the docstring for the example.py module.  Modules names should
have short, all-lowercase names.  The module name may have underscores if
this improves readability.
Every module should have a docstring at the very top of the file.  The
module's docstring may extend over multiple lines.  If your docstring does
extend over multiple lines, the closing three quotation marks must be on
a line by itself, preferably preceded by a blank line."""

__copyright__ = "tba"
__license__ = "tba"
__author__ = "tba"

# work around set path
import sys
sys.path.append('/home/dozeumbuw/Dokumente/ZNES/open_eGo/Coding/eTraGo/eTraGo/etrago')

import numpy as np
np.random.seed()
from egopowerflow.tools.tools import oedb_session
from egopowerflow.tools.io import NetworkScenario
import time
from egopowerflow.tools.plot import (plot_line_loading, plot_stacked_gen,
                                     add_coordinates, curtailment, gen_dist,
                                     storage_distribution)
from extras.utilities import load_shedding, data_manipulation_sh, results_to_csv
from cluster.networkclustering import busmap_from_psql, cluster_on_extra_high_voltage

args = {'network_clustering':False,
        'db': 'oedb2', # db session   ## oedb2
        'gridversion':None, #None for model_draft or Version number (e.g. v0.2.10) for grid schema
        'method': 'lopf', # lopf or pf
        'start_h': 2301,
        'end_h' : 2302,
        'scn_name': 'SH Status Quo',
        'ormcls_prefix': 'EgoGridPfHv', #if gridversion:'version-number' then 'EgoPfHv', if gridversion:None then 'EgoGridPfHv'
        'outfile': '/path', # state if and where you want to save pyomo's lp file
        'results': '/path', # state if and where you want to save results as csv
        'solver': 'gurobi', #glpk, cplex or gurobi
        'branch_capacity_factor': 1, #to globally extend or lower branch capacities
        'storage_extendable':True,
        'load_shedding':True,
        'generator_noise':False}

def etrago(args):
    session = oedb_session(args['db'])

    # additional arguments cfgpath, version, prefix
    scenario = NetworkScenario(session,
                               version=args['gridversion'],
                               prefix=args['ormcls_prefix'],
                               method=args['method'],
                               start_h=args['start_h'],
                               end_h=args['end_h'],
                               scn_name=args['scn_name'])

    network = scenario.build_network()

    # add coordinates
    network = add_coordinates(network)

    if args['branch_capacity_factor']:
        network.lines.s_nom = network.lines.s_nom*args['branch_capacity_factor']
        network.transformers.s_nom = network.transformers.s_nom*args['branch_capacity_factor']

    if args['generator_noise']:
        # add random noise to all generators with marginal_cost of 0.
        network.generators.marginal_cost[ network.generators.marginal_cost
         == 0] = abs(np.random.normal(0,0.00001,sum(network.generators.marginal_cost == 0)))

    if args['storage_extendable']:
        # set virtual storages to be extendable
        network.storage_units.p_nom_extendable = True
        # set virtual storage costs with regards to snapshot length
        network.storage_units.capital_cost = (network.storage_units.capital_cost /
        (8760//(args['end_h']-args['start_h']+1)))

    # for SH scenario run do data preperation:
    if args['scn_name'] == 'SH Status Quo':
        data_manipulation_sh(network)

    #load shedding in order to hunt infeasibilities
    if args['load_shedding']:
    	load_shedding(network)

    # network clustering
    if args['network_clustering']:
        network.generators.control="PV"
        busmap = busmap_from_psql(network, session, scn_name=args['scn_name'])
        network = cluster_on_extra_high_voltage(network, busmap, with_time=True)

    # start powerflow calculations
    x = time.time()
    network.lopf(scenario.timeindex, solver_name=args['solver'])
    y = time.time()
    z = (y - x) / 60 # z is time for lopf in minutes

    return network

network = etrago(args)

# start powerflow calculations
if args['method'] == 'lopf':
    x = time.time()
    network.lopf(scenario.timeindex, solver_name=args['solver'])
    y = time.time()
    z = (y - x) / 60 # z is time for lopf in minutes

# write results
#network.model.write(args['outfile'], io_options={'symbolic_solver_labels':
#                                                      True})
#results_to_csv(network, args['results'])

# plots

# make a line loading plot
#plot_line_loading(network)

# plot stacked sum of nominal power for each generator type and timestep
#plot_stacked_gen(network, resolution="MW")

# plot to show extendable storages
#storage_distribution(network)

# close session
#session.close()
<|endoftext|>


<commit_before># Copyright (C) 2010-2021  Vincent Pelletier <plr.vincent@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# pylint: disable=invalid-name, missing-docstring, too-many-public-methods

from ctypes import pointer
import itertools
import sys
import unittest
import usb1
from . import libusb1

buff_len = 1024
buffer_base = [x % 256 for x in range(buff_len)]
buff = bytes(buffer_base)
other_buff = bytes(reversed(buffer_base))
bytearray_buff = bytearray(buffer_base)

class USBContext(usb1.USBContext):
    def open(self):
        try:
            return super().open()
        except usb1.USBError:
            raise unittest.SkipTest(
                'usb1.USBContext() fails - no USB bus on system ?'
            )

class USBTransferTests(unittest.TestCase):
    @staticmethod
    def getTransfer(iso_packets=0):
        # Dummy handle
        return usb1.USBTransfer(
            context=None,
            handle=pointer(libusb1.libusb_device_handle()),
            iso_packets=iso_packets,
            before_submit=lambda x: None,
            after_completion=lambda x: None,
            registerFinalizer=lambda handle, finalizer: None,
            unregisterFinalizer=lambda handle: None,
        )

    @staticmethod
    def testGetVersion():
        """
        Just testing getVersion doesn't raise...
        """
        usb1.getVersion()

    @staticmethod
    def testHasCapability():
        """
        Just testing hasCapability doesn't raise...
        """
        usb1.hasCapability(usb1.CAP_HAS_CAPABILITY)

    def testSetControl(self):
        """
        Simplest test: feed some data, must not raise.
        """
        transfer = self.getTransfer()
        request_type = usb1.TYPE_STANDARD
        request = usb1.REQUEST_GET_STATUS
        value = 0
        index = 0
        def callback(transfer):
            pass
        user_data = []
        timeout = 1000

        # All provided, buffer variant
        transfer.setControl(
            request_type, request, value, index, buff,
            callback=callback, user_data=user_data, timeout=timeout)
        self.assertEqual(buff, transfer.getBuffer())
        self.assertRaises(ValueError, transfer.setBuffer, buff)
        # All provided, buffer length variant
        transfer.setControl(
            request_type, request, value, index, buff_len,
            callback=callback, user_data=user_data, timeout=timeout)
        # No timeout
        transfer.setControl(
            request_type, request, value, index, buff,
            callback=callback, user_data=user_data)
        # No user data
        transfer.setControl(
            request_type, request, value, index, buff, callback=callback)
        # No callback
        transfer.setControl(request_type, request, value, index, buff)

    def _testTransferSetter(self, transfer, setter_id):
        endpoint = 0x81
        def callback(transfer):
            pass
        user_data = []
        timeout = 1000
        setter = getattr(transfer, setter_id)
        # All provided, buffer variant
        setter(
            endpoint, buff, callback=callback, user_data=user_data,
            timeout=timeout)
        self.assertEqual(buff, transfer.getBuffer())
        transfer.setBuffer(other_buff)
        self.assertEqual(other_buff, transfer.getBuffer())
        transfer.setBuffer(bytearray_buff)
        self.assertEqual(bytearray_buff, transfer.getBuffer())
        transfer.setBuffer(buff_len)
        self.assertEqual(buff_len, len(transfer.getBuffer()))
        # All provided, buffer length variant
        setter(
            endpoint, buff_len, callback=callback, user_data=user_data,
            timeout=timeout)
        # No timeout
        setter(endpoint, buff, callback=callback, user_data=user_data)
        # No user data
        setter(endpoint, buff, callback=callback)
        # No callback
        setter(endpoint, buff)

    def testSetBulk(self):
        """
        Simplest test: feed some data, must not raise.
        Also, test setBuffer/getBuffer.
        """
        self._testTransferSetter(self.getTransfer(), 'setBulk')

    def testSetInterrupt(self):
        """
        Simplest test: feed some data, must not raise.
        Also, test setBuffer/getBuffer.
        """
        self._testTransferSetter(self.getTransfer(), 'setInterrupt')

    def testSetIsochronous(self):
        """
        Simplest test: feed some data, must not raise.
        Also, test setBuffer/getBuffer/getISOBufferList/iterISO.
        """
        iso_transfer_count = 16
        transfer = self.getTransfer(iso_transfer_count)
        self._testTransferSetter(transfer, 'setIsochronous')
        # Returns whole buffers
        self.assertEqual(
            bytearray(itertools.chain(*transfer.getISOBufferList())),
            buff,
        )
        # Returns actually transfered data, so here nothing
        self.assertEqual(bytearray(
            itertools.chain(*[x for _, x in transfer.iterISO()])),
            bytearray(),
        )
        # Fake reception of whole transfers
        c_transfer = getattr(
          transfer,
          '_' + transfer.__class__.__name__ + '__transfer'
        )
        for iso_metadata in libusb1.get_iso_packet_list(c_transfer):
            iso_metadata.actual_length = iso_metadata.length
        # Now iterISO returns everythig
        self.assertEqual(bytearray(
            itertools.chain(*[x for _, x in transfer.iterISO()])),
            buff,
        )

    def testSetGetCallback(self):
        transfer = self.getTransfer()
        def callback(transfer):
            pass
        transfer.setCallback(callback)
        got_callback = transfer.getCallback()
        self.assertEqual(callback, got_callback)

    def _testDescriptors(self, get_extra=False):
        """
        Test descriptor walk.
        Needs any usb device, which won't be opened.
        """
        with USBContext() as context:
            device_list = context.getDeviceList(skip_on_error=True)
            found = False
            seen_extra = False
            for device in device_list:
                device.getBusNumber()
                device.getPortNumber()
                device.getPortNumberList()
                device.getDeviceAddress()
                for settings in device.iterSettings():
                    for endpoint in settings:
                        pass
                for configuration in device.iterConfigurations():
                    if get_extra and len(configuration.getExtra()) > 0:
                        seen_extra = True
                    for interface in configuration:
                        for settings in interface:
                            if get_extra and len(settings.getExtra()) > 0:
                                seen_extra = True
                            for endpoint in settings:
                                if get_extra and len(endpoint.getExtra()) > 0:
                                    seen_extra = True
                                found = True
            if not found:
                raise unittest.SkipTest('descriptor walk test did not complete')
            if get_extra and not seen_extra:
                raise unittest.SkipTest('did not see any extra descriptors')

    def testDescriptors(self):
        self._testDescriptors()

    def testDescriptorsWithExtra(self):
        self._testDescriptors(get_extra=True)

    def testDefaultEnumScope(self):
        """
        Enum instances must only affect the scope they are created in.
        """
        ENUM_NAME = 'THE_ANSWER'
        ENUM_VALUE = 42
        local_dict = locals()
        global_dict = globals()
        self.assertEqual(local_dict.get(ENUM_NAME), None)
        self.assertEqual(global_dict.get(ENUM_NAME), None)
        self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)
        # pylint: disable=unused-variable
        TEST_ENUM = libusb1.Enum({ENUM_NAME: ENUM_VALUE})
        # pylint: enable=unused-variable
        self.assertEqual(local_dict.get(ENUM_NAME), ENUM_VALUE)
        self.assertEqual(global_dict.get(ENUM_NAME), None)
        self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)

    def testExplicitEnumScope(self):
        """
        Enum instances must only affect the scope they are created in.
        """
        ENUM_NAME = 'THE_ANSWER'
        ENUM_VALUE = 42
        local_dict = locals()
        global_dict = globals()
        self.assertEqual(local_dict.get(ENUM_NAME), None)
        self.assertEqual(global_dict.get(ENUM_NAME), None)
        self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)
        # pylint: disable=unused-variable
        TEST_ENUM = libusb1.Enum({ENUM_NAME: ENUM_VALUE}, global_dict)
        # pylint: enable=unused-variable
        try:
            self.assertEqual(local_dict.get(ENUM_NAME), None)
            self.assertEqual(global_dict.get(ENUM_NAME), ENUM_VALUE)
            self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)
        finally:
            del global_dict[ENUM_NAME]

    def testImplicitUSBContextOpening(self):
        """
        Test pre-1.5 API backward compatibility.
        First method call which needs a context succeeds.
        Further calls return None.
        """
        context = USBContext() # Deprecated
        try:
            fd_list = context.getPollFDList()
        except NotImplementedError:
            raise unittest.SkipTest('libusb without file descriptor events')
        self.assertNotEqual(fd_list, None)
        context.exit() # Deprecated
        self.assertEqual(context.getPollFDList(), None)

    def testHasVersion(self):
        # Property is present and non-empty
        self.assertTrue(usb1.__version__)

if __name__ == '__main__':
    unittest.main()
<commit_msg>usb1.testUSB1: Use a NULL-pointer instead as dummy device handle.<commit_after># Copyright (C) 2010-2021  Vincent Pelletier <plr.vincent@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# pylint: disable=invalid-name, missing-docstring, too-many-public-methods

from ctypes import pointer
import itertools
import sys
import unittest
import usb1
from . import libusb1

buff_len = 1024
buffer_base = [x % 256 for x in range(buff_len)]
buff = bytes(buffer_base)
other_buff = bytes(reversed(buffer_base))
bytearray_buff = bytearray(buffer_base)

class USBContext(usb1.USBContext):
    def open(self):
        try:
            return super().open()
        except usb1.USBError:
            raise unittest.SkipTest(
                'usb1.USBContext() fails - no USB bus on system ?'
            )

class USBTransferTests(unittest.TestCase):
    @staticmethod
    def getTransfer(iso_packets=0):
        # Dummy handle
        return usb1.USBTransfer(
            context=None,
            handle=None,
            iso_packets=iso_packets,
            before_submit=lambda x: None,
            after_completion=lambda x: None,
            registerFinalizer=lambda handle, finalizer: None,
            unregisterFinalizer=lambda handle: None,
        )

    @staticmethod
    def testGetVersion():
        """
        Just testing getVersion doesn't raise...
        """
        usb1.getVersion()

    @staticmethod
    def testHasCapability():
        """
        Just testing hasCapability doesn't raise...
        """
        usb1.hasCapability(usb1.CAP_HAS_CAPABILITY)

    def testSetControl(self):
        """
        Simplest test: feed some data, must not raise.
        """
        transfer = self.getTransfer()
        request_type = usb1.TYPE_STANDARD
        request = usb1.REQUEST_GET_STATUS
        value = 0
        index = 0
        def callback(transfer):
            pass
        user_data = []
        timeout = 1000

        # All provided, buffer variant
        transfer.setControl(
            request_type, request, value, index, buff,
            callback=callback, user_data=user_data, timeout=timeout)
        self.assertEqual(buff, transfer.getBuffer())
        self.assertRaises(ValueError, transfer.setBuffer, buff)
        # All provided, buffer length variant
        transfer.setControl(
            request_type, request, value, index, buff_len,
            callback=callback, user_data=user_data, timeout=timeout)
        # No timeout
        transfer.setControl(
            request_type, request, value, index, buff,
            callback=callback, user_data=user_data)
        # No user data
        transfer.setControl(
            request_type, request, value, index, buff, callback=callback)
        # No callback
        transfer.setControl(request_type, request, value, index, buff)

    def _testTransferSetter(self, transfer, setter_id):
        endpoint = 0x81
        def callback(transfer):
            pass
        user_data = []
        timeout = 1000
        setter = getattr(transfer, setter_id)
        # All provided, buffer variant
        setter(
            endpoint, buff, callback=callback, user_data=user_data,
            timeout=timeout)
        self.assertEqual(buff, transfer.getBuffer())
        transfer.setBuffer(other_buff)
        self.assertEqual(other_buff, transfer.getBuffer())
        transfer.setBuffer(bytearray_buff)
        self.assertEqual(bytearray_buff, transfer.getBuffer())
        transfer.setBuffer(buff_len)
        self.assertEqual(buff_len, len(transfer.getBuffer()))
        # All provided, buffer length variant
        setter(
            endpoint, buff_len, callback=callback, user_data=user_data,
            timeout=timeout)
        # No timeout
        setter(endpoint, buff, callback=callback, user_data=user_data)
        # No user data
        setter(endpoint, buff, callback=callback)
        # No callback
        setter(endpoint, buff)

    def testSetBulk(self):
        """
        Simplest test: feed some data, must not raise.
        Also, test setBuffer/getBuffer.
        """
        self._testTransferSetter(self.getTransfer(), 'setBulk')

    def testSetInterrupt(self):
        """
        Simplest test: feed some data, must not raise.
        Also, test setBuffer/getBuffer.
        """
        self._testTransferSetter(self.getTransfer(), 'setInterrupt')

    def testSetIsochronous(self):
        """
        Simplest test: feed some data, must not raise.
        Also, test setBuffer/getBuffer/getISOBufferList/iterISO.
        """
        iso_transfer_count = 16
        transfer = self.getTransfer(iso_transfer_count)
        self._testTransferSetter(transfer, 'setIsochronous')
        # Returns whole buffers
        self.assertEqual(
            bytearray(itertools.chain(*transfer.getISOBufferList())),
            buff,
        )
        # Returns actually transfered data, so here nothing
        self.assertEqual(bytearray(
            itertools.chain(*[x for _, x in transfer.iterISO()])),
            bytearray(),
        )
        # Fake reception of whole transfers
        c_transfer = getattr(
          transfer,
          '_' + transfer.__class__.__name__ + '__transfer'
        )
        for iso_metadata in libusb1.get_iso_packet_list(c_transfer):
            iso_metadata.actual_length = iso_metadata.length
        # Now iterISO returns everythig
        self.assertEqual(bytearray(
            itertools.chain(*[x for _, x in transfer.iterISO()])),
            buff,
        )

    def testSetGetCallback(self):
        transfer = self.getTransfer()
        def callback(transfer):
            pass
        transfer.setCallback(callback)
        got_callback = transfer.getCallback()
        self.assertEqual(callback, got_callback)

    def _testDescriptors(self, get_extra=False):
        """
        Test descriptor walk.
        Needs any usb device, which won't be opened.
        """
        with USBContext() as context:
            device_list = context.getDeviceList(skip_on_error=True)
            found = False
            seen_extra = False
            for device in device_list:
                device.getBusNumber()
                device.getPortNumber()
                device.getPortNumberList()
                device.getDeviceAddress()
                for settings in device.iterSettings():
                    for endpoint in settings:
                        pass
                for configuration in device.iterConfigurations():
                    if get_extra and len(configuration.getExtra()) > 0:
                        seen_extra = True
                    for interface in configuration:
                        for settings in interface:
                            if get_extra and len(settings.getExtra()) > 0:
                                seen_extra = True
                            for endpoint in settings:
                                if get_extra and len(endpoint.getExtra()) > 0:
                                    seen_extra = True
                                found = True
            if not found:
                raise unittest.SkipTest('descriptor walk test did not complete')
            if get_extra and not seen_extra:
                raise unittest.SkipTest('did not see any extra descriptors')

    def testDescriptors(self):
        self._testDescriptors()

    def testDescriptorsWithExtra(self):
        self._testDescriptors(get_extra=True)

    def testDefaultEnumScope(self):
        """
        Enum instances must only affect the scope they are created in.
        """
        ENUM_NAME = 'THE_ANSWER'
        ENUM_VALUE = 42
        local_dict = locals()
        global_dict = globals()
        self.assertEqual(local_dict.get(ENUM_NAME), None)
        self.assertEqual(global_dict.get(ENUM_NAME), None)
        self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)
        # pylint: disable=unused-variable
        TEST_ENUM = libusb1.Enum({ENUM_NAME: ENUM_VALUE})
        # pylint: enable=unused-variable
        self.assertEqual(local_dict.get(ENUM_NAME), ENUM_VALUE)
        self.assertEqual(global_dict.get(ENUM_NAME), None)
        self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)

    def testExplicitEnumScope(self):
        """
        Enum instances must only affect the scope they are created in.
        """
        ENUM_NAME = 'THE_ANSWER'
        ENUM_VALUE = 42
        local_dict = locals()
        global_dict = globals()
        self.assertEqual(local_dict.get(ENUM_NAME), None)
        self.assertEqual(global_dict.get(ENUM_NAME), None)
        self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)
        # pylint: disable=unused-variable
        TEST_ENUM = libusb1.Enum({ENUM_NAME: ENUM_VALUE}, global_dict)
        # pylint: enable=unused-variable
        try:
            self.assertEqual(local_dict.get(ENUM_NAME), None)
            self.assertEqual(global_dict.get(ENUM_NAME), ENUM_VALUE)
            self.assertEqual(getattr(libusb1, ENUM_NAME, None), None)
        finally:
            del global_dict[ENUM_NAME]

    def testImplicitUSBContextOpening(self):
        """
        Test pre-1.5 API backward compatibility.
        First method call which needs a context succeeds.
        Further calls return None.
        """
        context = USBContext() # Deprecated
        try:
            fd_list = context.getPollFDList()
        except NotImplementedError:
            raise unittest.SkipTest('libusb without file descriptor events')
        self.assertNotEqual(fd_list, None)
        context.exit() # Deprecated
        self.assertEqual(context.getPollFDList(), None)

    def testHasVersion(self):
        # Property is present and non-empty
        self.assertTrue(usb1.__version__)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># Copyright 2006 James Tauber and contributors
# Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pyjamas import DOM
from pyjamas import Factory
from __pyjamas__ import console

from Composite import Composite
from DeckPanel import DeckPanel
from VerticalPanel import VerticalPanel
from Panel import PanelBase
from TabBar import TabBar

class TabPanel(PanelBase, Composite):
    def __init__(self, tabBar=None, **kwargs):
        self.children = [] # TODO: can self.children be used instead?
        self.tab_names = {}
        self.deck = kwargs.pop('Deck', None)
        if self.deck is None:
            self.deck = DeckPanel(StyleName="gwt-TabPanelBottom")
        if tabBar is None:
            self.tabBar = TabBar()
        else:
            self.tabBar = tabBar
        self.tabListeners = []

        # this is awkward: VerticalPanel is the composite,
        # so we get the element here, and pass it in to VerticalPanel.
        element = kwargs.pop('Element', None)

        panel = VerticalPanel(Element=element)
        panel.add(self.tabBar)
        if self.deck.getParent() is None:
            panel.add(self.deck)
            panel.setCellHeight(self.deck, "100%")
        self.tabBar.setWidth("100%")
        self.tabBar.addTabListener(self)

        kwargs['StyleName'] = kwargs.get('StyleName', "gwt-TabPanel")

        PanelBase.__init__(self)
        Composite.__init__(self, panel, **kwargs)

    def add(self, widget, tabText=None, asHTML=False, name=None):
        """ tabText=None now means insert a spacer, pushed out at 100%
            width so that any subsequent tabs added will be pushed to
            the right hand side.

            name refers to an optional name (string) where the tab can
            be removed by name, if desired, using TabBar.remove.
        """
        self.insert(widget, tabText, asHTML, self.getWidgetCount(), name)

    def addTabListener(self, listener):
        self.tabListeners.append(listener)

    def getDeckPanel(self):
        return self.deck

    def getTabBar(self):
        return self.tabBar

    def insert(self, widget, tabText, asHTML=False, beforeIndex=None,
                                      name=None):
        if beforeIndex is None:
            beforeIndex = asHTML
            asHTML = False

        self.children.insert(beforeIndex, widget)
        if name is not None:
            self.tab_names[name] = widget
        self.tabBar.insertTab(tabText, asHTML, beforeIndex)
        self.deck.insert(widget, beforeIndex)

    def onBeforeTabSelected(self, sender, tabIndex):
        for listener in self.tabListeners:
            if not listener.onBeforeTabSelected(sender, tabIndex):
                return False
        return True

    def onTabSelected(self, sender, tabIndex):
        self.deck.showWidget(tabIndex)
        for listener in self.tabListeners:
            listener.onTabSelected(sender, tabIndex)

    def remove(self, widget):
        """ widget to remove can be by integer, a widget in the panel,
            or by name (string).  if by name, the name has to be one
            which was given to add or insert
        """
        if isinstance(widget, str):
            widget = self.tab_names[widget]
        elif isinstance(widget, int):
            widget = self.getWidget(widget)

        for (k, w) in self.tab_names.items():
            if widget == w:
                self.tab_names.pop(k)
                break

        index = self.getWidgetIndex(widget)
        if index == -1:
            return False

        self.children.remove(widget)
        self.tabBar.removeTab(index)
        self.deck.remove(widget)
        return True

    def removeTabListener(self, listener):
        self.tabListeners.remove(listener)

    def selectTab(self, index):
        self.tabBar.selectTab(index)

Factory.registerClass('pyjamas.ui.TabPanel', 'TabPanel', TabPanel)

<commit_msg>extend selectTab to allow tab by widget or name<commit_after># Copyright 2006 James Tauber and contributors
# Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pyjamas import DOM
from pyjamas import Factory
from __pyjamas__ import console

from Composite import Composite
from DeckPanel import DeckPanel
from VerticalPanel import VerticalPanel
from Panel import PanelBase
from TabBar import TabBar

class TabPanel(PanelBase, Composite):
    def __init__(self, tabBar=None, **kwargs):
        self.children = [] # TODO: can self.children be used instead?
        self.tab_names = {}
        self.deck = kwargs.pop('Deck', None)
        if self.deck is None:
            self.deck = DeckPanel(StyleName="gwt-TabPanelBottom")
        if tabBar is None:
            self.tabBar = TabBar()
        else:
            self.tabBar = tabBar
        self.tabListeners = []

        # this is awkward: VerticalPanel is the composite,
        # so we get the element here, and pass it in to VerticalPanel.
        element = kwargs.pop('Element', None)

        panel = VerticalPanel(Element=element)
        panel.add(self.tabBar)
        if self.deck.getParent() is None:
            panel.add(self.deck)
            panel.setCellHeight(self.deck, "100%")
        self.tabBar.setWidth("100%")
        self.tabBar.addTabListener(self)

        kwargs['StyleName'] = kwargs.get('StyleName', "gwt-TabPanel")

        PanelBase.__init__(self)
        Composite.__init__(self, panel, **kwargs)

    def add(self, widget, tabText=None, asHTML=False, name=None):
        """ tabText=None now means insert a spacer, pushed out at 100%
            width so that any subsequent tabs added will be pushed to
            the right hand side.

            name refers to an optional name (string) where the tab can
            be removed by name, if desired, using TabBar.remove.
        """
        self.insert(widget, tabText, asHTML, self.getWidgetCount(), name)

    def addTabListener(self, listener):
        self.tabListeners.append(listener)

    def getDeckPanel(self):
        return self.deck

    def getTabBar(self):
        return self.tabBar

    def insert(self, widget, tabText, asHTML=False, beforeIndex=None,
                                      name=None):
        if beforeIndex is None:
            beforeIndex = asHTML
            asHTML = False

        self.children.insert(beforeIndex, widget)
        if name is not None:
            self.tab_names[name] = widget
        self.tabBar.insertTab(tabText, asHTML, beforeIndex)
        self.deck.insert(widget, beforeIndex)

    def onBeforeTabSelected(self, sender, tabIndex):
        for listener in self.tabListeners:
            if not listener.onBeforeTabSelected(sender, tabIndex):
                return False
        return True

    def onTabSelected(self, sender, tabIndex):
        self.deck.showWidget(tabIndex)
        for listener in self.tabListeners:
            listener.onTabSelected(sender, tabIndex)

    def remove(self, widget):
        """ widget to remove can be by integer, a widget in the panel,
            or by name (string).  if by name, the name has to be one
            which was given to add or insert
        """
        if isinstance(widget, str):
            widget = self.tab_names[widget]
        elif isinstance(widget, int):
            widget = self.getWidget(widget)

        for (k, w) in self.tab_names.items():
            if widget == w:
                self.tab_names.pop(k)
                break

        index = self.getWidgetIndex(widget)
        if index == -1:
            return False

        self.children.remove(widget)
        self.tabBar.removeTab(index)
        self.deck.remove(widget)
        return True

    def removeTabListener(self, listener):
        self.tabListeners.remove(listener)

    def selectTab(self, index):
        """ manual tab selection. tab can be selected:
             * by index,
             * by tab name (matching TabPanel.insert name arg if given)
             * or by widget.
        """
        if isinstance(index, str):
            index = self.tab_names[index]
        elif not isinstance(index, int):
            index = self.getWidgetIndex(index)

        self.tabBar.selectTab(index)

Factory.registerClass('pyjamas.ui.TabPanel', 'TabPanel', TabPanel)

<|endoftext|>


<commit_before>'''
Generate a UUID to use as an api_key & remind where to put it
'''
import uuid
new_key=uuid.uuid4()

print "NEW API KEY:{0}".format(new_key)

print "Place in the S3 token_auth file wrapped as so:"
print ""
print "#<COMMENT WITH USER ID -- so know who's using the key>"
print "if ($http_X_Authentication_Token = '{0}') {{".format(new_key)
print "    set $not_authed 0;"
print "}"
print "\n\n"
print "Then rebuild the solr index beanstalk to pickup the new key from S3"
<commit_msg>make work with python3<commit_after>'''
Generate a UUID to use as an api_key & remind where to put it
'''
import uuid
new_key=uuid.uuid4()

print("NEW API KEY:{0}".format(new_key))

print("Place in the S3 token_auth file wrapped as so:")
print("")
print("#<COMMENT WITH USER ID -- so know who's using the key>")
print("if ($http_X_Authentication_Token = '{0}') {{".format(new_key))
print("    set $not_authed 0;")
print("}")
print("\n\n")
print("Then rebuild the solr index beanstalk to pickup the new key from S3")
<|endoftext|>


<commit_before>#! /usr/bin/env python

from doit.task import dict_to_task
from doit.cmd_base import TaskLoader
from doit.doit_cmd import DoitMain
import argparse
import os
import sys

parser = argparse.ArgumentParser(description='Map GAM-seq reads and create BigWigs and fastQC files')
parser.add_argument('input_fastqs', metavar='INPUT_FASTQ', nargs='*', help='One or more input fastq files.')
parser.add_argument('-g','--genome_file', metavar='GENOME_FILE', help='File containing chromosome names and lengths')
parser.add_argument('-p','--num_process', metavar='NUM_PROCESSES', type=int, default=1, help='Number of tasks to execute in parallel')
parser.add_argument('-o','--output_dir', metavar='OUPUT_DIRECTORY', help='Write segmentation, matrix etc. to this directory')
parser.add_argument('-w','--window_sizes', metavar='WINDOW_SIZE', default=[1000,5000,10000,50000,100000,500000], type=int, nargs='+', help='File containing chromosome names and lengths')

args = parser.parse_args()

def get_mapping_tasks(input_fastq):
    """Take the path to one bamfile and return the doit tasks which would convert it to a BigWig"""

    output_dir = os.path.dirname(input_fastq)
    basename = os.path.splitext(os.path.basename(input_fastq))[0]

    def with_extension(extension): return os.path.join(output_dir, basename + extension)

    tasks_to_run = []

    tasks_to_run.append({
                "name"     : 'Mapping fastq',
                "actions"  : ['bowtie2 -x genome -U %(dependencies)s | sed \'/XS : /d\' | samtools view -F 4 -bS - > %(targets)s'],
                "targets"  : [with_extension(".bam")],
                "file_dep" : [input_fastq],
                })

    tasks_to_run.append({
                "name"     : 'Sorting bam',
                "actions"  : ['samtools sort %(dependencies)s %(targets)s',
                              'mv %(targets)s.bam %(targets)s'],
                "targets"  : [with_extension(".sorted.bam")],
                "file_dep" : [with_extension(".bam")],
                })

    tasks_to_run.append({
                "name"     : 'Removing duplications',
                "actions"  : ['samtools rmdup -s %(dependencies)s %(targets)s',],
                "targets"  : [with_extension(".rmdup.bam")],
                "file_dep" : [with_extension(".sorted.bam")],
                })

    tasks_to_run.append({
                "name"     : 'Converting bam to bedgraph',
                "actions"  : ['genomeCoverageBed -bg -ibam %(dependencies)s -g %(genome_file)s > %(targets)s'],
                "targets"  : [with_extension(".bedgraph")],
                "file_dep" : [with_extension(".rmdup.bam")],
                })

    tasks_to_run.append({
                "name"     : 'Converting bedgraph to bigwig',
                "actions"  : ['bedGraphToBigWig %(dependencies)s %(genome_file)s %(targets)s'],
                "targets"  : [with_extension(".bw")],
                "file_dep" : [with_extension(".bedgraph")],
                })

    tasks_to_run.append({
                'name'     : 'Indexing bam',
                'actions'  : ['samtools index %(dependencies)s',],
                'targets'  : [with_extension(".rmdup.bam.bai")],
                'file_dep' : [with_extension(".rmdup.bam")],
                })

    for task in tasks_to_run:
        task["name"] = task["name"] + " (%s)" % task["targets"][0]

    return tasks_to_run

def get_coverage_tasks(tasks_to_run, window, output_dir):
    """Take the path to one bamfile and return the doit tasks which would convert it to a BigWig"""

    bamfiles = []
    for task in tasks_to_run:
        if task['name'].startswith('Removing'):
            bamfiles.extend(task['targets'])

    indexes = []
    for task in tasks_to_run:
        if task['name'].startswith('Indexing'):
            indexes.append(task['name'])

    tasks_to_run.append({
            'name' : 'Getting coverage over {0}bp windows'.format(window),
            'actions' : ['echo "window %(dependencies)s" > %(targets)s',
                         'bash -i -c "multiBamCov -bams %(dependencies)s -bed <(bedtools makewindows -w {0} -g %(genome_file)s) >> %(targets)s"' .format(window)],
            'targets' : [os.path.join(output_dir, 'coverage_at_{0}bp.multibam'.format(window))],
            'file_dep' : bamfiles,
            'task_dep' : indexes,
           })

    tasks_to_run.append({
            'name'     :  'Getting threshold at {0}bp'.format(window),
            'actions'  : ['%(python_exec)s %(threshold_script)s --header-lines 1 %(dependencies)s > %(targets)s'],
            'targets'  : [os.path.join(output_dir, 'threshold_at_{0}bp.txt'.format(window))],
            'file_dep' : [os.path.join(output_dir, 'coverage_at_{0}bp.multibam'.format(window))],
            })

    tasks_to_run.append({
            'name'     : 'Segmenting data at {0}bp'.format(window),
            'actions'  : ['%(python_exec)s %(segmentation_script)s --header-lines 1 -t %(dependencies)s {coverage_file} > %(targets)s'.format(
                                                         coverage_file=os.path.join(output_dir, 'coverage_at_{0}bp.multibam'.format(window)))],
            'targets'  : [os.path.join(output_dir, 'segmentation_at_{0}bp.multibam'.format(window))],
            'file_dep' : [os.path.join(output_dir, 'threshold_at_{0}bp.txt'.format(window))],
            })

    """
    tasks_to_run.append({
            'name'     :  'Getting matrix at {0}bp'.format(window),
            'actions'  : ['%(python_exec)s %(matrix_script)s --hdf5-path %(targets)s %(dependencies)s'],
            'targets'  : [os.path.join(output_dir, '{0}bp_matrix.hdf5'.format(window))],
            'file_dep' : [os.path.join(output_dir, 'segmentation_at_{0}bp.multibam'.format(window))],
            })
            """

    return tasks_to_run

def get_segmentation_bed_tasks(tasks_to_run, segmentation_file, window):

    bamfiles = []
    for task in tasks_to_run:
        if task['name'].startswith('Removing'):
            bamfiles.extend(task['targets'])

    for input_bam in bamfiles:

        output_dir = os.path.dirname(input_bam)
        basename = os.path.splitext(os.path.basename(input_bam))[0]

        def with_extension(extension): return os.path.join(output_dir, basename + extension)
        action = '%(python_exec)s %(segmentation_bed_script)s -s %(dependencies)s -n {input_bam}  | tr "-" "\t" | bedtools merge > %(targets)s'.format(input_bam=input_bam)
        print action
        tasks_to_run.append({
                    'name'     : 'Getting segmentation bed at {0}bp for {1}'.format(window, input_bam),
                    'actions'  : [action,],
                    'targets'  : [with_extension(".segmentation_{0}bp.bed".format(window))],
                    'file_dep' : [segmentation_file],
                    })

    return tasks_to_run

class MyTaskLoader(TaskLoader):
    def __init__(self, tasks_to_run, args):
        self.tasks_to_run = tasks_to_run
        self.doit_db = os.path.join(self.get_db_dir(args), '.doit.mapping_db')
        self.num_process = args.num_process
        self.substitute_tasks(args)
        super(TaskLoader, self).__init__()

    def get_db_dir(self, args):

        output_dir = args.output_dir
        return output_dir

    def substitute_tasks(self, args):

        def get_script(script_name): return os.path.join(os.path.dirname(__file__), script_name)

        subs_dictionary = vars(args)
        subs_dictionary.update({'dependencies'        : '%(dependencies)s',
                                "targets"             : '%(targets)s',
                                "python_exec"         : sys.executable,
                                "threshold_script"    : get_script('threshold_by_reads.py'),
                                "segmentation_script" : get_script('get_threshold.py'),
                                "segmentation_bed_script" : get_script('extract_segmentation.py'),
                                "matrix_script"       : get_script('gam_matrix.py')})

        for task in self.tasks_to_run:
            task['actions'] = [ action % subs_dictionary for action in task['actions'] ]

    def load_tasks(self, cmd, opt_values, pos_args):
        task_list = [ dict_to_task(my_task) for my_task in self.tasks_to_run ]
        config = {'verbosity': 2,
                  'dep_file' : self.doit_db,
                  'num_process' : self.num_process,
                 }
        return task_list, config

def main(args):

    tasks_to_run = []
    for input_fastq in args.input_fastqs:
        tasks_to_run.extend( get_mapping_tasks(input_fastq))

    for window in args.window_sizes:
        tasks_to_run = get_coverage_tasks(tasks_to_run, window, args.output_dir)
        segmentation_file = os.path.join(args.output_dir, 'segmentation_at_{0}bp.multibam'.format(window))

        tasks_to_run = get_segmentation_bed_tasks(tasks_to_run, segmentation_file, window)

    sys.exit(DoitMain(MyTaskLoader(tasks_to_run, args)).run([]))

if __name__ == "__main__":

    args = parser.parse_args()

    if not args.output_dir:
        args.output_dir = os.getcwd()

    main(args)
<commit_msg>Removed some debugging stuff<commit_after>#! /usr/bin/env python

from doit.task import dict_to_task
from doit.cmd_base import TaskLoader
from doit.doit_cmd import DoitMain
import argparse
import os
import sys

parser = argparse.ArgumentParser(description='Map GAM-seq reads and create BigWigs and fastQC files')
parser.add_argument('input_fastqs', metavar='INPUT_FASTQ', nargs='*', help='One or more input fastq files.')
parser.add_argument('-g','--genome_file', metavar='GENOME_FILE', help='File containing chromosome names and lengths')
parser.add_argument('-p','--num_process', metavar='NUM_PROCESSES', type=int, default=1, help='Number of tasks to execute in parallel')
parser.add_argument('-o','--output_dir', metavar='OUPUT_DIRECTORY', help='Write segmentation, matrix etc. to this directory')
parser.add_argument('-w','--window_sizes', metavar='WINDOW_SIZE', default=[1000,5000,10000,50000,100000,500000], type=int, nargs='+', help='File containing chromosome names and lengths')

args = parser.parse_args()

def get_mapping_tasks(input_fastq):
    """Take the path to one bamfile and return the doit tasks which would convert it to a BigWig"""

    output_dir = os.path.dirname(input_fastq)
    basename = os.path.splitext(os.path.basename(input_fastq))[0]

    def with_extension(extension): return os.path.join(output_dir, basename + extension)

    tasks_to_run = []

    tasks_to_run.append({
                "name"     : 'Mapping fastq',
                "actions"  : ['bowtie2 -x genome -U %(dependencies)s | sed \'/XS : /d\' | samtools view -F 4 -bS - > %(targets)s'],
                "targets"  : [with_extension(".bam")],
                "file_dep" : [input_fastq],
                })

    tasks_to_run.append({
                "name"     : 'Sorting bam',
                "actions"  : ['samtools sort %(dependencies)s %(targets)s',
                              'mv %(targets)s.bam %(targets)s'],
                "targets"  : [with_extension(".sorted.bam")],
                "file_dep" : [with_extension(".bam")],
                })

    tasks_to_run.append({
                "name"     : 'Removing duplications',
                "actions"  : ['samtools rmdup -s %(dependencies)s %(targets)s',],
                "targets"  : [with_extension(".rmdup.bam")],
                "file_dep" : [with_extension(".sorted.bam")],
                })

    tasks_to_run.append({
                "name"     : 'Converting bam to bedgraph',
                "actions"  : ['genomeCoverageBed -bg -ibam %(dependencies)s -g %(genome_file)s > %(targets)s'],
                "targets"  : [with_extension(".bedgraph")],
                "file_dep" : [with_extension(".rmdup.bam")],
                })

    tasks_to_run.append({
                "name"     : 'Converting bedgraph to bigwig',
                "actions"  : ['bedGraphToBigWig %(dependencies)s %(genome_file)s %(targets)s'],
                "targets"  : [with_extension(".bw")],
                "file_dep" : [with_extension(".bedgraph")],
                })

    tasks_to_run.append({
                'name'     : 'Indexing bam',
                'actions'  : ['samtools index %(dependencies)s',],
                'targets'  : [with_extension(".rmdup.bam.bai")],
                'file_dep' : [with_extension(".rmdup.bam")],
                })

    for task in tasks_to_run:
        task["name"] = task["name"] + " (%s)" % task["targets"][0]

    return tasks_to_run

def get_coverage_tasks(tasks_to_run, window, output_dir):
    """Take the path to one bamfile and return the doit tasks which would convert it to a BigWig"""

    bamfiles = []
    for task in tasks_to_run:
        if task['name'].startswith('Removing'):
            bamfiles.extend(task['targets'])

    indexes = []
    for task in tasks_to_run:
        if task['name'].startswith('Indexing'):
            indexes.append(task['name'])

    tasks_to_run.append({
            'name' : 'Getting coverage over {0}bp windows'.format(window),
            'actions' : ['echo "window %(dependencies)s" > %(targets)s',
                         'bash -i -c "multiBamCov -bams %(dependencies)s -bed <(bedtools makewindows -w {0} -g %(genome_file)s) >> %(targets)s"' .format(window)],
            'targets' : [os.path.join(output_dir, 'coverage_at_{0}bp.multibam'.format(window))],
            'file_dep' : bamfiles,
            'task_dep' : indexes,
           })

    tasks_to_run.append({
            'name'     :  'Getting threshold at {0}bp'.format(window),
            'actions'  : ['%(python_exec)s %(threshold_script)s --header-lines 1 %(dependencies)s > %(targets)s'],
            'targets'  : [os.path.join(output_dir, 'threshold_at_{0}bp.txt'.format(window))],
            'file_dep' : [os.path.join(output_dir, 'coverage_at_{0}bp.multibam'.format(window))],
            })

    tasks_to_run.append({
            'name'     : 'Segmenting data at {0}bp'.format(window),
            'actions'  : ['%(python_exec)s %(segmentation_script)s --header-lines 1 -t %(dependencies)s {coverage_file} > %(targets)s'.format(
                                                         coverage_file=os.path.join(output_dir, 'coverage_at_{0}bp.multibam'.format(window)))],
            'targets'  : [os.path.join(output_dir, 'segmentation_at_{0}bp.multibam'.format(window))],
            'file_dep' : [os.path.join(output_dir, 'threshold_at_{0}bp.txt'.format(window))],
            })

    """
    tasks_to_run.append({
            'name'     :  'Getting matrix at {0}bp'.format(window),
            'actions'  : ['%(python_exec)s %(matrix_script)s --hdf5-path %(targets)s %(dependencies)s'],
            'targets'  : [os.path.join(output_dir, '{0}bp_matrix.hdf5'.format(window))],
            'file_dep' : [os.path.join(output_dir, 'segmentation_at_{0}bp.multibam'.format(window))],
            })
            """

    return tasks_to_run

def get_segmentation_bed_tasks(tasks_to_run, segmentation_file, window):

    bamfiles = []
    for task in tasks_to_run:
        if task['name'].startswith('Removing'):
            bamfiles.extend(task['targets'])

    for input_bam in bamfiles:

        output_dir = os.path.dirname(input_bam)
        basename = os.path.splitext(os.path.basename(input_bam))[0]

        def with_extension(extension): return os.path.join(output_dir, basename + extension)

        tasks_to_run.append({
                    'name'     : 'Getting segmentation bed at {0}bp for {1}'.format(window, input_bam),
                    'actions'  : ['%(python_exec)s %(segmentation_bed_script)s -s %(dependencies)s -n {input_bam}  | tr "-" "\t" | bedtools merge > %(targets)s'.format(input_bam=input_bam),],
                    'targets'  : [with_extension(".segmentation_{0}bp.bed".format(window))],
                    'file_dep' : [segmentation_file],
                    })

    return tasks_to_run

class MyTaskLoader(TaskLoader):
    def __init__(self, tasks_to_run, args):
        self.tasks_to_run = tasks_to_run
        self.doit_db = os.path.join(self.get_db_dir(args), '.doit.mapping_db')
        self.num_process = args.num_process
        self.substitute_tasks(args)
        super(TaskLoader, self).__init__()

    def get_db_dir(self, args):

        output_dir = args.output_dir
        return output_dir

    def substitute_tasks(self, args):

        def get_script(script_name): return os.path.join(os.path.dirname(__file__), script_name)

        subs_dictionary = vars(args)
        subs_dictionary.update({'dependencies'        : '%(dependencies)s',
                                "targets"             : '%(targets)s',
                                "python_exec"         : sys.executable,
                                "threshold_script"    : get_script('threshold_by_reads.py'),
                                "segmentation_script" : get_script('get_threshold.py'),
                                "segmentation_bed_script" : get_script('extract_segmentation.py'),
                                "matrix_script"       : get_script('gam_matrix.py')})

        for task in self.tasks_to_run:
            task['actions'] = [ action % subs_dictionary for action in task['actions'] ]

    def load_tasks(self, cmd, opt_values, pos_args):
        task_list = [ dict_to_task(my_task) for my_task in self.tasks_to_run ]
        config = {'verbosity': 2,
                  'dep_file' : self.doit_db,
                  'num_process' : self.num_process,
                 }
        return task_list, config

def main(args):

    tasks_to_run = []
    for input_fastq in args.input_fastqs:
        tasks_to_run.extend( get_mapping_tasks(input_fastq))

    for window in args.window_sizes:
        tasks_to_run = get_coverage_tasks(tasks_to_run, window, args.output_dir)
        segmentation_file = os.path.join(args.output_dir, 'segmentation_at_{0}bp.multibam'.format(window))

        tasks_to_run = get_segmentation_bed_tasks(tasks_to_run, segmentation_file, window)

    sys.exit(DoitMain(MyTaskLoader(tasks_to_run, args)).run([]))

if __name__ == "__main__":

    args = parser.parse_args()

    if not args.output_dir:
        args.output_dir = os.getcwd()

    main(args)
<|endoftext|>