historyUtils.py :  » UML » Python-UML-Tool » pyut-1.4.0 » src » 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 » UML » Python UML Tool 
Python UML Tool » pyut 1.4.0 » src » historyUtils.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@author P. Dabrowski <przemek.dabrowski@destroy-display.com> (18.11.2005)
This module defines the format of the serialized commands and command groups
of PyUt's history (undo/redo).
It gives also some tools to set keywords in the format automaticaly.
The format is textual, based on 'tokens' (identificators) which can have
two forms (without the spaces):

    1) token_begin token_name token_end
    2) token_begin token_name token_assign token_value token_end

Where token_begin, token_end and token_assign are special sequences of
charcters defines in this module.
token_name is a characters sequence freely choosed by the user. But be carefull
because some special sequences are defined below.
token_value is only used with the second form of token. It allows to set a
value to a token. a token value CAN't be similar partially or completly to a
reserved sequence (see below)
Normally, you should not use the first form, but the second one, for e.g. to
(un)serialize data used by a command.
To see how it works, please see UnitTestHistory
"""

#for serialisation : delimitations of a token
TOKEN_BEGIN = "<"
TOKEN_END = ">"

#when a token has an assigned value, whe use this symbol for the serialization
TOKEN_ASSIGN = "="

#it's the escape sequence, used if the token name or value contains a control
#sequence like TOKEN_ASSIGN for e.g.
TOKEN_ESCAPE = "\\"

#to limit the begining and the end of a serialized commands group
GROUP_BEGIN_ID = "BEGIN_COMMAND_GROUP"
GROUP_END_ID = "END_COMMAND_GROUP"

#to find the comment/description of a command group
GROUP_COMMENT_ID = "GROUP_COMMENT"

#to limit the begining and the end of a serialized command
COMMAND_BEGIN_ID = "BEGIN_COMMAND"
COMMAND_END_ID = "END_COMMAND"

#used in the unserialization to build the right command
COMMAND_CLASS_ID = "COMMAND_CLASS"
COMMAND_MODULE_ID = "COMMAND_MODULE"

#if a sequence contains a control sequence, TOKEN_ESCAPE should
#be added to at the begining of the control sequence.
#NOTE:TOKEN_ESCAPE must be first in the list
CTRL_SEQUENCES = [TOKEN_ESCAPE, TOKEN_BEGIN, TOKEN_END, TOKEN_ASSIGN]

#if a sequence contains a reserved sequence, an exception should be raised.
RESERVED_SEQUENCES = [GROUP_BEGIN_ID, GROUP_END_ID, GROUP_COMMENT_ID,
                      COMMAND_BEGIN_ID, COMMAND_END_ID, COMMAND_CLASS_ID,
                      COMMAND_MODULE_ID]

#defines the base name of the file which will contain the serialized commands.
HISTORY_FILE_NAME = "pyutHistory"


def makeToken(tokenId):
    """
    @return a token (string) that is standard for all histories.
    @param tokenId (string) : name (identificator) of the token
    """
    return TOKEN_BEGIN + tokenId + TOKEN_END

#>------------------------------------------------------------------------

def makeValuatedToken(tokenId, value):
    """
    @return a valuated token (string) in the format of the history
    manager. Use it in the serialize method of a command, so that
    you can get it back with the getTokenValue method.
    Notes : Raise an exception if tokenId or value are partially or
    completly a reserved sequence.

    @param tokenId (string) : name of the token
    @param value (string)   : value of the token
    """

    #check if the value isn't a reserved sequence and
    #if it is the case we raise an exception
    for sequence in RESERVED_SEQUENCES:
        if value.find(sequence) > -1:
            raise RuntimeError("'" + sequence + "' is a reserved sequence.")

    #check if the value isn't a control sequence and
    #if it is the case, an escape sequence is added.
    for sequence in CTRL_SEQUENCES:
        value = value.replace(sequence, TOKEN_ESCAPE + sequence)
        
    return makeToken(tokenId + TOKEN_ASSIGN + value)

#>------------------------------------------------------------------------

def getTokenValue(tokenId, serializedInfos):
    """
    @return the value (string) of the specified token extracted
    from the specified string. To use in the unserialize method of
    a command to get back a value.
    Notes : the token had to be created by makeToken method.

    @param tokenId (string)         : name of the token
    @param serializedInfos (string) : string which contains the infos
                                      needed to unserialize a command
    """

    #to not to work on the original
    value = serializedInfos
    
    #find a char that is not present in the value
    tempEscape = chr(1)
    while value.find(tempEscape) > -1:
        tempEscape = chr(ord(tempEscape + 1))
        
    #replace the double escape sequences by a char that is not
    #present in the original value
    value = value.replace(TOKEN_ESCAPE + TOKEN_ESCAPE, tempEscape)
    
    #find the start position of the value which is just after the end
    #of the token name followed by the assignement token.
    startPos = value.find(tokenId)
    startPos = startPos + len(tokenId) + len(TOKEN_ASSIGN)
    #value = value[startPos : len(value)]
    
    #find the end position which is just before TOKEN_ASSIGN
    endPos = value.find(TOKEN_END, startPos)

    #check if there isn't a escape token before TOKEN_END what
    #would means that the TOKEN_END sequence is a part of the
    #value, so we check for the next TOKEN_END.
    while cmp(value[endPos - len(TOKEN_ESCAPE) : endPos],
              TOKEN_ESCAPE) == 0:
        endPos = value.find(TOKEN_END, endPos + 1)
    value = value[startPos : endPos]

    #remove all the escape sequences
    value = value.replace(TOKEN_ESCAPE, "")
    #add simple escape sequences where they where double
    value = value.replace(tempEscape, TOKEN_ESCAPE)
    
    return value

#>------------------------------------------------------------------------
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.