Differences between tepache and sweettepache

From OLPC

Jump to: navigation, search

Index: sweettepache.py

=======================================================
--- sweettepache.py	(revision 1)
+++ sweettepache.py	(revision 2)
@@ -113,6 +113,7 @@
         self.indent = 4 * " "
 
     def write(self, output_file=None):
+        global options, status
         module_code = ""
         if output_file:
             self.data["module"] = self.output_file = output_file
@@ -146,7 +147,64 @@
         except IOError, e:
             printerr(e)
             return False
+        
+        if not options.no_sugar:
 
+            activity_file = self.output_file[:-3] + 'Activity.py'
+            if os.path.isfile(activity_file):
+                shutil.copy(activity_file, activity_file + '.bak')
+            activity_code = sugar_activity_format % self.data
+
+
+            try:
+                self.output = codecs.open(activity_file, "w", "utf-8")
+                self.output.write(activity_code)
+                self.output.close()
+            except IOError, e:
+                printerr(e)
+                return False
+      
+            #write setup.py format
+            setup_code = setup_format % self.data
+            try:
+                self.output = codecs.open("setup.py", "w", "utf-8")
+                self.output.write(setup_code)
+                self.output.close()
+            except IOError, e:
+                printerr(e)
+                return False
+
+            if not os.path.isdir('activity'):
+                #mkdir nameActivity.activity/activity
+                os.makedirs('activity')
+                os.chdir("activity")
+
+                if not options.no_icon:
+                    #write nameActivity.activity/activity/activity-name.svg
+                    icon_code = icon_format
+                    icon_file = 'activity-' + self.output_file[:-3] + '.svg'
+                    try:
+                        self.output = codecs.open(icon_file, "w", "utf-8")
+                        self.output.write(icon_code)
+                        self.output.close()
+
+                    except IOError, e:
+                        printerr(e)
+                        return False
+
+                #write nameActivity.activity/activity/activity.info
+                info_code = activity_info_format %self.data
+                info_file = 'activity.info'
+                try:
+                    self.output = codecs.open(info_file, "w", "utf-8")
+                    self.output.write(info_code)
+                    self.output.close()
+                except IOError, e:
+                    printerr(e)
+                    return False
+
+                os.chdir(os.pardir)
+
         return True
 
     def parse(self):
@@ -347,60 +405,60 @@
         open(source_filename, "w").write(normalized_source_data)
 
 
-def get_binaries_path():

-    binaries_path_list =  os.environ["PATH"].split(os.pathsep)

-    if __WIN32__:

-        try:

-            import win32con

-            import win32api

-            import pywintypes

-            gnu_path = ""

+def get_binaries_path():
+    binaries_path_list =  os.environ["PATH"].split(os.pathsep)
+    if __WIN32__:
+        try:
+            import win32con
+            import win32api
+            import pywintypes
+            gnu_path = ""
             try:
                 winreg_key = win32con.HKEY_LOCAL_MACHINE
-                winreg_subkey = "SOFTWARE\\GnuWin32"

-                h = win32api.RegOpenKey(winreg_key, winreg_subkey)

-                gnu_path = win32api.RegQueryValueEx(h, "InstallPath")[0]

-                win32api.RegCloseKey(h)

-                gnu_path_bin = os.path.join(gnu_path, "bin")

-                binaries_path_list.insert(0, gnu_path_bin)

-            except pywintypes.error, e:

-                printerr("You haven't installed any GnuWin32 program.")

-        except ImportError:

-            printerr("I can't look for programs in the win32 registry")

-            printerr("The pywin32 extension should be installed")

-            printerr("Download it from %s\n" % pywin32_home)

-    return binaries_path_list

-

-def get_programs_paths(programs_list):

-    if __WIN32__:

-        exe_ext = ".exe"

-    else:

-        exe_ext = ""

-    path_list =  get_binaries_path()

+                winreg_subkey = "SOFTWARE\\GnuWin32"
+                h = win32api.RegOpenKey(winreg_key, winreg_subkey)
+                gnu_path = win32api.RegQueryValueEx(h, "InstallPath")[0]
+                win32api.RegCloseKey(h)
+                gnu_path_bin = os.path.join(gnu_path, "bin")
+                binaries_path_list.insert(0, gnu_path_bin)
+            except pywintypes.error, e:
+                printerr("You haven't installed any GnuWin32 program.")
+        except ImportError:
+            printerr("I can't look for programs in the win32 registry")
+            printerr("The pywin32 extension should be installed")
+            printerr("Download it from %s\n" % pywin32_home)
+    return binaries_path_list
+
+def get_programs_paths(programs_list):
+    if __WIN32__:
+        exe_ext = ".exe"
+    else:
+        exe_ext = ""
+    path_list =  get_binaries_path()
     programs_paths = [None,]*len(programs_list)
-    for i,program in enumerate(programs_list):

+    for i,program in enumerate(programs_list):
         for path in path_list:
             program_path = os.path.join(path, program) + exe_ext
-            if os.path.isfile(program_path):

-                if " " in program_path:

+            if os.path.isfile(program_path):
+                if " " in program_path:
                     program_path = '"%s"' % program_path
                 programs_paths[i] = program_path
-    return programs_paths

+    return programs_paths
 
 def get_required_programs():
-    program_names = ["diff", "patch"]

-    package_names = ["diffutils", "patch"]

-

-    programs_paths = get_programs_paths(program_names)

-    for i,program_path in enumerate(programs_paths):

+    program_names = ["diff", "patch"]
+    package_names = ["diffutils", "patch"]
+
+    programs_paths = get_programs_paths(program_names)
+    for i,program_path in enumerate(programs_paths):
         if not program_path:
-            program, package = program_names[i], package_names[i]

-            printerr("Required program %s could not be found" % program)

-            printerr("Is the package %s installed?" % package)

-            if __WIN32__:            

+            program, package = program_names[i], package_names[i]
+            printerr("Required program %s could not be found" % program)
+            printerr("Is the package %s installed?" % package)
+            if __WIN32__:            
                 printerr("Download it from %s" % gnuwin32_home)
             return None
-    return programs_paths

+    return programs_paths
 
 def get_options_status():
     usage = "usage: %prog [options] [GLADE_FILE] [OUTPUT_FILE]"
@@ -409,6 +467,9 @@
     version = "1.0"
     parser = optparse.OptionParser(usage=usage, version=version,
                                       description=description)
+    parser.add_option("-i", "--no-icon", dest = "no_icon",
+                      action="store_true",
+                      help="I already have an activity icon")
     parser.add_option("-g", "--glade", dest="glade_file",
                       help="file to parse")
     parser.add_option("-o", "--output", dest="output_file",
@@ -416,6 +477,9 @@
     parser.add_option("-n", "--no-helper", dest="no_helper",
                       action="store_true",
                       help="Do not write the helper module")
+    parser.add_option('-s', "--without-sugar", dest = "no_sugar",
+                      action="store_true",
+                      help="No sweetening needed")
     parser.add_option("-t", "--use-tabs", dest="use_tabs",
                       action="store_true",
                       help="\
@@ -434,10 +498,11 @@
     return options, status
 
 def main():
-    programs_paths = get_required_programs()

-    if not programs_paths:
-        return -1

-    diff_bin, patch_bin = programs_paths

+    #programs_paths = get_required_programs()
+    #if not programs_paths:
+    #    return -1
+    #diff_bin, patch_bin = programs_paths
+    global options, status
 
     options, status = get_options_status()
     if not status:
@@ -459,37 +524,35 @@
     exists_output_file = os.path.exists(output_file)
     exists_output_file_orig = os.path.exists(output_file_orig)
 
+    if exists_output_file:
+        #back up the original
+        shutil.copy(output_file, output_file_bak)
+
     if not exists_output_file_orig and exists_output_file:
-        printerr('File "%s" exists' % short_f)
-        printerr('but "%s" does not.' % short_f_orig)
-        printerr("That means your custom code would be overwritten.")
-        printerr('Please manually remove "%s"' % short_f)
-        printerr("from this directory.")
-        printerr("Anyway, I\'ll create a backup for you in")
-        printerr('"%s"' % short_f_bak)
-        shutil.copy(output_file, output_file_bak)
-        return -1
+        shutil.copy(output_file, output_file_orig)
+        exists_output_file_orig = True
+
     if options.use_tabs:
         code_writer.indent = "\t"
     if exists_output_file_orig and exists_output_file:
         if not options.use_tabs:
             normalize_indentation(output_file_orig)
             normalize_indentation(output_file)
-        diff_command = "%s -U1 %s %s > %s"
-        diff_args = (diff_bin, output_file_orig, output_file, diff_file)
-        os.system(diff_command % diff_args)
-        shutil.copy(output_file, output_file_bak)
         if not code_writer.write(output_file):
             os.remove(diff_file)
             return -1
+        diff_command = "%s -U3 %s %s > %s"
+        diff_bin = "diff"
+        diff_args = (diff_bin, output_file, output_file_orig, diff_file)
+        os.system(diff_command % diff_args)
         orig_callbacks = get_callbacks_from_code(output_file_orig)
         callbacks = get_callbacks_from_code(output_file)
         renamed_symbols = get_renamed_symbols(orig_callbacks, callbacks)
         if renamed_symbols:
             diff_apply_renamed_symbols(diff_file, renamed_symbols)
-        shutil.copy(output_file, output_file_orig)
-        patch_command = "%s -fp0 < %s"
-        patch_args = (patch_bin, diff_file) 
+        patch_command = "%s -fp0 %s < %s"
+        patch_bin = "patch"
+        patch_args = (patch_bin, output_file, diff_file) 
         if os.system(patch_command % patch_args):
             os.remove(diff_file)
             return -1
@@ -497,7 +560,6 @@
     else:
         if not code_writer.write(output_file):
             return -1
-        shutil.copy(output_file, output_file_orig)
 
     os.chmod(output_file, 0755)
     if not options.no_helper and not os.path.isfile(helper_module):
@@ -592,6 +654,9 @@
 
 instance_format = '''\
 %(t)s%(instance)s = %(class)s()
+%(t)swidget = %(instance)s.get_widget('%(root)s')
+%(t)swidget.connect("destroy", lambda w: gtk.main_quit())
+%(t)swidget.show()
 '''
 
 run_format = '''\
@@ -604,6 +669,134 @@
 #-- main }
 '''
 
+sugar_activity_format= """\
+import %(app_name)s
+
+from sugar.activity import activity
+
+class %(app_name)sActivity(activity.Activity):
+    def __init__(self, handle):
+        activity.Activity.__init__(self, handle)
+
+        self.testname = '%(app_name)s'
+        self.set_title(self.testname)
+        
+        glade = %(app_name)s.%(class)s(root='vbox1')
+        self.vbox = glade.get_widget('vbox1')
+        widgets = self.get_children()
+        widget = widgets[0]
+        widget.pack_start(self.vbox)
+
+	toolbox = activity.ActivityToolbox(self)
+	self.set_toolbox(toolbox)
+	toolbox.show()  
+"""
+
+setup_format= """\
+#!/usr/bin/env python
+from sugar.activity import bundlebuilder
+if __name__ == "__main__":
+    bundlebuilder.start("%(app_name)sActivity")
+"""
+
+icon_format= """\
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://web.resource.org/cc/"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="50"
+   height="50"
+   id="svg2"
+   sodipodi:version="0.32"
+   inkscape:version="0.45.1"
+   sodipodi:docname="activity-gui.svg"
+   sodipodi:docbase="/home/tony/Documents/Activities/GUIActivity.activity/activity"
+   inkscape:output_extension="org.inkscape.output.svg.inkscape">
+  <metadata
+     id="metadata9">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <defs
+     id="defs7" />
+  <sodipodi:namedview
+     inkscape:window-height="627"
+     inkscape:window-width="910"
+     inkscape:pageshadow="2"
+     inkscape:pageopacity="0.0"
+     guidetolerance="10.0"
+     gridtolerance="10.0"
+     objecttolerance="10.0"
+     borderopacity="1.0"
+     bordercolor="#666666"
+     pagecolor="#ffffff"
+     id="base"
+     inkscape:zoom="8.14"
+     inkscape:cx="25"
+     inkscape:cy="25"
+     inkscape:window-x="0"
+     inkscape:window-y="25"
+     inkscape:current-layer="svg2" />
+  <rect
+     x="0.26289925"
+     y="2.3513515"
+     width="48"
+     height="48"
+     style="fill:#ffffff;stroke:#000000;stroke-width:2"
+     id="rect4" />
+  <text
+     xml:space="preserve"
+     style="font-size:8px;font-style:normal;font-weight:normal;text-align:center;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
+     x="8.7223587"
+     y="12.776413"
+     id="text2161"><tspan
+       sodipodi:role="line"
+       id="tspan2163"
+       x="8.7223587"
+       y="12.776413" /></text>
+  <text
+     xml:space="preserve"
+     style="font-size:14px;font-style:normal;font-weight:normal;text-align:start;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
+     x="10.687961"
+     y="3.6855037"
+     id="text2165"><tspan
+       sodipodi:role="line"
+       x="10.687961"
+       y="3.6855037"
+       id="tspan2169"></tspan><tspan
+       sodipodi:role="line"
+       x="10.687961"
+       y="21.185504"
+       id="tspan2162">Test</tspan><tspan
+       sodipodi:role="line"
+       x="10.687961"
+       y="38.685504"
+       id="tspan2327">GUI</tspan></text>
+</svg>
+"""
+
+activity_info_format = """\
+[Activity]
+name = %(app_name)s
+service_name = org.laptop.%(app_name)sActivity
+class = %(app_name)sActivity.%(app_name)sActivity
+exec = sugar-activity %(app_name)sActivity.%(app_name)sActivity
+icon = activity-%(app_name)s
+activity_version = 1
+show_launcher = yes
+"""
+
 SimpleGladeApp_content = '''\
 """
  SimpleGladeApp.py
Personal tools
  • Log in
  • Login with OpenID
About OLPC
About the laptop
About the tablet
Projects
OLPC wiki
Toolbox