class TargetStationManualDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.TargetStationManual, itypes.TargetStationManualInfo, **kwargs) def list(self): """ :rtype: list[itypes.TargetStationManualInfo] """ return self.da_base.list() def list_by_corridor(self, corridor_name): """ :type corridor_name: str :rtype: list[itypes.TargetStationManualInfo] """ return self.da_base.search([('corridor_name', corridor_name)], cond='match') def get_by_id(self, id): """ :type id: int :rtype: itypes.TargetStationInfo """ return self.da_base.get_data_by_id(id) def delete(self, id, autocommit=False): """ :type id: int :type autocommit: bool """ return self.da_base.delete(id, autocommit=autocommit) def insert(self, tsi, autocommit=False): """ :type tsi: itypes.TargetStationInfo :type autocommit: bool :raise AlreadyExist :rtype: model.TargetStation """ return self.da_base.insert(tsi, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class WinterSeasonDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.WinterSeason, itypes.WinterSeasonInfo, **kwargs) def get_by_months(self, months): """ return nsr_data by months :type months: list[(int, int)] :rtype: itypes.WinterSeasonInfo """ year, mstr = itypes.WinterSeasonInfo.months_str(months) try: md = self.da_base.session.query(model.WinterSeason).filter(model.WinterSeason.months == mstr).first() if not md: return None return self.da_base.to_info(md) except NoResultFound as ex: return None def list(self): """ :rtype: list[itypes.WinterSeasonInfo] """ return self.da_base.list() def get_by_id(self, id): """ :type id: int :rtype: itypes.WinterSeasonInfo """ return self.da_base.get_data_by_id(id) def get_by_year(self, year, **kwargs): """ :type year: int :rtype: itypes.WinterSeasonInfo """ as_model = kwargs.get('as_model', False) try: model_data = self.da_base.session.query(model.WinterSeason).filter(model.WinterSeason.year == year).one() if not model_data: return None if as_model: return model_data else: return self.da_base.to_info(model_data) except NoResultFound as ex: return None def delete(self, id, autocommit=False): """ :type id: int :type autocommit: bool """ return self.da_base.delete(id, autocommit=autocommit) def insert(self, ws, autocommit=False): """ :type ws: itypes.WinterSeasonInfo :type autocommit: bool :raise AlreadyExist :rtype: model.WinterSeason """ return self.da_base.insert(ws, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class TargetLaneConfigDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.TargetLaneConfig, itypes.TargetLaneConfigInfo, **kwargs) def years(self): """ :rtype: list[int]: """ ys = [] for tlci in self.da_base.list(group_by='winterseason_id'): if tlci._winterseason.year not in ys: ys.append(tlci._winterseason.year) return sorted(ys) def list(self): """ :rtype: list[itypes.TargetLaneConfigInfo] """ return self.da_base.list() def list_by_winterseason(self, wid): """ :type wid: int :rtype: list[itypes.TargetLaneConfigInfo] """ return self.da_base.search([('winterseason_id', wid)], cond='match') def list_by_year(self, year, **kwargs): """ :type year: int :rtype: list[itypes.TargetLaneConfigInfo] """ as_model = kwargs.get('as_model', False) qry = self.da_base.session.query(model.TargetLaneConfig).filter( model.TargetLaneConfig._winterseason.has(year=year)) res = [] for item in qry: if as_model: res.append(item) else: res.append(self.da_base.to_info(item)) return res def get_by_station_id(self, year, station_id, **kwargs): """ :type year: int :type station_id: str :rtype: itypes.TargetLaneConfigInfo """ as_model = kwargs.get('as_model', False) item = (self.da_base.session.query(model.TargetLaneConfig) .filter(model.TargetLaneConfig._winterseason.has(year=year)) .filter(model.TargetLaneConfig.station_id == station_id).first()) if not item: return None if as_model: return item else: return self.da_base.to_info(item) def list_by_corridor_name(self, year, corridor_name, **kwargs): """ :type year: int :rtype: list[itypes.TargetLaneConfigInfo] """ as_model = kwargs.get('as_model', False) qry = self.da_base.session.query(model.TargetLaneConfig).filter( model.TargetLaneConfig._winterseason.has(year=year)).filter(model.TargetLaneConfig.corridor_name == corridor_name) res = [] for item in qry: if as_model: res.append(item) else: res.append(self.da_base.to_info(item)) return res def get_by_id(self, id): """ :type id: int :rtype: itypes.TargetLaneConfigInfo """ return self.da_base.get_data_by_id(id) def delete(self, id, autocommit=False): """ :type id: int :type autocommit: bool """ return self.da_base.delete(id, autocommit=autocommit) def insert(self, tsi, autocommit=False): """ :type tsi: itypes.TargetLaneConfigInfo :type autocommit: bool :raise AlreadyExist :rtype: model.TargetStation """ return self.da_base.insert(tsi, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class SnowEventDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.SnowEvent, SnowEventInfo, **kwargs) def list(self): """ :rtype: list[SnowEventInfo] """ return self.da_base.list() def list_by_year(self, years): """ :type years: list[int] :rtype: list[SpecialEventInfo] """ results = [] for year in years: res = self.da_base.search_date_range( ('start_time', datetime.datetime(int(year), 1, 1, 0, 0, 0)), ('end_time', datetime.datetime(int(year), 12, 31, 11, 59, 59))) if res: results.extend(res) return results def years(self): """ :rtype: list[int]: """ ys = [] for snei in self.da_base.list(as_model=True): y = snei.start_time.year if y not in ys: ys.append(y) return sorted(ys) def get_by_id(self, id): """ :type id: int :rtype: SnowEventInfo """ return self.da_base.get_data_by_id(id) def get_by_name(self, route_name): """ :type route_name: str :rtype: SnowEventInfo """ return self.da_base.get_data_by_name(route_name) def delete(self, id, autocommit=False): deleted = self.da_base.delete(id, autocommit=autocommit) if deleted: da_snowmgmt = SnowMgmtDataAccess(session=self.da_base.session) for snm in da_snowmgmt.search([('sevent_id', 1)], cond='match', as_model=True): da_snowmgmt.delete(snm.id, autocommit=autocommit) return deleted def insert(self, r, autocommit=False): """ :type r: SnowEventInfo :type autocommit: bool :raise AlreadyExist :rtype: model.SnowEvent """ return self.da_base.insert(r, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class SnowRouteDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.SnowRoute, SnowRouteInfo, **kwargs) def list(self, **kwargs): """ :rtype: list[SnowRouteInfo] """ return self.da_base.list(**kwargs) def get_by_id(self, route_id): """ :type route_id: int :rtype: SnowRouteInfo """ return self.da_base.get_data_by_id(route_id) def get_by_name(self, route_name): """ :type route_name: str :rtype: SnowRouteInfo """ return self.da_base.get_data_by_name(route_name) def delete(self, id, autocommit=False): """ :type route_name: str :type autocommit: bool """ deleted = self.da_base.delete(id, autocommit=autocommit) return deleted def get_by_rnode_id(self, year, rnode_id, **kwargs): """ :param year: :type year: int :param rnode_id: :type rnode_id: str :rtype: SnowRouteInfo """ as_model = kwargs.get('as_model', False) sstr = '"%s"' % rnode_id item = self.da_base.session.query(model.SnowRoute).filter( and_( or_( model.SnowRoute.route1.like('%{}%'.format(sstr)), model.SnowRoute.route2.like('%{}%'.format(sstr)), ), model.SnowRoute._snowroute_group.has(year=year))).first() if as_model: return item else: return self.da_base.to_info(item) def list_by_year(self, year, **kwargs): """ :param year: :type year: int :rtype: list[SnowRouteInfo] """ as_model = kwargs.get('as_model', False) qry = self.da_base.session.query(model.SnowRoute).filter( model.SnowRoute._snowroute_group.has(year=year)) res = [] for item in qry: if as_model: res.append(item) else: res.append(self.da_base.to_info(item)) return res def insert(self, r, autocommit=False): """ :type r: SnowRouteInfo :type autocommit: bool :raise AlreadyExist :rtype: model.SnowRoute """ return self.da_base.insert(r, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def search(self, searches, op='and', cond='match', **kwargs): """ search nsr_data **Example** - find item that `sevent_id` is 1 >>> search([('sevent_id', 1)] - find item that `name` == 'test and start_time == 2016-01-01 00:00 and end_time == 2016-01-01 07:00 >>> search([('name', 'test'), ('start_time', datetime(2016, 1, 1, 0, 0)), ('end_time', 2016, 1, 1, 7, 0)], op='and', cond='match') - find items that `ymds` contains one of 2014, 2015 and 2016 >>> search([('ymds', y) for y in [2014, 2015, 2016]], op='or', cond='like') """ return self.da_base.search(searches, op, cond, **kwargs) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class DataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.NormalData, itypes.NormalDataInfo, **kwargs) def list(self): """ :rtype: list[itypes.NormalDataInfo] """ return self.da_base.list() def list_by_winterseason(self, wid): """ :type wid: int :rtype: list[itypes.NormalDataInfo] """ return self.da_base.search([('winterseason_id', wid)], cond='match') def get_by_id(self, id): """ :type id: int :rtype: itypes.NormalDataInfo """ return self.da_base.get_data_by_id(id) def get_by_station(self, wid, station_id): """ :type wid: int :type station_id: str :rtype: itypes.NormalDataInfo """ try: md = self.da_base.session.query(model.NormalData).filter(model.NormalData.station_id == station_id, model.NormalData.winterseason_id == wid).one() return self.da_base.to_info(md) except NoResultFound as ex: return None def delete(self, id, autocommit=False): """ :type id: int :type autocommit: bool """ return self.da_base.delete(id, autocommit=autocommit) def insert(self, ndi, autocommit=False): """ :type ndi: itypes.NormalDataInfo :type autocommit: bool :raise AlreadyExist :rtype: model.NormalData """ return self.da_base.insert(ndi, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class NormalFunctionDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.NormalFunction, itypes.NormalFunctionInfo, **kwargs) def list(self): """ :rtype: list[itypes.NormalFunctionInfo] """ return self.da_base.list() def get_by_station(self, year, station_id, **kwargs): """ :type year: int :rtype: itypes.NormalFunctionInfo """ try: md = self.da_base.session.query(model.NormalFunction).filter( model.NormalFunction.station_id == station_id, model.NormalFunction._winterseason.has(year=year)).one() return self.da_base.to_info(md) except NoResultFound as ex: return None except Exception as ex: from pyticas.tool import tb tb.traceback(ex) return None def list_by_winterseason(self, wid): """ :type wid: int :rtype: list[itypes.NormalFunctionInfo] """ return self.da_base.search([('winterseason_id', wid)], cond='match') def get_by_id(self, id): """ :type id: int :rtype: itypes.NormalFunctionInfo """ return self.da_base.get_data_by_id(id) def get_by_winter_id(self, wid, station_id): """ :type wid: int :type station_id: str :rtype: itypes.NormalFunctionInfo """ try: md = self.da_base.session.query(model.NormalFunction).filter( model.NormalFunction.station_id == station_id, model.NormalFunction.winterseason_id == wid).one() return self.da_base.to_info(md) except NoResultFound as ex: return None except Exception as ex: from pyticas.tool import tb tb.traceback(ex) return None def delete(self, id, autocommit=False): """ :type id: int :type autocommit: bool """ return self.da_base.delete(id, autocommit=autocommit) def insert(self, nfi, autocommit=False): """ :type nfi: itypes.NormalFunctionInfo :type autocommit: bool :raise AlreadyExist :rtype: model.NormalFunction """ return self.da_base.insert(nfi, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()
class SnowRouteGroupDataAccess(object): def __init__(self, **kwargs): self.da_base = DataAccessBase(model.SnowRouteGroup, SnowRouteGroupInfo, **kwargs) def list(self, **kwargs): """ :rtype: list[SnowRouteGroupInfo] """ return self.da_base.list(**kwargs) def list_by_year(self, years): """ :type years: list[int] :return: """ if years: wheres = [('year', y) for y in years] return self.da_base.search(wheres, op='or', cond='match') else: return self.da_base.search([('year', None), ('year', '')], op='or', cond='match') def search(self, searches, op='and', cond='match', **kwargs): """ search nsr_data **Example** - find item that `sevent_id` is 1 >>> search([('sevent_id', 1)] - find item that `name` == 'test and start_time == 2016-01-01 00:00 and end_time == 2016-01-01 07:00 >>> search([('name', 'test'), ('start_time', datetime(2016, 1, 1, 0, 0)), ('end_time', 2016, 1, 1, 7, 0)], op='and', cond='match') - find items that `years` contains one of 2014, 2015 and 2016 >>> search([(ymds, y) for y in [2014, 2015, 2016]], op='or', cond='like') :rtype: list[SnowRouteGroupInfo] """ return self.da_base.search(searches, op, cond, **kwargs) def years(self): """ :rtype: list[int]: """ ys = [] for snrgi in self.da_base.list(): if snrgi.year not in ys: ys.append(snrgi.year) return sorted(ys) def get_by_id(self, snrg_id): """ :type snrg_id: int :rtype: SnowRouteGroupInfo """ return self.da_base.get_data_by_id(snrg_id) def get_by_name(self, prj_id): """ :type prj_id: str :rtype: SnowRouteGroupInfo """ return self.da_base.get_data_by_name(prj_id) def delete(self, id, autocommit=False): """ :type route_name: str :type autocommit: bool """ deleted = self.da_base.delete(id, autocommit=autocommit) return deleted def insert(self, r, autocommit=False): """ :type r: SnowRouteGroupInfo :type autocommit: bool :raise AlreadyExist :rtype: model.SnowRoute """ return self.da_base.insert(r, autocommit=autocommit) def update(self, id, field_data, autocommit=False): """ :type id: int :type field_data: dict :raise AlreadyExist :rtype: bool """ return self.da_base.update(id, field_data, autocommit=autocommit) def rollback(self): self.da_base.session.rollback() def commit(self): self.da_base.commit() def close(self): self.da_base.close()