# Path: tasks/meta.py
# class OBSTimespan(UniqueMixin, Base):
#     '''
#     Describes a timespan table in our database.
#     '''
#     __tablename__ = 'obs_timespan'
# 
#     id = Column(Text, primary_key=True)  # fully-qualified id
#     alias = Column(Text)  # alias for backwards compatibility
#     name = Column(Text)  # human-readable name
#     description = Column(Text)  # human-readable description
#     timespan = Column(DATERANGE)
#     weight = Column(Integer, default=0)
# 
#     @classmethod
#     def unique_hash(cls, id):
#         return id
# 
#     @classmethod
#     def unique_filter(cls, query, id):
#         return query.filter(OBSTimespan.id == id)
# 
# def current_session():
#     '''
#     Returns the session relevant to the currently operating :class:`Task`, if
#     any.  Outside the context of a :class:`Task`, this can still be used for
#     manual session management.
#     '''
#     return _current_session.get()


import re
import calendar
from dateutil.parser import parse
from tasks.meta import OBSTimespan, current_session
        ts_name = '{year}-{month:02d}'.format(year=year, month=month)
        ts_description = '{month_name} {year}'.format(month_name=month_name, year=year)
        ts_timespan = '[{year}-{month:02d}-01, {year}-{month:02d}-{last_day}]'.format(year=year, month=month,
                                                                                      last_day=last_day_of_month)
    # Year and quarter, for example '2017Q3'
    elif re.match('\d{4}Q\d{1}$', timespan_str):
        year, quarter = list(map(int, timespan_str.split('Q')))
        if quarter < 1 or quarter > 4:
            raise ValueError("Invalid timespan '{timespan}' (invalid quarter)".format(timespan=timespan_id))

        last_month = quarter * 3
        initial_mont = last_month - 2
        last_day = calendar.monthrange(year, last_month)[1]

        ts_id = '{year}Q{quarter}'.format(year=year, quarter=quarter)
        ts_alias = '{year}Q{quarter}'.format(year=year, quarter=quarter)
        ts_name = '{year}Q{quarter}'.format(year=year, quarter=quarter)
        ts_description = '{year}, {quarter} quarter'.format(year=year, quarter=QUARTERS[quarter])
        ts_timespan = '[{year}-{ini_month:02d}-01, {year}-{end_month:02d}-{last_day}]'.format(year=year,
                                                                                              ini_month=initial_mont,
                                                                                              end_month=last_month,
                                                                                              last_day=last_day)
    # Day, month, year, for example '20171231'
    elif re.match('\d{8}$', timespan_str):
        date = parse(timespan_str)

        last_day_of_month = calendar.monthrange(date.year, date.month)[1]

        ts_id = date.strftime('%Y%m%d')
        ts_alias = date.strftime('%Y%m%d')

# Path: tasks/meta.py
# class OBSTimespan(UniqueMixin, Base):
#     '''
#     Describes a timespan table in our database.
#     '''
#     __tablename__ = 'obs_timespan'
# 
#     id = Column(Text, primary_key=True)  # fully-qualified id
#     alias = Column(Text)  # alias for backwards compatibility
#     name = Column(Text)  # human-readable name
#     description = Column(Text)  # human-readable description
#     timespan = Column(DATERANGE)
#     weight = Column(Integer, default=0)
# 
#     @classmethod
#     def unique_hash(cls, id):
#         return id
# 
#     @classmethod
#     def unique_filter(cls, query, id):
#         return query.filter(OBSTimespan.id == id)
# 
# def current_session():
#     '''
#     Returns the session relevant to the currently operating :class:`Task`, if
#     any.  Outside the context of a :class:`Task`, this can still be used for
#     manual session management.
#     '''
#     return _current_session.get()


# Path: lib/timespan.py
import re
import calendar
from dateutil.parser import parse
from tasks.meta import OBSTimespan, current_session

        ts_name = '{year}-{month:02d}'.format(year=year, month=month)
        ts_description = '{month_name} {year}'.format(month_name=month_name, year=year)
        ts_timespan = '[{year}-{month:02d}-01, {year}-{month:02d}-{last_day}]'.format(year=year, month=month,
                                                                                      last_day=last_day_of_month)
    # Year and quarter, for example '2017Q3'
    elif re.match('\d{4}Q\d{1}$', timespan_str):
        year, quarter = list(map(int, timespan_str.split('Q')))
        if quarter < 1 or quarter > 4:
            raise ValueError("Invalid timespan '{timespan}' (invalid quarter)".format(timespan=timespan_id))

        last_month = quarter * 3
        initial_mont = last_month - 2
        last_day = calendar.monthrange(year, last_month)[1]

        ts_id = '{year}Q{quarter}'.format(year=year, quarter=quarter)
        ts_alias = '{year}Q{quarter}'.format(year=year, quarter=quarter)
        ts_name = '{year}Q{quarter}'.format(year=year, quarter=quarter)
        ts_description = '{year}, {quarter} quarter'.format(year=year, quarter=QUARTERS[quarter])
        ts_timespan = '[{year}-{ini_month:02d}-01, {year}-{end_month:02d}-{last_day}]'.format(year=year,
                                                                                              ini_month=initial_mont,
                                                                                              end_month=last_month,
                                                                                              last_day=last_day)
    # Day, month, year, for example '20171231'
    elif re.match('\d{8}$', timespan_str):
        date = parse(timespan_str)

        last_day_of_month = calendar.monthrange(date.year, date.month)[1]

        ts_id = date.strftime('%Y%m%d')
        ts_alias = date.strftime('%Y%m%d')

        ts_name = date.strftime('%Y%m%d')