configurationengine/source/scripts/conesub_import_browserbookmarks.py.old
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #!/usr/bin/env python
       
     2 #
       
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description: 
       
    16 
       
    17 import os
       
    18 import logging
       
    19 import codecs
       
    20 
       
    21 from optparse import OptionParser, OptionGroup
       
    22 
       
    23 import cone_common
       
    24 from cone.public import api, plugin, utils, exceptions, container
       
    25 
       
    26 VERSION = '1.0'
       
    27 
       
    28 logger = logging.getLogger('cone')
       
    29 elems = {
       
    30 'Type': 0,
       
    31 'Name': 1,
       
    32 'ParentFolderName': 2,
       
    33 'URL': 3,
       
    34 'AccessPoint': 4,
       
    35 'UserName': 5,
       
    36 'Password': 6,
       
    37 'ReadOnly': 7,
       
    38 'FactoryItem': 8,
       
    39 'ContextId': 9,
       
    40 'Preferred': 10 
       
    41     }
       
    42     
       
    43 def main():
       
    44     parser = OptionParser(version="%%prog %s" % VERSION)
       
    45   
       
    46     parser.add_option("-c", "--configuration",\
       
    47                         dest="configuration",\
       
    48                         help="defines the name of the configuration for the action",\
       
    49                         metavar="CONFIG")
       
    50   
       
    51     parser.add_option("-v", "--verbose",\
       
    52                       dest="verbose",\
       
    53                       help="Print error,warning and information on system out. \
       
    54                       Possible choises: Default is 3.\
       
    55                                      NONE          0.\
       
    56                                      CRITICAL      1\
       
    57                                      ERROR         2\
       
    58                                      WARNING       3\
       
    59                                      INFO          4\
       
    60                                      DEBUG         5\
       
    61                                      ",\
       
    62                       default=3,\
       
    63                       metavar="LEVEL")
       
    64     parser.add_option("-p", "--project",\
       
    65                        dest="project",\
       
    66                        help="defines the location of current project. Default is the current working directory.",\
       
    67                        default=".",\
       
    68                        metavar="STORAGE")
       
    69     
       
    70     bgroup = OptionGroup(parser, 'Import browser bookmarks options',
       
    71                     'The import_browserbookmarks functionality is meant to import browser bookmarks to a given configuration.')
       
    72   
       
    73     bgroup.add_option("-i", "--input",\
       
    74                    dest="input",\
       
    75                    type="string",
       
    76                    help="input BrowserBookmarks file for the importing.",
       
    77                    metavar="FILE",\
       
    78                    default=None)
       
    79   
       
    80     layers = None
       
    81     current = None
       
    82 
       
    83     parser.add_option_group(bgroup)
       
    84     (options, args) = parser.parse_args()
       
    85 
       
    86     api.get_console_logger().setLevel(cone_common.get_log_level(options.verbose))
       
    87 
       
    88     if not options.input:
       
    89         parser.error("Input file must be given!")
       
    90 
       
    91     logger.info('Open file %s.' % options.input)
       
    92     
       
    93 
       
    94     current = api.Project(api.Storage.open(options.project,"a"))
       
    95     active_root = current.get_storage().get_active_configuration()
       
    96     if not options.configuration:
       
    97         if active_root == "":
       
    98             parser.error("configuration must be given")
       
    99         else:
       
   100             logging.getLogger('cone').info('No configuration given! Using active root configuration %s' % active_root)
       
   101             options.configuration = active_root
       
   102     config  = current.get_configuration(options.configuration)
       
   103     dview = config.get_default_view()
       
   104     bookmarkfea = dview.get_feature('BookmarkItems.BookmarkItem')
       
   105     
       
   106     inputfile = codecs.open(options.input,encoding="utf16")
       
   107     firstelem = True
       
   108     for line in inputfile.readlines():
       
   109         # skip comments 
       
   110         data = line.split('#',1)
       
   111         dataelem = data[0]
       
   112         dataelem = dataelem.strip()
       
   113         dataelem = dataelem.rstrip('\n')
       
   114         
       
   115         if dataelem != "":       
       
   116             # Split the datarow in to data elems 
       
   117             #print "DATAELEM: %s!" % dataelem
       
   118             dataelems = dataelem.split(',')
       
   119             print "Elem found %s" % str(dataelems[elems['Name']])
       
   120             # Build the new data sequence
       
   121             browserseq = []
       
   122             for feaname in bookmarkfea.list_features():
       
   123                 try:
       
   124                     browserseq.append(str(dataelems[elems[feaname]]))
       
   125                 except IndexError:
       
   126                     pass
       
   127                 
       
   128             print browserseq
       
   129             if firstelem:
       
   130                 bookmarkfea.add_sequence(browserseq, policy=container.REPLACE)                  
       
   131                 data = bookmarkfea.get_data()[-1]
       
   132                 data._get_data().policy = 'replace'
       
   133                 firstelem = False
       
   134             else:
       
   135                 bookmarkfea.add_sequence(browserseq, policy=container.APPEND)
       
   136 
       
   137     config.save()        
       
   138                 
       
   139     logger.info('Done!')
       
   140     
       
   141 if __name__ == "__main__":
       
   142     main()
       
   143 
       
   144