Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a system to automatically push code from pc to device (Sourcery refactored) #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/PyJniusTester.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added __pycache__/client.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion buildozer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ version = 0.2

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy==master,pygments,androidstorage4kivy
requirements = python3,kivy==master,pygments,androidstorage4kivy,kivymd

# (str) Custom source folders for requirements
# Sets custom source for any requirements with recipes
Expand Down
33 changes: 33 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import socket
import threading


class SharedCode:
def __init__(self, host, port):
self._lock = threading.Lock()
self._data = None
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect((host, int(port)))

def get_data(self):
with self._lock:
return self._data

def set_data(self, value):
with self._lock:
self._data = value

def data_pull(self):

try:
# Receive data from the server
print("grabbing")
#recv limit is to allow large amounts of data
data = self.client_socket.recv(500000).decode()
self.set_data(data)
print("Received:", data)

except KeyboardInterrupt:
# Close the client socket when Ctrl+C is pressed
self.client_socket.close()
print("Client disconnected.")
68 changes: 59 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import io
import sys
import threading

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.codeinput import CodeInput
from kivy import platform
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import OptionProperty, ColorProperty, BooleanProperty
from kivy.uix.popup import Popup
from kivymd.app import MDApp
from kivymd.uix.button import MDTextButton
from kivymd.uix.textfield import MDTextField
from kivy.utils import get_color_from_hex
from kivy.uix.label import Label
from traceback import format_exc
from kivy.clock import Clock
from datetime import datetime
from os.path import join

from client import SharedCode

if platform == "android":

from androidstorage4kivy import SharedStorage, Chooser
Expand All @@ -33,11 +43,11 @@ def get_app_name():
'''
context = mActivity.getApplicationContext()
appinfo = context.getApplicationInfo()
if appinfo.labelRes:
name = context.getString(appinfo.labelRes)
else:
name = appinfo.nonLocalizedLabel.toString()
return name
return (
context.getString(appinfo.labelRes)
if appinfo.labelRes
else appinfo.nonLocalizedLabel.toString()
)

Window.softinput_mode = "below_target"
kv = """
Expand Down Expand Up @@ -125,6 +135,12 @@ def get_app_name():
halign: 'center'
on_release:
app.save_file()
Button:
text: "Connect to pc"
text_size: self.width, None
halign: 'center'
on_release:
app.connection_dialog()

#</KvLang>
"""
Expand All @@ -134,7 +150,25 @@ def on_triple_tap(self):
self.focus = True
Clock.schedule_once(lambda dt: self.select_all())

class PyjniusTester(App):
class ConnectionBox(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = 'vertical'

host_input = MDTextField(mode="rectangle", hint_text="Ip address")
port_input = MDTextField(mode="rectangle", hint_text="Port")
self.add_widget(host_input)
self.add_widget(port_input)

sub_container = BoxLayout(orientation='horizontal')
accept_button = MDTextButton(text="Accept", color='blue', on_release=lambda x: threading.Thread(
target=lambda: TesterApp.rewire_data(host=host_input.text, port=port_input.text)
).start())
sub_container.add_widget(accept_button)
self.add_widget(sub_container)


class PyjniusTester(MDApp):
color_mode = OptionProperty("Light", options=["Light", "Dark"])
light_background = ColorProperty("#e6e6e6")
dark_background = ColorProperty('#262626')
Expand Down Expand Up @@ -163,6 +197,22 @@ def toast():
toast()
"""

def connection_dialog(self):
connection_dialog = Popup(title='Test popup', size_hint_y = 0.5, content=ConnectionBox())
connection_dialog.open()

def update_code(self, operator):
self.root.ids['code'].text = operator.get_data()

def rewire_data(self, host, port):
operator = SharedCode(host, port)
print("in")
while True:
operator.data_pull()
Clock.schedule_once(lambda x: self.update_code(operator), 0.1)



def open_file(self):
'''
Calls Android Chooser to pick a file and then chooser_callback
Expand Down Expand Up @@ -208,7 +258,7 @@ def save_file(self):
if platform == "android":
# 1. save file to private storage:
time = datetime.now().strftime("%d_%m_%d_%H_%M_%S")
file_name = "jnius_code_"+ time +".txt"
file_name = f"jnius_code_{time}.txt"
with open(join(".",file_name),'w',encoding= 'utf-8') as fwrite:
fwrite.write(self.root.ids.code.text)

Expand Down Expand Up @@ -241,5 +291,5 @@ def run_code(code, error):
except Exception as e:
error.color = [1, 0, 0, 1]
error.text = f"{str(format_exc())}\nMain error is....\n{e}"

PyjniusTester().run()
TesterApp = PyjniusTester()
TesterApp.run()
1 change: 1 addition & 0 deletions sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("non local host success!")