示例#1
0
class PVersion(PBasic):
    """
        version.json bearbeiten
    """
    micodb = None

    def __init__(self):
        PBasic.__init__(self)
        self.init()

    def init(self):
        self.micodb = JsonDictDb(PStatics.getVersionfile(), False)

        if not self.micodb.exist():
            self.micodb.set(version_data)
            self.micodb.save()

    def write_versionfile(self):
        da = self.micodb.get('1')
        pprint.pprint(
            {
                'date': da['date'],
                'version': str(da['major']) + '.' + str(da['minor']) + '.' + str(da['revision']),
                'build': da['build']
            }
        )
        self._write()

    def save_version(self):
        self.micodb.save()

    def increment_build(self):
        da = self.micodb.get('1')
        da['build'] += 1
        da['date'] = self.jetzt
        self.save_version()

    def increment_major(self):
        da = self.micodb.get('1')
        da['major'] += 1
        da['minor'] = '0'
        da['revision'] = '0'
        self.save_version()

    def increment_minor(self):
        da = self.micodb.get('1')
        da['minor'] += 1
        da['revision'] = 0
        self.save_version()

    def increment_revision(self):
        da = self.micodb.get('1')
        da['revision'] += 1
        self.save_version()
示例#2
0
class PHistory(PBasic):
    """
        history.json bearbeiten
    """

    data = None

    def __init__(self):
        PBasic.__init__(self)
        self.init()

    def init(self):
        self.micodb = JsonDictDb(PStatics.getHistoryfile(), False)

        if not self.micodb.exist():
            # self.micodb.set(history_data)
            # self.micodb.save()

            # da = self.micodb.get("1")
            self.micodb.set(self.getNewDataset())
            self.micodb.save()

    def getNewDataset(self, da=None):
        if da is None:
            da = history_data
        da['id'] = self.getNewId()
        da['start'] = self.jetzt
        da['end'] = self.jetzt
        return da

    # (IMHO) the simplest approach:
    def sortedDictValues1(adict):
        items = adict.items()
        items.sort()
        return [value for key, value in items]

    # an alternative implementation, which
    # happens to run a bit faster for large
    # dictionaries on my machine:
    def sortedDictValues2(adict):
        keys = adict.keys()
        keys.sort()
        return [dict[key] for key in keys]

    # a further slight speed-up on my box
    # is to map a bound-method:
    def sortedDictValues3(adict):
        keys = adict.keys()
        keys.sort()
        return map(adict.get, keys)

    def makezeit(self, lastline):
        """ Die letzte Zeit berechnen bevor die neue Zeile eingefügt wird.
        :param lastline:
        :return:
        """
        t1 = datetime.datetime.strptime(lastline['start'], self.ccFormat)
        t2 = datetime.datetime.strptime(lastline['end'], self.ccFormat)
        diff = t2 - t1
        return int(diff.total_seconds() / 60)

    def increment_history(self):
        a = 0

        self.data = self.micodb.getlast()

        # self.data = self.micodb.getlistlast('1')
        # self.data = []

        newid = self.getNewId()

        a = self.micodb.getId()

        # datum/zeit von jetzt
        # aktuelle datum/Zeit (datetime)
        nun1 = datetime.datetime.now()

        # endzeit holen und umrechen (datetime)
        nun2 = datetime.datetime.strptime(self.data['end'], self.ccFormat)

        # differenz
        difftime = nun1 - nun2
        diff30 = (difftime - datetime.timedelta(minutes=30)).total_seconds()

        if diff30 > 0:
            # Differenz ist größer 30 minuten. Neue Zeile einfügen.

            # im vorhandenen Datensatz die anzahl der Minuten setzen
            self.data['minutes'] = self.makezeit(self.data)
            # einen neuen Datensatz einfügen
            self.micodb.set(self.getNewDataset())
        else:
            # Differenz ist kleiner als 30 Minuten. Der Wert 'end' wird aktualisiert
            self.data['end'] = self.jetzt
            self.data['minutes'] = self.makezeit(self.data)

        self.micodb.save()

    def listen(self):
        """ listet die History files auf """
        pprint.pprint(self.micodb.db)