-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
1274 lines (1011 loc) · 36.2 KB
/
utils.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright (c) 2013,14 Walter Bender
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import os
import json
import subprocess
import dbus
import stat
import glob
import urllib
from random import uniform
import tempfile
import cairo
import email.utils
import re
import time
import configparser
from gi.repository import Vte
from gi.repository import Gio
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import GConf
from gi.repository import GObject
from sugar3 import env
from sugar3 import profile
from sugar3.datastore import datastore
from sugar3.graphics.xocolor import XoColor
from jarabe import config
from jarabe.model import shell
import logging
_logger = logging.getLogger('training-activity-testutils')
_user_extensions_path = os.path.join(env.get_profile_path(), 'extensions')
_STATUS_CHARGING = 0
_STATUS_DISCHARGING = 1
_STATUS_FULLY_CHARGED = 2
_STATUS_NOT_PRESENT = 3
_UP_DEVICE_IFACE = 'org.freedesktop.UPower.Device'
_UP_TYPE_BATTERY = 2
_UP_STATE_UNKNOWN = 0
_UP_STATE_CHARGING = 1
_UP_STATE_DISCHARGING = 2
_UP_STATE_EMPTY = 3
_UP_STATE_FULL = 4
_UP_STATE_CHARGE_PENDING = 5
_UP_STATE_DISCHARGE_PENDING = 6
_WARN_MIN_PERCENTAGE = 15
_MINIMUM_SPACE = 1024 * 1024 * 10
_DBUS_SERVICE = 'org.sugarlabs.SugarServices'
_DBUS_SHELL_IFACE = 'org.sugarlabs.SugarServices'
_DBUS_PATH = '/org/sugarlabs/SugarServices'
volume_monitor = None
battery_model = None
proxy = None
bundle_icons = {}
TRAINING_DATA = 'training-data-%s'
TRAINING_SUFFIX = '.txt'
def file_to_base64(path):
''' Given a file, convert its contents to base64 '''
base64file = os.path.join('/tmp', 'base64tmp')
cmd = 'base64 <' + path + ' >' + base64file
subprocess.check_call(cmd, shell=True)
fd = open(base64file, 'r')
base64data = fd.read()
fd.close()
os.remove(base64file)
return base64data
def pixbuf_to_base64(pixbuf, width=120, height=90):
''' Convert pixbuf to base64-encoded data '''
pixbuf = pixbuf.scale_simple(
width, height, GdkPixbuf.InterpType.NEAREST)
path = os.path.join('/tmp', 'imagetmp.png')
pixbuf.savev(path, "png", [], [])
base64data = file_to_base64(path)
os.remove(path)
return base64data
def base64_to_file(base64data, path):
''' Given a file, convert its contents from base64 '''
base64file = os.path.join('/tmp', 'base64tmp')
fd = open(base64file, 'w')
fd.write(base64data)
fd.close()
cmd = 'base64 -d <' + base64file + '>' + path
subprocess.check_call(cmd, shell=True)
os.remove(base64file)
def base64_to_pixbuf(base64data, width=120, height=90):
''' Convert base64-encoded data to a pixbuf '''
path = os.path.join('/tmp', 'imagetmp.png')
base64_to_file(base64data, path)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, width, height)
os.remove(path)
return pixbuf
def _find_bundles():
global bundle_icons
info_files = []
for root in GLib.get_system_data_dirs():
info_files += glob.glob(os.path.join(root,
'sugar',
'activities',
'*.activity',
'activity',
'activity.info'))
for path in info_files:
with open(path, 'r') as fd:
cp = configparser.ConfigParser()
try:
cp.read_file(fd)
except configparser.MissingSectionHeaderError as e:
_logger.error('Error reading %s: %s' % (path, e))
continue
section = 'Activity'
if cp.has_option(section, 'bundle_id'):
bundle_id = cp.get(section, 'bundle_id')
else:
continue
if cp.has_option(section, 'icon'):
icon = cp.get(section, 'icon')
dirname = os.path.dirname(path)
bundle_icons[bundle_id] = os.path.join(dirname, icon + '.svg')
def get_bundle_icons():
global bundle_icons
if bundle_icons == {}:
_find_bundles()
return bundle_icons
def bundle_id_to_icon(bundle_id):
global bundle_icons
if bundle_icons == {}:
_find_bundles()
if bundle_id in bundle_icons:
return bundle_icons[bundle_id]
else:
return None
def _luminance(color):
''' Calculate luminance value '''
return int(color[1:3], 16) * 0.3 + int(color[3:5], 16) * 0.6 + \
int(color[5:7], 16) * 0.1
def lighter_color(colors):
''' Which color is lighter? Use that one for the text nick color '''
if _luminance(colors[0]) > _luminance(colors[1]):
return 0
return 1
def darker_color(colors):
''' Which color is darker? Use that one for the text background '''
return 1 - lighter_color(colors)
def save_pixbuf_to_file(pixbuf, path):
pixbuf.savev(path, 'png', [], [])
def get_pixbuf_from_journal(dsobject, w, h):
""" Load a pixbuf from a Journal object. """
pixbufloader = \
GdkPixbuf.PixbufLoader.new_with_mime_type('image/png')
pixbufloader.set_size(min(300, int(w)), min(225, int(h)))
try:
pixbufloader.write(dsobject.metadata['preview'])
pixbuf = pixbufloader.get_pixbuf()
except BaseException:
pixbuf = None
pixbufloader.close()
return pixbuf
def is_valid_email_entry(entry):
if len(entry) == 0:
return False
realname, email_address = email.utils.parseaddr(entry)
if email_address == '':
return False
if not re.match(r'[^@]+@[^@]+\.[^@]+', email_address):
return False
return True
def recently(time):
return time - (60 * 60) # within the hour
def get_log_file(bundle_id):
log_dir = os.path.join(env.get_profile_path(), 'logs')
log_files = glob.glob(os.path.join(log_dir, '%s*.log' % bundle_id))
if len(log_files) > 0:
sorted_log_files = sorted(log_files)
return sorted_log_files[-1]
else:
return None
def take_screen_shot():
tmp_dir = os.path.join(env.get_profile_path(), 'data')
fd, file_path = tempfile.mkstemp(dir=tmp_dir, suffix='.png')
os.close(fd)
window = Gdk.get_default_root_window()
width, height = window.get_width(), window.get_height()
screenshot_surface = Gdk.Window.create_similar_surface(
window, cairo.CONTENT_COLOR, width, height)
cr = cairo.Context(screenshot_surface)
Gdk.cairo_set_source_window(cr, window, 0, 0)
cr.paint()
screenshot_surface.write_to_png(file_path)
return file_path
def reboot():
global proxy
if proxy is None:
bus = dbus.SessionBus()
try:
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
except Exception as e:
_logger.error('ERROR rebooting Sugar (proxy): %s' % e)
_vte_reboot()
try:
dbus.Interface(proxy, _DBUS_SERVICE).Reboot()
except Exception as e:
_logger.error('ERROR rebooting Sugar: %s' % e)
_vte_reboot()
def _vte_reboot():
_logger.error('Trying VTE method...')
# If we cannot reboot using the Sugar service, try from a VT
vt = Vte.Terminal()
success_, pid = vt.fork_command_full(
Vte.PtyFlags.DEFAULT,
os.environ["HOME"],
['/usr/bin/sudo', '/usr/sbin/reboot'],
[],
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
None,
None)
_logger.error('VTE %s %s' % (str(success_), str(pid)))
def _get_webservice_paths():
paths = []
for path in [os.path.join(_user_extensions_path, 'webservice'),
os.path.join(config.ext_path, 'webservice')]:
if os.path.exists(path):
paths.append(path)
return paths
def _get_webservice_module_paths():
webservice_module_paths = []
for webservice_path in _get_webservice_paths():
for path in os.listdir(webservice_path):
service_path = os.path.join(webservice_path, path)
if os.path.isdir(service_path):
webservice_module_paths.append(service_path)
return webservice_module_paths
def _get_webaccount_paths():
paths = []
for path in [os.path.join(_user_extensions_path, 'cpsection',
'webaccount', 'services'),
os.path.join(config.ext_path, 'cpsection', 'webaccount',
'services')]:
if os.path.exists(path):
paths.append(path)
return paths
def get_webservice_names():
names = []
paths = _get_webservice_module_paths()
for path in paths:
names.append(os.path.basename(path))
return names
def get_webservice_path(name):
paths = _get_webservice_module_paths()
for path in paths:
if os.path.basename(path) == name:
return path
return None
def get_webservice_icon_path(name):
paths = _get_webservice_module_paths()
for path in paths:
if os.path.basename(path) == name:
icon_path = os.path.join(path, 'icons', name + '.svg')
if os.path.exists(icon_path):
return icon_path
else:
svgs = look_for_file_type(os.path.join(path, 'icons'), 'svg')
if len(svgs) > 0:
return svgs[0]
return None
def get_webaccount_path(name):
paths = _get_webaccount_paths()
for path in paths:
target = os.path.join(path, name)
if os.path.exists(target):
return target
return None
def look_for_file_type(path, suffix):
return glob.glob(os.path.join(path, '*' + suffix))
def check_volume_suffix(volume_file):
_logger.debug('check_volume_suffix %s' % (volume_file))
if volume_file.endswith(TRAINING_SUFFIX):
_logger.debug('return %s' % (TRAINING_DATA % volume_file[-13:]))
return TRAINING_DATA % volume_file[-13:]
elif volume_file.endswith('.bin'): # See SEP-33
new_volume_file = volume_file[:-4] + TRAINING_SUFFIX
print (new_volume_file)
os.rename(volume_file, new_volume_file)
_logger.debug('return %s' % (TRAINING_DATA % new_volume_file[-13:]))
return TRAINING_DATA % new_volume_file[-13:]
else: # No suffix
_logger.debug('NO SUFFIX: %s' % volume_file)
new_volume_file = volume_file + TRAINING_SUFFIX
_logger.debug(new_volume_file)
os.rename(volume_file, new_volume_file)
_logger.debug('return %s' % (TRAINING_DATA % new_volume_file[-13:]))
return TRAINING_DATA % new_volume_file[-13:]
def look_for_training_data(path):
''' look for .txt suffix, .bin suffix, and finally, no suffix '''
training_data = []
glob_data = glob.glob(os.path.join(path, 'training-data-*.txt'))
for path in glob_data:
# Ignore files starting with # or ending with ~
if path[0] == '#':
continue
if path[-1] == '~':
continue
training_data.append(path)
glob_data = glob.glob(os.path.join(path, 'training-data-*.bin'))
for path in glob_data:
# Ignore files starting with # or ending with ~
if path[0] == '#':
continue
if path[-1] == '~':
continue
training_data.append(path)
glob_data = glob.glob(os.path.join(path, 'training-data-*'))
for path in glob_data:
# Ignore files starting with # or ending with ~
if path[0] == '#':
continue
if path[-1] == '~':
continue
# Make sure we are not adding the same file twice
if path in training_data:
continue
training_data.append(path)
return training_data
def get_email_from_training_data(path):
try:
fd = open(path, 'r')
json_data = fd.read()
fd.close()
except Exception as e:
_logger.error('Could not read from %s: %s' % (path, e))
return None
try:
if len(json_data) > 0:
data = json.loads(json_data)
else:
return None
except ValueError as e:
_logger.error('Cannot read training data: %s' % e)
return None
if 'email_address' in data:
return data['email_address']
else:
return None
def get_name_from_training_data(path):
try:
fd = open(path, 'r')
json_data = fd.read()
fd.close()
except Exception as e:
_logger.error('Could not read from %s: %s' % (path, e))
return None
try:
if len(json_data) > 0:
data = json.loads(json_data)
else:
return None
except ValueError as e:
_logger.error('Cannot read training data: %s' % e)
return None
if 'name' in data:
return data['name'].replace(',', ' ')
else:
return None
def get_completed_from_training_data(path):
try:
fd = open(path, 'r')
json_data = fd.read()
fd.close()
except Exception as e:
_logger.error('Could not read from %s: %s' % (path, e))
return None
try:
if len(json_data) > 0:
data = json.loads(json_data)
else:
return None
except ValueError as e:
_logger.error('Cannot read training data: %s' % e)
return None
if 'completion_percentage' in data:
return data['completion_percentage']
else:
return None
def look_for_xlw(path):
return glob.glob(os.path.join(path, '*.xlw'))
def look_for_xls(path):
return glob.glob(os.path.join(path, '*.xls'))
def remove_xlw_suffix(path):
if os.path.exists(path):
if path[-4:] == '.xlw':
results = subprocess.check_output(['mv', path, path[:-4]])
def set_read_write(path):
if os.path.exists(path):
# results = subprocess.check_output(['chmod', '+w', path])
os.chmod(path, stat.S_IWRITE | stat.S_IREAD)
def unexpected_training_data_files(path, name):
''' There should be at most one file training-data-XXXX-XXXX and it should
match the volume path basename. '''
files = look_for_training_data(path)
if len(files) > 1:
_logger.error(files)
return True
if len(files) == 1 and not os.path.exists(os.path.join(path, name)):
_logger.error(files)
return True
return False
def is_full(path, required=_MINIMUM_SPACE):
''' Make sure we have some room to write our data '''
volume_status = os.statvfs(path)
free_space = volume_status.f_bsize * volume_status.f_bavail
_logger.debug('free space: %d MB' % int(free_space / (1024 * 1024)))
if free_space < required:
_logger.error('free space: %d MB' % int(free_space / (1024 * 1024)))
return True
return False
def is_writeable(path):
''' Make sure we can write to the data file '''
if not os.path.exists(path):
return False
stats = os.stat(path)
if (stats.st_uid == os.geteuid() and stats.st_mode & stat.S_IWUSR) or \
(stats.st_gid == os.getegid() and stats.st_mode & stat.S_IWGRP) or \
(stats.st_mode & stat.S_IWOTH):
return True
return False
def is_landscape():
return Gdk.Screen.width() > Gdk.Screen.height()
def get_safe_text(text):
return urllib.pathname2url(text.encode('ascii', 'xmlcharrefreplace'))
def get_battery_level():
global battery_model
if battery_model is None:
bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
up_proxy = bus.get_object('org.freedesktop.UPower',
'/org/freedesktop/UPower')
upower = dbus.Interface(up_proxy, 'org.freedesktop.UPower')
for device_path in upower.EnumerateDevices():
device = bus.get_object('org.freedesktop.UPower', device_path)
device_prop_iface = dbus.Interface(device, dbus.PROPERTIES_IFACE)
device_type = device_prop_iface.Get(_UP_DEVICE_IFACE, 'Type')
if device_type == _UP_TYPE_BATTERY:
battery_model = DeviceModel(device)
return battery_model.props.level
def get_sound_level():
client = GConf.Client.get_default()
return client.get_int('/desktop/sugar/sound/volume')
def is_clipboard_text_available():
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
text_view = Gtk.TextView()
text_buffer = text_view.get_buffer()
text_buffer.paste_clipboard(clipboard, None, True)
bounds = text_buffer.get_bounds()
return len(text_buffer.get_text(bounds[0], bounds[1], True)) > 0
def get_volume_names():
global volume_monitor
if volume_monitor is None:
volume_monitor = Gio.VolumeMonitor.get()
names = []
for mount in volume_monitor.get_mounts():
names.append(mount.get_name())
return names
def generate_uid(left=None):
if left is None:
left = '%04x' % int(uniform(0, int(0xFFFF)))
right = '%04x' % int(uniform(0, int(0xFFFF)))
uid = '%s-%s' % (left, right)
return uid.upper()
def format_volume_name(name):
''' Looking for XXXX-XXXX format '''
def is_hex(string):
for c in string.upper():
if c not in '0123456789ABCDEF':
return False
return True
if '-' not in name:
return generate_uid()
hex_strings = name.split('-')
if len(hex_strings) != 2:
return generate_uid()
if len(hex_strings[0]) != 4:
return generate_uid()
if not is_hex(hex_strings[0]):
return generate_uid()
if len(hex_strings[1]) < 4:
return generate_uid(hex_strings[0])
if not is_hex(hex_strings[0]):
return generate_uid(hex_strings[0])
return name[0:9]
def get_modified_time(path):
try:
return int(os.path.getmtime(path))
except OSError as e:
logging.error('Could not get modified time for %s: %s' % (path, e))
return time.time()
def unmount(path):
global volume_monitor
if volume_monitor is None:
volume_monitor = Gio.VolumeMonitor.get()
target = None
for mount in volume_monitor.get_mounts():
if mount.get_root().get_path() == path:
target = mount
break
def __unmount_cb(mount, result, user_data):
logging.debug('__unmount_cb %r %r', mount, result)
mount.unmount_with_operation_finish(result)
if target is not None:
_logger.debug('unmounting %s' % path)
target.unmount_with_operation(0, None, None, __unmount_cb, None)
def get_volume_paths():
global volume_monitor
if volume_monitor is None:
volume_monitor = Gio.VolumeMonitor.get()
paths = []
for mount in volume_monitor.get_mounts():
paths.append(mount.get_root().get_path())
return paths
def get_device_path(target):
# There must be a Gio.VolumeMonitor way of doing this
results = subprocess.check_output(['df']).split('\n')
for line in results:
mount = line.split(' ')
if mount[-1] == target:
return mount[0]
return None
def dos_fsck(target):
_logger.error('Using VTE to dosfsck -a %s' % target)
vt = Vte.Terminal()
success_, pid = vt.fork_command_full(
Vte.PtyFlags.DEFAULT,
os.environ["HOME"],
['/usr/bin/sudo', '/usr/sbin/dosfsck', '-a', target],
[],
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
None,
None)
_logger.error('VTE %s %s' % (str(success_), str(pid)))
def get_number_of_mounted_volumes():
global volume_monitor
if volume_monitor is None:
volume_monitor = Gio.VolumeMonitor.get()
return len(volume_monitor.get_mounts())
def _get_dmi(node):
''' The desktop management interface should be a reliable source
for product and version information. '''
path = os.path.join('/sys/class/dmi/id', node)
try:
return open(path).readline().strip()
except BaseException:
return None
def is_XO():
version = _get_dmi('product_version')
if version is None:
hwinfo_path = '/bin/olpc-hwinfo'
if os.path.exists(hwinfo_path) and os.access(hwinfo_path, os.X_OK):
model = subprocess.check_output([hwinfo_path, 'model'])
version = model.strip()
if version in ['1', '1.5', '1.75', '4']:
return True
else:
# Some systems (e.g. ARM) don't have dmi info
if os.path.exists('/sys/devices/platform/lis3lv02d/position'):
return True
elif os.path.exists('/etc/olpc-release'):
return True
return False
def is_game_key(keyname):
if keyname in ['KP_Up', 'KP_Down', 'KP_Left', 'KP_Right',
'KP_Page_Down', 'KP_Page_Up', 'KP_End', 'KP_Home']:
return True
else:
return False
def is_tablet_mode():
if not os.path.exists('/dev/input/event4'):
return False
try:
output = subprocess.call(
['evtest', '--query', '/dev/input/event4', 'EV_SW',
'SW_TABLET_MODE'])
except (OSError, subprocess.CalledProcessError):
return False
if str(output) == '10':
return True
return False
def is_expanded(toolbar_button):
return toolbar_button.is_expanded()
def is_fullscreen(activity):
return activity._is_fullscreen
def get_starred():
dsobjects, nobjects = datastore.find({'keep': '1'})
return dsobjects
def get_starred_count():
dsobjects, nobjects = datastore.find({'keep': '1'})
return nobjects
def get_description(activity):
if 'description' in activity.metadata:
return activity.metadata['description']
else:
return ''
def get_title(activity):
if 'title' in activity.metadata:
return activity.metadata['title']
else:
return ''
def get_sugarservices_version():
global proxy
if proxy is None:
bus = dbus.SessionBus()
try:
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
except Exception as e:
_logger.error('ERROR getting sugarservice service: %s' % e)
return 0
try:
return dbus.Interface(proxy, _DBUS_SERVICE).GetVersion()
except Exception as e:
_logger.error('ERROR getting sugarservice version: %s' % e)
return 0
def is_activity_open(bundle_name):
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
return \
dbus.Interface(proxy, _DBUS_SERVICE).GetActivityName() == \
bundle_name and is_activity_view()
except Exception as e:
_logger.error('ERROR getting activity name %s' % e)
return False
def is_journal_open():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
return dbus.Interface(proxy, _DBUS_SERVICE).IsJournal() and \
is_activity_view()
except Exception as e:
_logger.error('ERROR getting zoom level %s' % e)
return False
def is_activity_view():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
zoom_level = \
dbus.Interface(proxy, _DBUS_SERVICE).GetZoomLevel()
except Exception as e:
_logger.error('ERROR getting zoom level %s' % e)
return False
return zoom_level == shell.ShellModel.ZOOM_ACTIVITY
def is_home_view():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
zoom_level = \
dbus.Interface(proxy, _DBUS_SERVICE).GetZoomLevel()
except Exception as e:
_logger.error('ERROR getting zoom level %s' % e)
return False
return zoom_level == shell.ShellModel.ZOOM_HOME
def is_neighborhood_view():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
zoom_level = \
dbus.Interface(proxy, _DBUS_SERVICE).GetZoomLevel()
except Exception as e:
_logger.error('ERROR getting zoom level %s' % e)
return False
return zoom_level == shell.ShellModel.ZOOM_MESH
def goto_activity_view():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
dbus.Interface(proxy, _DBUS_SERVICE).SetZoomLevel(
shell.ShellModel.ZOOM_ACTIVITY)
except Exception as e:
_logger.error('ERROR setting zoom level %s' % e)
def goto_journal():
''' Actually go to the journal '''
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
if dbus.Interface(proxy, _DBUS_SERVICE).OpenJournal():
dbus.Interface(proxy, _DBUS_SERVICE).SetZoomLevel(
shell.ShellModel.ZOOM_ACTIVITY)
else:
_logger.error('Could not find journal to open???')
except Exception as e:
_logger.error('ERROR calling open journal: %s' % e)
def set_journal_active():
''' Just set the Journal as the active activity in the Home View '''
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
if dbus.Interface(proxy, _DBUS_SERVICE).OpenJournal():
dbus.Interface(proxy, _DBUS_SERVICE).SetZoomLevel(
shell.ShellModel.ZOOM_HOME)
else:
_logger.error('Could not find journal to open???')
except Exception as e:
_logger.error('ERROR calling open journal: %s' % e)
def goto_home_view():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
dbus.Interface(proxy, _DBUS_SERVICE).SetZoomLevel(
shell.ShellModel.ZOOM_HOME)
except Exception as e:
_logger.error('ERROR setting zoom level %s' % e)
def goto_neighborhood_view():
global proxy
if proxy is None:
bus = dbus.SessionBus()
proxy = bus.get_object(_DBUS_SERVICE, _DBUS_PATH)
try:
dbus.Interface(proxy, _DBUS_SERVICE).SetZoomLevel(
shell.ShellModel.ZOOM_MESH)
except Exception as e:
_logger.error('ERROR setting zoom level %s' % e)
def get_share_scope(activity):
if 'share-scope' in activity.metadata:
return activity.metadata['share-scope'] == 'public'
return False
def saw_new_launch(bundle_id, timestamp):
for activity in get_activity(bundle_id):
if get_last_launch_time(activity) > int(timestamp):
return True
return False
def saw_new_instance(bundle_id, timestamp):
for activity in get_activity(bundle_id):
if get_creation_time(activity) > int(timestamp):
return True
return False
def get_creation_time(activity):
if 'creation_time' in activity.metadata:
return int(activity.metadata['creation_time'])
else:
_logger.error('No creation time found')
return 0
def get_last_launch_time(activity):
if 'launch-times' in activity.metadata:
launch_times = activity.metadata['launch-times'].split(',')
try:
return int(launch_times[-1])
except Exception as e:
_logger.error('Malformed launch times found: %s' % e)
return 0
else:
# _logger.error('No launch times found')
return 0
def get_launch_count(activity):
if 'launch-times' in activity.metadata:
return len(activity.metadata['launch-times'].split(','))
else:
return 0
def get_colors():
client = GConf.Client.get_default()
return XoColor(client.get_string('/desktop/sugar/user/color'))
def get_nick():
return profile.get_nick_name()
def get_favorites():
favorites_path = env.get_profile_path('favorite_activities')
if os.path.exists(favorites_path):
favorites_data = json.load(open(favorites_path))