source: subversion/applications/rendering/toposm/common.py @ 29599

Last change on this file since 29599 was 25435, checked in by ahlzen, 9 years ago

Minor refactoring and cleanup of the TopOSM code.

File size: 1.6 KB
Line 
1#!/usr/bin/python
2
3"""common.py: Common utility functions for TopOSM"""
4
5import os, time, sys
6from os import path
7from threading import Lock
8
9from env import *
10
11__author__      = "Lars Ahlzen"
12__copyright__   = "(c) Lars Ahlzen 2008-2011"
13__license__     = "GPLv2"
14
15
16class ConsoleManager:
17    lock = Lock()
18    def printMessage(self, message):
19        ConsoleManager.lock.acquire()
20        print message
21        ConsoleManager.lock.release()
22
23class ErrorLog:
24    lock = Lock()
25    def log(self, message, exception = None):
26        timestr = time.strftime('[%Y-%m-%d %H:%M:%S]')
27        ErrorLog.lock.acquire()
28        try:
29            file = open(ERRORLOG, 'a')
30            file.write("%s %s (%s)\n" % (timestr, message, str(exception)))
31            file.close()
32        except:
33            print "Failed to write to the error log:"
34            print "%s %s" % (sys.exc_info()[0], sys.exc_info()[1])
35        finally:
36            ErrorLog.lock.release()       
37
38console = ConsoleManager()
39errorLog = ErrorLog()
40
41
42# global locking object for file system ops (e.g. creating directories)
43fslock = Lock()
44
45def ensureDirExists(path):
46    fslock.acquire()
47    try:
48        if not os.path.isdir(path):
49            os.makedirs(path)
50    finally:
51        fslock.release()
52
53def tryRemove(filename):
54    fslock.acquire()
55    try:
56        if path.isfile(filename):
57            os.remove(filename)
58    finally:
59        fslock.release()
60
61def writeEmpty(filename):
62    "Overwrites the specified filename with a new empty file."
63    fslock.acquire()
64    try:
65        open(filename, 'w').close();
66    finally:
67        fslock.release()
68
69
Note: See TracBrowser for help on using the repository browser.