2015-07-12 20:36:46 +02:00
import argparse
import sys
import os
2016-11-18 20:00:15 +01:00
import locale
2017-01-21 23:00:13 +01:00
import re
2015-04-18 03:02:08 +02:00
import ConfigParser
2015-07-12 20:36:46 +02:00
2015-04-18 03:02:08 +02:00
class Config ( object ) :
2015-07-12 20:36:46 +02:00
2015-07-17 00:28:43 +02:00
def __init__ ( self , argv ) :
2016-11-18 20:15:51 +01:00
self . version = " 0.5.1 "
2017-02-05 18:36:58 +01:00
self . rev = 1859
2015-07-17 00:28:43 +02:00
self . argv = argv
self . action = None
Version 0.3.6, Rev879, Fix sidebar error on description missing, New trayicon, New favicon, Disable some functions on MultiUser proxies, New homepage, Replace only the last ? in SQL queries, Alwaays grant ADMIN permission to homepage site, Announce before publish if no peers, configSet, serverShutdown, ADMIN WebsocketAPI command, Stop Tor client before updating, Ignore peer ip packing error, Ignore db files from git, Fix safari ajax error when UiPassword enabled
2016-02-02 11:40:45 +01:00
self . config_file = " zeronet.conf "
2015-07-17 00:28:43 +02:00
self . createParser ( )
self . createArguments ( )
def createParser ( self ) :
# Create parser
self . parser = argparse . ArgumentParser ( formatter_class = argparse . ArgumentDefaultsHelpFormatter )
self . parser . register ( ' type ' , ' bool ' , self . strToBool )
self . subparsers = self . parser . add_subparsers ( title = " Action to perform " , dest = " action " )
2015-07-12 20:36:46 +02:00
def __str__ ( self ) :
return str ( self . arguments ) . replace ( " Namespace " , " Config " ) # Using argparse str output
# Convert string to bool
def strToBool ( self , v ) :
return v . lower ( ) in ( " yes " , " true " , " t " , " 1 " )
# Create command line arguments
def createArguments ( self ) :
2015-09-10 23:25:09 +02:00
trackers = [
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
" zero://boot3rdez4rzn36x.onion:15441 " ,
" zero://boot.zeronet.io#f36ca555bee6ba216b14d10f38c16f7769ff064e0e37d887603548cc2e64191d:15441 " ,
2015-11-17 12:48:03 +01:00
" udp://tracker.coppersurfer.tk:6969 " ,
2015-09-10 23:25:09 +02:00
" udp://tracker.leechers-paradise.org:6969 " ,
" udp://9.rarbg.com:2710 " ,
2017-01-21 22:59:10 +01:00
" http://tracker.opentrackr.org:1337/announce " ,
2016-09-09 12:24:14 +02:00
" http://explodie.org:6969/announce " ,
" http://tracker1.wasabii.com.tw:6969/announce "
2015-09-10 23:25:09 +02:00
]
2015-07-12 20:36:46 +02:00
# Platform specific
if sys . platform . startswith ( " win " ) :
coffeescript = " type %s | tools \\ coffee \\ coffee.cmd "
else :
coffeescript = None
2016-11-18 21:39:53 +01:00
try :
language , enc = locale . getdefaultlocale ( )
language = language . split ( " _ " ) [ 0 ]
except Exception :
language = " en "
2016-11-18 20:00:15 +01:00
2015-07-17 00:28:43 +02:00
use_openssl = True
2015-07-12 20:36:46 +02:00
2017-01-07 01:06:24 +01:00
if repr ( 1483108852.565 ) != " 1483108852.565 " :
fix_float_decimals = True
else :
fix_float_decimals = False
2017-01-22 21:22:53 +01:00
this_file = os . path . abspath ( __file__ ) . replace ( " \\ " , " / " )
if this_file . endswith ( " /Contents/Resources/core/src/Config.py " ) :
2017-01-21 23:00:13 +01:00
# Running as ZeroNet.app
2017-01-24 16:36:51 +01:00
if this_file . startswith ( " /Application " ) or this_file . startswith ( " /private " ) or this_file . startswith ( os . path . expanduser ( " ~/Library " ) ) :
2017-01-21 23:00:13 +01:00
# Runnig from non-writeable directory, put data to Application Support
2017-01-22 21:22:53 +01:00
start_dir = os . path . expanduser ( " ~/Library/Application Support/ZeroNet " ) . decode ( sys . getfilesystemencoding ( ) )
2017-01-21 23:00:13 +01:00
else :
# Running from writeable directory put data next to .app
2017-01-22 21:22:53 +01:00
start_dir = re . sub ( " /[^/]+/Contents/Resources/core/src/Config.py " , " " , this_file ) . decode ( sys . getfilesystemencoding ( ) )
2017-01-21 23:00:13 +01:00
config_file = start_dir + " /zeronet.conf "
data_dir = start_dir + " /data "
log_dir = start_dir + " /log "
2017-01-22 21:22:53 +01:00
elif this_file . endswith ( " /core/src/Config.py " ) :
2017-01-21 23:00:13 +01:00
# Running as exe or source is at Application Support directory, put var files to outside of core dir
2017-01-22 21:22:53 +01:00
start_dir = this_file . replace ( " /core/src/Config.py " , " " ) . decode ( sys . getfilesystemencoding ( ) )
2017-01-21 23:00:13 +01:00
config_file = start_dir + " /zeronet.conf "
data_dir = start_dir + " /data "
log_dir = start_dir + " /log "
2017-01-09 01:28:50 +01:00
else :
config_file = " zeronet.conf "
data_dir = " data "
log_dir = " log "
2015-07-12 20:36:46 +02:00
# Main
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " main " , help = ' Start UiServer and FileServer (default) ' )
2015-07-12 20:36:46 +02:00
# SiteCreate
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " siteCreate " , help = ' Create a new site ' )
2015-07-12 20:36:46 +02:00
2016-01-19 20:42:00 +01:00
# SiteNeedFile
action = self . subparsers . add_parser ( " siteNeedFile " , help = ' Get a file from site ' )
action . add_argument ( ' address ' , help = ' Site address ' )
action . add_argument ( ' inner_path ' , help = ' File inner path ' )
2016-03-19 18:11:12 +01:00
# SiteDownload
action = self . subparsers . add_parser ( " siteDownload " , help = ' Download a new site ' )
action . add_argument ( ' address ' , help = ' Site address ' )
2015-07-12 20:36:46 +02:00
# SiteSign
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " siteSign " , help = ' Update and sign content.json: address [privatekey] ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' address ' , help = ' Site to sign ' )
action . add_argument ( ' privatekey ' , help = ' Private key (default: ask on execute) ' , nargs = ' ? ' )
action . add_argument ( ' --inner_path ' , help = ' File you want to sign (default: content.json) ' ,
default = " content.json " , metavar = " inner_path " )
2017-01-27 12:02:14 +01:00
action . add_argument ( ' --remove_missing_optional ' , help = ' Remove optional files that is not present in the directory ' , action = ' store_true ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' --publish ' , help = ' Publish site after the signing ' , action = ' store_true ' )
# SitePublish
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " sitePublish " , help = ' Publish site to other peers: address ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' address ' , help = ' Site to publish ' )
action . add_argument ( ' peer_ip ' , help = ' Peer ip to publish (default: random peers ip from tracker) ' ,
default = None , nargs = ' ? ' )
action . add_argument ( ' peer_port ' , help = ' Peer port to publish (default: random peer port from tracker) ' ,
default = 15441 , nargs = ' ? ' )
action . add_argument ( ' --inner_path ' , help = ' Content.json you want to publish (default: content.json) ' ,
default = " content.json " , metavar = " inner_path " )
# SiteVerify
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " siteVerify " , help = ' Verify site files using sha512: address ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' address ' , help = ' Site to verify ' )
# dbRebuild
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " dbRebuild " , help = ' Rebuild site database cache ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' address ' , help = ' Site to rebuild ' )
# dbQuery
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " dbQuery " , help = ' Query site sql cache ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' address ' , help = ' Site to query ' )
action . add_argument ( ' query ' , help = ' Sql query ' )
# PeerPing
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " peerPing " , help = ' Send Ping command to peer ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' peer_ip ' , help = ' Peer ip ' )
action . add_argument ( ' peer_port ' , help = ' Peer port ' , nargs = ' ? ' )
# PeerGetFile
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " peerGetFile " , help = ' Request and print a file content from peer ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' peer_ip ' , help = ' Peer ip ' )
action . add_argument ( ' peer_port ' , help = ' Peer port ' )
action . add_argument ( ' site ' , help = ' Site address ' )
action . add_argument ( ' filename ' , help = ' File name to request ' )
2015-07-25 13:38:58 +02:00
action . add_argument ( ' --benchmark ' , help = ' Request file 10x then displays the total time ' , action = ' store_true ' )
2015-07-12 20:36:46 +02:00
2015-07-25 13:38:58 +02:00
# PeerCmd
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " peerCmd " , help = ' Request and print a file content from peer ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' peer_ip ' , help = ' Peer ip ' )
action . add_argument ( ' peer_port ' , help = ' Peer port ' )
action . add_argument ( ' cmd ' , help = ' Command to execute ' )
action . add_argument ( ' parameters ' , help = ' Parameters to command ' , nargs = ' ? ' )
# CryptSign
2015-07-17 00:28:43 +02:00
action = self . subparsers . add_parser ( " cryptSign " , help = ' Sign message using Bitcoin private key ' )
2015-07-12 20:36:46 +02:00
action . add_argument ( ' message ' , help = ' Message to sign ' )
action . add_argument ( ' privatekey ' , help = ' Private key ' )
# Config parameters
2016-03-06 00:55:50 +01:00
self . parser . add_argument ( ' --verbose ' , help = ' More detailed logging ' , action = ' store_true ' )
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --debug ' , help = ' Debug mode ' , action = ' store_true ' )
self . parser . add_argument ( ' --debug_socket ' , help = ' Debug socket connections ' , action = ' store_true ' )
2016-03-12 23:10:31 +01:00
self . parser . add_argument ( ' --debug_gevent ' , help = ' Debug gevent functions ' , action = ' store_true ' )
2015-07-17 00:28:43 +02:00
2015-09-02 19:15:55 +02:00
self . parser . add_argument ( ' --batch ' , help = " Batch mode (No interactive input for commands) " , action = ' store_true ' )
2017-01-09 01:28:50 +01:00
self . parser . add_argument ( ' --config_file ' , help = ' Path of config file ' , default = config_file , metavar = " path " )
self . parser . add_argument ( ' --data_dir ' , help = ' Path of data directory ' , default = data_dir , metavar = " path " )
self . parser . add_argument ( ' --log_dir ' , help = ' Path of logging directory ' , default = log_dir , metavar = " path " )
2015-07-17 00:28:43 +02:00
2016-11-18 20:00:15 +01:00
self . parser . add_argument ( ' --language ' , help = ' Web interface language ' , default = language , metavar = ' language ' )
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --ui_ip ' , help = ' Web interface bind address ' , default = " 127.0.0.1 " , metavar = ' ip ' )
self . parser . add_argument ( ' --ui_port ' , help = ' Web interface bind port ' , default = 43110 , type = int , metavar = ' port ' )
self . parser . add_argument ( ' --ui_restrict ' , help = ' Restrict web access ' , default = False , metavar = ' ip ' , nargs = ' * ' )
self . parser . add_argument ( ' --open_browser ' , help = ' Open homepage in web browser automatically ' ,
nargs = ' ? ' , const = " default_browser " , metavar = ' browser_name ' )
Version 0.3.6, Rev879, Fix sidebar error on description missing, New trayicon, New favicon, Disable some functions on MultiUser proxies, New homepage, Replace only the last ? in SQL queries, Alwaays grant ADMIN permission to homepage site, Announce before publish if no peers, configSet, serverShutdown, ADMIN WebsocketAPI command, Stop Tor client before updating, Ignore peer ip packing error, Ignore db files from git, Fix safari ajax error when UiPassword enabled
2016-02-02 11:40:45 +01:00
self . parser . add_argument ( ' --homepage ' , help = ' Web interface Homepage ' , default = ' 1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D ' ,
2015-07-17 00:28:43 +02:00
metavar = ' address ' )
2016-12-04 18:57:08 +01:00
self . parser . add_argument ( ' --updatesite ' , help = ' Source code update site ' , default = ' 1UPDatEDxnvHDo7TXvq6AEBARfNkyfxsp ' ,
metavar = ' address ' )
2016-08-20 10:39:52 +02:00
self . parser . add_argument ( ' --size_limit ' , help = ' Default site size limit in MB ' , default = 10 , type = int , metavar = ' size ' )
2017-01-05 03:03:17 +01:00
self . parser . add_argument ( ' --connected_limit ' , help = ' Max connected peer per site ' , default = 6 , type = int , metavar = ' connected_limit ' )
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --fileserver_ip ' , help = ' FileServer bind address ' , default = " * " , metavar = ' ip ' )
self . parser . add_argument ( ' --fileserver_port ' , help = ' FileServer bind port ' , default = 15441 , type = int , metavar = ' port ' )
self . parser . add_argument ( ' --disable_udp ' , help = ' Disable UDP connections ' , action = ' store_true ' )
self . parser . add_argument ( ' --proxy ' , help = ' Socks proxy address ' , metavar = ' ip:port ' )
2015-09-10 23:25:09 +02:00
self . parser . add_argument ( ' --ip_external ' , help = ' Set reported external ip (tested on start if None) ' , metavar = ' ip ' )
self . parser . add_argument ( ' --trackers ' , help = ' Bootstraping torrent trackers ' , default = trackers , metavar = ' protocol://address ' , nargs = ' * ' )
2015-09-12 17:13:34 +02:00
self . parser . add_argument ( ' --trackers_file ' , help = ' Load torrent trackers dynamically from a file ' , default = False , metavar = ' path ' )
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --use_openssl ' , help = ' Use OpenSSL liblary for speedup ' ,
type = ' bool ' , choices = [ True , False ] , default = use_openssl )
2016-03-19 18:05:49 +01:00
self . parser . add_argument ( ' --disable_db ' , help = ' Disable database updating ' , action = ' store_true ' )
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --disable_encryption ' , help = ' Disable connection encryption ' , action = ' store_true ' )
self . parser . add_argument ( ' --disable_sslcompression ' , help = ' Disable SSL compression to save memory ' ,
type = ' bool ' , choices = [ True , False ] , default = True )
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
self . parser . add_argument ( ' --keep_ssl_cert ' , help = ' Disable new SSL cert generation on startup ' , action = ' store_true ' )
2016-03-06 02:17:58 +01:00
self . parser . add_argument ( ' --max_files_opened ' , help = ' Change maximum opened files allowed by OS to this value on startup ' ,
2016-03-12 23:11:08 +01:00
default = 2048 , type = int , metavar = ' limit ' )
2015-07-25 13:38:58 +02:00
self . parser . add_argument ( ' --use_tempfiles ' , help = ' Use temporary files when downloading (experimental) ' ,
type = ' bool ' , choices = [ True , False ] , default = False )
self . parser . add_argument ( ' --stream_downloads ' , help = ' Stream download directly to files (experimental) ' ,
type = ' bool ' , choices = [ True , False ] , default = False )
2015-08-06 00:51:25 +02:00
self . parser . add_argument ( " --msgpack_purepython " , help = ' Use less memory, but a bit more CPU power ' ,
2015-09-02 19:15:55 +02:00
type = ' bool ' , choices = [ True , False ] , default = True )
2017-01-07 01:06:24 +01:00
self . parser . add_argument ( " --fix_float_decimals " , help = ' Fix content.json modification date float precision on verification ' ,
type = ' bool ' , choices = [ True , False ] , default = fix_float_decimals )
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --coffeescript_compiler ' , help = ' Coffeescript compiler for developing ' , default = coffeescript ,
metavar = ' executable_path ' )
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
self . parser . add_argument ( ' --tor ' , help = ' enable: Use only for Tor peers, always: Use Tor for every connection ' , choices = [ " disable " , " enable " , " always " ] , default = ' enable ' )
self . parser . add_argument ( ' --tor_controller ' , help = ' Tor controller address ' , metavar = ' ip:port ' , default = ' 127.0.0.1:9051 ' )
self . parser . add_argument ( ' --tor_proxy ' , help = ' Tor proxy address ' , metavar = ' ip:port ' , default = ' 127.0.0.1:9050 ' )
2016-12-27 15:15:02 +01:00
self . parser . add_argument ( ' --tor_password ' , help = ' Tor controller password ' , metavar = ' password ' )
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
2015-07-17 00:28:43 +02:00
self . parser . add_argument ( ' --version ' , action = ' version ' , version = ' ZeroNet %s r %s ' % ( self . version , self . rev ) )
return self . parser
2015-07-12 20:36:46 +02:00
2015-09-12 17:13:34 +02:00
def loadTrackersFile ( self ) :
self . trackers = [ ]
for tracker in open ( self . trackers_file ) :
if " :// " in tracker :
self . trackers . append ( tracker . strip ( ) )
# Find arguments specified for current action
2015-07-12 20:36:46 +02:00
def getActionArguments ( self ) :
back = { }
arguments = self . parser . _subparsers . _group_actions [ 0 ] . choices [ self . action ] . _actions [ 1 : ] # First is --version
for argument in arguments :
back [ argument . dest ] = getattr ( self , argument . dest )
return back
2015-07-17 00:28:43 +02:00
# Try to find action from argv
2015-07-12 20:36:46 +02:00
def getAction ( self , argv ) :
actions = [ action . choices . keys ( ) for action in self . parser . _actions if action . dest == " action " ] [ 0 ] # Valid actions
found_action = False
2015-07-17 00:28:43 +02:00
for action in actions : # See if any in argv
2015-07-12 20:36:46 +02:00
if action in argv :
found_action = action
break
return found_action
2015-07-17 00:28:43 +02:00
# Move plugin parameters to end of argument list
def moveUnknownToEnd ( self , argv , default_action ) :
valid_actions = sum ( [ action . option_strings for action in self . parser . _actions ] , [ ] )
valid_parameters = [ ]
plugin_parameters = [ ]
plugin = False
for arg in argv :
if arg . startswith ( " -- " ) :
if arg not in valid_actions :
plugin = True
else :
plugin = False
elif arg == default_action :
plugin = False
if plugin :
plugin_parameters . append ( arg )
else :
valid_parameters . append ( arg )
return valid_parameters + plugin_parameters
# Parse arguments from config file and command line
def parse ( self , silent = False , parse_config = True ) :
if silent : # Don't display messages or quit on unknown parameter
original_print_message = self . parser . _print_message
original_exit = self . parser . exit
2015-09-27 02:08:53 +02:00
def silencer ( parser , function_name ) :
2015-07-17 00:28:43 +02:00
parser . exited = True
return None
self . parser . exited = False
2015-09-27 02:08:53 +02:00
self . parser . _print_message = lambda * args , * * kwargs : silencer ( self . parser , " _print_message " )
self . parser . exit = lambda * args , * * kwargs : silencer ( self . parser , " exit " )
2015-07-17 00:28:43 +02:00
argv = self . argv [ : ] # Copy command line arguments
if parse_config :
argv = self . parseConfig ( argv ) # Add arguments from config file
self . parseCommandline ( argv , silent ) # Parse argv
self . setAttributes ( )
if silent : # Restore original functions
if self . parser . exited and self . action == " main " : # Argument parsing halted, don't start ZeroNet with main action
self . action = None
self . parser . _print_message = original_print_message
self . parser . exit = original_exit
2015-07-12 20:36:46 +02:00
# Parse command line arguments
2015-07-17 00:28:43 +02:00
def parseCommandline ( self , argv , silent = False ) :
2015-07-12 20:36:46 +02:00
# Find out if action is specificed on start
action = self . getAction ( argv )
2015-07-17 00:28:43 +02:00
if not action :
2015-07-12 20:36:46 +02:00
argv . append ( " main " )
2015-07-17 00:28:43 +02:00
action = " main "
argv = self . moveUnknownToEnd ( argv , action )
if silent :
res = self . parser . parse_known_args ( argv [ 1 : ] )
if res :
self . arguments = res [ 0 ]
else :
self . arguments = { }
else :
self . arguments = self . parser . parse_args ( argv [ 1 : ] )
2015-07-12 20:36:46 +02:00
# Parse config file
def parseConfig ( self , argv ) :
# Find config file path from parameters
if " --config_file " in argv :
Version 0.3.6, Rev879, Fix sidebar error on description missing, New trayicon, New favicon, Disable some functions on MultiUser proxies, New homepage, Replace only the last ? in SQL queries, Alwaays grant ADMIN permission to homepage site, Announce before publish if no peers, configSet, serverShutdown, ADMIN WebsocketAPI command, Stop Tor client before updating, Ignore peer ip packing error, Ignore db files from git, Fix safari ajax error when UiPassword enabled
2016-02-02 11:40:45 +01:00
self . config_file = argv [ argv . index ( " --config_file " ) + 1 ]
2015-07-12 20:36:46 +02:00
# Load config file
Version 0.3.6, Rev879, Fix sidebar error on description missing, New trayicon, New favicon, Disable some functions on MultiUser proxies, New homepage, Replace only the last ? in SQL queries, Alwaays grant ADMIN permission to homepage site, Announce before publish if no peers, configSet, serverShutdown, ADMIN WebsocketAPI command, Stop Tor client before updating, Ignore peer ip packing error, Ignore db files from git, Fix safari ajax error when UiPassword enabled
2016-02-02 11:40:45 +01:00
if os . path . isfile ( self . config_file ) :
2015-07-12 20:36:46 +02:00
config = ConfigParser . ConfigParser ( allow_no_value = True )
Version 0.3.6, Rev879, Fix sidebar error on description missing, New trayicon, New favicon, Disable some functions on MultiUser proxies, New homepage, Replace only the last ? in SQL queries, Alwaays grant ADMIN permission to homepage site, Announce before publish if no peers, configSet, serverShutdown, ADMIN WebsocketAPI command, Stop Tor client before updating, Ignore peer ip packing error, Ignore db files from git, Fix safari ajax error when UiPassword enabled
2016-02-02 11:40:45 +01:00
config . read ( self . config_file )
2015-07-12 20:36:46 +02:00
for section in config . sections ( ) :
for key , val in config . items ( section ) :
if section != " global " : # If not global prefix key with section
key = section + " _ " + key
if val :
2015-09-12 17:13:34 +02:00
for line in val . strip ( ) . split ( " \n " ) : # Allow multi-line values
2015-09-10 23:25:09 +02:00
argv . insert ( 1 , line )
2015-07-12 20:36:46 +02:00
argv . insert ( 1 , " -- %s " % key )
return argv
# Expose arguments as class attributes
def setAttributes ( self ) :
# Set attributes from arguments
2015-07-17 00:28:43 +02:00
if self . arguments :
args = vars ( self . arguments )
for key , val in args . items ( ) :
setattr ( self , key , val )
def loadPlugins ( self ) :
from Plugin import PluginManager
@PluginManager.acceptPlugins
class ConfigPlugin ( object ) :
def __init__ ( self , config ) :
self . parser = config . parser
self . createArguments ( )
def createArguments ( self ) :
pass
ConfigPlugin ( self )
2015-04-18 03:02:08 +02:00
2016-11-07 22:51:43 +01:00
def saveValue ( self , key , value ) :
if not os . path . isfile ( self . config_file ) :
content = " "
else :
content = open ( self . config_file ) . read ( )
lines = content . splitlines ( )
global_line_i = None
key_line_i = None
i = 0
for line in lines :
if line . strip ( ) == " [global] " :
global_line_i = i
if line . startswith ( key + " = " ) :
key_line_i = i
i + = 1
if value is None : # Delete line
if key_line_i :
del lines [ key_line_i ]
else : # Add / update
new_line = " %s = %s " % ( key , str ( value ) . replace ( " \n " , " " ) . replace ( " \r " , " " ) )
if key_line_i : # Already in the config, change the line
lines [ key_line_i ] = new_line
elif global_line_i is None : # No global section yet, append to end of file
lines . append ( " [global] " )
lines . append ( new_line )
else : # Has global section, append the line after it
lines . insert ( global_line_i + 1 , new_line )
open ( self . config_file , " w " ) . write ( " \n " . join ( lines ) )
2015-04-18 03:02:08 +02:00
2015-07-17 00:28:43 +02:00
config = Config ( sys . argv )