Skip to content

Commit

Permalink
various bigfixing & rewritings, & vscode implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
C2N14 committed Jul 17, 2018
1 parent aec1dbe commit c92b8c5
Show file tree
Hide file tree
Showing 15 changed files with 2,386 additions and 185 deletions.
13 changes: 4 additions & 9 deletions README.md
Expand Up @@ -19,13 +19,7 @@ Simple, set-and-forget python application for changing between GNOME themes acco

**[RECOMMENDED]** You can go to [releases](https://github.com/C2N14/AutomaThemely/releases) and download whichever package suits your needs

~~Or install it through snapcraft~~ ([doesn't work at the moment, some issue with snapcraft](https://forum.snapcraft.io/t/update-all-python-snaps-not-working-with-classic-confinement-even-with-cleanbuild/5971))

```
snap install automathemely --classic
```

Or even clone the project and install it yourself (icons and shortcuts will not be installed)
Or clone the project and install it yourself (icons and shortcuts will not be installed)

```
git clone https://github.com/C2N14/AutomaThemely.git
Expand All @@ -41,7 +35,7 @@ Once installed, run once to generate a settings file
automathemely
```

And then run again with `-m` or `--manage` use the settings manager (requires GTK)
And then run again with `-m` or `--manage` use the settings manager (requires GTK 3.0+)

```
automathemely --manage
Expand Down Expand Up @@ -74,7 +68,8 @@ In the case of it failing to do its job (please report to [issues](https://githu

## Notes

* Although it is specifically made for GNOME, it should be compatible with Unity, and other desktop environments could be implemented in the future.
* This program assumes that a day in your location has both sunrise and sunset in the same 0 to 24 hr day span, and if you decide to set a custom time offset make sure both of these events still occur within this span
* Although it is specifically made for GNOME, it should be compatible with Unity and other GNOME based environments, and other desktop environments *could* be implemented in the future.
* This program requires an active internet connection **ONLY** if you set *Auto Location* on (it uses your ip to determine your geolocation), otherwise you can manually set your location and you won't need it.
* You should never really manually edit the version number on the configuration file because it will most likely be overwritten
* Tested with Ubuntu 18.04 & Ubuntu 16.04
Expand Down
2 changes: 1 addition & 1 deletion automathemely/__init__.py
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path

_ROOT = os.path.abspath(os.path.dirname(__file__))
__version__ = 1.0
__version__ = 1.1


def get_resource(path=''):
Expand Down
12 changes: 9 additions & 3 deletions automathemely/autoth_tools/__init__.py
@@ -1,3 +1,9 @@
from . import argmanager
from . import updsunhours
from . import settsmanager
import glob
from os.path import dirname, basename, isfile

modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

# Dynamically import all modules
# noinspection PyPep8
from . import *
25 changes: 14 additions & 11 deletions automathemely/autoth_tools/argmanager.py
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
import json
from sys import exit, path
import argparse
import pickle as pkl
import fcntl
import copy
import fcntl
import json
import pickle as pkl
from sys import exit

from automathemely import get_local

parser = argparse.ArgumentParser()
Expand All @@ -17,7 +18,7 @@
default=False)


# For --setting arg
# For --setting arg
def lookup_dic(d, k):
val = d
for key in k:
Expand All @@ -28,7 +29,7 @@ def lookup_dic(d, k):
return True


# For --setting arg
# For --setting arg
def write_dic(dic, lis, val):
if len(lis) == 0:
return val
Expand All @@ -38,7 +39,7 @@ def write_dic(dic, lis, val):
return dic


# For --list arg
# For --list arg
def print_list(d, indent=0):
for key, value in d.items():
print('{}{}'.format('\t' * indent, key), end='')
Expand All @@ -49,16 +50,18 @@ def print_list(d, indent=0):
print(' = {}'.format(value))


# ARGUMENTS FUNCTION
# ARGUMENTS FUNCTION
def main(us_se):
args = parser.parse_args()

# LIST
if args.list:
print('Current settings:')
print_list(us_se)
exit()

# SET
elif args.setting:

if not args.setting.count('=') == 1:
exit('\nERROR: Invalid string (None or more than one "=" signs)')

Expand Down Expand Up @@ -104,7 +107,7 @@ def main(us_se):
else:
exit('\nERROR: Key "{}" not found'.format(to_set_key))

# MANAGE
# MANAGE
elif args.manage:
from . import settsmanager

Expand All @@ -127,7 +130,7 @@ def main(us_se):
else:
return 'No changes were made', True

# UPDATE
# UPDATE
elif args.update:
from . import updsunhours
output, is_error = updsunhours.main(us_se)
Expand Down
113 changes: 113 additions & 0 deletions automathemely/autoth_tools/extratools.py
@@ -0,0 +1,113 @@
#!/usr/bin/env python3
import json
import os
from pathlib import Path
from subprocess import check_output, CalledProcessError


# For getting vscode themes
def scan_vscode_extensions(path):
th = []
try:
for k in next(os.walk(path))[1]:
if 'theme' in k:
with open(os.path.join(path, k, 'package.json')) as f:
data = json.load(f)
if 'themes' in data['contributes']:
for i in data['contributes']['themes']:
if 'id' in i:
th.append(i['id'])
elif 'label' in i:
th.append(i['label'])
except StopIteration:
pass
return th


def get_extra_themes(extra, placeholder=False):
if extra == 'atom':
default = dict(themes=[], syntaxes=[])
if placeholder:
return default, True
try:
atom_packs = check_output('apm list --themes --bare', shell=True).decode('utf-8')
except CalledProcessError:
return default, True

atom_packs = atom_packs.split('\n')
for i, v in enumerate(atom_packs):
atom_packs[i] = v.split('@')[0]
atom_packs = list(filter(None, atom_packs))
atom_themes = []
atom_syntaxes = []
for v in atom_packs:
if 'syntax' in v:
atom_syntaxes.append(v)
else:
atom_themes.append(v)
return {'themes': sorted(atom_themes), 'syntaxes': sorted(atom_syntaxes)}, False

if extra == 'vscode':
default = dict(themes=[])
if placeholder:
return default, True

# All possible paths that I know of that can contain VSCode extensions
vscode_extensions_paths = ['/snap/vscode/current/usr/share/code/resources/app/extensions',
'/usr/share/code/resources/app/extensions',
os.path.join(Path.home(), '.vscode/extensions')]
vscode_themes = []
for p in vscode_extensions_paths:
vscode_themes += scan_vscode_extensions(p)

if not vscode_themes:
return default, True

else:
return {'themes': sorted(vscode_themes)}, False


def set_extra_theme(us_se, extra, theme_type):
import getpass
username = getpass.getuser()
if extra == 'atom':
import shutil

target_file = '/home/{}/.atom/config.cson'.format(username)
if not os.path.isfile(target_file):
return True

i = 0
with open(target_file) as fileIn, open(
target_file + '.tmp'.format(username), 'w') as fileOut:
# First look for line with "themes", then go from bottom to top writing lines
for item in fileIn:
if i == 1:
# Make sure it has the same spaces as the original file
item = ' ' * (len(item) - len(item.lstrip(' '))) + '"' \
+ us_se['extras']['atom']['themes'][theme_type]['syntax'] + '"'
i -= 1
if i == 2:
# MakeReally sure it has the same spaces as the original file
item = ' ' * (len(item) - len(item.lstrip(' '))) + '"' \
+ us_se['extras']['atom']['themes'][theme_type]['theme'] + '"'
i -= 1
if item.strip().startswith('themes:'):
i = 2

fileOut.write(item)

shutil.move(target_file + '.tmp', target_file)

elif extra == 'vscode':
target_file = '/home/{}/.config/Code/User/settings.json'.format(username)
if not os.path.isfile(target_file):
return True

with open(target_file) as f:
p = json.load(f)

p.update({'workbench.colorTheme': us_se['extras']['vscode']['themes'][theme_type]})

with open(target_file, 'w') as f:
json.dump(p, f, indent=4)

0 comments on commit c92b8c5

Please sign in to comment.