From 468d7dd65e4fa4eba2434eb2e19b717999d62eb6 Mon Sep 17 00:00:00 2001 From: Prince Date: Thu, 25 Jun 2015 01:23:01 +0530 Subject: [PATCH 1/2] issue : #20 - import/export channels tested without gui --- .gitignore | 2 +- data/channels/sample_data | 0 data/glade/mainwindow.glade | 3 +- src/common/file_util.py | 13 ++++ src/common/json_exporter.py | 10 +++ src/common/json_importer.py | 11 +++ src/controller/main_window_controller.py | 8 +- src/model/category.py | 25 +++++++ src/model/channel.py | 38 ++++++++++ src/model/channel_encoder.py | 10 +++ src/model/peer_thread.py | 4 +- src/model/vlc_player.py | 2 +- src/p2psp_application_gui.py | 4 +- src/test_import_export_channels.py | 94 ++++++++++++++++++++++++ src/view/main_window.py | 1 - 15 files changed, 213 insertions(+), 12 deletions(-) create mode 100644 data/channels/sample_data create mode 100644 src/common/json_exporter.py create mode 100644 src/common/json_importer.py create mode 100644 src/model/category.py create mode 100644 src/model/channel.py create mode 100644 src/model/channel_encoder.py create mode 100644 src/test_import_export_channels.py diff --git a/.gitignore b/.gitignore index 3d5b8d6..ff84262 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.pyc *~ -*.log +*.log* diff --git a/data/channels/sample_data b/data/channels/sample_data new file mode 100644 index 0000000..e69de29 diff --git a/data/glade/mainwindow.glade b/data/glade/mainwindow.glade index 7ee194d..536d94d 100644 --- a/data/glade/mainwindow.glade +++ b/data/glade/mainwindow.glade @@ -34,12 +34,11 @@ True True - GDK_BUTTON_PRESS_MASK | GDK_STRUCTURE_MASK + s True False - GDK_BUTTON_PRESS_MASK | GDK_STRUCTURE_MASK vertical diff --git a/src/common/file_util.py b/src/common/file_util.py index 0995abd..9418b08 100644 --- a/src/common/file_util.py +++ b/src/common/file_util.py @@ -24,4 +24,17 @@ def get_user_interface(dire, fName): def find_file(dire, fName): path = os.path.join(os.path.dirname(dire), fName) return path + +@exc_handler +def file_size(path): + f = open(path,"r") + f.seek(0, os.SEEK_END) + size = f.tell() + return size + +@exc_handler +def file_del(path): + f = open(path,"w") + del(f) + diff --git a/src/common/json_exporter.py b/src/common/json_exporter.py new file mode 100644 index 0000000..cf2c3d8 --- /dev/null +++ b/src/common/json_exporter.py @@ -0,0 +1,10 @@ +import json +from common.decorators import exc_handler + +class JSON_Exporter(): + + @exc_handler + def to_JSON(self,path,channels,encoder): + json_file = open(path,"w") + json.dump(channels , json_file, indent = 4 , cls = encoder) + json_file.close() diff --git a/src/common/json_importer.py b/src/common/json_importer.py new file mode 100644 index 0000000..be74cba --- /dev/null +++ b/src/common/json_importer.py @@ -0,0 +1,11 @@ +import json +from common.decorators import exc_handler + +class JSON_Importer(): + + @exc_handler + def from_JSON(self,path): + json_file = open(path,"r") + data = json.load(json_file) + json_file.close() + return data diff --git a/src/controller/main_window_controller.py b/src/controller/main_window_controller.py index f5bc0af..4c00978 100644 --- a/src/controller/main_window_controller.py +++ b/src/controller/main_window_controller.py @@ -3,8 +3,7 @@ from common.decorators import exc_handler from adapter.buffering_adapter import Buffering_Adapter from adapter.speed_adapter import Speed_Adapter - from gi.repository import GObject - from gi.repository import Gtk + from gi.repository import Gdk from gi.repository import GdkX11 import common.file_util as file_util from model.peer_thread import Peer_Thread @@ -47,7 +46,7 @@ def start_peer(self): self.app_window.buffer_status_bar.show() thread1 = Peer_Thread(1, "Peer Thread") thread1.start() - print 'thread started' + print('thread started') @exc_handler @@ -154,8 +153,11 @@ def toggle_status_box(self,widget,data=None): @exc_handler def _realized(self,widget,data=None): + cursor = Gdk.Cursor.new(Gdk.CursorType.ARROW) self.vlc_player_instance = self.app_model.get_vlc_player_instance() self.win_id = widget.get_window().get_xid() self.toggle_player_type(self.win_id) + widget.get_window().set_cursor(cursor) + print('surface_cursor = ' + str(widget.get_window().get_cursor().__class__ )) self.app_window.player_surface.connect("configure_event", self.redraw_surface) diff --git a/src/model/category.py b/src/model/category.py new file mode 100644 index 0000000..49567c5 --- /dev/null +++ b/src/model/category.py @@ -0,0 +1,25 @@ +#from collections import defaultdict +class Category(): + + def __init__(self,name): + self.name = name + self.channels = {} + + def get_channels(self): + return self.channels + + def set_name(self,name): + self.name = name + + def get_name(self): + return self.name + + def add(self,key,channel): + self.channels[key] = channel + + def remove(self,key): + del(self.channels[key]) + + def get_channel(self,key): + return self.channels[key] + diff --git a/src/model/channel.py b/src/model/channel.py new file mode 100644 index 0000000..582e857 --- /dev/null +++ b/src/model/channel.py @@ -0,0 +1,38 @@ +class Channel(): + + def __init__(self,data): + self.name = data["name"] + self.description = data["description"] + self.thumbnail_url = data["thumbnail_url"] + self.splitter_addr = data["splitter_addr"] + self.splitter_port = data["splitter_port"] + + def set_name(self,name): + self.name = name + + def get_name(self): + return self.name + + def set_thumbnail_url(self,url): + self.thumbnail_url = url + + def get_thumbnail_url(self): + return self.thumbnail_url + + def set_description(self,description): + self.description = description + + def get_description(self): + return self.description + + def set_splitter_addr(self,addr): + self.splitter_addr = addr + + def get_splitter_addr(self): + return self.splitter_addr + + def set_splitter_port(self,port): + self.splitter_port = port + + def get_splitter_port(self): + return self.splitter_port diff --git a/src/model/channel_encoder.py b/src/model/channel_encoder.py new file mode 100644 index 0000000..9f1cd64 --- /dev/null +++ b/src/model/channel_encoder.py @@ -0,0 +1,10 @@ +import json +from channel import Channel + +class Channel_Encoder(json.JSONEncoder): + + def default(self,obj): + if isinstance(obj, Channel): + return obj.__dict__ + else: + return json.JSONEncoder.default(self, obj) diff --git a/src/model/peer_thread.py b/src/model/peer_thread.py index cf0903e..2b4ed10 100644 --- a/src/model/peer_thread.py +++ b/src/model/peer_thread.py @@ -21,7 +21,7 @@ def __init__(self, threadID, name): @exc_handler def run(self): - print "Starting " + self.name + print("Starting " + self.name) self.peer_active = True self.x=peer.Peer() - print "Exiting " + self.name + print("Exiting " + self.name) diff --git a/src/model/vlc_player.py b/src/model/vlc_player.py index fc5b2ac..52c48c3 100644 --- a/src/model/vlc_player.py +++ b/src/model/vlc_player.py @@ -22,7 +22,7 @@ def __init__(self): # Creates Vlc Instance self.vlcInstance = vlc.Instance("--no-xlib") self.player = self.vlcInstance.media_player_new() - self.em = self.player.event_manager() + self.player.video_set_mouse_input(False) def _get_media(self,Source): MEDIA_SOURCE = Source diff --git a/src/p2psp_application_gui.py b/src/p2psp_application_gui.py index b2cf051..053e7ef 100644 --- a/src/p2psp_application_gui.py +++ b/src/p2psp_application_gui.py @@ -18,7 +18,7 @@ def main_app(): App.show() Gtk.main() App.quit() - print "Exiting Gtk-Main Thread" - + print("Exiting Gtk-Main Thread") + if __name__ == "__main__": main_app() diff --git a/src/test_import_export_channels.py b/src/test_import_export_channels.py new file mode 100644 index 0000000..19a7a1a --- /dev/null +++ b/src/test_import_export_channels.py @@ -0,0 +1,94 @@ +#script tested with python2.7 +from model.category import Category +from model.channel import Channel +from common.json_importer import JSON_Importer +from common.json_exporter import JSON_Exporter +from model.channel_encoder import Channel_Encoder +from core._print_ import _print_ +import common.file_util as file_util + +import unittest +import json + +def get_data(): + + data = {"monitor": + { "name" : "monitor" + ,"thumbnail_url" : "http:/xyz.com" + ,"description" : "monitor" + ,"splitter_addr" : "localhost" + ,"splitter_port" : "4552" } + } + return data + +path = file_util.find_file(__file__, + "../data/channels/sample_data") + +#unittests are sorted according to their name and then run. +#edit tearDown method to retain exported channels file and vice-versa. +class Import_Export_Test(unittest.TestCase): + + exported_data = None + imported_data = None + + def tearDown(self): + if Import_Export_Test.imported_data is None: + pass + else: + file_util.file_del(path) + #pass + + def test_01_delete_channels_data(self): + + print("\n") + _print_("deleting existing sample_data...") + file_util.file_del(path) + size = file_util.file_size(path) + self.assertEqual(0,size) + + print("----------------------------------------------------------------------") + + def test_02_export_channels(self): + + Import_Export_Test.exported_data = get_data() + + monitor_channel = Channel(Import_Export_Test.exported_data["monitor"]) + + test_category = Category("test") + test_category.add("monitor",monitor_channel) + + Import_Export_Test.exported_data["monitor1"] = get_data()["monitor"] + test_category.add("monitor1",monitor_channel) + + exporter = JSON_Exporter() + exporter.to_JSON(path,test_category.get_channels(),Channel_Encoder) + print("\n") + _print_("exporting channels to json file = " + + path + + "\n" + +json.dumps(Import_Export_Test.exported_data + ,indent = 4 , cls = Channel_Encoder)) + + size = file_util.file_size(path) + self.assertNotEqual(0,size) + + print("----------------------------------------------------------------------") + + def test_03_import_channels(self): + importer = JSON_Importer() + Import_Export_Test.imported_data = importer.from_JSON(path) + print("\n") + _print_("importing channels from json file = " + + path + + "\n" + +json.dumps(Import_Export_Test.imported_data,indent = 4)) + + self.assertEqual(Import_Export_Test.exported_data + ,Import_Export_Test.imported_data) + + print("----------------------------------------------------------------------") + + +if __name__ == "__main__": + suite = unittest.TestLoader().loadTestsFromTestCase(Import_Export_Test) + unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/src/view/main_window.py b/src/view/main_window.py index 0580b2c..d3577a1 100644 --- a/src/view/main_window.py +++ b/src/view/main_window.py @@ -1,7 +1,6 @@ import sys import traceback try: - from gi.repository import GObject from gi.repository import Gtk from gi.repository import Gdk import common.file_util as file_util From 9ab5e0085dcf0b4ed19b49b58353bfe8eca23595 Mon Sep 17 00:00:00 2001 From: Prince Date: Thu, 25 Jun 2015 12:37:20 +0530 Subject: [PATCH 2/2] uploading sample channel data --- data/channels/sample_data.json | 16 ++++++++++++++++ src/test_import_export_channels.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 data/channels/sample_data.json diff --git a/data/channels/sample_data.json b/data/channels/sample_data.json new file mode 100644 index 0000000..6919243 --- /dev/null +++ b/data/channels/sample_data.json @@ -0,0 +1,16 @@ +{ + "monitor": { + "splitter_addr": "localhost", + "splitter_port": "4552", + "thumbnail_url": "http:/xyz.com", + "name": "monitor", + "description": "monitor" + }, + "monitor1": { + "splitter_addr": "localhost", + "splitter_port": "4552", + "thumbnail_url": "http:/xyz.com", + "name": "monitor", + "description": "monitor" + } +} \ No newline at end of file diff --git a/src/test_import_export_channels.py b/src/test_import_export_channels.py index 19a7a1a..ff58660 100644 --- a/src/test_import_export_channels.py +++ b/src/test_import_export_channels.py @@ -22,7 +22,7 @@ def get_data(): return data path = file_util.find_file(__file__, - "../data/channels/sample_data") + "../data/channels/sample_data.json") #unittests are sorted according to their name and then run. #edit tearDown method to retain exported channels file and vice-versa.