Example #1
0
    def _save_to_history(self, key, value):
        javaprops = JavaProperties()
        historyFileName = "{}/.{}".format(self.historyFilePath, self.repoName)

        try:
            with open(historyFileName) as f:
                javaprops.load(f)
        except (FileNotFoundError, UnboundLocalError):
            logger.debug("Could not read history file %s" % historyFileName)
            pass

        javaprops.set_property(key, value)

        try:
            basedir = os.path.dirname(historyFileName)
            if not os.path.exists(basedir):
                os.makedirs(basedir)
            with open(historyFileName, mode='w') as f:
                javaprops.store(f)
            self.history = javaprops.get_property_dict()
        except (FileNotFoundError, UnboundLocalError):
            logger.debug("Could not save to history file %s" % historyFileName)
            raise

        return
Example #2
0
class History:

    
    #self.history = self._load_history()

    def __init__(self, base_path, reponame):
        self.javaprops = JavaProperties()
        self.base_path = base_path
        self.reponame = reponame
        self.property_dict = {LAST_TRACKED_TIMESTAMP_HISTORY_FIELD:None}
        self.filename = "{}/{}".format(base_path, reponame + ".dat")

        try:
            with open(self.filename) as f:
                self.javaprops.load(f)
            self.property_dict = self.javaprops.get_property_dict()
            logger.debug("Read succesfully history file %s" % self.filename)
        except (FileNotFoundError, UnboundLocalError):
            logger.debug("History file %s does not exist. Creating one..." % self.filename)

    def save_last_tracked_timestamp(self, timestamp):
        try:
            if not os.path.exists(self.base_path):
                os.makedirs(self.base_path)
            self.javaprops.set_property(LAST_TRACKED_TIMESTAMP_HISTORY_FIELD, timestamp)    
            with open(self.filename, mode='w') as f:
                self.javaprops.store(f)
            self.property_dict = self.javaprops.get_property_dict()    
        except (FileNotFoundError, UnboundLocalError):
            logger.debug("Could not save to history file %s" % self.filename)
            raise

    def get_last_tracked_timestamp(self):
        return self.property_dict.get(LAST_TRACKED_TIMESTAMP_HISTORY_FIELD, None)
    def test_store(self):
        filename = os.path.join(TEST_DATA_FOLDER, 'simple.properties')
        simple_java_properties = JavaProperties()
        with open(filename) as property_file:
            simple_java_properties.load(property_file)
        simple_java_properties['country'] = 'Angola'
        output_filename = os.path.join(OUTPUT_FOLDER, 'simple_%s.properties' % datetime.now().strftime('%Y%m%d_%H%M'))
        with open(output_filename, mode='w') as output:
            simple_java_properties.store(output)

        with open(output_filename) as property_file:
            simple_java_properties.load(property_file)

        self.assertEqual('Angola', simple_java_properties['country'])