home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / config.py < prev    next >
Encoding:
Python Source  |  2006-03-02  |  2.1 KB  |  56 lines

  1. #!/usr/bin/python
  2. # Example demonstrating how to use the configuration/commandline system
  3. # for configuration.
  4. # Some valid command lines..
  5. #   config.py -h --help            ; Turn on help
  6. #   config.py -no-h --no-help --help=no  ; Turn off help
  7. #   config.py -qqq -q=3            ; verbosity to 3
  8. #   config.py -c /etc/apt/apt.conf ; include that config file]
  9. #   config.py -o help=true         ; Turn on help by giving a config file string
  10. #   config.py -no-h -- -help       ; Turn off help, specify the file '-help'
  11. # -c and -o are standard APT-program options.
  12.  
  13. # This shows how to use the system for configuration and option control.
  14. # The other varient is for ISC object config files. See configisc.py.
  15. import apt_pkg,sys,posixpath;
  16.  
  17. # Create a new empty Configuration object - there is also the system global
  18. # configuration object apt_pkg.Config which is used interally by apt-pkg
  19. # routines to control unusual situations. I recommend using the sytem global
  20. # whenever possible..
  21. Cnf = apt_pkg.newConfiguration();
  22.  
  23. print "Command line is",sys.argv
  24.  
  25. # Load the default configuration file, InitConfig() does this better..
  26. Cnf.Set("config-file","/etc/apt/apt.conf");  # or Cnf["config-file"] = "..";
  27. if posixpath.exists(Cnf.FindFile("config-file")):
  28.    apt_pkg.ReadConfigFile(Cnf,"/etc/apt/apt.conf");
  29.  
  30. # Merge the command line arguments into the configuration space
  31. Arguments = [('h',"help","help"),
  32.              ('v',"version","version"),
  33.              ('q',"quiet","quiet","IntLevel"),
  34.              ('c',"config-file","","ConfigFile"),
  35.          ('o',"option","","ArbItem")]
  36. print "FileNames",apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
  37.  
  38. print "Quiet level selected is",Cnf.FindI("quiet",0);
  39.  
  40. # Do some stuff with it
  41. if Cnf.FindB("version",0) == 1:
  42.    print "Version selected - 1.1";
  43.  
  44. if Cnf.FindB("help",0) == 1:
  45.    print apt_pkg.Package,apt_pkg.Version,"for",apt_pkg.Architecture, \
  46.          "compiled on",apt_pkg.Date,apt_pkg.Time;
  47.    print "Hi, I am the help text for this program";
  48.    sys.exit(0);
  49.  
  50. print "No help for you, try -h";
  51.  
  52. # Print the configuration space
  53. print "The Configuration space looks like:";
  54. for I in Cnf.keys():
  55.    print "%s \"%s\";"%(I,Cnf[I]);
  56.