Browse Source

Basic functionality works

Wayne Werner 1 year ago
parent
commit
603560fe45
3 changed files with 165 additions and 28 deletions
  1. 3
    0
      Pipfile
  2. 16
    0
      Pipfile.lock
  3. 146
    28
      shibboleth.py

+ 3
- 0
Pipfile View File

@@ -0,0 +1,3 @@
1
+[[source]]
2
+url = "https://pypi.python.org/simple"
3
+verify_ssl = true

+ 16
- 0
Pipfile.lock View File

@@ -0,0 +1,16 @@
1
+{
2
+    "_meta": {
3
+        "hash": {
4
+            "sha256": "cb65967bdf3a7372749ce6144be4ee63f412aeeac624342fda3a2b58b1c43c15"
5
+        },
6
+        "requires": {},
7
+        "sources": [
8
+            {
9
+                "url": "https://pypi.python.org/simple",
10
+                "verify_ssl": true
11
+            }
12
+        ]
13
+    },
14
+    "default": {},
15
+    "develop": {}
16
+}

+ 146
- 28
shibboleth.py View File

@@ -14,15 +14,36 @@ DEFAULT_COLORS = {
14 14
     '6-waiting': 95, # light pink?
15 15
 }
16 16
 TAG_PATTERN = r'\[(.*)\]'
17
+PRIORITIES = {
18
+    '1':'1-now',
19
+    '2':'2-next',
20
+    '3':'3-soon',
21
+    '4':'4-later',
22
+    '5':'5-someday',
23
+    '6':'6-waiting',
24
+}
17 25
 
18 26
 
19 27
 class Filename:
20 28
     def __init__(self, filename):
29
+        self.tags = []
21 30
         tags_found = re.search(TAG_PATTERN, filename)
22 31
         if tags_found:
23
-            self.tags = tags_found.groups()[0].split()
32
+            self.tags.extend(tags_found.groups()[0].split())
33
+        if '1-now' in self.tags:
34
+            self._priority = '1-now'
35
+        elif '2-next' in self.tags:
36
+            self._priority = '2-next'
37
+        elif '3-soon' in self.tags:
38
+            self._priority = '3-soon'
39
+        elif '4-later' in self.tags:
40
+            self._priority = '4-later'
41
+        elif '5-someday' in self.tags:
42
+            self._priority = '5-someday'
43
+        elif '6-waiting' in self.tags:
44
+            self._priority = '6-waiting'
24 45
         else:
25
-            self.tags = None
46
+            self._priority = None
26 47
         self.filename = filename
27 48
 
28 49
     @property
@@ -34,40 +55,114 @@ class Filename:
34 55
             filename = filename.replace(tag, colorized)
35 56
         return filename
36 57
 
58
+    @property
59
+    def priority(self):
60
+        return self._priority
61
+
62
+    @priority.setter
63
+    def priority(self, value):
64
+        head, _, tail = self.filename.rpartition(self._priority)
65
+        filename = head + value + tail
66
+        os.rename(self.filename, filename)
67
+        self.filename = filename
68
+        self.tags.remove(self._priority)
69
+        self.tags.append(value)
70
+        self._priority = value
71
+
37 72
 
38 73
 class Shibboleth(cmd.Cmd):
39 74
     def __init__(self):
40 75
         super().__init__()
41
-        self.prompt = 'shibboleth\n>'
76
+        self.prompt = f'shibboleth:{os.getcwd()}\n>'
77
+        self.selected = None
42 78
 
43
-    def do_pls(self, line):
44
-        files = os.listdir(os.path.curdir)
79
+    def postcmd(self, stop, line):
80
+        self.prompt = f'shibboleth:{os.getcwd()}\n>'
81
+        if self.selected:
82
+            self.prompt = f'shibboleth:{self.selected.colorized}\n>'
83
+        return stop
45 84
 
46
-        if not line:
47
-            ordered = {
48
-                '1-now': [],
49
-                '2-next': [],
50
-                '3-soon': [],
51
-                '4-later': [],
52
-                '5-someday': [],
53
-                '6-waiting': [],
54
-            }
55
-            for filename in files:
56
-                filename = Filename(filename)
57
-                if '1-now' in filename.tags:
58
-                    ordered['1-now'].append(filename)
59
-                elif '2-next' in filename.tags:
60
-                    ordered['2-next'].append(filename)
61
-                elif '3-soon' in filename.tags:
62
-                    ordered['3-soon'].append(filename)
63
-                elif '4-later' in filename.tags:
64
-                    ordered['4-later'].append(filename)
65
-                elif '5-someday' in filename.tags:
66
-                    ordered['5-someday'].append(filename)
67
-                elif '6-waiting' in filename.tags:
68
-                    ordered['6-waiting'].append(filename)
85
+    def do_cd(self, line):
86
+        try:
87
+            os.chdir(line)
88
+        except Exception as e:
89
+            print(e)
69 90
 
91
+    def complete_cd(self, text, line, begidx, endidx):
92
+        paths = glob.glob(text+'*')
93
+        return paths
70 94
 
95
+    def do_pls(self, line):
96
+        '''
97
+        Priority list - list files in the folder that have the
98
+        specified priority.
99
+        '''
100
+        files = [Filename(name) for name in os.listdir(os.path.curdir)]
101
+        targets = {
102
+            '1': '1-now',
103
+            '2': '2-next',
104
+            '3': '3-soon',
105
+            '4': '4-later',
106
+            '5': '5-someday',
107
+            '6': '6-waiting',
108
+        }
109
+        if line:
110
+            try:
111
+                target = targets[line]
112
+            except KeyError:
113
+                print(f'Unknown priority {line!r}')
114
+                target = None
115
+
116
+        for file_ in files:
117
+            if not line or line == '1':
118
+                if '1-now' in file_.tags:
119
+                    print(file_.colorized)
120
+            elif target:
121
+                if target in file_.tags:
122
+                    print(file_.colorized)
123
+
124
+    def do_now(self, line):
125
+        self.do_pls(line='1')
126
+
127
+    def do_next(self, line):
128
+        self.do_pls(line='2')
129
+
130
+    def do_soon(self, line):
131
+        self.do_pls(line='3')
132
+
133
+    def do_later(self, line):
134
+        self.do_pls(line='4')
135
+
136
+    def do_someday(self, line):
137
+        self.do_pls(line='5')
138
+
139
+    def do_waiting(self, line):
140
+        self.do_pls(line='6')
141
+
142
+    def do_deselect(self, line):
143
+        self.selected = None
144
+
145
+    def do_select(self, line):
146
+        if not line:
147
+            self.do_deselect()
148
+        else:
149
+            if not os.path.isfile(line):
150
+                print(f'Unknown file {line!r}')
151
+            else:
152
+                self.selected = Filename(os.path.abspath(line))
153
+
154
+    def complete_select(self, text, line, begidx, endidx):
155
+        paths = glob.glob(text+'*')
156
+        return paths
157
+
158
+    def do_priority(self, line):
159
+        if not self.selected:
160
+            print('Select a file first and try again')
161
+        else:
162
+            try:
163
+                self.selected.priority = PRIORITIES[line]
164
+            except KeyError:
165
+                print(f'Unknown priority {line!r}')
71 166
 
72 167
     def do_ls(self, line):
73 168
         line = os.path.expanduser(line)
@@ -80,6 +175,29 @@ class Shibboleth(cmd.Cmd):
80 175
         for filename in files:
81 176
             print(Filename(filename).colorized)
82 177
 
178
+    def do_show(self, line):
179
+        if not self.selected or line:
180
+            print('Select a file and try again')
181
+        else:
182
+            filename = self.selected.filename if self.selected else line
183
+            print('*'*80)
184
+            with open(filename) as f:
185
+                print(f.read())
186
+            print('*'*80)
187
+
188
+    def do_edit(self, line):
189
+        if not self.selected or line:
190
+            print('Select a file and try again')
191
+        else:
192
+            filename = self.selected.filename if self.selected else line
193
+
194
+        try:
195
+            editor = os.environ['EDITOR']
196
+        except KeyError:
197
+            print('Set environment EDITOR and try again')
198
+        else:
199
+            os.system(f'{editor} "{filename}"')
200
+
83 201
     def do_exit(self, line):
84 202
         print('Goodbye!')
85 203
         return True

Loading…
Cancel
Save