httpserve
  • Overview
  • Repository
  • Tickets
  • Statistics

Repository

Put configuration file in a subdirectory

Parent commits : b13b2a8c3a85ad7dc686b4334f24fecb55c9bb16,
Children commits : 6e9f1d70a481e72af9d71b6f9b93fb73bd94af18,

By Laurent Defert on 2013-05-25 15:33:15
Put configuration file in a subdirectory

Browse content
Difference with parent commit b13b2a8c3a85ad7dc686b4334f24fecb55c9bb16
Files modified:
httpserve.py
--- 
+++ 
@@ -498,7 +498,7 @@
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Serve a file over http.')
     parser.add_argument('--config', '-c', metavar='configuration file', type=str, nargs=1,
-                       help='Configuration file', default=None)
+                       help='Configuration file', default='~/%s/%s' % (DEFAULT_CONF_DIR, DEFAULT_CONF_FILE))
     parser.add_argument('--count', '-n', metavar='n', type=int, nargs=1,
                        help='Uploads count before exiting', default=[1])
     parser.add_argument('--fork', '-k', metavar='n', type=int, nargs=1,
@@ -525,44 +525,43 @@
                         help='SSL private key file', default=None)
     parser.add_argument('--ssl_cert', type=str, nargs=1, metavar='file',
                         help='SSL certificate file', default=None)
-    parser.add_argument('--ssl_duration', type=int, nargs=1, metavar='hours',
-                        help='SSL generated certificate validity duration in hours', default=1)
+    parser.add_argument('--ssl_duration', type=int, nargs=1, metavar='days',
+                        help='SSL generated certificate validity duration in days', default=365)
     parser.add_argument('filename', type=str, nargs='+',
                        help='Files to serve')
     args = parser.parse_args()
 
     # Load config from configuration file
-    if args.config is None:
-        try:
-            args.config = [os.path.join(os.environ['HOME'], DEFAULT_CONF_FILE)]
+    args.config = os.path.expanduser(args.config)
+    if not os.path.exists(args.config):
+        # Write default configuration file
+        print 'Creating default configuration in file %s' % args.config
+        try:
+            conf_dir = os.path.join(os.environ['HOME'], DEFAULT_CONF_DIR)
+            if not os.path.isdir(conf_dir):
+                os.mkdir(conf_dir)
         except KeyError:
             pass
-
-    if not args.config is None:
-        args.config = args.config[0]
-        if not os.path.exists(args.config):
-            # Write default configuration file
-            print 'Creating default configuration in file %s' % args.config
-            fp = open(args.config, 'w')
-            fp.write(DEFAULT_CONF)
-            fp.close()
-        conf = ConfigParser.ConfigParser()
-        conf.read(args.config)
-
-        if conf.has_section(CONFIG_SECTION):
-            for option, value in conf.items(CONFIG_SECTION, {}):
-                if value.lower() in ['false', 'true', '0', '1']:
-                    if value.lower() in ['false', '0']:
-                        value = False
-                    else:
-                        value = True
-                if hasattr(args, option):  # Command line arguments have priority over config files ones
-                    kw = {option: value}
-                    parser.set_defaults(**kw)
+        fp = open(args.config, 'w')
+        fp.write(DEFAULT_CONF)
+        fp.close()
+    conf = ConfigParser.ConfigParser()
+    conf.read(args.config)
+
+    if conf.has_section(CONFIG_SECTION):
+        for option, value in conf.items(CONFIG_SECTION, {}):
+            if value.lower() in ['false', 'true', '0', '1']:
+                if value.lower() in ['false', '0']:
+                    value = False
                 else:
-                    print >> sys.stderr, 'Invalid option "%s"' % option
-                    sys.exit(1)
-            args = parser.parse_args()
+                    value = True
+            if hasattr(args, option):  # Command line arguments have priority over config files ones
+                kw = {option: value}
+                parser.set_defaults(**kw)
+            else:
+                print >> sys.stderr, 'Invalid option "%s"' % option
+                sys.exit(1)
+        args = parser.parse_args()
 
     for single_opt in ['config', 'count', 'bw_limit', 'public_ip', 'listen_address', 'listen_port', 'allowed_ip', 'forbidden_code', 'fork', 'ssl_proto', 'ssl_cert', 'ssl_key', 'ssl_duration']:
         val = getattr(args, single_opt)