Package client :: Package Dock :: Module DataGuesser
[hide private]
[frames] | no frames]

Source Code for Module client.Dock.DataGuesser

  1  """ 
  2  Overview 
  3  ======== 
  4  The data guessing module was created so that functions in in the dock widget would make somewhat intelligent 
  5  guesses at what the user might want to use ahead of time. This functionality is negelcted if the user wants it to 
  6  be, however, even guesses it does make are overwritable.  This is purely meant as a time saver for users. 
  7  """ 
  8   
  9  import PyQt4.QtGui 
 10   
 11  import re 
 12   
 13  # Note the `r` before any string containing '\b', this is 
 14  # because python interprets '\b' to be a backspace, but 
 15  # the re module takes it to mean a word-break.  The `r` 
 16  # signifies the string as being raw characters. 
 17   
18 -def GenerateRegExpFromName (name):
19 """ 20 Overview 21 ======== 22 This function will take the name provided and produce a regular expression that accepts 23 strings very similar to the name provided. The regular expression may start with a number, 24 underscore, or nothing. Then it must contain the name provided, in any mixture of capital and 25 lower case letters. Finally it may end with a number or underscore, or nothing of course. 26 27 @param name: The name which you want turned into a regular expression. 28 @type name: string 29 30 @return: _sre.SRE_Pattern, a compiled regular expression retrieved from the I{re.compile ()} method. 31 """ 32 33 # change each letter of the work into the form [Xx] 34 # to support any combination of lower/upper case letters 35 nameInv = name.swapcase () 36 nameFinal = "" 37 for index in xrange (len (name)): 38 nameFinal += "[" + name[index] + nameInv[index] + "]" 39 40 # and then return it's regular expression surrounded with 41 # non-letters and word-breaks, also it MUST be the beginning 42 # of the expression to match 43 return re.compile (r"^(\b|[0-9_])" + nameFinal + r"(\b|[0-9_])")
44
45 -def GenerateRegExpFromWord (word):
46 """ 47 Overview 48 ======== 49 This function is currently the exact same as L{GenerateRegExpFromName<GenerateRegExpFromName>}. Originally 50 there was going to be a distinction in the types of regular expression objects returned. As of now, the 51 appropriate dock widgets still make use of this method, so if a compiled regular expression is ever desired, 52 only this function must be changed. 53 54 @param word: The word which you want turned into a regular expression. 55 @type word: string 56 57 @return: _sre.SRE_Pattern, a compiled regular expression retrieved from the I{re.compile ()} method. 58 """ 59 60 # change each letter of the work into the form [Xx] 61 # to support any combination of lower/upper case letters 62 wordInv = word.swapcase () 63 wordFinal = "" 64 for index in xrange (len (word)): 65 wordFinal += "[" + word[index] + wordInv[index] + "]" 66 67 # and then return it's regular expression surrounded with 68 # non-letters and word-breaks 69 return re.compile (r"(\b|[0-9_])" + wordFinal + r"(\b|[0-9_])")
70
71 -def GuessAttributes (object, guessList):
72 """ 73 Overview 74 ======== 75 This function takes a DockWidget and a list of regular expressions. It will run down the Basin Object Tree, 76 attempting to add any object that fits into the regular expression. When that occurs, it starts at the top 77 the Basin Object Tree again and does the same for the second regular expression until all of the regular 78 expressions have been tried. Normal rules apply for adding objects to a Dock Widget's Holder. This means 79 that if the first two regular expressions find objects with different parents lists, the second will kick 80 the first one out. 81 82 @param object: This is the dock widget for which the guesses are being made on. 83 @type object: L{DockWidget<DockWidget>} 84 85 @param guessList: A list of all the compiled regular expressions for which guesses are being made 86 @type guessList: list <_sre.SRE_Pattern> 87 """ 88 89 # If you are guessing Attributes, clear out the dock first. 90 object.clear () 91 if ((not object._bridge._connected) and (not object._bridge._parallel)) or (not object._bridge._autoGuess): 92 return 93 94 # Define an iterator across the existing items in the region viewer 95 iter = PyQt4.QtGui.QTreeWidgetItemIterator (object._bridge._regionViewer.top) 96 iter += 1 97 98 while iter.value (): 99 # When you have no more guesses to make, return 100 if len (guessList) == 0: 101 return 102 # For each regular expression hit 103 if guessList[0].search (str (iter.value ().text (0))): 104 # Remove the current regular expression you were 105 # searching for 106 guessList.pop (0) 107 # Add the item in the region viewer that matched 108 #### Think about checking the list in the object 109 #### so that you don't have conflicting list parents 110 object.DoubleClicked (iter.value ()) 111 # And reset your iterator to start at the top again 112 iter = PyQt4.QtGui.QTreeWidgetItemIterator (object._bridge._regionViewer.top) 113 # Progress to the next item, if you just matched then 114 # the first item (the `BASIN` at the top) will never match 115 # anyway, so it's ok to skip it 116 iter += 1
117