コード例 #1
0
ファイル: pykmaze.py プロジェクト: eblot/pykmaze
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    log.addHandler(ch)
    
    try:
        if options.force and options.offline:
            raise AssertionError('Force and offline modes are mutually '
                                 'exclusive')
        keymaze = None
        if not options.offline:
            keymaze = KeymazePort(log, options.port)
        elif options.sync:
            raise AssertionError('Cannot sync from device in offline mode')
        cache = KeymazeCache(log, options.storage, keymaze)

        info = cache.get_information()

        if options.info:
            print ' Device: %s' % info['name']
            print ' Owner:  %s' % info['user']
            print ' S/N:    %s' % info['serialnumber']
            print ''
        
        device = cache.get_device(info['serialnumber'])
        
        tpcat = cache.get_trackpoint_catalog(device)
        
        if options.sync:
            reload_cache = False
コード例 #2
0
ファイル: pykmaze.py プロジェクト: zyphos/pykmaze
class Keymaze(object):
    def __init__(self, dbpath=None, port=None, offline_mode=False):
        log = logging.getLogger("pykmaze")
        log.setLevel(logging.INFO)
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        formatter = logging.Formatter("%(levelname)s - %(message)s")
        ch.setFormatter(formatter)
        log.addHandler(ch)
        self.log = log

        if port is None:
            serial_ports = get_available_serial_ports()
            if len(serial_ports) == 0:
                print "No serial port detected, going to offline mode"
                offline_mode = True
            else:
                print "Available serial port:"
                print serial_ports
                port = serial_ports[0]
                print "Taking %s" % port

        if dbpath is None:
            dbpath = self._get_default_dbpath()
        self.dbpath = dbpath

        if not offline_mode:
            self.set_online_mode(port)
        else:
            self.set_offline_mode()

    def set_offline_mode(self):
        self.init_keymaze(None)
        self.current_mode = "offline"

    def set_online_mode(self, port):
        keymaze_port = KeymazePort(self.log, port)
        self.init_keymaze(keymaze_port)
        self.current_mode = "online"

    def init_keymaze(self, keymaze_port):
        self.cache = KeymazeCache(self.log, self.dbpath, keymaze_port)
        info = self.cache.get_information()
        self.device = self.cache.get_device(info["serialnumber"])
        self.tpcat = self.cache.get_trackpoint_catalog(self.device)

    def _get_default_dbpath(self):
        dbname = "pykmaze.sqlite"
        dbpath = "HOME" in os.environ and os.environ["HOME"] or "."
        if sys.platform.lower() in ("darwin"):
            dbpath = os.path.join(dbpath, "Library", "Application Support", "Pykmaze", dbname)
        else:
            dbpath = os.path.join(dbpath, ".pykmaze", dbname)
        return dbpath

    def print_info(self):
        print " Device: %s" % self.info["name"]
        print " Owner:  %s" % self.info["user"]
        print " S/N:    %s" % self.info["serialnumber"]
        print ""

    def sync(self):
        if self.current_mode == "offline":
            raise AssertionError("Cannot sync from device in offline mode")
        reload_cache = False
        for tp in self.tpcat:
            if None in (tp["altmin"], tp["altmax"]):
                self.log.info("Should load sync track %d from device" % (int(tp["id"])))
                self.cache.get_trackpoints(self.device, tp)
                reload_cache = True
        if reload_cache:
            self.tpcat = self.cache.get_trackpoint_catalog(self.device)

    def show_catalog(self):
        show_trackpoints_catalog(self.tpcat)
        print ""

    def get_all_tracks(self):
        tracks = [tp["track"] for tp in self.tpcat]
        self.log.debug("Tracks %s" % tracks)
        return tracks

    def get_trackinfo_from_name(self, trackname):
        trackname = int(trackname)
        for tp in self.tpcat:
            if int(tp["id"]) == trackname:
                return tp
        raise AssertionError('Track "%s" does not exist' % trackname)

    def get_trackpoints(self, trackinfo):
        self.log.info("Recovering trackpoints for track %u" % trackinfo["id"])
        tpoints = self.cache.get_trackpoints(self.device, trackinfo)
        return tpoints

    def get_trackinfo(self, track):
        return filter(lambda x: x["id"] == track["id"], self.tpcat)[0]

    def trim_trackpoints(self, track_info, tpoints, trim):
        trims = parse_trim(trim.split(","))
        self.log.info("All points: %d" % len(tpoints))
        tpoints = trim_trackpoints(track_info, tpoints, trims)
        self.log.info("Filtered points: %d" % len(tpoints))
        return tpoints

    def export_track(self, track, filename, filetype, trim=None, prepend_datetime=False, zoffset=0, mode="default"):
        # filetype 'gpx', 'kmz' or 'km'
        if filetype not in ["kmz", "kml", "gpx"]:
            raise AssertionError("Unsupported filetype %s" % filetype)

        track_info = self.get_trackinfo(track)
        tpoints = self.get_trackpoints(track_info)
        if prepend_datetime:
            stime = time.localtime(track_info["start"])
            filename = os.path.join(
                os.path.dirname(filename), time.strftime("%Y-%m-%d-%H-%M-%S-", stime) + os.path.basename(filename)
            )

        is_km = filetype in ["kmz", "kml"]
        if is_km:
            from kml import KmlDoc
        if filetype == "gpx":
            from gpx import GpxDoc

        if trim:
            tpoints = trim_trackpoints(track_info, tpoints, trim)

        optpoints = optimize(tpoints, 0)
        self.log.info("Count: %u, opt: %u", len(tpoints), len(optpoints))
        if is_km:
            kml = KmlDoc(os.path.splitext(os.path.basename(filename))[0])
            kml.add_trackpoints(optpoints, int(zoffset), extrude="air" not in mode)
        if filetype == "kmz":
            import zipfile
            import cStringIO as StringIO

            out = StringIO.StringIO()
            kml.write(out)
            out.write("\n")
            z = zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED)
            z.writestr("doc.kml", out.getvalue())
        if filetype == "kml":
            with open(filename, "wt") as out:
                kml.write(out)
                out.write("\n")

        if filetype == "gpx":
            gpx = GpxDoc(os.path.splitext(os.path.basename(filename))[0], track_info["start"])
            gpx.add_trackpoints(optpoints, int(zoffset))
            with open(filename, "wt") as out:
                gpx.write(out)
                out.write("\n")

    def export_all_tracks(self, basename, filetype):
        for track in self.get_all_tracks():
            self.export_track(track, basename, filetype, prepend_datetime=True)
コード例 #3
0
ファイル: pykmaze.py プロジェクト: zyphos/pykmaze
 def init_keymaze(self, keymaze_port):
     self.cache = KeymazeCache(self.log, self.dbpath, keymaze_port)
     info = self.cache.get_information()
     self.device = self.cache.get_device(info["serialnumber"])
     self.tpcat = self.cache.get_trackpoint_catalog(self.device)
コード例 #4
0
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    log.addHandler(ch)
    
    try:
        if options.force and options.offline:
            raise AssertionError('Force and offline modes are mutually '
                                 'exclusive')
        keymaze = None
        if not options.offline:
            keymaze = KeymazePort(log, options.port)
        elif options.sync:
            raise AssertionError('Cannot sync from device in offline mode')
        cache = KeymazeCache(log, options.storage, keymaze)

        info = cache.get_information()

        if options.info:
            print >> sys.stdout, ' Device: %s' % info['name']
            print >> sys.stdout, ' Owner:  %s' % info['user']
            print >> sys.stdout, ' S/N:    %s' % info['serialnumber']
            print >> sys.stdout, ''
        
        device = cache.get_device(info['serialnumber'])
        
        tpcat = cache.get_trackpoint_catalog(device)
        
        if options.sync:
            reload_cache = False