1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# util.py
#
# Copyright (C) 2013 Tim Marston <tim@edm.am>
#
# This file is part of stdhome (hereafter referred to as "this program").
# See http://ed.am/dev/stdhome for more information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, stat, errno
def canonicalise_path( path ):
"""Return the canonicalised form of a path. That is, the fully-qualified
version of the supplied path, with ~ expanded to the user's home directory.
The returned path is guaranteed to always begin and end with a '/'.
"""
path = '/' + os.path.expanduser( path ).strip( '/' )
return path if path == '/' else path + '/'
# Class to provide the switch functionality that C/C++ programmers are so used
# to but which is missing in python. For more information, see
# http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/
class switch( object ):
def __init__( self, value ):
self.value = value
self.fall = False
def __iter__( self ):
"""Return the match method once, then stop
"""
yield self.match
raise StopIteration
def match( self, *args ):
"""Indicate whether or not to enter a case suite
"""
if self.fall or not args:
return True
elif self.value in args:
self.fall = True
return True
else:
return False
# enums
# see http://stackoverflow.com/a/1695250
def enum( **enums ):
return type( 'Enum', (), enums )
|