[Clippy-commits] SF.net SVN: clippy:[10] clippy
Status: Pre-Alpha
Brought to you by:
edheldil
|
From: <edh...@us...> - 2010-01-21 22:30:54
|
Revision: 10
http://clippy.svn.sourceforge.net/clippy/?rev=10&view=rev
Author: edheldil
Date: 2010-01-21 22:30:46 +0000 (Thu, 21 Jan 2010)
Log Message:
-----------
Work on refactoring config
Modified Paths:
--------------
clippy/config.py
clippy/loader.py
clippy/repository.py
clippy/ui/assistant/app.py
clippy/ui/shell/app.py
clippy/utils.py
Modified: clippy/config.py
===================================================================
--- clippy/config.py 2010-01-07 23:35:31 UTC (rev 9)
+++ clippy/config.py 2010-01-21 22:30:46 UTC (rev 10)
@@ -19,19 +19,23 @@
self.user_config = None
self.createConfig ()
-
# FIXME: is it a good idea?
#atexit.register (self.write_user_config, self.CONFIG_FILE)
- def read_file (self, filename):
+
+ def read_file (self, filename, user_config=False):
cfg = ConfigFile ()
cfg.read (filename)
self.addConfig (cfg)
+ if user_config:
+ self.user_config = cfg
+
def write_user_config (self):
if self.user_config and self.dirty:
self.user_config.write ()
+
def createConfig (self):
try:
os.mkdir (self.CONFIG_DIR)
@@ -43,8 +47,6 @@
#self.config['ui.win_main.geometry'] = '500x350+20+20'
-
-
def get (self, key, default = None):
# FIXME: get rid of default value?
try:
@@ -65,19 +67,23 @@
self.config[key] = value
# FIXME: set key in userconfig and make it dirty
+
def add (self, key, value):
if not key in self.config:
self.config[key] = value
+
def createKey (self, key, description, default):
self.config[key] = default
+
def addConfig (self, cfg):
if isinstance (cfg, ConfigFile):
cfg = cfg.config
for key, value in cfg.items ():
self.add (key, value)
+
# def getConfigTree (self, prefix):
# if not prefix.endswith ('.'):
# raise ValueError ("Prefix does not end with '.': " + prefix)
Modified: clippy/loader.py
===================================================================
--- clippy/loader.py 2010-01-07 23:35:31 UTC (rev 9)
+++ clippy/loader.py 2010-01-21 22:30:46 UTC (rev 10)
@@ -14,8 +14,8 @@
repository_map = {}
- def __init__ (self):
- pass
+ def __init__ (self, config):
+ self.config = config
def loadRepositories (self, repo_dir):
import clippy.repositories
@@ -50,21 +50,7 @@
id = config['id']
# FIXME: check that id is unique?
- klass = config['class']
- klass = self.getRepositoryClass (klass)
-
- # NOTE: the effect is that config keys defined in main config file
- # shadow those in a repo definition file and in a class.
- #for key, value in config.items ():
- # if key == 'id' or key == 'class':
- # continue
- #
- # key = 'repo.' + id + '.' + key
- # if not key in self.config:
- # #self.config[key] = value
- # pass
-
- self.registerRepository (id, klass, config)
+ self.registerRepository (id, None, config)
#yield True
#yield False
@@ -73,28 +59,73 @@
def registerRepository (id, klass, config = None):
# This method is called from repository modules
- # get repo's config params.
- # merge forward duplicate config keys, i.e. (name, dir, name) becomes (name, dir) (FIXME: just merge?)
- # add config params to global config if they are not there yet
# create instance and give it id and some ref to config
# repo's __init__() sets attrs based on repo.<id>.xxxx
+ if not id in Loader.repository_map:
+ Loader.repository_map[id] = [klass, config]
+ def setupRepositoryConfig (self, id, klass_id):
+ klass, config = self.repository_map[klass_id]
+ if klass is None:
+ return
+
+ if config is not None:
+ for key in config:
+ self.config.add ('repo.' + id + '.' + key, config[key])
+
params = klass.getConfigParams ()
- if config is None:
- config = {}
for param in reversed (params):
key = param[0]
- if not config.has_key (key):
- config[key] = param[4]
+ if key == 'id':
+ continue
+ self.config.add ('repo.' + id + '.' + key, param[4])
- Loader.repository_map[id] = (klass, config)
+ def setupRepositoryClass (self, id):
+ if not id:
+ print >>sys.stderr, "Skipping repo"
+ return None
+
+ klass, config = self.repository_map[id]
+ if klass:
+ return klass
+
+ # Mark this repo as "in progress", to prevent circular dependencies
+ self.repository_map[id][0] = True
+
+ try:
+ klass_id = config['class']
+ except KeyError:
+ print >>sys.stderr, "Skipping repo"
+ return None
+
+ klass = self.setupRepositoryClass (klass_id)
+ if isinstance (Repository, klass):
+ self.repository_map[id][0] = klass
+ return klass
+ else:
+ print >>sys.stderr, "Skipping repo"
+
+
+
+ def setupRepositories (self):
+ for id in self.repository_map:
+ self.setupRepositoryClass (id)
+
+ # FIXME: delete all repos without a class
+
+ for id in self.repository_map:
+ self.setupRepositoryConfig (id, id)
+ base = self.config.get ('repo.' + id + '.class')
+ if base:
+ self.setupRepositoryConfig (app, id, base)
+
def getRepositories (self):
return self.repository_map
def getRepository (self, id):
klass, config = self.getRepositoryClass (id)
- repo = klass (id, config.copy ())
+ repo = klass (id, self.config)
return repo
Modified: clippy/repository.py
===================================================================
--- clippy/repository.py 2010-01-07 23:35:31 UTC (rev 9)
+++ clippy/repository.py 2010-01-21 22:30:46 UTC (rev 10)
@@ -55,10 +55,10 @@
def getConfig (self, key):
# FIXME: recursive search in ancestors
- return self.config[key]
+ return self.config.get ('repo.' + self.id + '.' + key)
def setConfig (self, key, value):
- self.config[key] = value
+ self.config.set ('repo.' + self.id + '.' + key, value)
def getQueryParams (self):
# list of tuples: (key, label, type, desc, default)
Modified: clippy/ui/assistant/app.py
===================================================================
--- clippy/ui/assistant/app.py 2010-01-07 23:35:31 UTC (rev 9)
+++ clippy/ui/assistant/app.py 2010-01-21 22:30:46 UTC (rev 10)
@@ -9,7 +9,7 @@
import gtk
import gtk.glade
-from clippy import loader, utils
+from clippy import loader, utils, config
from clippy.ui import glfactory
from clippy.ui.assistant import win_main
from clippy.ui.assistant import win_repo_config
@@ -20,7 +20,10 @@
#################################################
#################################################
-class ClippyAssistant:
+class ClippyAssistant (object):
+ # NOTE: this would mean that the glade file would have to be with python files, not in /usr/share where it belongs
+ GLADE_FILE = os.path.join (os.path.dirname (__file__) , 'ui.glade')
+
column_hash = {
'name': ( 'Name', 'str'),
'id': ( 'ID', 'str'),
@@ -60,6 +63,8 @@
self.win_main = None
#win_main.app = app
+ self.config = config.Config ()
+
# FIXME: maybe cli args and config should be called first
#print "Init Core"
self.loader = loader.Loader ()
@@ -70,6 +75,7 @@
def main (self):
self.parse_args ()
+ self.read_config ()
self.setup ()
self.run ()
@@ -102,6 +108,13 @@
default = False,
help = "skip download confirmation page")
+# oparser.add_option ("-o",
+# "--option",
+# dest = "options",
+# action = "store_multiple",
+# default = [],
+# help = "set cfg file option")
+
(opts, args) = oparser.parse_args ()
@@ -118,9 +131,12 @@
self.skip_confirm = opts.skip_confirm
+ def read_config (self):
+ self.config.read_file (self.config.CONFIG_FILE, True)
+ self.config.read_file (self.config.SYS_CONFIG_FILE)
def setup (self):
- self.glade_file = os.path.expanduser (self.getConfig ('ui.glade_file'))
+ self.glade_file = os.path.expanduser (self.config.get ('ui.assistant.glade_file', self.GLADE_FILE))
#print "Loading UI"
@@ -137,7 +153,7 @@
gtk.main ()
def restore_geometry (self, window, window_name):
- geometry = self.getConfig ('ui.%s.geometry' %window_name)
+ geometry = self.config.get ('ui.assistant.%s.geometry' %window_name, '')
mo = re.match (r'(\d+)x(\d+)([+-]\d+)([+-]\d+)', geometry)
if mo is not None:
geometry = map (int, mo.group (3, 4, 1, 2))
@@ -148,7 +164,7 @@
x, y = window.get_position ()
w, h = window.get_size ()
geometry = "%dx%d+%d+%d" %(w, h, x, y)
- self.setConfig ('ui.%s.geometry'%window_name, geometry)
+ self.config.set ('ui.%s.geometry'%window_name, geometry)
# FIXME: account for fullscreen
def progress_fn (self, msg, percent):
@@ -167,17 +183,15 @@
# NOTE: this method should be overridden in a child class
- def getConfig (self, key):
- # FIXME: should be in default config?
- cfg = {
- 'ui.glade_file': os.path.join (os.path.dirname (__file__) , 'ui.glade'), # NOTE: glade file has to be with python files, not in /usr/share
- 'ui.win_main.geometry': '500x350+30+30',
- }
-
- return cfg[key]
+# def getConfig (self, key):
+# # FIXME: should be in default config?
+# cfg = {
+# 'ui.glade_file': os.path.join (os.path.dirname (__file__) , 'ui.glade'), # NOTE: glade file has to be with python files, not in /usr/share
+# 'ui.win_main.geometry': '500x350+30+30',
+# }
+#
+# return cfg[key]
- def setConfig (self, key, value):
- pass
# def open_url (self, url):
# if app.browser.find ('%s') >= 0:
Modified: clippy/ui/shell/app.py
===================================================================
--- clippy/ui/shell/app.py 2010-01-07 23:35:31 UTC (rev 9)
+++ clippy/ui/shell/app.py 2010-01-21 22:30:46 UTC (rev 10)
@@ -6,6 +6,7 @@
import readline
import sys
import traceback
+from clippy.config import Config
from clippy.loader import Loader
from clippy.query import Query
import clippy.utils as utils
@@ -14,8 +15,11 @@
HISTORY_FILE = os.path.join (utils.CONFIG_DIR, "cclippy.history")
def __init__ (self):
- self.loader = Loader ()
+ self.config = Config ()
+ # FIXME: read cfg files ...
+ self.loader = Loader (self.config)
self.loader.loadRepositories (utils.USER_REPO_DIR)
+ self.loader.setupRepositories ()
self._readline_init ()
self.encoding = 'ascii'
@@ -150,8 +154,8 @@
for (i, id) in enumerate (sorted (self.loader.getRepositories ())):
# FIXME: this is rather ugly
klass, config = self.loader.getRepositoryClass (id)
- name = config['name']
- desc = config['desc']
+ name = self.config.get ('repo.' + id + '.name')
+ desc = self.config.get ('repo.' + id + '.desc')
print '[%d]\t%s - %s\n\t\t%s' %(i, id, name, desc)
else:
try:
Modified: clippy/utils.py
===================================================================
--- clippy/utils.py 2010-01-07 23:35:31 UTC (rev 9)
+++ clippy/utils.py 2010-01-21 22:30:46 UTC (rev 10)
@@ -9,17 +9,7 @@
CONFIG_FILE = os.path.join (CONFIG_DIR, 'clippy.cfg')
USER_REPO_DIR = os.path.join (CONFIG_DIR, 'repositories')
-if False:
- if config is None:
- self.config = config.Config ()
- else:
- self.config = config
- #self.createConfig ()
- self.config.read (self.CONFIG_FILE)
- # FIXME: is it a good idea?
- atexit.register (self.writeConfigFile, self.CONFIG_FILE)
-
XFER_START = 0
XFER_FILE_START = 1
XFER_FILE_UPDATE = 2
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|