modutils.py :  » Development » Lyntin » lyntin-4.2 » lyntin » modules » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Lyntin 
Lyntin » lyntin 4.2 » lyntin » modules » modutils.py
#########################################################################
# This file is part of Lyntin.
#
# Lyntin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Lyntin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# copyright (c) Free Software Foundation 2001-2007
#
# $Id: modutils.py,v 1.4 2007/07/24 00:39:03 willhelm Exp $
#########################################################################
"""
This module holds helper functions for building other Lyntin modules.
This module will likely make things easier for you, however, it is not
an API module and the contents herein are subject to change if we
need to change them.  Having said that, I will note it doesn't change
much.
"""
import types
from lyntin import exported


def load_commands(commands_dict):
  """
  Takes in a dict and loads all the commands in that dict.

  The dict is a mapping of command name to arguments to be passed
  to exported.add_command.  Pretty much we just turn around and
  call exported.add_command with the arguments in the dict without
  any transformation at all.

  @param commands_dict: the map holding the command names and the
      arguments we need to call exported.add_command repeatedly
  @type  commands_dict: dict
  """
  for mem in commands_dict.keys(): 
    args = commands_dict[mem]
    if type(args) == types.TupleType:
      exported.add_command(*((mem,)+args))
    else:
      exported.add_command(mem, args)

def unload_commands(commands_list):
  """
  Takes in a list of command names and removes the commands from
  Lyntin by calling exported.remove_command.

  @param commands_list: the list of command names to remove
  @type  commands_list: list of strings
  """
  for mem in commands_list:
    exported.remove_command(mem)


def unsomething_helper(args, func, ses=None, sing="", plur=""):
  """
  Helps automate some of the un(something) commands.  These are
  commands that remove data from a given manager.  For example,
  unalias, unaction....

  This method also handles printing out status information to
  the user as to how many somethings were removed.

  @param args: the map with the str and quiet arguments in it
  @type  args: dict

  @param func: the function to call to actually do the work.
      it should take in a single string argument.
  @type  func: function

  @param ses: the session to apply this to
  @type  ses: Session

  @param sing: the singular form of the unsomething--for output
      (ex: "alias")
  @type  sing: string

  @param plur: the plural form of the unsomething--for output
      (ex: "aliases")
  @type  plur: string
  """
  text = args["str"]
  quiet = args["quiet"]

  if ses:
    removedthings = func(ses, text)
  else:
    removedthings = func(text)

  if not quiet:
    if len(removedthings) == 0:
      data = "un%s: No %s removed." % (sing, plur)
    else:
      data = []
      for mem in removedthings:
        if type(mem) == types.TupleType or type(mem) == types.ListType:
          mem = "{%s}" % ("} {".join(map(str, mem)))
          data.append("un%s: %s removed." % (sing, mem))
        else:
          data.append("un%s: {%s} removed." % (sing, mem))
      data = "\n".join(data)
    exported.write_message(data, ses)


# Local variables:
# mode:python
# py-indent-offset:2
# tab-width:2
# End:
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.