Changeset 95

Show
Ignore:
Timestamp:
Wed Aug 10 17:15:20 2005
Author:
fredrikc
Message:

Use XML as config file format.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/transolution/xliffeditor/gtk_main.py

    r94 r95  
    400 400  
    401 401         # automatic tm lookup  
    402           self.cb_tm_automatic_lookup.set_active(settings.tm.automatic_lookup)  
      402         self.cb_tm_automatic_lookup.set_active(settings.tm.automatic_lookup.lower() == 'true')  
    402 402  
    403 403         # tag protection  
     
    531 531  
    532 532         from webdict import WebDict  
    533           self.webdict = WebDict(settings.webdicts[settings.webdict.active])  
      533         self.webdict = WebDict()  
      534         for site in settings.webdict.sites:  
      535             if site.name == settings.webdict.current:  
      536                 self.webdict.setUrl(site.url,site.name,encoding=site.encoding)  
    534 537  
    535 538     def onQuit(self,*args):  
     
    745 748     def onTmAutomaticLookup(self,widget):  
    746 749         if widget.get_active():  
    747               settings.tm.automatic_lookup = True  
      750             settings.tm.automatic_lookup = str(True)  
    747 750             print "Automatic TM Lookup!"  
    748 751         else:  
    749               settings.tm.automatic_lookup = False  
      752             settings.tm.automatic_lookup = str(False)  
    749 752             print "Automatic TM lookup disabled"  
    750 753  
     
    794 797         print "WebLookup Config"  
    795 798         try:  
    796               drop_down_values = ['%s'%(key) for key,val in settings.webdicts.items()]  
      799             drop_down_values = [site.name for site in settings.webdict.sites]  
    796 799             drop_down_values.sort()  
    797 800         except IOError,e:  
     
    802 805                                         msg, _('Web post url'),  
    803 806                                         drop_down_values,  
    804                                           default_val=settings.webdict.active,  
      807                                         default_val=settings.webdict.current,  
    804 807                                         default_response=gtk.RESPONSE_OK)  
    805 808         if result_webdict:  
    806               # use the first part of the result string as it can have language description e.g. "AF-ZA (South Africa)"  
    807               settings.webdict.active = result_webdict.split()[0]  
    808               url = settings.webdicts[settings.webdict.active]  
    809               self.webdict.setUrl(settings.webdicts[settings.webdict.active])  
      809             settings.webdict.current = result_webdict  
      810             # Get the site that matches  
      811             site, = [site for site in settings.webdict.sites if site.name == settings.webdict.current]  
      812             self.webdict.setUrl(site.url,site.name,encoding=site.encoding)  
    810 813  
    811 814  
  • trunk/transolution/xliffeditor/gtk_documentviewpanel.py

    r91 r95  
    553 553     def openTransUnit(self,*args,**kwargs):  
    554 554         ret = DocumentViewPanelBase.openTransUnit(self,*args,**kwargs)  
    555           if not settings.tm.automatic_lookup:  
      555         if not settings.tm.automatic_lookup.lower =='true':  
    555 555             return ret  
    556 556  
  • trunk/transolution/xliffeditor/settings.py

    r94 r95  
    1 1 import os  
    2   import ConfigParser  
    3   from ConfigParser import NoSectionError,NoOptionError,MissingSectionHeaderError,ParsingError  
    4 2 import re  
    5 3 import sys  
     
    79 77  
    80 78  
    81   #-------------------------------  
    82    
    83   # This is the config spec  
    84   cspec = '''version = string  
    85   [tm]  
    86   address = string  
    87   automatic_lookup = option('False','True')  
    88    
    89   [fonts]  
    90   document_font = string  
    91   editor_font = string  
    92    
    93   [webdict]  
    94   active = string  
    95    
    96   [webdicts]  
    97   '''  
    98    
    99   default_conf = u'''version= 0.4b4  
    100   [tm]  
    101   address = 'http://localhost:6315'  
    102   automatic_lookup = False  
    103    
    104   [fonts]  
    105   document_font = ""  
    106   editor_font = ""  
    107    
    108   [webdict]  
    109   ; current active webdict  
    110   active = "leo_en-de_url"  
    111    
    112   [webdicts]  
    113   ; this is post urls to use, replace the search term with a %s  
    114   leo_en-de_url = "http://dict.leo.org/?searchLoc=-1&searchLocRelinked=-1&lp=ende&lp=ende&lang=en&searchLoc=1&searchLocRelinked=1&search=%s"  
    115   leo_de-en_url ="http://dict.leo.org/?searchLoc=1&searchLocRelinked=1&lp=ende&lp=ende&lang=en&searchLoc=-1&searchLocRelinked=1&search=%s"  
    116   google_en-de = "http://translate.google.com/translate_t?text=%s&ie=UTF-8&oe=UTF-8&langpair=en%%7Cde&hl=en&ie=UTF-8&oe=UTF-8"  
    117   google_de-en = "http://translate.google.com/translate_t?text=%s&ie=UTF-8&oe=UTF-8&langpair=de%%7Cen&hl=en&ie=UTF-8&oe=UTF-8"  
    118   '''  
    119    
    120 79 from transolution.path_mod import path  
    121 80  
     
    131 90         raise ConfigError('Cant get configuration root dir')  
    132 91  
    133       configfile = os.path.join(dir_root, '.xliffeditor')  
      92     configfile = os.path.join(dir_root, '.xliffeditor.xml')  
    133 92     return path(configfile)  
    134 93  
    135   from transolution.fullconfigobj import ConfigObj,Validator  
    136   from  StringIO import StringIO  
      94 import sys  
      95 from transolution.EaseXML import *  
    137 96  
      97 webdict_list = {}  
      98 webdict_list['leo_en-de_url'] = 'http://dict.leo.org/?searchLoc=-1&searchLocRelinked=-1&lp=ende&lp=ende&lang=en&searchLoc=1&searchLocRelinked=1&search=%s'  
      99 webdict_list['leo_de-en_url'] = 'http://dict.leo.org/?searchLoc=1&searchLocRelinked=1&lp=ende&lp=ende&lang=en&searchLoc=-1&searchLocRelinked=1&search=%s'  
      100 webdict_list['google_en-de'] = 'http://translate.google.com/translate_t?text=%s&ie=UTF-8&oe=UTF-8&langpair=en%%7Cde&hl=en&ie=UTF-8&oe=UTF-8'  
      101 webdict_list['google_de-en'] = 'http://translate.google.com/translate_t?text=%s&ie=UTF-8&oe=UTF-8&langpair=de%%7Cen&hl=en&ie=UTF-8&oe=UTF-8'  
      102  
      103  
      104 class tm(XMLObject):  
      105     _orderNodes = [ 'address', 'automatic_lookup' ]  
      106 ##    _stripStrings = False  
      107     address = TextNode(default='http://localhost:6315')  
      108     automatic_lookup = TextNode(default="False")  
      109  
      110 class fonts(XMLObject):  
      111     document_font= TextNode(default="")  
      112     editor_font= TextNode(default="")  
      113  
      114 class site(XMLObject):  
      115     name = StringAttribute()  
      116     encoding = StringAttribute(default='utf-8')  
      117     url = TextNode()  
      118      
      119 class webdict(XMLObject):  
      120     current = StringAttribute(optional=True)  
      121     sites = ListNode('site')  
      122  
      123 class config(XMLObject):  
      124     _entities = [ ('&xml;','eXtensible Markup Language')]  
      125     version = StringAttribute(optional=True)  
      126     tm = ItemNode('tm')  
      127     fonts = ItemNode('fonts')  
      128     webdict = ItemNode('webdict')  
      129      
      130 class xliffeditor(config):  
      131     def __init__(self,configfile=None):  
      132         config.__init__(self)  
      133         self._file = configfile  
    138 134  
    139   class Settings(ConfigObj):  
    140       # put setting classes here class == section, class properties == values  
    141       def __init__(self):  
    142           configfile = getConfigFile()  
    143           print "Config file:", configfile  
    144            
    145           # If there is no config file create one  
    146           if not configfile.isfile():  
    147               print "creating configfile"  
    148               configfile.write_text(default_conf,encoding='utf-8')  
    149    
    150           ConfigObj.__init__(self, configfile, configspec=StringIO(cspec))  
    151           if self.validate(val = Validator()) is not True:  
    152               print "The following config file settings (section,setting) had illegal values:%s"%(self.validate(val = Validator()),)  
    153           self._str2bool()  
    154    
    155       def _str2bool(self):  
    156           "convert string setting value to boolean"  
    157           if self.tm.automatic_lookup is None:  
    158               self.tm.automatic_lookup = 'False'  
    159           self.tm.automatic_lookup = self.istrue('automatic_lookup','tm')  
    160            
    161       def _bool2str(self):  
    162           "convert boolean setting to string"  
    163           self.tm.automatic_lookup = str(self.tm.automatic_lookup)  
    164            
    165 135     def save(self):  
    166           # save to file  
    167           self._bool2str()  
    168           self.write()  
    169           self._str2bool()  
    170    
    171   settings = Settings()  
      136         self._file.write_text(self.toXml())  
      137          
      138 configfile = getConfigFile()  
      139 settings = None  
      140 # If there is no config file create one  
      141 if not configfile.isfile():  
      142     settings = xliffeditor(configfile)  
      143     settings.tm = tm()  
      144     settings.fonts = fonts()  
      145     settings.webdict = webdict()  
      146     print "creating configfile"  
      147     for name,url in webdict_list.items():  
      148         settings.webdict.sites.append(site(name=name,url=url))  
      149     # set current to first site  
      150     settings.webdict.current = settings.webdict.sites[0].name  
      151     settings.save()  
      152 else:  
      153     settings = xliffeditor.fromXml(configfile.bytes())  
      154     settings._file = configfile