Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 import os
00002 
00003 PRODUCTS = {
00004     'atct'  : {'name': 'atcontenttypes', 'path': 'ATContentTypes'},
00005     'atrbw' : {'name': 'atreferencebrowserwidget', 'path': 'ATReferenceBrowserWidget'},
00006     'lp'    : {'name': 'linguaplone', 'path': 'LinguaPlone'},
00007     'plone' : {'name': 'plone', 'path': 'CMFPlone'},
00008     'plt'   : {'name': 'plonelanguagetool', 'path': 'PloneLanguageTool'},
00009     'prt'   : {'name': 'passwordresettool', 'path': 'PasswordResetTool'},
00010     'cmfpw' : {'name': 'cmfplacefulworkflow', 'path': 'CMFPlacefulWorkflow'},
00011     'cmfe'  : {'name': 'cmfeditions', 'path': 'CMFEditions'},
00012     'kupu'  : {'name': 'kupu/kupu', 'path': 'kupu'},
00013     'kupuconfig'  : {'name': 'kupu/kupuconfig', 'path': 'kupu'},
00014     'kupupox'  : {'name': 'kupu/kupupox', 'path': 'kupu'},
00015     }
00016 
00017 PRODUCTNAMES = [PRODUCTS[abbr]['name'] for abbr in PRODUCTS]
00018 
00019 def getPoFiles(product, all=False):
00020     """ Returns all product*.po files in the current folder """
00021     path = os.curdir
00022     folder = ''
00023     if '/' in product:
00024         folder, product = product.split('/')
00025         path = os.path.join(os.curdir, folder)
00026 
00027     files = os.listdir(path)
00028     if all:
00029         files = [f for f in files if f.startswith('%s-' % product) and f.endswith('.po')]
00030     else:
00031         files = [f for f in files if f.startswith('%s-' % product) and f.endswith('.po') and f != '%s-en.po' % product]
00032     if folder:
00033         files  = [folder + '/' + f for f in files]
00034     return files
00035 
00036 
00037 def getPotFiles(all=False):
00038     """ Returns all pot files in the current folder
00039         Normally it doesn't return manual.pots and generated.pots
00040     """
00041     files = os.listdir(os.curdir)
00042     if all:
00043         files = [f for f in files if f.endswith('.pot')]
00044     else:
00045         files = [f for f in files if f.endswith('.pot') and not f[:-4].endswith('manual') and not f[:-4].endswith('generated')]
00046     return files
00047 
00048 
00049 def getPoFilesAsCmdLine(product):
00050     files = getPoFiles(product)
00051     filestring = ''
00052     for f in files:
00053         filestring += f + ' '
00054     return filestring.rstrip()
00055 
00056 
00057 def getPotFilesAsCmdLine():
00058     files = getPotFiles()
00059     filestring = ''
00060     for f in files:
00061         filestring += f + ' '
00062     return filestring.rstrip()
00063 
00064 
00065 def getPoFilesByLanguageCode(lang):
00066     """ Returns all po files which ends with given language code."""
00067     files = os.listdir(os.curdir)
00068     files = [f for f in files if f.endswith('.po') and f[:-3].endswith(lang)]
00069     return files
00070 
00071 
00072 def getLanguage(product, f):
00073     """ Returns the language part of a po-file """
00074     lang = None
00075     if f.endswith('.po'):
00076         if f.startswith(product):
00077             lang = '-'.join(f.split('-')[1:])[:-3]
00078     return lang
00079 
00080 
00081 def getProduct(f):
00082     """ Returns the product part of a file. We assume files to be something like domain-language.po.
00083         Example: atcontenttypes-pt-br.po
00084     """
00085     assert f.endswith('.po') or f.endswith('.pot')
00086 
00087     f = f.split('.')[0] # strip of ending
00088     f = f.split('-')[0] # only take product
00089 
00090     return f
00091 
00092 
00093 def getLongProductName(product):
00094     """ Returns the product name for a known abbreviation."""
00095     if product in PRODUCTS.keys():
00096         return PRODUCTS[product]['name']
00097     return product
00098 
00099 
00100 def getProductPath(product):
00101     """ Returns the product path for a known abbreviation."""
00102     if product in PRODUCTS.keys():
00103         return PRODUCTS[product]['path']
00104     if product in PRODUCTNAMES:
00105         for abbr in PRODUCTS.keys():
00106             if PRODUCTS[abbr]['name'] == product:
00107                 return PRODUCTS[abbr]['path']
00108     return product