Skip to content

Commit

Permalink
Cleaning up the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
nithinphilips committed Dec 3, 2014
1 parent 22fbd5a commit e5e8253
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ time to change that password lest you get pwned!)::

odel localhost triPeople-triPeople-triEmployee.txt

Tririga has a limitation of 150 characters for all Data Integrator file names.
If the file name has more than 150 characters, Odel will truncate the file
name.

Waiting for Processing
----------------------
Normally Odel will terminate as soon as the file is transmitted to Tririga. It
Expand Down
47 changes: 32 additions & 15 deletions odel/diuploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def trim_filename(filename, maxlength=FILE_NAME_MAX_LEN):
If the extension alone is longer than the maxlength, the filename
>>> trim_filename("There-are-fifty-four-characters-in-this-file-name.txt", maxlength=10)
>>> trim_filename("There-are-fifty-four-characters-in-this-file-name.txt",
... maxlength=10)
'There-.txt'
>>> trim_filename("There-.txt", maxlength=10)
'There-.txt'
Expand Down Expand Up @@ -81,8 +82,11 @@ def parse_filename(filename, separator='-'):
>>> parse_filename("/home/odel/001-triPeople-triPeople-triEmployee.txt")
['triPeople', 'triPeople', 'triEmployee']
File name with full path and a sequence prefix and spaces around the separators:
>>> parse_filename("/home/odel/001 - triPeople - triPeople - triEmployee.txt")
File name with full path and a sequence prefix and spaces around the
separators:
>>> parse_filename(
... "/home/odel/001 - triPeople - triPeople - triEmployee.txt"
... )
['triPeople', 'triPeople', 'triEmployee']
If the file name does not have three parts a ValueError is raised:
Expand All @@ -99,7 +103,9 @@ def parse_filename(filename, separator='-'):
results = map(str.strip, results)
return results

raise ValueError("The filename must have at least three components separated by '-'.")
raise ValueError(
"The filename must have at least three components separated by '-'."
)

def normalize_url(url):
"""
Expand Down Expand Up @@ -167,7 +173,7 @@ def parse_url(url, port='9080'):
url = scheme + "://" + url

# Any ports in the input override the default port.
match = re.search(":(\d+)$", url)
match = re.search(r':(\d+)$', url)
if match:
port = int(match.group(1))

Expand All @@ -187,7 +193,7 @@ def parse_url(url, port='9080'):
@arg('--module', '-m', help="The name of the module to which to upload")
@arg('--businessobject', '-b',
help="The name of the business object to which to upload")
@arg('--action', '-a',
@arg('--action', '-a',
help="The action to apply to the newly created records. "
"By default the first possible action is applied.")
@arg('--form', '-f', help="The name of the form to which to upload")
Expand All @@ -196,15 +202,17 @@ def parse_url(url, port='9080'):
@arg('filename', help="The file to upload.")
@arg('url',
help="The URL to the TRIRIGA environment. Include any context paths. "
"This could be just the hostname. In that case port 9080 will be appended")
"This could be just the hostname. In that case port 9080 will be "
"appended")
def upload(url, filename, username="system", password="admin",
module=None, businessobject=None, form=None, action=None, wait=False):
module=None, businessobject=None, form=None, action=None,
wait=False):
"""
Uploads a file to Tririga Data Integrator.
The module, businessobject and form arguments are optional if the file is named
in the following pattern "<module>-<businessObject>-<form>.txt". Otherwise,
you must specify them as arguments.
The module, businessobject and form arguments are optional if the file is
named in the following pattern "<module>-<businessObject>-<form>.txt".
Otherwise, you must specify them as arguments.
"""

session = requests.Session()
Expand All @@ -227,9 +235,8 @@ def upload(url, filename, username="system", password="admin",
)

logging.debug("Uploading to {}({})::{}({})::{}({})".format(
module, moduleid, businessobject, businessobjectid, form, formid
)
)
module, moduleid, businessobject, businessobjectid, form, formid
))

if action:
transition = action
Expand Down Expand Up @@ -260,7 +267,17 @@ def upload(url, filename, username="system", password="admin",

files = {'theFile': open(filename, 'rb')}
filenameonly = os.path.basename(filename)
filenameonly = trim_filename(filenameonly)

trimmed_filename = trim_filename(filenameonly)
if trimmed_filename != filenameonly:
sys.stdout.write(
"File name is too long. It has been shortened to '{}'.\n".format
(
trimmed_filename
)
)

filenameonly = trimmed_filename

diparams = {
'updateAct': "createSpec",
Expand Down

0 comments on commit e5e8253

Please sign in to comment.