__init__.py revision 57
49N/A#!/usr/bin/python
49N/A#
49N/A# CDDL HEADER START
49N/A#
49N/A# The contents of this file are subject to the terms of the
49N/A# Common Development and Distribution License (the "License").
49N/A# You may not use this file except in compliance with the License.
49N/A#
49N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
49N/A# or http://www.opensolaris.org/os/licensing.
49N/A# See the License for the specific language governing permissions
49N/A# and limitations under the License.
49N/A#
49N/A# When distributing Covered Code, include this CDDL HEADER in each
49N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
49N/A# If applicable, add the following below this CDDL HEADER, with the
49N/A# fields enclosed by brackets "[]" replaced with your own identifying
49N/A# information: Portions Copyright [yyyy] [name of copyright owner]
49N/A#
49N/A# CDDL HEADER END
49N/A#
49N/A
49N/A#
49N/A# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
49N/A# Use is subject to license terms.
49N/A#
49N/A
49N/A"""
49N/Apackage containing packaging action (file type) modules
49N/A
49N/AThis package contains modules describing packaging actions, or file types. The
49N/Aactions are dynamically discovered, so that new modules can be placed in this
49N/Apackage directory and they'll just be picked up. The current package contents
49N/Acan be seen in the section "PACKAGE CONTENTS", below.
49N/A
51N/AThis package has one data member: "types". This is a dictionary which maps the
51N/Aaction names to the classes that represent them.
51N/A
51N/AThis package also has one function: "fromstr", which creates an action instance
51N/Abased on a str() representation of an action.
49N/A"""
49N/A
49N/Aimport inspect
49N/Aimport os
49N/A
49N/A# All modules in this package (all python files except __init__.py with their
49N/A# extensions stripped off).
49N/A__all__ = [
49N/A f[:-3]
49N/A for f in os.listdir(__path__[0])
49N/A if f.endswith(".py") and f != "__init__.py"
49N/A]
49N/A
49N/A# A dictionary of all the types in this package, mapping to the classes that
49N/A# define them.
49N/Atypes = {}
49N/Afor modname in __all__:
49N/A module = __import__("%s.%s" % (__name__, modname),
49N/A globals(), locals(), [modname])
49N/A
49N/A nvlist = inspect.getmembers(module, inspect.isclass)
49N/A
49N/A # Pull the class objects out of nvlist, keeping only those that are
49N/A # actually defined in this package.
49N/A classes = [
49N/A c[1]
49N/A for c in nvlist
49N/A if '.'.join(c[1].__module__.split('.')[:-1]) == __name__
49N/A ]
49N/A for cls in classes:
51N/A types[cls.name] = cls
49N/A
49N/A# Clean up after ourselves
49N/Adel f, modname, module, nvlist, classes, c, cls
51N/A
51N/Adef fromstr(str):
51N/A """Create an action instance based on a str() representation of an action.
51N/A """
57N/A type, str = str.split(" ", 1)
51N/A
51N/A if type not in types:
51N/A raise KeyError, "Action '%s'" % type
51N/A
51N/A # That is, if the first attribute is a hash
57N/A if str.find(" ") < str.find("="):
57N/A hash, str = str.split(" ", 1)
57N/A
57N/A # Split the string on spaces, then reconstruct and dequote quoted
57N/A # values.
57N/A list = str.split(" ")
51N/A
57N/A # Simple state machine to reconnect the elements that shouldn't have
57N/A # been split. Put the results into a new list since we can't modify the
57N/A # list we're iterating over.
57N/A state = 0
57N/A count = 0
57N/A nlist = []
57N/A for i in list:
57N/A if '="' in i:
57N/A start = count
57N/A n = i.replace('="', '=')
57N/A state = 1
57N/A elif i.endswith('"'):
57N/A n += " " + i[:-1]
57N/A nlist += [ n ]
57N/A state = 0
57N/A elif state == 1:
57N/A n += " " + i
57N/A else:
57N/A nlist += [ i ]
57N/A
57N/A count += 1
57N/A
57N/A list = nlist
57N/A
57N/A # Create a list of key/value lists
57N/A nlist = [kv.split("=", 1) for kv in nlist]
57N/A # Remove attribute duplication
57N/A attrset = set(zip(*nlist)[0])
57N/A # Put values belonging to the same attribute into individual lists
57N/A vallist = [[j[1] for j in nlist if j[0] == i] for i in attrset]
57N/A # Create a dict from the two lists
57N/A attrs = dict((o, a) for o, a in zip(attrset, vallist))
57N/A
57N/A # Convert any single-valued attributes to simple strings.
57N/A # attrs = dict(
57N/A # [ (k, v) for k, v in attrs.iteritems() if len(v) > 1 ] +
57N/A # [ (k, v[0]) for k, v in attrs.iteritems() if len(v) == 1 ]
57N/A # )
57N/A for a in attrs:
57N/A if len(attrs[a]) == 1:
57N/A attrs[a] = attrs[a][0]
51N/A
51N/A action = types[type](**attrs)
51N/A if "hash" in locals():
51N/A action.hash = hash
51N/A
51N/A return action