<commit_before>"""
Created December 2013

@author: <EMAIL>

This script loads Visomics-style analyses into the Arbor TreeStore database.

Requirements:
    - xmltodict (can be pip installed)
"""

from ArborFileManagerAPI import ArborFileManager
api = ArborFileManager()
api.initDatabaseConnection()

import json
import os
import sys
import xmltodict

# check that analysis directory has been passed in via the command line
if len(sys.argv) < 2:
  print sys.argv[0] + " <path/to/xml_dir>"
  sys.exit(1)

xml_dir = sys.argv[1]
xml_files = os.listdir(xml_dir)

for xml_file in xml_files:
  if not xml_file.endswith(".xml"):
    continue

  # find each matching pair of .xml & .R files
  analysis_name = xml_file[:-4]

  script_path = xml_dir + "/" + analysis_name + ".R"
  if not os.path.exists(script_path):
    print "Skipping %s because %s does not exist" % (analysis_name, script_path)
    continue

  # load their contents into a Python dictionary
  f = file(xml_dir + "/" + xml_file, "r")
  xml_contents = f.read()
  f.close()

  f = file(script_path, "r")
  script_contents = f.read()
  f.close()

  analysis_item = xmltodict.parse(xml_contents)
  analysis_item["analysis"]["script"] = script_contents

  # future proofing for other types of analyses
  analysis_item["analysis"]["type"] = "vtk_r"

  # use this dictionary to insert this analysis into the TreeStore
  api.newAnalysis(analysis_name, analysis_item)
  print "newAnalysis called for %s" % analysis_name<commit_msg>add ability to import python analyses<commit_after>"""
Created December 2013

@author: <EMAIL>

This script loads Visomics-style analyses into the Arbor TreeStore database.

Requirements:
    - xmltodict (can be pip installed)
"""

from ArborFileManagerAPI import ArborFileManager
api = ArborFileManager()
api.initDatabaseConnection()

import json
import os
import sys
import xmltodict

# check that analysis directory has been passed in via the command line
if len(sys.argv) < 2:
  print sys.argv[0] + " <path/to/xml_dir>"
  sys.exit(1)

xml_dir = sys.argv[1]
xml_files = os.listdir(xml_dir)

for xml_file in xml_files:
  if not xml_file.endswith(".xml"):
    continue

  # find each matching pair of .xml & .R files
  analysis_name = xml_file[:-4]

  script_path = ""
  analysis_type = ""
  r_script_path = xml_dir + "/" + analysis_name + ".R"
  python_script_path = xml_dir + "/" + analysis_name + ".py"
  if os.path.exists(r_script_path):
    script_path = r_script_path
    analysis_type = "vtkr"
  elif os.path.exists(python_script_path):
    script_path = python_script_path
    analysis_type = "vtkpython"
  else:
    print "Skipping %s because corresponding script does not exist" % analysis_name
    continue

  # load their contents into a Python dictionary
  f = file(xml_dir + "/" + xml_file, "r")
  xml_contents = f.read()
  f.close()

  f = file(script_path, "r")
  script_contents = f.read()
  f.close()

  analysis_item = xmltodict.parse(xml_contents)
  analysis_item["analysis"]["script"] = script_contents
  analysis_item["analysis"]["type"] = analysis_type

  # use this dictionary to insert this analysis into the TreeStore
  api.newAnalysis(analysis_name, analysis_item)
  print "newAnalysis called for %s" % analysis_name
{'fraction_non_alphanumeric': 0.10514888337468982, 'fraction_numerical': 0.005583126550868486, 'mean_word_length': 4.286885245901639, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 37}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3486081', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1055, 'n_words': 307}