-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjournal.py
More file actions
147 lines (126 loc) · 5.66 KB
/
journal.py
File metadata and controls
147 lines (126 loc) · 5.66 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
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
#! /usr/bin/python3
#
# Author: Arjun Sarwal arjun@laptop.org
# Copyright (C) 2007, Arjun Sarwal
# Copyright (C) 2009-12 Walter Bender
# Copyright (C) 2016, James Cameron [Gtk+ 3.0]
#
#
# 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 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from gi.repository import Gdk
import cairo
import time
import os
import io
import dbus
from gettext import gettext as _
from sugar3.datastore import datastore
from sugar3.graphics import style
# Initialize logging.
import logging
log = logging.getLogger('measure-activity')
log.setLevel(logging.DEBUG)
class DataLogger():
''' Handles all of the data I/O with the Journal '''
MODE_LABELS = {
'sound': _('Sound'),
'resistance': _('Ohms'),
'voltage': _('Volts'),
'frequency': _('Hz')}
def __init__(self, activity):
''' We store csv data in the Journal entry for Measure; screen captures
are stored in separate Journal entries '''
self.activity = activity
self.data_buffer = []
def start_new_session(self, user='', xscale=0, yscale=0,
logging_interval='', channels=1, mode='sound'):
''' Start a new logging session by updating session parameters '''
self.activity.session_id += 1
self.data_buffer.append('%s: %d' % (_('Session'),
self.activity.session_id))
self.data_buffer.append('%s: %s' % (_('User'), user))
self.data_buffer.append('%s: %s' % (_('Mode'), self.MODE_LABELS[mode]))
self.data_buffer.append('%s: %s' % (
_('Date'), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())))
self.data_buffer.append('%s: %s' % (_('Interval'),
str(logging_interval)))
if channels > 1:
self.data_buffer.append('%s: %d' % (_('Channels'),
channels))
return self.activity.session_id
def write_value(self, value='', channel=None, sample=0):
'''Append the value passed to data_buffer '''
if channel is None:
self.data_buffer.append('%d: %s' % (sample, str(value)))
elif self.activity.wave.visibility[channel]:
self.data_buffer.append('%d.%d: %s' % (
sample, channel, str(value)))
def stop_session(self):
'''Write the data_buffer onto a file'''
return
def take_screenshot(self, capture_count=1):
''' Take a screenshot and save to the Journal '''
tmp_file_path = os.path.join(
os.environ['SUGAR_ACTIVITY_ROOT'], 'instance',
'screen_capture_' + str(capture_count) + '.png')
window = self.activity.wave.get_window()
width, height = window.get_width(), window.get_height()
surface = Gdk.Window.create_similar_surface(window,
cairo.CONTENT_COLOR,
width, height)
cr = cairo.Context(surface)
Gdk.cairo_set_source_window(cr, window, 0, 0)
cr.paint()
surface.write_to_png(tmp_file_path)
if os.path.exists(tmp_file_path):
dsobject = datastore.create()
try:
dsobject.metadata['title'] = '%s %d' % (_('Waveform'),
capture_count)
dsobject.metadata['keep'] = '0'
dsobject.metadata['buddies'] = ''
dsobject.metadata['preview'] = self._get_preview_data(surface)
dsobject.metadata['icon-color'] = self.activity.icon_colors
dsobject.metadata['mime_type'] = 'image/png'
dsobject.set_file_path(tmp_file_path)
datastore.write(dsobject)
finally:
dsobject.destroy()
del dsobject
os.remove(tmp_file_path)
return True
return False
def _get_preview_data(self, screenshot_surface):
screenshot_width = screenshot_surface.get_width()
screenshot_height = screenshot_surface.get_height()
preview_width, preview_height = style.zoom(300), style.zoom(225)
preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
preview_width, preview_height)
cr = cairo.Context(preview_surface)
scale_w = preview_width * 1.0 / screenshot_width
scale_h = preview_height * 1.0 / screenshot_height
scale = min(scale_w, scale_h)
translate_x = int((preview_width - (screenshot_width * scale)) / 2)
translate_y = int((preview_height - (screenshot_height * scale)) / 2)
cr.translate(translate_x, translate_y)
cr.scale(scale, scale)
cr.set_source_rgba(1, 1, 1, 0)
cr.set_operator(cairo.OPERATOR_SOURCE)
cr.paint()
cr.set_source_surface(screenshot_surface)
cr.paint()
preview_str = io.StringIO()
preview_surface.write_to_png(preview_str)
return dbus.ByteArray(preview_str.getvalue())