-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdl
More file actions
executable file
·114 lines (93 loc) · 3.31 KB
/
cdl
File metadata and controls
executable file
·114 lines (93 loc) · 3.31 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020-2024 Jared Daniel Carbonell Recomendable.
from getpass import getuser
import os
import sys
import subprocess
# Computer-specific constants
USERNAME = getuser()
# Ubuntu / Fedora using GNOME
FILE_MANAGER_CMD = 'nautilus'
HOME_DIR = 'home'
DROPBOX_DIR = '/{}/{}/Dropbox/'.format(HOME_DIR, USERNAME)
# macOS
#FILE_MANAGER_CMD = 'open'
#HOME_DIR = 'Users'
#DROPBOX_DIR = '/{}/{}/Library/CloudStorage/Dropbox/'.format(HOME_DIR, USERNAME)
# File locations
# Structure:
# 'CMD_PARAM': [AUTO_NAV_DEPTH, 'RELATIVE FILE_LOCATION']
NAV_DIRS = {
'u-admin' : [3, 'UoA/ADMIN'],
# 'u-clubs' : [0, 'UoA/CLUBS'],
# 'u-competitions' : [2, 'UoA/COMPETITIONS'],
'u-courses' : [1, 'UoA/COURSES'],
# 'u-extcurr' : [3, 'UoA/EXTCURR'],
'u-ta' : [2, 'UoA/COURSES/2024-S1/ENGTA'],
'u-w201' : [0, 'UoA/WORK/COMPSYS201-2024'],
'u-w281' : [0, 'UoA/WORK/SOFTENG281-2024'],
'u-w350' : [0, 'UoA/WORK/SOFTENG350-2024'],
'u-w701' : [0, 'UoA/WORK/SOFTENG701-2024'],
'u-w770' : [0, 'UoA/WORK/SOFTENG770-2024'],
'u-wra270' : [0, 'UoA/WORK/RA-MECHENG270-2022'],
'u-wrapoly' : [0, 'UoA/WORK/RA-POLYGLOT-PROJECT'],
'u-wraps' : [0, 'UoA/WORK/RA-PROGRAM-SYNTHESIS'],
'bc' : [2, 'Bestcoder'],
}
# Utility Functions
def list_dir():
'''List directories in current working directory, ascending order.'''
return sorted(os.listdir())
def auto_nav(target_dir, auto_nav_depth):
'''Navigate to latest folder.'''
os.chdir(target_dir)
target_dir += '/'
while auto_nav_depth > 0:
target_dir += str(list_dir()[-1]) + '/'
os.chdir(target_dir)
auto_nav_depth -= 1
return target_dir
def parse_dir_path(cmd_param):
'''Parse absolute path to target directory.'''
auto_nav_depth, relative_target_dir = NAV_DIRS[cmd_param]
target_dir = DROPBOX_DIR + relative_target_dir
return auto_nav(target_dir, auto_nav_depth)
def parse_cmd(dir_path):
return '{} "{}"'.format(FILE_MANAGER_CMD, dir_path)
def run(cmd):
'''Send command to shell.'''
subprocess.Popen(['bash', '-c', cmd])
def form_help_doc():
'''Return help documenation.'''
doc = 'Usage: cdl [-p] [options]\n\nOptions:\n'
for key in NAV_DIRS:
doc += ' {:10} go to ~/Dropbox/{}\n'.format(key, NAV_DIRS[key][1])
return doc
def run_cmd(cmd_param, print_only=False):
dir_path = parse_dir_path(cmd_param)
if print_only:
print(dir_path)
return
command = parse_cmd(dir_path)
print('OPENING "{}"'.format(dir_path))
run(command)
# Main
if __name__ == '__main__':
if len(sys.argv) == 2:
cmd_param = sys.argv[1]
if cmd_param in NAV_DIRS:
run_cmd(cmd_param)
elif cmd_param in ('-h', '--help', '-p'):
print(form_help_doc())
else:
print('Argument not supported.')
elif len(sys.argv) == 3:
if sys.argv[1] in NAV_DIRS and sys.argv[2] == '-p':
run_cmd(sys.argv[1], True)
elif sys.argv[1] == '-p' and sys.argv[2] in NAV_DIRS:
run_cmd(sys.argv[2], True)
else:
print(form_help_doc())
else:
print(form_help_doc())