Package sabx10 :: Package osm :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module sabx10.osm.utils

 1  ############################################################################### 
 2  # 
 3  # sabx10 - an SABX file manipulation library 
 4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
 5  # 
 6  # This file is part of sabx10. 
 7  # 
 8  # sabx10 is free software: you can redistribute it and/or modify it under the 
 9  # terms of the GNU General Public License as published by the Free Software 
10  # Foundation, either version 3 of the License, or (at your option) any later 
11  # version. 
12  # 
13  # sabx10 is distributed in the hope that it will be useful, but WITHOUT ANY 
14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
15  # A PARTICULAR PURPOSE.  See the GNU General Public License for more details. 
16  # 
17  # You should have received a copy of the GNU General Public License along with 
18  # sabx10.  If not, see <http://www.gnu.org/licenses/>. 
19  # 
20  ############################################################################### 
21  """ 
22  Utility routines. 
23  """ 
24   
25  import os, sys 
26   
27 -class BaseId(object):
28 """ 29 Generate sequential, unique ids. 30 31 @ivar next_id: next id to return 32 @type next_id: C{int} 33 """
34 - def __init__(self, start_id=1):
35 """ 36 Save the starting id. 37 38 @param start_id: starting id 39 @type start_id: C{int} 40 """ 41 self.next_id = start_id
42
43 - def next(self):
44 """ 45 Get the next id in the sequence and increment it. 46 47 @return: next id 48 @rtype: C{int} 49 """ 50 ret_val = self.next_id 51 self.next_id += 1 52 return ret_val
53
54 -class NodeId(BaseId):
55 """ 56 A L{BaseId} modification that starts numbering after the ids in the 57 waypoints for a given segment list. 58 """ 59
60 - def __init__(self, seg_list):
61 """ 62 Get the max id in the segment list and start at the next highest one. 63 64 @param seg_list: C{list} of L{Segment}s to check against 65 @type seg_list: C{list} of L{Segment} 66 """ 67 start_id = max([max([int(pt.id) for pt in seg.waypoints]) 68 for seg in seg_list]) + 1 69 BaseId.__init__(self, start_id)
70
71 -def determine_path():
72 """ 73 Determine the path to the file containing this routine. This is handy for 74 getting the directory a package is located in. Obviously, this works best 75 when all the files for a package are in the same directory and not split 76 into sub-directories. 77 78 This is based on code found in the distutils tutorial at 79 U{http://wiki.python.org/moin/Distutils/Tutorial}. Apparently the tutorial 80 author found it in wxglade.py. 81 82 @return: directory part of path this file is in 83 @rtype: C{string} 84 """ 85 root = __file__ 86 if os.path.islink (root): 87 root = os.path.realpath (root) 88 return os.path.dirname (os.path.abspath (root))
89