home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / LanguageSelector / LanguageSelector.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.7 KB  |  211 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import warnings
  5. warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
  6. import apt
  7. import apt_pkg
  8. import os.path as os
  9. import shutil
  10. import subprocess
  11. import thread
  12. import time
  13. import gettext
  14. import sys
  15. import string
  16. import tempfile
  17. import re
  18. import FontConfig
  19. from gettext import gettext as _
  20. from LocaleInfo import LocaleInfo
  21. from LangCache import *
  22.  
  23. class LanguageSelectorBase(object):
  24.     ''' base class for language-selector code '''
  25.     
  26.     def __init__(self, datadir = ''):
  27.         self._datadir = datadir
  28.         self._localeinfo = LocaleInfo('%s/data/languagelist' % self._datadir)
  29.         self._cache = None
  30.  
  31.     
  32.     def openCache(self, progress):
  33.         self._cache = LanguageSelectorPkgCache(self._localeinfo, progress)
  34.  
  35.     
  36.     def getMissingLangPacks(self):
  37.         '''
  38.         return a list of langauge packs that are not installed
  39.         but should be installed
  40.         '''
  41.         missing = []
  42.         for langInfo in self._cache.getLanguageInformation():
  43.             trans_package = 'language-pack-%s' % langInfo.languageCode
  44.             if self._cache.has_key(trans_package) and self._cache[trans_package].isInstalled:
  45.                 for pkg, translation in self._cache.pkg_translations:
  46.                     missing += self.missingTranslationPkgs(pkg, translation + langInfo.languageCode)
  47.                 
  48.         
  49.         default_lang = self.getSystemDefaultLanguage()
  50.         if default_lang is None:
  51.             return missing
  52.         if '_' in default_lang:
  53.             default_lang = default_lang.split('_')[0]
  54.         
  55.         trans_package = 'language-pack-%s' % default_lang
  56.         if self._cache.has_key(trans_package) and not (self._cache[trans_package].isInstalled):
  57.             missing += [
  58.                 trans_package]
  59.             for pkg, translation in self._cache.pkg_translations:
  60.                 missing += self.missingTranslationPkgs(pkg, translation + default_lang)
  61.             
  62.         
  63.         support_packages = LanguageSelectorPkgCache._getPkgList(self._cache, default_lang)
  64.         for support_package in support_packages:
  65.             if self._cache.has_key(support_package) and not (self._cache[support_package].isInstalled):
  66.                 missing.append(support_package)
  67.                 continue
  68.         
  69.         return missing
  70.  
  71.     
  72.     def getUserDefaultLanguage(self):
  73.         fname = os.path.expanduser('~/.dmrc')
  74.         if os.path.exists(fname):
  75.             for line in open(fname):
  76.                 match = re.match('Language=(.*)$', line)
  77.                 if match:
  78.                     if '.' in match.group(1):
  79.                         return match.group(1).split('.')[0]
  80.                     return match.group(1)
  81.                 match
  82.             
  83.         
  84.  
  85.     
  86.     def getSystemDefaultLanguage(self):
  87.         conffiles = [
  88.             '/etc/default/locale',
  89.             '/etc/environment']
  90.         for fname in conffiles:
  91.             if os.path.exists(fname):
  92.                 for line in open(fname):
  93.                     match = re.match('LANG="(.*)"$', line)
  94.                     if match:
  95.                         if '.' in match.group(1):
  96.                             return match.group(1).split('.')[0]
  97.                         return match.group(1)
  98.                     match
  99.                 
  100.         
  101.  
  102.     
  103.     def runAsRoot(self, cmd):
  104.         ''' abstract interface for the frontends to run specific commands as root'''
  105.         if os.getuid() == 0:
  106.             return subprocess.call(cmd)
  107.         raise AttributeError, 'this method needs to be overwriten by the subclass'
  108.  
  109.     
  110.     def setSystemDefaultLanguage(self, defaultLanguageCode):
  111.         ''' this updates the system default language '''
  112.         conffiles = [
  113.             '/etc/default/locale',
  114.             '/etc/environment']
  115.         for fname in conffiles:
  116.             out = tempfile.NamedTemporaryFile()
  117.             foundLanguage = False
  118.             foundLang = False
  119.             if os.path.exists(fname):
  120.                 for line in open(fname):
  121.                     tmp = string.strip(line)
  122.                     if tmp.startswith('LANGUAGE='):
  123.                         foundLanguage = True
  124.                         line = 'LANGUAGE="%s"\n' % self._localeinfo.makeEnvString(defaultLanguageCode)
  125.                     
  126.                     if tmp.startswith('LANG='):
  127.                         foundLang = True
  128.                         line = 'LANG="%s.UTF-8"\n' % defaultLanguageCode
  129.                     
  130.                     out.write(line)
  131.                 
  132.             
  133.             if foundLanguage == False:
  134.                 line = 'LANGUAGE="%s"\n' % self._localeinfo.makeEnvString(defaultLanguageCode)
  135.                 out.write(line)
  136.             
  137.             if foundLang == False:
  138.                 line = 'LANG="%s.UTF-8"\n' % defaultLanguageCode
  139.                 out.write(line)
  140.             
  141.             out.flush()
  142.             self.runAsRoot([
  143.                 '/bin/cp',
  144.                 out.name,
  145.                 fname])
  146.         
  147.         fc = FontConfig.FontConfigHack()
  148.         if defaultLanguageCode in fc.getAvailableConfigs():
  149.             self.runAsRoot([
  150.                 'fontconfig-voodoo',
  151.                 '-f',
  152.                 '--set=%s' % defaultLanguageCode])
  153.         else:
  154.             self.runAsRoot([
  155.                 'fontconfig-voodoo',
  156.                 '-f',
  157.                 '--remove-current'])
  158.  
  159.     
  160.     def setUserDefaultLanguage(self, defaultLanguageCode):
  161.         ''' this updates the user default language '''
  162.         fname = os.path.expanduser('~/.dmrc')
  163.         out = tempfile.NamedTemporaryFile()
  164.         foundLang = False
  165.         if os.path.exists(fname):
  166.             for line in open(fname):
  167.                 tmp = string.strip(line)
  168.                 if tmp.startswith('Language='):
  169.                     foundLang = True
  170.                     line = 'Language=%s.UTF-8\n' % defaultLanguageCode
  171.                 
  172.                 out.write(line)
  173.             
  174.         
  175.         if foundLang == False:
  176.             line = 'Language=%s.UTF-8\n' % defaultLanguageCode
  177.             out.write(line)
  178.         
  179.         out.flush()
  180.         shutil.copy(out.name, fname)
  181.         os.chmod(fname, 420)
  182.  
  183.     
  184.     def missingTranslationPkgs(self, pkg, translation_pkg):
  185.         ''' this will check if the given pkg is installed and if
  186.             the needed translation package is installed as well
  187.  
  188.             It returns a list of packages that need to be 
  189.             installed
  190.         '''
  191.         missing = []
  192.         if not self._cache.has_key(pkg):
  193.             return missing
  194.         if not self._cache[pkg].isInstalled:
  195.             return missing
  196.         for pkg in self._cache:
  197.             if pkg.name == translation_pkg or pkg.name.startswith(translation_pkg + '-'):
  198.                 if not (pkg.isInstalled) and pkg.candidateVersion != None:
  199.                     missing.append(pkg.name)
  200.                 
  201.             pkg.candidateVersion != None
  202.         
  203.         return missing
  204.  
  205.  
  206. if __name__ == '__main__':
  207.     lsb = LanguageSelectorBase(datadir = '..')
  208.     lsb.openCache(apt.progress.OpProgress())
  209.     print lsb.verifyPackageLists()
  210.  
  211.