configurationengine/source/plugins/symbian/ConeThemePlugin/themeplugin/theme_resource.py
author terytkon
Thu, 11 Mar 2010 17:04:37 +0200
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
permissions -rw-r--r--
Adding EPL version of configurationengine.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     1
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     2
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     3
# All rights reserved.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     4
# This component and the accompanying materials are made available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     5
# under the terms of "Eclipse Public License v1.0"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     6
# which accompanies this distribution, and is available
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     8
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
     9
# Initial Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    11
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    12
# Contributors:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    13
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    14
# Description: 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    15
#
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    16
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    17
from shutil import copy
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    18
import os
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    19
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    20
class ThemeResource:   
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    21
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    22
    This class represents the records about the theme resources which are saved in *.pkg file.
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    23
    Every record contains a filename of the resource (for example "themepackage.mbm"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    24
    and a path where this resource will be copied in the output directory
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    25
    (for example "!:\private\10207114\import\99d49b086e6097b8\themepackage.mbm")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    26
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    27
   
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    28
    def __init__(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    29
        self.list_resource=[]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    30
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    31
    def parse_pkg_file(self,file_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    32
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    33
        parses *.pkg file and returns a array of classes ThemeResource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    34
        The class ThemeResource contains a name of theme resource 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    35
        and a path where this resource will be copied in the output directory
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    36
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    37
        pkg_file=file(file_path,'r')
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    38
        is_found_else=False
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    39
        row = ""
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    40
        # for every row in pkg file
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    41
        for row in pkg_file:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    42
            #if it finds tag "ELSE" then it begins load the records about the theme resources
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    43
            if row.startswith("ELSE"):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    44
                is_found_else = True
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    45
              
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    46
            if(is_found_else):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    47
                parts_of_row = row.split("\"")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    48
                #the loading record has to have 5 parts separated "\"
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    49
                if len(parts_of_row) == 5:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    50
                    #gets the path of the theme resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    51
                    path = parts_of_row[3]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    52
                    #removes these chars "!:\" from the path of theme resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    53
                    path = self.modify_resource_path(path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    54
                    #parts_of_row[1 is the filename of the theme resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    55
                    resource = Resource(parts_of_row[1], path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    56
                    self.list_resource.append(resource)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    57
   
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    58
        pkg_file.close()  
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    59
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    60
    def copy_files_from_theme(self, source_path, output_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    61
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    62
        copies theme resources from  source directory to theirs target paths 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    63
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    64
        for resource in self.list_resource:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    65
           source_file = os.path.join(source_path, resource.get_filename())
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    66
           target_dir =  os.path.join(output_path, resource.get_path())
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    67
           self.copy_files(source_file, target_dir)          
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    68
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    69
    def copy_files(self, source_path, target_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    70
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    71
        copy files from source to target. If the target directory doesn't exist then it is created
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    72
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    73
        if os.path.exists(source_path) != True or os.path.isdir(source_path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    74
            return
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    75
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    76
        if os.path.exists(target_path) != True:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    77
            os.makedirs(target_path)
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    78
        copy(source_path, target_path)  
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    79
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    80
    def modify_resource_path(self, path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    81
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    82
        Modifies the path of them resource. 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    83
        If the paths contains string "private" or "Data" (it says that the path is target path) then it removes 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    84
        these chars "!:\" from the path of theme resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    85
        """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    86
        if path.find("private") != -1 or path.find("Data") != -1:
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    87
            if path.startswith("!:\\"):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    88
                index = path.rfind("\\")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    89
                path =  path[3:index]
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    90
            
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    91
        return path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    92
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    93
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    94
class Resource(object):   
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    95
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    96
    This class represents a record about the theme resource. It contains a filename of the resource 
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    97
    (for example "themepackage.mbm" and a path where this resource will be copied in the output directory
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    98
    (for example "!:\private\10207114\import\99d49b086e6097b8\themepackage.mbm")
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
    99
    """
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   100
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   101
    def __init__(self, filename,path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   102
        # the name of theme resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   103
        self.filename = filename
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   104
        # the path of theme resource
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   105
        self.path = path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   106
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   107
    def get_filename(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   108
        return self.filename
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   109
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   110
    def get_path(self):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   111
        return self.path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   112
    
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   113
    def set_path(self,path):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   114
        self.path = path
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   115
        
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   116
    def set_filename(self,filename):
2e8eeb919028 Adding EPL version of configurationengine.
terytkon
parents:
diff changeset
   117
        self.filename = filename