Browse Source
moved opts to json file and added a handler for the opts
moved opts to json file and added a handler for the opts
finished the path abbreviation and picker functionmaster
6 changed files with 225 additions and 71 deletions
-
18ext_open/ext_open.py
-
3ext_open/opener.py
-
95ext_open/opts.json
-
138ext_open/opts.py
-
2ext_open/searcher.py
-
40ext_open/util.py
@ -0,0 +1,95 @@ |
|||
{ |
|||
"bools": [ |
|||
{ |
|||
"action": "store_true", |
|||
"ostring": ["+i", "++case-insensitive"] |
|||
}, |
|||
{ |
|||
"action": "store_true", |
|||
"help": "Search for exact application name", |
|||
"ostring": ["+E", "++exact"] |
|||
}, |
|||
{ |
|||
"action": "store_true", |
|||
"help": "Use regex to search", |
|||
"ostring": ["+r", "++regex"] |
|||
}, |
|||
{ |
|||
"action": "store_true", |
|||
"help": "Prints path instead of opening application", |
|||
"ostring": ["+p", "++path"] |
|||
}, |
|||
{ |
|||
"action": "store_true", |
|||
"help": "Open in editor", |
|||
"ostring": ["+e", "++edit"] |
|||
} |
|||
], |
|||
"exe": [ |
|||
{ |
|||
"action": "store_true", |
|||
"help": "Open windowless", |
|||
"ostring": ["+w", "++windowless"] |
|||
}, |
|||
{ |
|||
"help": "Directory to start in", |
|||
"metavar": "dir", |
|||
"ostring": ["+d", "++start-in"] |
|||
}, |
|||
{ |
|||
"help": "Query to search for", |
|||
"ostring": ["query"] |
|||
}, |
|||
{ |
|||
"action": "store", |
|||
"help": "Command-line args", |
|||
"nargs": "*", |
|||
"ostring": ["arg"] |
|||
} |
|||
], |
|||
"exts": [ |
|||
{ |
|||
"ext": "py", |
|||
"opts": [] |
|||
}, |
|||
{ |
|||
"ext": "java", |
|||
"opts": [] |
|||
} |
|||
], |
|||
"other": [ |
|||
{ |
|||
"help": "Directory to start in", |
|||
"metavar": "dir", |
|||
"ostring": ["+d", "++start-in"] |
|||
}, |
|||
{ |
|||
"help": "Query to search for", |
|||
"ostring": ["query"] |
|||
}, |
|||
{ |
|||
"action": "store", |
|||
"help": "Command-line args", |
|||
"nargs": "*", |
|||
"ostring": ["arg"] |
|||
} |
|||
], |
|||
"prefix": [ |
|||
{ |
|||
"action": "store_true", |
|||
"help": "Update the listings for ext", |
|||
"ostring": ["+u", "++update"] |
|||
}, |
|||
{ |
|||
"help": "Trim backup history, either all or N updates", |
|||
"metavar": "(all | N)", |
|||
"ostring": ["+t", "++trim-history"] |
|||
}, |
|||
{ |
|||
"action": "store_true", |
|||
"help": "updates the modified times for paths in config\nuseful for ensuring all new files are found", |
|||
"ostring": ["+m", "++update-modified-times"] |
|||
} |
|||
], |
|||
"prefix_char": "+" |
|||
} |
|||
@ -1,53 +1,87 @@ |
|||
prefix_char = '+' |
|||
prefix = [ |
|||
{ |
|||
'ostring':['+u','++update'], |
|||
'help': 'Update the listings for ext', |
|||
'action':'store_true', |
|||
}, |
|||
{ |
|||
'ostring':['+t','++trim-history'], |
|||
'help': 'Trim backup history, either all or N updates', |
|||
'metavar': '(all | N)' |
|||
}, |
|||
{ |
|||
'ostring':['+m','++update-modified-times'], |
|||
'action':'store_true', |
|||
'help': '''updates the modified times for paths in config |
|||
useful for ensuring all new files are found''', |
|||
}, |
|||
] |
|||
bools = [ |
|||
{'action': 'store_true', 'ostring': ['+i', '++case-insensitive']}, |
|||
{'action': 'store_true', 'help': 'Search for exact application name', 'ostring': ['+E', '++exact']}, |
|||
{'action': 'store_true', 'help': 'Use regex to search', 'ostring': ['+r', '++regex']}, |
|||
{'action': 'store_true', 'help': 'Prints path instead of opening application', 'ostring': ['+p', '++path']}, |
|||
{'action': 'store_true', 'help': 'Open in editor', 'ostring': ['+e', '++edit']}, |
|||
] |
|||
|
|||
other = [ |
|||
{'help': 'Directory to start in', 'ostring': ['+d', '++start-in'],'metavar':'dir'}, |
|||
{'ostring':['query'],'help':'Query to search for'}, |
|||
{'action': 'store', 'help': 'Command-line args', 'nargs': '*', 'ostring': ['arg']}, |
|||
] |
|||
import json |
|||
import os |
|||
import copy |
|||
class ArgumentHandler: |
|||
def __init__(self,args_file): |
|||
|
|||
self.args_file = args_file |
|||
self.exts = set() |
|||
|
|||
with open(self.args_file) as file: |
|||
self.data = json.load(file) |
|||
|
|||
for _ext in self.data['exts']: |
|||
|
|||
ext = _ext['ext'] |
|||
self.exts.add(ext) |
|||
args = _ext['opts'] |
|||
|
|||
self.set(ext,ArgumentContainer(args)) |
|||
|
|||
for key,value in self.data.items(): |
|||
if key != 'exts' and isinstance(value,list): |
|||
self.set(key,ArgumentContainer(value)) |
|||
|
|||
self.prefix_char = self.data['prefix_char'] |
|||
|
|||
def get(self,item): |
|||
return self.__dict__[item] |
|||
|
|||
def set(self,item,value): |
|||
self.__dict__[item] = value |
|||
|
|||
def save(self): |
|||
data = [] |
|||
for ext in self.exts: |
|||
data.append({'ext':ext,'opts':self.get(ext).to_dict()}) |
|||
valid = set(self.__dict__.keys()) - self.exts |
|||
valid -= {'self.args_file'} |
|||
for key in valid: |
|||
value = self.get(key) |
|||
if isinstance(value,str): |
|||
self.data[key] = value |
|||
elif isinstance(value,ArgumentContainer): |
|||
self.data[key] = value.to_dict() |
|||
|
|||
def add_ext(self,ext): |
|||
self.set(ext,ArgumentContainer([])) |
|||
self.exts.add(ext) |
|||
|
|||
def commit(self): |
|||
backup_name = self.args_file + '.bak' |
|||
if os.path.exists(backup_name): |
|||
os.remove(backup_name) |
|||
os.copy(self.args_file,self.backup_name) |
|||
with open(self.args_file,'w') as file: |
|||
json.dump(self.data,file) |
|||
|
|||
def revert(self): |
|||
backup_name = self.args_file + '.bak' |
|||
if os.path.exists(backup_name): |
|||
os.remove(self.args_file) |
|||
os.rename(backup_name,self.args_file) |
|||
|
|||
class ArgumentContainer: |
|||
def __init__(self,l_of_args): |
|||
self.args = l_of_args |
|||
def add_argument(self,*args,**kwargs): |
|||
new_arg = { |
|||
'ostring': args, |
|||
} |
|||
new_arg.update(kwargs) |
|||
self.args.append(new_arg) |
|||
|
|||
def to_dict(self): |
|||
return copy.deepcopy(self.args) |
|||
|
|||
def __str__(self): |
|||
ostrings = (opt['ostring'] for opt in self.args) |
|||
f_ostrings = ('({})'.format(', '.join(ostring)) for ostring in ostrings) |
|||
return '[{}]'.format(', '.join(f_ostrings)) |
|||
|
|||
def __repr__(self): |
|||
return str(self) |
|||
|
|||
cur = ArgumentHandler('opts.json') |
|||
globals().update(cur.data) |
|||
common = bools+other |
|||
exts = [ |
|||
{ |
|||
'ext' : 'py', |
|||
'opts' : [] |
|||
}, |
|||
{ |
|||
'ext' : 'java', |
|||
'opts': [] |
|||
} |
|||
] |
|||
exe = [ |
|||
{ |
|||
'ostring':['+w','++windowless'], |
|||
'help': 'Open windowless', |
|||
'action': 'store_true', |
|||
}, |
|||
{'help': 'Directory to start in', 'ostring': ['+d', '++start-in'],'metavar':'dir'}, |
|||
{'ostring':['query'],'help':'Query to search for'}, |
|||
{'action': 'store', 'help': 'Command-line args', 'nargs': '*', 'ostring': ['arg']}, |
|||
] |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue