__init__.py :  » Development » SnapLogic » snaplogic » common » 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 » SnapLogic 
SnapLogic » snaplogic » common » __init__.py
# $SnapHashLicense:
# 
# SnapLogic - Open source data services
# 
# Copyright (C) 2008 - 2009, SnapLogic, Inc.  All rights reserved.
# 
# See http://www.snaplogic.org for more information about
# the SnapLogic project. 
# 
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
# 
# "SnapLogic" is a trademark of SnapLogic, Inc.
# 
# 
# $

# $Id: __init__.py 9868 2009-11-21 17:50:45Z dhiraj $
""" snaplogic.common Package """

import os
from string import Template

def sqlite_iter(cursor):
    """
    Safely generate results of sqlite query

    Under certain conditions and system configurations, pysqlite can
    suffer from a bug when iterating through a result set using the
    "for row in cursor:" idiom.  A row will be dropped from the result
    set some small fraction of the time.

    This function is a generator that produces the results in an
    alternative, more robust way.  Instead of using "for row in
    cursor:", use "for row in sqlite_iter(cursor):".

    @param cursor : cursor
    @type  cursor : pysqlite2.dbapi2.Cursor

    @return       : 0 or more rows (result set)
    @rtype        : generated list
    
    """
    BUFR = 100 # how many rows to buffer
    while 1:
        rows = cursor.fetchmany(BUFR)
        if not rows:
            break
        for row in rows:
            yield row

def expand_variables(string_with_vars):
    """
    Expand variables in string using OS environment.
    Currently only expands SNAP_HOME (and if it's not defined uses dot).
    E.g. given this input:
    "$SNAP_HOME","/opt/snaplogic/extensions/components" 
    it would return:
    "/opt/snaplogic/2.0.5","/opt/snaplogic/extensions/components"
    if SNAP_HOME=/opt/snaplogic/2.0.5
    
    If OS environment variable SNAP_HOME isn't set,
    dot is substituted for SNAP_HOME in string.
    
    @param string_with_vars: String possibly containing parameters
    @type string_with_vars: str  
    """
    # If there is no environment variable SNAP_HOME, assume it's dot.
    # This is needed because default values for some of our config
    # parameters are defined via SNAP_HOME.
    #
    # E.g. the default value for the log_dir is now $SNAP_HOME/logs 
    # (it used to be logs). The QA suite doesn't set that variable.
    # So we default it to be backward-compatible.
    snap_home = os.environ.get("SNAP_HOME", ".")
    
    # Environment variable may be set up using quotes.
    # Remove these, since they aren't legal as part of a path 
    snap_home = snap_home.replace("\"", "")

    # Use python string template to expand SNAP_HOME in string
    # using the value of the environment variable SNAP_HOME
    template = Template(string_with_vars)
    return template.safe_substitute(SNAP_HOME = snap_home)

def parse_size_param(sz_str):
    """
    Convert size in terms of byte/mb/kb/gb/tb to a numeric value in bytes.
    
    @param sz_str: The string form of size
    @type sz_str:  str
    
    @return: The numeric value in terms of bytes.
    @rtype:  long
    
    """
    if sz_str.isdigit():
       sz_num = long(sz_str)
    else:
        invalid_value = False
        try:
            sz_str = sz_str.lower()
            if sz_str.endswith("kb"):
                sz_num = long(sz_str[:-2])
                sz_num *= 1024
            elif sz_str.endswith("mb"):
                sz_num = long(sz_str[:-2])
                sz_num *= 1048576
            elif sz_str.endswith("gb"):
                sz_num = long(sz_str[:-2])
                sz_num *= 1073741824L
            elif sz_str.endswith("tb"):
                # Warning: This may need a python interpreter compiled with special flags
                # to handle large files (see http://docs.python.org/lib/posix-large-files.html).
                # Also, the OS should be capable of supporting large files.
                sz_num = long(sz_str[:-2])
                sz_num *= 1099511627776L
            else:
                return None
        except Exception, e:
            return None
    
    return sz_num
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.