Skip to content

Commit

Permalink
undefined names fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vtr0n committed Mar 19, 2018
1 parent 320c4c7 commit ed9a0a6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 98 deletions.
65 changes: 35 additions & 30 deletions EXAMPLE-WIDGETLIST.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
#!/usr/bin/env python
import npyscreen
#npyscreen.disableColor()


# npyscreen.disableColor()
class ActionFormExample(npyscreen.ActionForm):
initialWidgets = [
(npyscreen.TitleText, {'w_id': 'TextLine', 'name': "Text:"}),
(npyscreen.TitleFilename, {'name' : "Filename:"}),
(npyscreen.TitleFilename, {'name' : "Filename:"}),
(npyscreen.TitleDateCombo, {'name' : "Date:"}),
(npyscreen.TitleSlider, {'out_of': 12, 'name' : "Slider"}),
(npyscreen.MultiLineEdit, {'value' : """try typing here!\nMutiline text, press ^R to reformat.\n""", 'max_height': 5,})
]
initialWidgets = [
(npyscreen.TitleText, {'w_id': 'TextLine', 'name': "Text:"}),
(npyscreen.TitleFilename, {'name': "Filename:"}),
(npyscreen.TitleFilename, {'name': "Filename:"}),
(npyscreen.TitleDateCombo, {'name': "Date:"}),
(npyscreen.TitleSlider, {'out_of': 12, 'name': "Slider"}),
(npyscreen.MultiLineEdit,
{'value': """try typing here!\nMutiline text, press ^R to reformat.\n""", 'max_height': 5, })
]


class TestApp(npyscreen.NPSApp):
def main(self):
# These lines create the form and populate it with widgets.
# A fairly complex screen in only 8 or so lines of code - a line for each control.
F = ActionFormExample(name = "Welcome to Npyscreen",)
#t = F.add(npyscreen.TitleText, name = "Text:", )
#fn = F.add(npyscreen.TitleFilename, name = "Filename:")
#dt = F.add(npyscreen.TitleDateCombo, name = "Date:")
#s = F.add(npyscreen.TitleSlider, out_of=12, name = "Slider")
#ml= F.add(npyscreen.MultiLineEdit,
# value = """try typing here!\nMutiline text, press ^R to reformat.\n""",
# max_height=5, rely=9)
#ms= F.add(npyscreen.TitleSelectOne, max_height=4, value = [1,], name="Pick One",
# values = ["Option1","Option2","Option3"], scroll_exit=True)
#ms2= F.add(npyscreen.TitleMultiSelect, max_height=4, value = [1,], name="Pick Several",
# values = ["Option1","Option2","Option3"], scroll_exit=True)
#
# This lets the user play with the Form.
F.edit()
return F.get_widget('TextLine').value
def main(self):
# These lines create the form and populate it with widgets.
# A fairly complex screen in only 8 or so lines of code - a line for each control.
F = ActionFormExample(name="Welcome to Npyscreen", )
# t = F.add(npyscreen.TitleText, name = "Text:", )
# fn = F.add(npyscreen.TitleFilename, name = "Filename:")
# dt = F.add(npyscreen.TitleDateCombo, name = "Date:")
# s = F.add(npyscreen.TitleSlider, out_of=12, name = "Slider")
# ml= F.add(npyscreen.MultiLineEdit,
# value = """try typing here!\nMutiline text, press ^R to reformat.\n""",
# max_height=5, rely=9)
# ms= F.add(npyscreen.TitleSelectOne, max_height=4, value = [1,], name="Pick One",
# values = ["Option1","Option2","Option3"], scroll_exit=True)
# ms2= F.add(npyscreen.TitleMultiSelect, max_height=4, value = [1,], name="Pick Several",
# values = ["Option1","Option2","Option3"], scroll_exit=True)
#
# This lets the user play with the Form.
F.edit()
return F.get_widget('TextLine').value


if __name__ == "__main__":
App = TestApp()
print App.run()
App = TestApp()
App.run()
141 changes: 73 additions & 68 deletions npyscreen/compatibility_code/oldtreeclasses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from .. import wgmultiline as multiline
from .. import wgcheckbox as checkbox
from . import npysNPSTree as NPSTree
from ..wgmultilinetree import TreeLine, TreeLineAnnotated
import weakref
import curses


#####################################################################################
Expand All @@ -9,7 +12,6 @@
#####################################################################################



# OLD TREE WIDGET
class MultiLineTree(multiline.MultiLine):
def _setMyValues(self, tree):
Expand All @@ -19,47 +21,50 @@ def _setMyValues(self, tree):
raise TypeError("MultiLineTree widget can only contain a NPSTreeData object in its values attribute")
else:
self._myFullValues = tree

def _getApparentValues(self):
return self._myFullValues.getTreeAsList()

def _walkMyValues(self):
return self._myFullValues.walkTree()

def _delMyValues(self):
self._myFullValues = None

values = property(_getApparentValues, _setMyValues, _delMyValues)

def get_tree_display(self, vl):
dp = vl.findDepth()
if dp > 0:
control_chars = "| " * (dp-1) + "|-"
control_chars = "| " * (dp - 1) + "|-"
else:
control_chars = ""
if vl.hasChildren():
if vl.expanded:
control_chars = control_chars + "+"
else:
else:
control_chars = control_chars + ">"
else:
control_chars = control_chars + ""
return control_chars

def _set_line_values(self, line, value_indexer):
try:
line.value = self.get_tree_display(self.values[value_indexer]) + " " + self.display_value(self.values[value_indexer]) + " "
line.value = self.get_tree_display(self.values[value_indexer]) + " " + self.display_value(
self.values[value_indexer]) + " "
line.hidden = False
except IndexError:
self._set_line_blank(line)
except TypeError:
self._set_line_blank(line)

def display_value(self, vl):
return str(vl.getContentForDisplay())

return str(vl.getContentForDisplay())


class SelectOneTree(MultiLineTree):
_contained_widgets = checkbox.RoundCheckBox

def _print_line(self, line, value_indexer):
try:
line.value = self.values[value_indexer]
Expand All @@ -80,8 +85,8 @@ def _print_line(self, line, value_indexer):
line.name = None
line.hidden = True

line.highlight= False
line.highlight = False

def update(self, clear=True):
if self.hidden and clear:
self.clear()
Expand All @@ -99,25 +104,25 @@ def update(self, clear=True):

def h_select(self, ch):
try:
self.value = [weakref.proxy(self.values[self.cursor_line]),]
self.value = [weakref.proxy(self.values[self.cursor_line]), ]
except TypeError:
# Actually, this is inefficient, since with the NPSTree class (default) we will always be here - since by default we will
# try to create a weakref to a weakref, and that will fail with a type-error. BUT we are only doing it on a keypress, so
# it shouldn't create a huge performance hit, and is future-proof. Code replicated in h_select_exit
self.value = [self.values[self.cursor_line],]
self.value = [self.values[self.cursor_line], ]

def h_select_exit(self, ch):
try:
self.value = [weakref.proxy(self.values[self.cursor_line]),]
self.value = [weakref.proxy(self.values[self.cursor_line]), ]
except TypeError:
# Actually, this is inefficient, since with the NPSTree class (default) we will always be here - since by default we will
# try to create a weakref to a weakref, and that will fail with a type-error. BUT we are only doing it on a keypress, so
# it shouldn't create a huge performance hit, and is future-proof.
self.value = [self.values[self.cursor_line],]
self.value = [self.values[self.cursor_line], ]
if self.return_exit:
self.editing = False
self.how_exited=True
self.how_exited = True

def h_set_filtered_to_selected(self, ch):
if len(self._filtered_values_cache) < 2:
self.value = self.get_filtered_values()
Expand All @@ -134,9 +139,10 @@ def h_set_filtered_to_selected(self, ch):

class MultiLineTreeNew(multiline.MultiLine):
# Experimental

_contained_widgets = TreeLineAnnotated
#_contained_widgets = TreeLine

# _contained_widgets = TreeLine
def _setMyValues(self, tree):
if tree == [] or tree == None:
self._myFullValues = NPSTree.NPSTreeData()
Expand All @@ -147,107 +153,104 @@ def _setMyValues(self, tree):
raise TypeError("MultiLineTree widget can only contain a NPSTreeData object in its values attribute")
else:
self._myFullValues = tree

def convertToTree(tree):
"Override this function to convert a set of values to a tree."
return None

def clearDisplayCache(self):
self._cached_tree = None
self._cached_sort = None
self._cached_tree_as_list = None

def _getApparentValues(self):
try:
if self._cached_tree is weakref.proxy(self._myFullValues) and \
(self._cached_sort == (self._myFullValues.sort, self._myFullValues.sort_function)):
(self._cached_sort == (self._myFullValues.sort, self._myFullValues.sort_function)):
return self._cached_tree_as_list
except:
pass
self._cached_tree = weakref.proxy(self._myFullValues)
self._cached_sort = (self._myFullValues.sort, self._myFullValues.sort_function)
self._cached_tree_as_list = self._myFullValues.getTreeAsList()
return self._cached_tree_as_list

def _walkMyValues(self):
return self._myFullValues.walkTree()

def _delMyValues(self):
self._myFullValues = None

values = property(_getApparentValues, _setMyValues, _delMyValues)

def filter_value(self, index):
if self._filter in self.display_value(self.values[index].getContent()):
return True
else:
return False
#def display_value(self, vl):

# def display_value(self, vl):
# return vl

def set_up_handlers(self):
super(MultiLineTreeNew, self).set_up_handlers()
self.handlers.update({
ord('<'): self.h_collapse_tree,
ord('>'): self.h_expand_tree,
ord('['): self.h_collapse_tree,
ord(']'): self.h_expand_tree,
ord('{'): self.h_collapse_all,
ord('}'): self.h_expand_all,
ord('h'): self.h_collapse_tree,
ord('l'): self.h_expand_tree,
ord('<'): self.h_collapse_tree,
ord('>'): self.h_expand_tree,
ord('['): self.h_collapse_tree,
ord(']'): self.h_expand_tree,
ord('{'): self.h_collapse_all,
ord('}'): self.h_expand_all,
ord('h'): self.h_collapse_tree,
ord('l'): self.h_expand_tree,
})



#def display_value(self, vl):
# def display_value(self, vl):
# return vl



def _before_print_lines(self):
pass

def _set_line_values(self, line, value_indexer):
line._tree_real_value = None
line._tree_depth = False
line._tree_real_value = None
line._tree_depth = False
line._tree_sibling_next = False
line._tree_has_children = False
line._tree_expanded = False
line._tree_last_line = False
line._tree_depth_next = False
line._tree_ignore_root = None
line._tree_expanded = False
line._tree_last_line = False
line._tree_depth_next = False
line._tree_ignore_root = None
try:
line.value = self.display_value(self.values[value_indexer])
line._tree_real_value = self.values[value_indexer]
line._tree_ignore_root = self._myFullValues.ignoreRoot
try:
line._tree_depth = self.values[value_indexer].findDepth()
line._tree_depth = self.values[value_indexer].findDepth()
line._tree_has_children = self.values[value_indexer].hasChildren()
line._tree_expanded = self.values[value_indexer].expanded
line._tree_expanded = self.values[value_indexer].expanded
except:
line._tree_depth = False
line._tree_depth = False
line._tree_has_children = False
line._tree_expanded = False
line._tree_expanded = False
try:
if line._tree_depth == self.values[value_indexer+1].findDepth():
if line._tree_depth == self.values[value_indexer + 1].findDepth():
line._tree_sibling_next = True
else:
line._tree_sibling_next = False

except:
line._sibling_next = False
line._tree_last_line = True
try:
line._tree_depth_next = self.values[value_indexer+1].findDepth()
line._tree_depth_next = self.values[value_indexer + 1].findDepth()
except:
line._tree_depth_next = False
line.hidden = False
except IndexError:
self._set_line_blank(line)
except TypeError:
self._set_line_blank(line)

def h_collapse_tree(self, ch):
if self.values[self.cursor_line].expanded and self.values[self.cursor_line].hasChildren():
self.values[self.cursor_line].expanded = False
Expand All @@ -272,27 +275,29 @@ def h_expand_tree(self, ch):
v.expanded = True
self._cached_tree = None
self.display()

def h_collapse_all(self, ch):
for v in self._myFullValues.walkTree(onlyExpanded=True):
v.expanded = False
self._cached_tree = None
self.cursor_line = 0
self.display()

def h_expand_all(self, ch):
for v in self._myFullValues.walkTree(onlyExpanded=False):
v.expanded = True
v.expanded = True
self._cached_tree = None
self.cursor_line = 0
self.cursor_line = 0
self.display()


class MultiLineTreeNewAnnotated(MultiLineTreeNew):
_contained_widgets = TreeLineAnnotated


class MultiLineTreeNewAction(MultiLineTreeNew, multiline.MultiLineAction):
pass


class MultiLineTreeNewAnnotatedAction(MultiLineTreeNew, multiline.MultiLineAction):
_contained_widgets = TreeLineAnnotated

1 change: 1 addition & 0 deletions npyscreen/wgmultiselecttree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import wgmultilinetree as multilinetree
from . import wgcheckbox as checkbox
import weakref
import curses


class MultiSelectTree(multilinetree.SelectOneTree):
Expand Down

0 comments on commit ed9a0a6

Please sign in to comment.