Oktay Acikalin’s Blog

Oktay Acikalin’s Blog

coding, aurora, fun and life

Oktay Acikalin’s Blog RSS Feed
 
 
 
 

writing a simple file converter

today i would like to show you a little example on how to combine some useful techniques for cleaner coding with aurora1. our goal is to create 2 extensions which do similar things on different ends, combined with some command line interface. in the first place, i would like to demonstrate some old and new features. i assume, that you are experienced enough to understand what’s going on by reading the code - complicated parts will be explained.

things we use for this are:

  • ext.mod call style
  • config/boot/extmod_drop_duplicates
  • signals
  • some helpers

note: i’m normally using tabs instead of spaces but the syntax highlight plugin replaces them with spaces. it also messes up single quotes - you should replace them with normal single quotes instead.

so let’s start by making out where to drop our new project. my installation uses ~/Sites as my docroot. i have a test folder in ~/Sites/test which is my starting point.

let’s create a project. open up your console, go into your test-folder ( or whatever ) and do:

$> ama -cp fileconv -ca -cli

this will create a project, a simple application and a cli file.

$> cd fileconv/module/fileconv.appl/

now we create two extensions for reading and writing files:

$> ama -ce xml -ce yaml

now open up the xml extension with your favorite text editor and flesh it out with some methods for reading and writing:

public function read ( $filename )
{
    $this -> data = xml::file_get_contents ( $filename ) ;
}

public function write ( $filename )
{
    xml::file_put_contents ( $filename, $this -> data ) ;
}

do the same with the yaml extension:

public function read ( $filename )
{
    $this -> data = yaml::file_get_contents ( $filename ) ;
}

public function write ( $filename )
{
    yaml::file_put_contents ( $filename, $this -> data ) ;
}

setup the $data var in your appl file right into the class:

public $data = array () ;

replace all cli routes ( cli file ) with these ( no tabs - just 2 leading spaces! ):

- { full: ‘read-xml’, args: [ filename ], method: ‘xml.read’ }
- { full: ‘write-xml’, args: [ filename ], method: ‘xml.write’ }

- { full: ‘read-yaml’, args: [ filename ], method: ‘yaml.read’ }
- { full: ‘write-yaml’, args: [ filename ], method: ‘yaml.write’ }

and finally go to ../../ ( your project’s root folder ), edit config/boot.yml, set extmod_drop_duplicates to yes and try this:

$> ./run

fileconv v0.1 ( development )

USAGE: ./run option argument(s)

OPTIONS:
  --clear-cache
  --shell
  --create-manpages
  --web-access-deactivate message
  --web-access-activate
  --htaccess-write
  --read-xml filename
  --write-xml filename
  --read-yaml filename
  --write-yaml filename

now guess what? your file converter is ready for work :). try these and check the outputted files:

$> ./run --read-yaml config/boot.yml --write-xml output.xml
$> ./run --read-xml output.xml --write-yaml output.yml
$> ./run --read-xml output.xml --write-xml output2.xml
$> diff output.xml output2.xml
$> ./run --read-yaml output.yml --write-yaml output2.yml
$> diff output.yml output2.yml
$> ./run --read-yaml output.yml --write-xml output2.xml
$> diff output.xml output2.xml

you should not encounter any errors. if you do please let me know.

now what have we seen?

  • we used ama to create multiple things in one run
  • we created extensions for our application
  • saw what the xml and yaml helper can simplify
  • how cli files can easily extend your command line

“signals?” you might ask.. yeah.. how do we utilize signals in this simple app? well, what does this command line util not do? feedback - right - we don’t see any feedback!
so what can we do? we could insert some error handling and “doing this”, “doing that” messages. signals are not good at catching errors - that chapter is not written, yet. but signals can interfere application and library calls. so we could output some “doing this” and “doing that” messages without changing the code in the extensions. you might also do some user authentication within a web app, but we keep it simple.

$> cd module/fileconv.appl/

$> ama -cs

edit fileconv.sgn and replace present signals with this:

- { signal: ‘call_ext_method’, sender: ‘fileconv_application’, recipient: [ this, log ] }

add the following to the fileconv.appl:

public function log ( $data, $signal, $sender )
{
    extract ( $data ) ;
    aurora::info_msg ( ‘ %s -> %s %ss %s’, $name, $extension, $method, $args [ ‘filename’ ] ) ;
}
$> cd ../..

$> ./run --read-yaml output.yml --write-xml output2.xml
MSG:  fileconv -> yaml reads output.yml
MSG:  fileconv -> xml writes output2.xml

that’s it.

have fun :)

  1. you’ll need to have aurora 0.9.6 properly installed to walk through this tutorial []

Leave a Reply