"""Recording objects """ import os.path import logging import uuid from sqlobject import SQLObject, StringCol, IntCol, BoolCol, \ MultipleJoin, ForeignKey import config from channeltuner import ChannelTuner log = logging.getLogger("calliope.tuner") class Tuner(SQLObject): # Internal properties of the device adapter = StringCol(default=None) manufacturer = StringCol(default=None) product = StringCol(default=None) # Unique identification url = StringCol(default=None) tid = IntCol(default=-1) uuid = StringCol(default="") # Other static properties local = BoolCol(default=True) channels = MultipleJoin("ChannelTuner") last_update = IntCol(default=0) # Dynamic properties: current state state = StringCol(default="") pid = IntCol(default=None) def setup(self, url, tid): log.info("Setting up tuner: %s %d", url, tid) tuning = os.path.join(config.channels, "channels%d" % tid) log.debug("From tuner file: %s", tuning) self.adapter = os.path.join("/", "dev", "dvb", "adapter%d" % tid) self.url = url % tid self.tid = tid self.manufacturer, self.product = Tuner.hw_identity(tid) self.last_update = time.time() self.load_channels(tuning) self.uuid = uuid.uuid4().hex return self def load_channels(self, tuning): """(Generate and) load the channels file. """ inf = open(tuning, "r") for line in inf: line = line.rstrip() bits = line.split(":") log.debug("Channel/tuner: " + line) ChannelTuner(tuner=self).setup(*bits) inf.close() def get_data(self): d = {} for key in ("adapter", "url", "tid", "manufacturer", "product", "state", "uuid", "last_update"): d[key] = getattr(self, key) return d @staticmethod def hw_identity(tid): """Return (manufacturer, product) for the given tuner device""" sysfs = os.path.join("/", "sys", "class", "dvb", "dvb%d" % tid) manufacturer = None product = None # Load some basic identity information from sysfs. # The sysfs kernel docs say a lot about what not to do (and # this is one of those), but don't say what you *should* do # instead. So, stuff 'em... try: inf = open(os.path.join(sysfs + ".frontend0", "device", "manufacturer"), "r") manufacturer = inf.read().strip() inf.close() except IOError: pass try: inf = open(os.path.join(sysfs + ".frontend0", "device", "product"), "r") product = inf.read().strip() inf.close() except IOError: pass return (manufacturer, product) @staticmethod def list_hw_ids(): """Enumerate the hardware, at the most basic level""" if hasattr(config, "faketuners"): return config.faketuners devs = set() sysfs = os.path.join("/", "sys", "class", "dvb") try: for f in os.listdir(sysfs): devs.add(f.split(".")[0]) return [int(d[3:]) for d in devs] except OSError: return []