summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-26 14:49:16 +0100
committerhut <hut@lavabit.com>2009-12-26 14:49:16 +0100
commit0da7b3961de437415753383a5c2b7efaafcc4586 (patch)
tree8a4216a7a1f73eb665b08b5e7199a1e2291cf0f5
parent656b444dde67463c75420ad91a7e65f32fd85efc (diff)
downloadranger-0da7b3961de437415753383a5c2b7efaafcc4586.tar.gz
moved the command parser into a seperate file
-rw-r--r--ranger/commands.py44
-rw-r--r--ranger/ext/command_parser.py51
2 files changed, 61 insertions, 34 deletions
diff --git a/ranger/commands.py b/ranger/commands.py
index 26c96e1b..a3cc131e 100644
--- a/ranger/commands.py
+++ b/ranger/commands.py
@@ -1,47 +1,23 @@
 import os
 from ranger.shared import FileManagerAware
 from ranger.gui.widgets import console_mode as cmode
-
-# -------------------------------- helper classes
-
-class parse(object):
-	"""Parse commands and extract information"""
-	def __init__(self, line):
-		self.line = line
-		self.chunks = line.split()
-
-		try:
-			self.firstpart = line[:line.rindex(' ') + 1]
-		except ValueError:
-			self.firstpart = ''
-
-	def chunk(self, n, otherwise=''):
-		if len(self.chunks) > n:
-			return self.chunks[n]
-		else:
-			return otherwise
-
-
-	def __add__(self, newpart):
-		return self.firstpart + newpart
+from ranger.ext.command_parser import LazyParser as parse
 
 class Command(FileManagerAware):
 	"""Abstract command class"""
 	name = None
-	line = ''
 	def __init__(self, line, mode):
 		self.line = line
 		self.mode = mode
 
 	def execute(self):
-		pass
+		"""Override this"""
 
 	def tab(self):
-		pass
+		"""Override this"""
 
 	def quick_open(self):
-		pass
-
+		"""Override this"""
 
 # -------------------------------- definitions
 
@@ -56,7 +32,7 @@ class cd(Command):
 	def execute(self):
 		line = parse(self.line)
 		try:
-			destination = line.chunks[1]
+			destination = line.rest(1)
 		except IndexError:
 			destination = '~'
 
@@ -72,7 +48,7 @@ class cd(Command):
 		pwd = self.fm.env.pwd.path
 
 		try:
-			rel_dest = line.chunks[1]
+			rel_dest = line.rest(1)
 		except IndexError:
 			rel_dest = ''
 
@@ -110,7 +86,7 @@ class cd(Command):
 		pwd = self.fm.env.pwd.path
 
 		try:
-			rel_dest = line.chunks[1]
+			rel_dest = line.rest(1)
 		except IndexError:
 			return False
 
@@ -130,7 +106,7 @@ class find(Command):
 			self._search()
 
 		import re
-		search = parse(self.line).chunk(1)
+		search = parse(self.line).rest(1)
 		search = re.escape(search)
 		self.fm.env.last_search = re.compile(search, re.IGNORECASE)
 
@@ -146,7 +122,7 @@ class find(Command):
 		line = parse(self.line)
 		pwd = self.fm.env.pwd
 		try:
-			arg = line.chunks[1]
+			arg = line.rest(1)
 		except IndexError:
 			return False
 		
@@ -180,7 +156,7 @@ class mkdir(Command):
 	def execute(self):
 		line = parse(self.line)
 		try:
-			self.fm.mkdir(line.chunks[1])
+			self.fm.mkdir(line.rest(1))
 		except IndexError:
 			pass
 
diff --git a/ranger/ext/command_parser.py b/ranger/ext/command_parser.py
new file mode 100644
index 00000000..432f0364
--- /dev/null
+++ b/ranger/ext/command_parser.py
@@ -0,0 +1,51 @@
+class LazyParser(object):
+	"""Parse commands and extract information"""
+	def __init__(self, line):
+		self.line = line
+		self._chunks = None
+		self._rests = None
+		self._rests_loaded = 0
+		self._rests_gen_instance = None
+
+		try:
+			self.firstpart = line[:line.rindex(' ') + 1]
+		except ValueError:
+			self.firstpart = ''
+
+	def chunk(self, n, otherwise=''):
+		"""Chunks are pieces of the command seperated by spaces"""
+		if self._chunks is None:
+			self._chunks = line.split()
+
+		if len(self._chunks) > n:
+			return self._chunks[n]
+		else:
+			return otherwise
+
+	def rest(self, n, otherwise=''):
+		"""Rests are the strings which come after each word."""
+		if self._rests is None:
+			self._rests = list(self._rest_generator())
+			# TODO: Don't calculate all the rest elements if not needed
+
+		if len(self._rests) > n:
+			return self._rests[n]
+		else:
+			return otherwise
+
+	def _rest_generator(self):
+		lastrest = self.line
+		n = 0
+		while n < len(lastrest):
+			if lastrest[n] == ' ':
+				n += 1
+			else:
+				yield lastrest[n:]
+				n = lastrest.find(' ', n) + 1
+				if n <= 0:
+					break
+				lastrest = lastrest[n:]
+				n = 0
+
+	def __add__(self, newpart):
+		return self.firstpart + newpart
1db73e7e5ae19adfd'>^
a654e4ec ^
204dae92 ^

















201458e3 ^
204dae92 ^
201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^




201458e3 ^
204dae92 ^
201458e3 ^
204dae92 ^
805d58c6 ^

204dae92 ^
805d58c6 ^

204dae92 ^





201458e3 ^
5fe060d5 ^
9e751bb8 ^
2c678a4e ^
5fe060d5 ^
805d58c6 ^





9e751bb8 ^

















5fe060d5 ^

805d58c6 ^










9e751bb8 ^






5fe060d5 ^
9e751bb8 ^






672e3e50 ^


a654e4ec ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164