Package: sept
Simple Extensible Path TemplateThe Simple Extensible Path Template (sept) is a simple to use, simple to configure templating system designed for you!If you are a developer, SEPT can help you put the power back into the hands of your users.If you are an artist, producer, or editor, you can use SEPT to control naming of thousands of files automatically in a consistent way!We have written a "Getting Started" Tutorial forUsersand forDeveloperswhich will get you up and running with SEPT quickly.We also support a "live" tutorial thanks toGoogle Colab.TheUsertutorial and theDeveloperThe rest of this document will give you a quick look at SEPT templates and some examples. This may be to abrupt of a jump for you and I would recommend viewing the "Getting Started" tutorials first.Table Of ContentsExamplesHello WorldCustom TokensNested OperatorsOperators ArgumentsCustom OperatorsProvided OperatorsloweruppersubstrreplacedatefmtExamplesHello World ExampleThere is some test coverage which can operate as simple examples that may go beyond this example.fromseptimportPathTemplateParserdata={"first":"Alex","last":"Hughes",}template_str="/home/{{lower: first}}"parser=PathTemplateParser()template=parser.validate_template(template_str)resolved_path=template.resolve(data)# /home/alexIn the above example, we are executing thelowerOperator on thefirstToken.
Now, we haven't defined afirsttoken but becausefirstmatches exactly to a top level value in thedata, it gets resolved automatically for us.
ThelowerOperator is quite simple and performs astr.lower()operation on the value that our Token returns, in this case "Alex" is given to thelowerOperator.A slightly more complex exampleIn this example, we don't have a top level key that matches exactly, we have to define a custom Token that will dig deep into our data dictionary and pull out the value we want.fromseptimportPathTemplateParser,Tokendata={"user":{"HumanUser":{"first":"Alex","last":"Hughes",},},}classFirstNameToken(Token):name="first"defgetValue(self,data):returndata.get("user",{}).get("HumanUser",{}).get("first")template_str="/home/{{lower: first}}{{upper: first}}"parser=PathTemplateParser(additional_tokens=[FirstNameToken])template=parser.validate_template(template_str)resolved_path=template.resolve(data)# /home/alexALEXAs you can see above, we had to pass an additional keyword argument to thePathTemplateParserclass.
This argument, and the corresponding Operator one, expect a list of python class objects inheriting from Token or Operator.This is also the first example where we have multiple expressions in it.Nested OperatorsIt is possible to nest multiple Operators against a single Token.
The thing to remember in this case is that Tokens are resolved inside out.fromseptimportPathTemplateParser,Tokendata={"first":"Alex","last":"Hughes",}template_str="/home/{{substr[0, 1]: {{lower: first}}}}{{lower: last}}"parser=PathTemplateParser()template=parser.validate_template(template_str)resolved_path=template.resolve(data)# /home/ahughesIn the above example, we have two main expressions,{{substr[0, 1]: {{lower: first}}}}and{{lower: last}}.
Ignore the syntax of thesubstrOperator for now and take a look at the nestedlower+firstexpression.
The way this gets resolved is by first callingloweron thefirstToken and then passing the result of that Operator, to thesubstrOperator.Passing Arguments to OperatorsIn the previous example I told you to ignore the{{substr[0, 1]: ...}}syntax you saw.
That was us passing arguments to thesubstrOperator.  In the case above, we passed 2 values, 0 and 1.
InseptOperators can accept arguments and they are defined within square brackets, comma delimited.Custom OperatorsimportsixfromseptimportPathTemplateParser,Operatordata={"first":"Alex","last":"Hughes",}classReverseOperator(Operator):name="reverse"DATA_TYPES=(six.text_type,six.binary_type)defis_invalid(self,token_value):ifisinstance(token_value,self.DATA_TYPES):# Is validreturnNoneelifnottoken_value:# Value is emptyreturn"Missing text value"return"Value must be one of the following data types ({})".format(self.DATA_TYPES)defexecute(self,input_data):output_data=""forcharinreversed(input_data):output_data+=charreturnoutput_datatemplate_str="/home/{{reverse: last}}"parser=PathTemplateParser(additional_operators=[ReverseOperator])template=parser.validate_template(template_str)resolved_path=template.resolve(data)# /home/sehguhThis is very similar to our Token example, the Operator schema is two functions.defis_invalid(self,token_value):"""is_invalid will check the value the is about to be operated on.If the value will not work for some reason (wrong datatype, etc),this method should return an error message as a string.If it looks good to go, just return None:param Any token_value: Data that would be operated on:return: A Falsey value if everything is ok, or an error string if not.:rtype: None|str"""defexecute(self,input_data):"""execute does the actual work of your custom Operator.It will take in the data passed and return the transformed data asoutput.:param Any input_data::return: The transformed data according to whatever the Operator issupposed to do.:rtype: Any"""ContributingContributions are encouraged!
If you have a large code refactor or a significant change, starting an issue to discuss the change and plan is recommended.
If you want to help with documentation, that is somewhere that we also need more work on as well.