/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/program.py

  • Committer: Tim Marston
  • Date: 2014-04-06 14:09:14 UTC
  • Revision ID: tim@ed.am-20140406140914-lyd6n7proccrcr7v
minor tweaks to variable names and comments

Show diffs side-by-side

added added

removed removed

19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
22
 
import os, sys, getopt
 
22
import os, sys, getopt, ConfigParser
23
23
import the
24
 
from config import Config
25
24
from vcs.vcs import Vcs
26
25
 
27
26
 
124
123
                return None
125
124
 
126
125
 
 
126
        def get_command_argument( self, args ):
 
127
                """
 
128
                Find the first program argument what isn't an option argument.
 
129
 
 
130
        Arguments:
 
131
        - `args`: the program arguments
 
132
        """
 
133
                while args:
 
134
                        if args[ 0 ] == '--':
 
135
                                return args[ 1 ] if len( args ) > 1 else None
 
136
                        if args[ 0 ][ 0 : 1 ] != '-':
 
137
                                return args[ 0 ]
 
138
                        args = args[ 1 : ]
 
139
                return None
 
140
 
 
141
 
127
142
        def run( self ):
128
143
                # make an initial attempt to parse the command line, looking only for
129
144
                # --help and --version, so that they have the chance to run without a
147
162
                        pass
148
163
 
149
164
                # read program configuration
150
 
                the.config = Config()
 
165
                self.read_config()
151
166
 
152
 
                # the first argument should be the command
153
 
                the.command = sys.argv[ 1 ] if len( sys.argv ) > 1 else None
 
167
                # find the first non-option argument (the command)
 
168
                the.command = self.get_command_argument( sys.argv[ 1: ] )
154
169
                if the.command == None:
155
170
                        self.print_usage( "missing command" )
156
171
 
189
204
                        instance.run()
190
205
                except Vcs.VcsError as e:
191
206
                        message = e.msg.rstrip()
192
 
                        if the.verbose >= 1:
 
207
                        if the.verbose:
193
208
                                message += '\n\nOUTPUT:\n' + e.output.rstrip()
194
209
                        self.die( message )
195
210
                except self.FatalError as e:
196
211
                        self.die( e.msg )
197
212
 
198
213
 
 
214
        def read_config( self ):
 
215
                config = ConfigParser.SafeConfigParser( allow_no_value = True )
 
216
                config.read( the.config_file )
 
217
                if config.has_option( 'stdhome', 'home-dir' ):
 
218
                        the.home_dir = config.get( 'stdhome', 'home-dir' )
 
219
 
 
220
 
199
221
        class UsageError( Exception ):
200
222
 
201
223
                def __init__( self, error_message ):