Make better script

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Make better script

Post by kim »

I'm have multiple "fileinfo" parts in my script and I want to make it use the same code for all, BUT I really need help with this one ?

https://github.com/filebot/scripts/blob ... roovy#L294

somthing like

Code: Select all

def xml = XML {
	movie {
		title(i.name)
		getFileInfo()
	}
}
stuff I have looked at but can't make use of:
http://groovy-lang.org/processing-xml.html
https://mrhaki.blogspot.com/2010/04/gro ... ation.html
https://mrhaki.blogspot.com/2009/10/gro ... -with.html
https://stackoverflow.com/questions/614 ... ilder-node
http://docs.groovy-lang.org/latest/html ... ilder.html
https://www.tutorialspoint.com/groovy/groovy_xml.htm

btw, maybe we can use mkp.yieldUnescaped() e.g. on:
https://github.com/filebot/scripts/blob ... roovy#L151
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Make better script

Post by rednoah »

Interesting problem. Kinda tricky.

I found Googling for "nested markupbuilder" to be very helpful:
https://stackoverflow.com/questions/309 ... map-to-xml

Here's my solution for reusable fragments:

Code: Select all

def fragment = { k, v ->
	return {
		key(k)
		value(v)
	}
}


XML{
	root {
		entry fragment('A', 1)
		entry fragment('B', 2)
	}
}
:idea: Please read the FAQ and How to Request Help.
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Make better script

Post by kim »

thx, but I fail to see how I can use it ?
output:

Code: Select all

<call>
  <root>
    <entry>
      <key>A</key>
      <value>1</value>
    </entry>
    <entry>
      <key>B</key>
      <value>2</value>
    </entry>
  </root>
</call>
using

Code: Select all

import groovy.xml.*

def XML = new MarkupBuilder()

def fragment = { k, v ->
	return {
		key(k)
		value(v)
	}
}

XML{
	root {
		entry fragment('A', 1)
		entry fragment('B', 2)
	}
}
and if I can get this to work, it does not look like I will save any filesize bytes or better code then before (why I wanted this) ?

btw: I have a "fileinfo" part under fetchSeriesNfoTVDb, fetchSeriesNfoTMDb, fetchSeriesNfoTVmaze using 72 lines and fetchMovieNfo using 79 lines of code
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Make better script

Post by rednoah »

Presumably, the goal is code re-use, and as you can see in this example, code is re-used:

Code: Select all

def getFileInfo = { f ->
	return {
		name(f.name)
		size(f.length())
		exists(f.exists())
	}
}


def f1 = 'File 1.txt' as File
def f2 = 'File 2.txt' as File

XML{
	root {
		fileinfo getFileInfo(f1)
		fileinfo getFileInfo(f2)
	}
}

Code: Select all

<root>
  <fileinfo>
    <name>File 1.txt</name>
    <size>0</size>
    <exists>false</exists>
  </fileinfo>
  <fileinfo>
    <name>File 2.txt</name>
    <size>0</size>
    <exists>false</exists>
  </fileinfo>
</root>
:idea: If you're using MarkupBuilder like this, it's for code re-use, not for readability. MarkupBuilder is naturally more readable if you the code looks exactly like the XML document you want to generate.
:idea: Please read the FAQ and How to Request Help.
Post Reply