<commit_before>#!/usr/bin/env python

"""
Uses the extended ContentHandler from xml_driver to extract the needed fields
from patent grant documents
"""

import cStringIO
from xml_driver import *
from xml_util import *
from xml.sax import xmlreader

class PatentGrant(object):

    def __init__(self, filename, is_string=False):
        xh = XMLHandler()
        parser = make_parser()
        parser.setContentHandler(xh)
        parser.setFeature(handler.feature_external_ges, False)
        l = xmlreader.Locator()
        xh.setDocumentLocator(l)
        if is_string:
            parser.parse(cStringIO.StringIO(filename))
        else:
            parser.parse(filename)
        self.xml = xh.root.us_patent_grant.us_bibliographic_data_grant

        self.country = self.xml.publication_reference.contents_of('country')[0]
        self.patent = normalize_document_identifier(self.xml.publication_reference.contents_of('doc_number')[0])
        self.kind = self.xml.publication_reference.contents_of('kind')[0]
        self.date_grant = self.xml.publication_reference.contents_of('date')[0]
        self.pat_type = self.xml.application_reference[0].get_attribute('appl-type')
        self.date_app = self.xml.application_reference.contents_of('date')[0]
        self.country_app = self.xml.application_reference.contents_of('country')[0]
        self.patent_app = self.xml.application_reference.contents_of('doc_number')[0]
        self.code_app = self.xml.contents_of('us_application_series_code')[0]
        self.clm_num = self.xml.contents_of('number_of_claims')[0]
        self.classes = self._classes()
        self.abstract = self.xml.contents_of('abstract','')
        self.invention_title = self._invention_title()
        self.asg_list = self._asg_list()
        self.cit_list = self._cit_list()
        self.rel_list = self._rel_list()
        self.inv_list = self._inv_list()
        self.law_list = self._law_list()

    def _invention_title(self):
        original = self.xml.contents_of('invention_title')[0]
        if isinstance(original, list):
          original = ''.join(original)
        return original

    def _classes(self):
        main = self.xml.classification_national.contents_of('main_classification')
        further = self.xml.classification_national.contents_of('further_classification')
        it = [main[0] if has_content(main) else []]
        if has_content(further):
            it.extend(further)
        return [ [x[:3].replace(' ',''), x[3:].replace(' ','')] for x in it]

    def _name_helper(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True)
        lastname = tag_root.contents_of('last_name', as_string=True)
        return associate_prefix(firstname, lastname)

    def _name_helper_dict(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True)
        lastname = tag_root.contents_of('last_name', as_string=True)
        firstname, lastname = associate_prefix(firstname, lastname)
        return {'name_first':firstname, 'name_last':lastname}

    def _add_sequence(self, list_of_fields):
        """
        Given a list of lists (the internal lists will represent lawyers, etc), extend
        each list by the order it is received. The first list will have sequence 0,
        the second will have sequence 1, etc
        """
        res = []
        for idx, item in enumerate(list_of_fields):
            item.append(idx)
            res.append(item)
        return res

    def _asg_list(self):
        doc = self.xml.assignees.assignee
        data = []
        if not doc: return []
        if doc.first_name:
            data = [1]
            firstname, lastname = self._name_helper(doc)
            data.extend(lastname)
            data.extend(firstname)
        else:
            data = [0]
            data.extend(doc.contents_of('orgname'))
            data.extend(doc.contents_of('role'))
        for tag in ['street','city','state','country','postcode']:
            data.extend(doc.addressbook.address.contents_of(tag))
        data.extend(doc.nationality.contents_of('country'))
        data.extend(doc.residence.contents_of('country'))
        return [data]

    def _cit_list(self):
        res = []
        citations = self.xml.references_cited.citation
        for citation in citations:
            cit_data = citation.contents_of('category')
            if citation.patcit:
                for tag in ['country','doc_number','date','kind','name']:
                    contents = citation.contents_of(tag)
                    if isinstance(contents, list) and contents:
                        cit_data.append(contents[0])
                    else:
                        cit_data.append(contents if has_content(contents) else '')
                cit_data.append('')
            if citation.othercit:
                contents = citation.contents_of('othercit')
                for chunk in contents:
                    cit_data.extend(['','','','',''])
                    if isinstance(chunk,list):
                        cit_data.append(''.join([escape_html_nosub(x) for x in chunk]).upper())
                    else:
                        cit_data.append(escape_html_nosub(chunk))
            res.append(cit_data)
        return res

    def _rel_helper(self, base, roots, taglist):
        """
        Given a list of XMLElements as the [roots], look for each of the tags
        in [taglist] and create a list of the contents of the tags for each of
        the roots. Starts each of the content lists with [base]
        """
        res = []
        for root in roots:
            data = base
            for tag in taglist:
                contents = root.contents_of(tag,default=[''])
                data.extend(contents[:1] if isinstance(contents,list) else [contents])
            res.append(data)
        return res

    def _rel_list(self):
        res = []
        taglist = ['doc_number','country','kind']
        for tag in ['continuation_in_part','continuation','division','reissue']:
            main = self.xml.__getattr__(tag)
            if not main:
                continue
            tag = tag.replace('_','-').upper()
            relations = main.relation # get all relations
            for relation in relations:
                if relation.child_doc:
                    res.extend(self._rel_helper([tag, -1], relation.child_doc, taglist))
                base = [tag, 1]
                taglist.extend(['date','parent_status'])
                if relation.parent_doc:
                    res.extend(self._rel_helper(base, relation.parent_doc, taglist))
                if relation.parent_doc.parent_grant_document:
                    res.extend(self._rel_helper(base, relation.parent_doc.parent_grant_document, taglist))
                if relation.parent_doc.parent_pct_document:
                    res.extend(self._rel_helper(base, relation.parent_doc.parent_pct_document, taglist))
            if res: break
        for tag in ['related-publication','us-provisional-application']:
            if not self.xml.__getattr__(tag):
                continue
            if self.xml.document_id:
                tmp = [tag, 0]
                for nested in ['doc_number','country','kind']:
                    tmp.extend(self.xml.document_id.contents_of(nested))
                res.append(tmp)
            if res: break
        return res

    def augmented_rel_list(self):
        """includes the sequence number at the end of the list"""
        res = []
        seq = []
        root = self.xml.us_related_documents
        if not root: return
        root = root[0]
        for usreldoc in root.children:
            for relation in usreldoc.relation:
                relnumber = -1 if relation.child_doc else 1
                docinfo = [usreldoc._name, relnumber, relation.contents_of('doc_number')[0],\
                          relation.contents_of('country')[0], relation.contents_of('kind')]
                print docinfo

    def _inv_list(self):
        inventors = self.xml.parties.applicant
        if not inventors: return []
        res = []
        for inventor in inventors:
            data = []
            firstname, lastname = self._name_helper(inventor.addressbook)
            data.append(lastname)
            data.append(firstname)
            for tag in ['street','city','state','country','postcode']:
                data.append(inventor.addressbook.address.contents_of(tag,as_string=True))
            data.append(inventor.nationality.contents_of('country',as_string=True))
            data.append(inventor.residence.contents_of('country',as_string=True))
            res.append(data)
        return res

    def _law_list(self):
        lawyers = self.xml.parties.agents.agent
        if not lawyers: return []
        res = []
        for lawyer in lawyers:
            data = []
            firstname, lastname = self._name_helper(lawyer)
            data.append(lastname)
            data.append(firstname)
            data.append(lawyer.contents_of('country',as_string=True))
            data.append(lawyer.contents_of('orgname',as_string=True))
            res.append(data)
        return res

    def assignee_list(self):
        """
        Returns list of dictionaries:
        assignee:
          name_last
          name_first
          residence
          nationality
          sequence
        location:
          city
          state
          country
        """
        assignees = self.xml.assignees.assignee
        if not assignees: return []
        res = []
        for i,assignee in enumerate(assignees):
            # add assignee data
            asg_dict = {}
            asg_dict.update(self._name_helper_dict(assignee)) # add firstname, lastname
            asg_dict['orgname'] = assignee.contents_of('orgname',as_string=True)
            asg_dict['role'] = assignee.contents_of('role',as_string=True)
            asg_dict['nationality'] = assignee.nationality.contents_of('country')[0]
            asg_dict['residence'] = assignee.nationality.contents_of('country')[0]
            asg_dict['sequence'] = i
            # add location data for assignee
            location = {}
            for tag in ['city','state','country']:
                location[tag] = assignee.contents_of(tag,as_string=True)
            res.append([asg_dict, location])
        return res

    def citation_list(self):
        """
        Returns list of dictionaries:
        citation:
          date
          name
          kind
          country
          category
          number
          sequence
        OR
        otherreference:
          text
          sequence
        """
        citations = self.xml.references_cited.citation
        if not citations: return []
        res = []
        for i,citation in enumerate(citations):
            data = {}
            for tag in ['date','name','kind','category']:
                data[tag] = citation.contents_of(tag, as_string=True)
            data['country'] = citation.contents_of('country', default=[''])[0]
            doc_number = citation.contents_of('doc_number', as_string=True)
            data['number'] = normalize_document_identifier(doc_number)
            data['text'] = citation.contents_of('othercit', as_string=True)
            data['sequence'] = i
            res.append(data)
        return res

    def inventor_list(self):
        """
        Returns list of lists of inventor dictionary and location dictionary
        inventor:
          name_last
          name_first
          nationality
          sequence
        location:
          city
          state
          country
        """
        inventors = self.xml.parties.applicant
        if not inventors: return []
        res = []
        for i,inventor in enumerate(inventors):
            # add inventor data
            inv = {}
            inv.update(self._name_helper_dict(inventor.addressbook))
            inv['nationality'] = inventor.nationality.contents_of('country', as_string=True)
            inv['sequence'] = i
            # add location data for inventor
            loc = {}
            for tag in ['city','state','country']:
                loc[tag] = inventor.addressbook.contents_of(tag,as_string=True)
            res.append([inv, loc])
        return res

<commit_msg>change asg_dict to asg to be consistent<commit_after>#!/usr/bin/env python

"""
Uses the extended ContentHandler from xml_driver to extract the needed fields
from patent grant documents
"""

import cStringIO
from xml_driver import *
from xml_util import *
from xml.sax import xmlreader

class PatentGrant(object):

    def __init__(self, filename, is_string=False):
        xh = XMLHandler()
        parser = make_parser()
        parser.setContentHandler(xh)
        parser.setFeature(handler.feature_external_ges, False)
        l = xmlreader.Locator()
        xh.setDocumentLocator(l)
        if is_string:
            parser.parse(cStringIO.StringIO(filename))
        else:
            parser.parse(filename)
        self.xml = xh.root.us_patent_grant.us_bibliographic_data_grant

        self.country = self.xml.publication_reference.contents_of('country')[0]
        self.patent = normalize_document_identifier(self.xml.publication_reference.contents_of('doc_number')[0])
        self.kind = self.xml.publication_reference.contents_of('kind')[0]
        self.date_grant = self.xml.publication_reference.contents_of('date')[0]
        self.pat_type = self.xml.application_reference[0].get_attribute('appl-type')
        self.date_app = self.xml.application_reference.contents_of('date')[0]
        self.country_app = self.xml.application_reference.contents_of('country')[0]
        self.patent_app = self.xml.application_reference.contents_of('doc_number')[0]
        self.code_app = self.xml.contents_of('us_application_series_code')[0]
        self.clm_num = self.xml.contents_of('number_of_claims')[0]
        self.classes = self._classes()
        self.abstract = self.xml.contents_of('abstract','')
        self.invention_title = self._invention_title()
        self.asg_list = self._asg_list()
        self.cit_list = self._cit_list()
        self.rel_list = self._rel_list()
        self.inv_list = self._inv_list()
        self.law_list = self._law_list()

    def _invention_title(self):
        original = self.xml.contents_of('invention_title')[0]
        if isinstance(original, list):
          original = ''.join(original)
        return original

    def _classes(self):
        main = self.xml.classification_national.contents_of('main_classification')
        further = self.xml.classification_national.contents_of('further_classification')
        it = [main[0] if has_content(main) else []]
        if has_content(further):
            it.extend(further)
        return [ [x[:3].replace(' ',''), x[3:].replace(' ','')] for x in it]

    def _name_helper(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True)
        lastname = tag_root.contents_of('last_name', as_string=True)
        return associate_prefix(firstname, lastname)

    def _name_helper_dict(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True)
        lastname = tag_root.contents_of('last_name', as_string=True)
        firstname, lastname = associate_prefix(firstname, lastname)
        return {'name_first':firstname, 'name_last':lastname}

    def _add_sequence(self, list_of_fields):
        """
        Given a list of lists (the internal lists will represent lawyers, etc), extend
        each list by the order it is received. The first list will have sequence 0,
        the second will have sequence 1, etc
        """
        res = []
        for idx, item in enumerate(list_of_fields):
            item.append(idx)
            res.append(item)
        return res

    def _asg_list(self):
        doc = self.xml.assignees.assignee
        data = []
        if not doc: return []
        if doc.first_name:
            data = [1]
            firstname, lastname = self._name_helper(doc)
            data.extend(lastname)
            data.extend(firstname)
        else:
            data = [0]
            data.extend(doc.contents_of('orgname'))
            data.extend(doc.contents_of('role'))
        for tag in ['street','city','state','country','postcode']:
            data.extend(doc.addressbook.address.contents_of(tag))
        data.extend(doc.nationality.contents_of('country'))
        data.extend(doc.residence.contents_of('country'))
        return [data]

    def _cit_list(self):
        res = []
        citations = self.xml.references_cited.citation
        for citation in citations:
            cit_data = citation.contents_of('category')
            if citation.patcit:
                for tag in ['country','doc_number','date','kind','name']:
                    contents = citation.contents_of(tag)
                    if isinstance(contents, list) and contents:
                        cit_data.append(contents[0])
                    else:
                        cit_data.append(contents if has_content(contents) else '')
                cit_data.append('')
            if citation.othercit:
                contents = citation.contents_of('othercit')
                for chunk in contents:
                    cit_data.extend(['','','','',''])
                    if isinstance(chunk,list):
                        cit_data.append(''.join([escape_html_nosub(x) for x in chunk]).upper())
                    else:
                        cit_data.append(escape_html_nosub(chunk))
            res.append(cit_data)
        return res

    def _rel_helper(self, base, roots, taglist):
        """
        Given a list of XMLElements as the [roots], look for each of the tags
        in [taglist] and create a list of the contents of the tags for each of
        the roots. Starts each of the content lists with [base]
        """
        res = []
        for root in roots:
            data = base
            for tag in taglist:
                contents = root.contents_of(tag,default=[''])
                data.extend(contents[:1] if isinstance(contents,list) else [contents])
            res.append(data)
        return res

    def _rel_list(self):
        res = []
        taglist = ['doc_number','country','kind']
        for tag in ['continuation_in_part','continuation','division','reissue']:
            main = self.xml.__getattr__(tag)
            if not main:
                continue
            tag = tag.replace('_','-').upper()
            relations = main.relation # get all relations
            for relation in relations:
                if relation.child_doc:
                    res.extend(self._rel_helper([tag, -1], relation.child_doc, taglist))
                base = [tag, 1]
                taglist.extend(['date','parent_status'])
                if relation.parent_doc:
                    res.extend(self._rel_helper(base, relation.parent_doc, taglist))
                if relation.parent_doc.parent_grant_document:
                    res.extend(self._rel_helper(base, relation.parent_doc.parent_grant_document, taglist))
                if relation.parent_doc.parent_pct_document:
                    res.extend(self._rel_helper(base, relation.parent_doc.parent_pct_document, taglist))
            if res: break
        for tag in ['related-publication','us-provisional-application']:
            if not self.xml.__getattr__(tag):
                continue
            if self.xml.document_id:
                tmp = [tag, 0]
                for nested in ['doc_number','country','kind']:
                    tmp.extend(self.xml.document_id.contents_of(nested))
                res.append(tmp)
            if res: break
        return res

    def augmented_rel_list(self):
        """includes the sequence number at the end of the list"""
        res = []
        seq = []
        root = self.xml.us_related_documents
        if not root: return
        root = root[0]
        for usreldoc in root.children:
            for relation in usreldoc.relation:
                relnumber = -1 if relation.child_doc else 1
                docinfo = [usreldoc._name, relnumber, relation.contents_of('doc_number')[0],\
                          relation.contents_of('country')[0], relation.contents_of('kind')]
                print docinfo

    def _inv_list(self):
        inventors = self.xml.parties.applicant
        if not inventors: return []
        res = []
        for inventor in inventors:
            data = []
            firstname, lastname = self._name_helper(inventor.addressbook)
            data.append(lastname)
            data.append(firstname)
            for tag in ['street','city','state','country','postcode']:
                data.append(inventor.addressbook.address.contents_of(tag,as_string=True))
            data.append(inventor.nationality.contents_of('country',as_string=True))
            data.append(inventor.residence.contents_of('country',as_string=True))
            res.append(data)
        return res

    def _law_list(self):
        lawyers = self.xml.parties.agents.agent
        if not lawyers: return []
        res = []
        for lawyer in lawyers:
            data = []
            firstname, lastname = self._name_helper(lawyer)
            data.append(lastname)
            data.append(firstname)
            data.append(lawyer.contents_of('country',as_string=True))
            data.append(lawyer.contents_of('orgname',as_string=True))
            res.append(data)
        return res

    def assignee_list(self):
        """
        Returns list of dictionaries:
        assignee:
          name_last
          name_first
          residence
          nationality
          sequence
        location:
          city
          state
          country
        """
        assignees = self.xml.assignees.assignee
        if not assignees: return []
        res = []
        for i,assignee in enumerate(assignees):
            # add assignee data
            asg = {}
            asg.update(self._name_helper_dict(assignee)) # add firstname, lastname
            asg['orgname'] = assignee.contents_of('orgname',as_string=True)
            asg['role'] = assignee.contents_of('role',as_string=True)
            asg['nationality'] = assignee.nationality.contents_of('country')[0]
            asg['residence'] = assignee.nationality.contents_of('country')[0]
            asg['sequence'] = i
            # add location data for assignee
            loc = {}
            for tag in ['city','state','country']:
                loc[tag] = assignee.contents_of(tag,as_string=True)
            res.append([asg, loc])
        return res

    def citation_list(self):
        """
        Returns list of dictionaries:
        citation:
          date
          name
          kind
          country
          category
          number
          sequence
        OR
        otherreference:
          text
          sequence
        """
        citations = self.xml.references_cited.citation
        if not citations: return []
        res = []
        for i,citation in enumerate(citations):
            data = {}
            for tag in ['date','name','kind','category']:
                data[tag] = citation.contents_of(tag, as_string=True)
            data['country'] = citation.contents_of('country', default=[''])[0]
            doc_number = citation.contents_of('doc_number', as_string=True)
            data['number'] = normalize_document_identifier(doc_number)
            data['text'] = citation.contents_of('othercit', as_string=True)
            data['sequence'] = i
            res.append(data)
        return res

    def inventor_list(self):
        """
        Returns list of lists of inventor dictionary and location dictionary
        inventor:
          name_last
          name_first
          nationality
          sequence
        location:
          city
          state
          country
        """
        inventors = self.xml.parties.applicant
        if not inventors: return []
        res = []
        for i,inventor in enumerate(inventors):
            # add inventor data
            inv = {}
            inv.update(self._name_helper_dict(inventor.addressbook))
            inv['nationality'] = inventor.nationality.contents_of('country', as_string=True)
            inv['sequence'] = i
            # add location data for inventor
            loc = {}
            for tag in ['city','state','country']:
                loc[tag] = inventor.addressbook.contents_of(tag,as_string=True)
            res.append([inv, loc])
        return res

<|endoftext|>


<commit_before>#
# JavaScript Tools - Compressor Module
# Copyright 2010 Sebastian Werner
#

import re, sys, json
from js.tokenizer.Lang import keywords
from js.parser.Lang import expressions

__all__ = [ "compress" ]

#
# Shared data
#

__simpleProperty = re.compile("^[a-zA-Z_$][a-zA-Z0-9_$]*$")
__semicolonSymbol = ";"
__commaSymbol = ","

#
# Main
#

def compress(node):
    type = node.type
    result = None

    if type in simple:
        result = type
    elif type in prefixes:
        if getattr(node, "postfix", False):
            result = compress(node[0]) + prefixes[node.type]
        else:
            result = prefixes[node.type] + compress(node[0])

    elif type in dividers:
        result = dividers[node.type].join(map(compress, node))
    else:
        try:
            result = globals()["__" + type](node)
        except KeyError:
            print("Compressor does not support type '%s' from line %s in file %s" % (type, node.line, node.getFileName()))
            print(node.toJson())
            sys.exit(1)

    if getattr(node, "parenthesized", None):
        return "(%s)" % result
    else:
        return result

def statements(node):
    result = []
    for child in node:
        result.append(compress(child))

    return "".join(result)

def addSemicolon(result):
    if not result.endswith(__semicolonSymbol):
        return result + __semicolonSymbol
    else:
        return result

def removeSemicolon(result):
    if result.endswith(__semicolonSymbol):
        return result[:-len(__semicolonSymbol)]
    else:
        return result

#
# Data
#

simple = ["true", "false", "null", "this", "debugger"]

dividers = {
    "plus"          : '+',
    "minus"         : '-',
    "mul"           : '*',
    "div"           : '/',
    "mod"           : '%',
    "dot"           : '.',
    "or"            : "||",
    "and"           : "&&",
    "strict_eq"     : '===',
    "eq"            : '==',
    "strict_ne"     : '!==',
    "ne"            : '!=',
    "lsh"           : '<<',
    "le"            : '<=',
    "lt"            : '<',
    "ursh"          : '>>>',
    "rsh"           : '>>',
    "ge"            : '>=',
    "gt"            : '>',
    "bitwise_or"    : '|',
    "bitwise_xor"   : '^',
    "bitwise_and"   : '&',
    "comma"         : ',',
    "instanceof"    : ' instanceof ',
    "in"            : ' in ',
}

prefixes = {
    "increment"     : "++",
    "decrement"     : "--",
    "bitwise_not"   : '~',
    "not"           : "!",
    "unary_plus"    : "+",
    "unary_minus"   : "-",
    "delete"        : "delete ",
    "new"           : "new ",
    "typeof"        : "typeof "
}

#
# Script Scope
#

def __script(node):
    return statements(node)

#
# Expressions
#

def __object_init(node):
    return "{%s}" % __commaSymbol.join(map(compress, node))

def __property_init(node):
    key = compress(node[0])
    value = compress(node[1])

    if type(key) in [int,float]:
        pass

    # Protect keywords and special characters
    elif key in keywords or not __simpleProperty.match(key):
        key = __string(node[0])

    return "%s:%s" % (key, value)

def __array_init(node):
    def helper(child):
        return compress(child) if child != None else ""

    return "[%s]" % ",".join(map(helper, node))

def __array_comp(node):
    return "[%s %s]" % (compress(node.expression), compress(node.tail))

def __string(node):
    return json.JSONEncoder().encode(node.value)

def __number(node):
    value = node.value
    if int(value) == value:
        value = int(value)
    else:
        conv = str(value)
        if conv.startswith("0."):
            value = conv[1:]

    return "%s" % value

def __regexp(node):
    return node.value

def __identifier(node):
    return node.value

def __list(node):
    return ",".join(map(compress, node))

def __index(node):
    return "%s[%s]" % (compress(node[0]), compress(node[1]))

def __declaration(node):
    names = getattr(node, "names", None)
    if names:
        result = compress(names)
    else:
        result = node.name

    initializer = getattr(node, "initializer", None)
    if initializer:
        result += "=%s" % compress(node.initializer)

    return result

def __assign(node):
    assignOp = getattr(node[0], "assignOp", None)
    operator = "=" if not assignOp else dividers[assignOp] + "="

    return compress(node[0]) + operator + compress(node[1])

def __call(node):
    return "%s(%s)" % (compress(node[0]), compress(node[1]))

def __new_with_args(node):
    return "new %s(%s)" % (compress(node[0]), compress(node[1]))

def __exception(node):
    return node.value

def __generator(node):
    """ Generator Expression """
    result = compress(getattr(node, "expression"))
    tail = getattr(node, "tail", None)
    if tail:
        result += " %s" % compress(tail)

    return result

def __comp_tail(node):
    """  Comprehensions Tails """
    result = compress(getattr(node, "for"))
    guard = getattr(node, "guard", None)
    if guard:
        result += "if(%s)" % compress(guard)

    return result

#
# Statements :: Core
#

def __block(node):
    return "{%s}" % removeSemicolon(statements(node))

def __let_block(node):
    begin = "let(%s)" % ",".join(map(compress, node.variables))
    if hasattr(node, "block"):
        end = compress(node.block)
    elif hasattr(node, "expression"):
        end = compress(node.expression)

    return begin + end

def __const(node):
    return addSemicolon("const %s" % __list(node))

def __var(node):
    return addSemicolon("var %s" % __list(node))

def __let(node):
    return addSemicolon("let %s" % __list(node))

def __semicolon(node):
    expression = getattr(node, "expression", None)
    return addSemicolon(compress(expression) if expression else "")

def __label(node):
    return addSemicolon("%s:%s" % (node.label, compress(node.statement)))

def __break(node):
    return addSemicolon("break" if not hasattr(node, "label") else "break %s" % node.label)

def __continue(node):
    return addSemicolon("continue" if not hasattr(node, "label") else "continue %s" % node.label)

#
# Statements :: Functions
#

def __function(node):
    if node.type == "setter":
        result = "set"
    elif node.type == "getter":
        result = "get"
    else:
        result = "function"

    name = getattr(node, "name", None)
    if name:
        result += " %s" % name

    params = getattr(node, "params", None)
    result += "(%s)" % compress(params) if params else "()"

    # keep expression closure format (may be micro-optimized for other code, too)
    if getattr(node, "expressionClosure", False):
        result += compress(node.body)
    else:
        result += "{%s}" % removeSemicolon(compress(node.body))

    return result

def __getter(node):
    return __function(node)

def __setter(node):
    return __function(node)

def __return(node):
    result = "return"
    if hasattr(node, "value"):
        valueCode = compress(node.value)

        # Micro optimization: Don't need a space when a block/map/array/group/strings are returned
        if not valueCode.startswith(("(","[","{","'",'"')):
            result += " "

        result += valueCode

    return addSemicolon(result)

#
# Statements :: Exception Handling
#

def __throw(node):
    return addSemicolon("throw %s" % compress(node.exception))

def __try(node):
    result = "try{%s}" % statements(node.tryBlock)

    for catch in node:
        if catch.type == "catch":
            if hasattr(catch, "guard"):
                result += "catch(%s if %s){%s}" % (compress(catch.exception), compress(catch.guard), statements(catch.block))
            else:
                result += "catch(%s){%s}" % (compress(catch.exception), statements(catch.block))

    if hasattr(node, "finallyBlock"):
        result += "finally{%s}" % statements(node.finallyBlock)

    return result

#
# Statements :: Loops
#

def __while(node):
    body = compress(node.body)
    return "while(%s)%s" % (compress(node.condition), body)

def __do(node):
    # block unwrapping don't help to reduce size on this loop type
    # but if it happens (don't like to modify a global function to fix a local issue), we
    # need to fix the body and re-add braces around the statement
    body = compress(node.body)
    if not body.startswith("{"):
        body = "{%s}" % body

    return addSemicolon("do%swhile(%s)" % (body, compress(node.condition)))

def __for_in(node):
    # Optional variable declarations
    varDecl = getattr(node, "varDecl", None)

    # Body is optional - at least in comprehensions tails
    body = getattr(node, "body", None)
    if body:
        body = compress(body)
    else:
        body = ""

    return "for(%s in %s)%s" % (compress(node.iterator), compress(node.object), body)

def __for(node):
    setup = getattr(node, "setup", None)
    condition = getattr(node, "condition", None)
    update = getattr(node, "update", None)

    result = "for("
    result += addSemicolon(compress(setup) if setup else "")
    result += addSemicolon(compress(condition) if condition else "")
    result += compress(update) if update else ""
    result += ")%s" % compress(node.body)

    return result

#
# Statements :: Conditionals
#

def __hook(node):
    """aka ternary operator"""
    condition = node.condition
    thenPart = node.thenPart
    elsePart = node.elsePart

    if condition.type == "not":
        [thenPart,elsePart] = [elsePart,thenPart]
        condition = condition[0]

    return "%s?%s:%s" % (compress(condition), compress(thenPart), compress(elsePart))

def __if(node):
    result = "if(%s)%s" % (compress(node.condition), compress(node.thenPart))

    elsePart = getattr(node, "elsePart", None)
    if elsePart:
        result += "else"

        elseCode = compress(elsePart)

        # Micro optimization: Don't need a space when the child is a block
        # At this time the brace could not be part of a map declaration (would be a syntax error)
        if not elseCode.startswith("{"):
            result += " "

        result += elseCode

    return result

def __switch(node):
    result = "switch(%s){" % compress(node.discriminant)
    for case in node:
        if case.type == "case":
            result += "case %s:" % compress(case.label)
        elif case.type == "default":
            result += "default:"
        else:
            continue

        for statement in case.statements:
            temp = compress(statement)
            if len(temp) > 0:
                result += addSemicolon(temp)

    return "%s}" % removeSemicolon(result)
        <commit_msg>Minor improvement<commit_after>#
# JavaScript Tools - Compressor Module
# Copyright 2010 Sebastian Werner
#

import re, sys, json
from js.tokenizer.Lang import keywords
from js.parser.Lang import expressions

__all__ = [ "compress" ]

#
# Shared data
#

__simpleProperty = re.compile("^[a-zA-Z_$][a-zA-Z0-9_$]*$")
__semicolonSymbol = ";"
__commaSymbol = ","

#
# Main
#

def compress(node):
    type = node.type
    result = None

    if type in simple:
        result = type
    elif type in prefixes:
        if getattr(node, "postfix", False):
            result = compress(node[0]) + prefixes[node.type]
        else:
            result = prefixes[node.type] + compress(node[0])

    elif type in dividers:
        result = dividers[node.type].join(map(compress, node))
    else:
        try:
            result = globals()["__" + type](node)
        except KeyError:
            print("Compressor does not support type '%s' from line %s in file %s" % (type, node.line, node.getFileName()))
            print(node.toJson())
            sys.exit(1)

    if getattr(node, "parenthesized", None):
        return "(%s)" % result
    else:
        return result

def statements(node):
    result = []
    for child in node:
        result.append(compress(child))

    return "".join(result)

def addSemicolon(result):
    if not result.endswith(__semicolonSymbol):
        return result + __semicolonSymbol
    else:
        return result

def removeSemicolon(result):
    if result.endswith(__semicolonSymbol):
        return result[:-len(__semicolonSymbol)]
    else:
        return result

#
# Data
#

simple = ["true", "false", "null", "this", "debugger"]

dividers = {
    "plus"          : '+',
    "minus"         : '-',
    "mul"           : '*',
    "div"           : '/',
    "mod"           : '%',
    "dot"           : '.',
    "or"            : "||",
    "and"           : "&&",
    "strict_eq"     : '===',
    "eq"            : '==',
    "strict_ne"     : '!==',
    "ne"            : '!=',
    "lsh"           : '<<',
    "le"            : '<=',
    "lt"            : '<',
    "ursh"          : '>>>',
    "rsh"           : '>>',
    "ge"            : '>=',
    "gt"            : '>',
    "bitwise_or"    : '|',
    "bitwise_xor"   : '^',
    "bitwise_and"   : '&',
    "comma"         : ',',
    "instanceof"    : ' instanceof ',
    "in"            : ' in ',
}

prefixes = {
    "increment"     : "++",
    "decrement"     : "--",
    "bitwise_not"   : '~',
    "not"           : "!",
    "unary_plus"    : "+",
    "unary_minus"   : "-",
    "delete"        : "delete ",
    "new"           : "new ",
    "typeof"        : "typeof "
}

#
# Script Scope
#

def __script(node):
    return statements(node)

#
# Expressions
#

def __object_init(node):
    return "{%s}" % __commaSymbol.join(map(compress, node))

def __property_init(node):
    key = compress(node[0])
    value = compress(node[1])

    if type(key) in [int,float]:
        pass

    # Protect keywords and special characters
    elif key in keywords or not __simpleProperty.match(key):
        key = __string(node[0])

    return "%s:%s" % (key, value)

def __array_init(node):
    def helper(child):
        return compress(child) if child != None else ""

    return "[%s]" % ",".join(map(helper, node))

def __array_comp(node):
    return "[%s %s]" % (compress(node.expression), compress(node.tail))

def __string(node):
    return json.JSONEncoder().encode(node.value)

def __number(node):
    value = node.value
    if int(value) == value:
        value = int(value)
    else:
        conv = str(value)
        if conv.startswith("0."):
            value = conv[1:]

    return "%s" % value

def __regexp(node):
    return node.value

def __identifier(node):
    return node.value

def __list(node):
    return ",".join(map(compress, node))

def __index(node):
    return "%s[%s]" % (compress(node[0]), compress(node[1]))

def __declaration(node):
    names = getattr(node, "names", None)
    if names:
        result = compress(names)
    else:
        result = node.name

    initializer = getattr(node, "initializer", None)
    if initializer:
        result += "=%s" % compress(node.initializer)

    return result

def __assign(node):
    assignOp = getattr(node[0], "assignOp", None)
    operator = "=" if not assignOp else dividers[assignOp] + "="

    return compress(node[0]) + operator + compress(node[1])

def __call(node):
    return "%s(%s)" % (compress(node[0]), compress(node[1]))

def __new_with_args(node):
    return "new %s(%s)" % (compress(node[0]), compress(node[1]))

def __exception(node):
    return node.value

def __generator(node):
    """ Generator Expression """
    result = compress(getattr(node, "expression"))
    tail = getattr(node, "tail", None)
    if tail:
        result += " %s" % compress(tail)

    return result

def __comp_tail(node):
    """  Comprehensions Tails """
    result = compress(getattr(node, "for"))
    guard = getattr(node, "guard", None)
    if guard:
        result += "if(%s)" % compress(guard)

    return result

#
# Statements :: Core
#

def __block(node):
    return "{%s}" % removeSemicolon(statements(node))

def __let_block(node):
    begin = "let(%s)" % ",".join(map(compress, node.variables))
    if hasattr(node, "block"):
        end = compress(node.block)
    elif hasattr(node, "expression"):
        end = compress(node.expression)

    return begin + end

def __const(node):
    return addSemicolon("const %s" % __list(node))

def __var(node):
    return addSemicolon("var %s" % __list(node))

def __let(node):
    return addSemicolon("let %s" % __list(node))

def __semicolon(node):
    expression = getattr(node, "expression", None)
    return addSemicolon(compress(expression) if expression else "")

def __label(node):
    return addSemicolon("%s:%s" % (node.label, compress(node.statement)))

def __break(node):
    return addSemicolon("break" if not hasattr(node, "label") else "break %s" % node.label)

def __continue(node):
    return addSemicolon("continue" if not hasattr(node, "label") else "continue %s" % node.label)

#
# Statements :: Functions
#

def __function(node):
    if node.type == "setter":
        result = "set"
    elif node.type == "getter":
        result = "get"
    else:
        result = "function"

    name = getattr(node, "name", None)
    if name:
        result += " %s" % name

    params = getattr(node, "params", None)
    result += "(%s)" % compress(params) if params else "()"

    # keep expression closure format (may be micro-optimized for other code, too)
    if getattr(node, "expressionClosure", False):
        result += compress(node.body)
    else:
        result += "{%s}" % removeSemicolon(compress(node.body))

    return result

def __getter(node):
    return __function(node)

def __setter(node):
    return __function(node)

def __return(node):
    result = "return"
    if hasattr(node, "value"):
        valueCode = compress(node.value)

        # Micro optimization: Don't need a space when a block/map/array/group/strings are returned
        if not valueCode.startswith(("(","[","{","'",'"')):
            result += " "

        result += valueCode

    return addSemicolon(result)

#
# Statements :: Exception Handling
#

def __throw(node):
    return addSemicolon("throw %s" % compress(node.exception))

def __try(node):
    result = "try{%s}" % statements(node.tryBlock)

    for catch in node:
        if catch.type == "catch":
            if hasattr(catch, "guard"):
                result += "catch(%s if %s){%s}" % (compress(catch.exception), compress(catch.guard), statements(catch.block))
            else:
                result += "catch(%s){%s}" % (compress(catch.exception), statements(catch.block))

    if hasattr(node, "finallyBlock"):
        result += "finally{%s}" % statements(node.finallyBlock)

    return result

#
# Statements :: Loops
#

def __while(node):
    body = compress(node.body)
    return "while(%s)%s" % (compress(node.condition), body)

def __do(node):
    # block unwrapping don't help to reduce size on this loop type
    # but if it happens (don't like to modify a global function to fix a local issue), we
    # need to fix the body and re-add braces around the statement
    body = compress(node.body)
    if not body.startswith("{"):
        body = "{%s}" % body

    return addSemicolon("do%swhile(%s)" % (body, compress(node.condition)))

def __for_in(node):
    # Optional variable declarations
    varDecl = getattr(node, "varDecl", None)

    # Body is optional - at least in comprehensions tails
    body = getattr(node, "body", None)
    if body:
        body = compress(body)
    else:
        body = ""

    return "for(%s in %s)%s" % (compress(node.iterator), compress(node.object), body)

def __for(node):
    setup = getattr(node, "setup", None)
    condition = getattr(node, "condition", None)
    update = getattr(node, "update", None)

    result = "for("
    result += addSemicolon(compress(setup) if setup else "")
    result += addSemicolon(compress(condition) if condition else "")
    result += compress(update) if update else ""
    result += ")%s" % compress(node.body)

    return result

#
# Statements :: Conditionals
#

def __hook(node):
    """aka ternary operator"""
    condition = node.condition
    thenPart = node.thenPart
    elsePart = node.elsePart

    if condition.type == "not":
        [thenPart,elsePart] = [elsePart,thenPart]
        condition = condition[0]

    return "%s?%s:%s" % (compress(condition), compress(thenPart), compress(elsePart))

def __if(node):
    result = "if(%s)%s" % (compress(node.condition), compress(node.thenPart))

    elsePart = getattr(node, "elsePart", None)
    if elsePart:
        result += "else"

        elseCode = compress(elsePart)

        # Micro optimization: Don't need a space when the child is a block
        # At this time the brace could not be part of a map declaration (would be a syntax error)
        if not elseCode.startswith(("{", "(")):
            result += " "

        result += elseCode

    return result

def __switch(node):
    result = "switch(%s){" % compress(node.discriminant)
    for case in node:
        if case.type == "case":
            result += "case %s:" % compress(case.label)
        elif case.type == "default":
            result += "default:"
        else:
            continue

        for statement in case.statements:
            temp = compress(statement)
            if len(temp) > 0:
                result += addSemicolon(temp)

    return "%s}" % removeSemicolon(result)
        <|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

{
    'name': 'Base',
    'version': '1.2',
    'category': 'Generic Modules/Base',
    'description': """The kernel of OpenERP, needed for all installation.""",
    'author': 'OpenERP s.a.',
    'maintainer': 'OpenERP s.a.',
    'website': 'http://www.openerp.com',
    'depends': [],
    'init_xml': [
        'base_data.xml',
        'security/base_security.xml',
        'base_menu.xml',
        'res/res_security.xml',
        'res/res_config.xml',
        'data/res.country.state.csv'
    ],
    'update_xml': [
        'base_update.xml',
        'ir/wizard/wizard_menu_view.xml',
        'ir/ir.xml',
        'ir/workflow/workflow_view.xml',
        'module/module_wizard.xml',
        'module/module_view.xml',
        'module/module_web_view.xml',
        'module/module_data.xml',
        'module/module_report.xml',
        'res/res_request_view.xml',
        'res/res_lang_view.xml',
        'res/res_log_view.xml',
        'res/res_company_view.xml',
        'res/partner/partner_report.xml',
        'res/partner/partner_view.xml',
        'res/partner/partner_wizard.xml',
        'res/bank_view.xml',
        'res/country_view.xml',
        'res/res_currency_view.xml',
        'res/partner/crm_view.xml',
        'res/partner/wizard/partner_sms_send_view.xml',
        'res/partner/partner_data.xml',
        'res/ir_property_view.xml',
        'security/base_security.xml',
        'maintenance/maintenance_view.xml',
        'security/ir.model.access.csv'

    ],
    'demo_xml': [
        'base_demo.xml',
        'res/partner/partner_demo.xml',
        'res/partner/crm_demo.xml',
    ],
    'test': [
        'test/base_test.xml',
        #'test/base_test.yml'
        'test/test_context.xml',
        'bug_lp541545.xml',
    ],
    'installable': True,
    'active': True,
    'certificate': '0076807797149',
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>Base: remove ref to non-existent test file.<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

{
    'name': 'Base',
    'version': '1.2',
    'category': 'Generic Modules/Base',
    'description': """The kernel of OpenERP, needed for all installation.""",
    'author': 'OpenERP s.a.',
    'maintainer': 'OpenERP s.a.',
    'website': 'http://www.openerp.com',
    'depends': [],
    'init_xml': [
        'base_data.xml',
        'security/base_security.xml',
        'base_menu.xml',
        'res/res_security.xml',
        'res/res_config.xml',
        'data/res.country.state.csv'
    ],
    'update_xml': [
        'base_update.xml',
        'ir/wizard/wizard_menu_view.xml',
        'ir/ir.xml',
        'ir/workflow/workflow_view.xml',
        'module/module_wizard.xml',
        'module/module_view.xml',
        'module/module_web_view.xml',
        'module/module_data.xml',
        'module/module_report.xml',
        'res/res_request_view.xml',
        'res/res_lang_view.xml',
        'res/res_log_view.xml',
        'res/res_company_view.xml',
        'res/partner/partner_report.xml',
        'res/partner/partner_view.xml',
        'res/partner/partner_wizard.xml',
        'res/bank_view.xml',
        'res/country_view.xml',
        'res/res_currency_view.xml',
        'res/partner/crm_view.xml',
        'res/partner/wizard/partner_sms_send_view.xml',
        'res/partner/partner_data.xml',
        'res/ir_property_view.xml',
        'security/base_security.xml',
        'maintenance/maintenance_view.xml',
        'security/ir.model.access.csv'

    ],
    'demo_xml': [
        'base_demo.xml',
        'res/partner/partner_demo.xml',
        'res/partner/crm_demo.xml',
    ],
    'test': [
        'test/base_test.xml',
        #'test/base_test.yml'
        'test/test_context.xml',
    ],
    'installable': True,
    'active': True,
    'certificate': '0076807797149',
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before>#!/usr/bin/env python

# MIT License
# ===========

# Copyright (c) 2012 Paul Ollivier <paul.ollivier.contact@gmail.com>

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import socket
import os
import subprocess
import logging as log

log.basicConfig(filename="CascadeServer.log", level=log.DEBUG)

class CascadeServer:
    """Main Cascade Server class. Handles the file upload, and adding it to btcli"""
    def __init__(self, port):
        log.debug("Initializing CascadeServer...")
        self.port = port
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        log.info("Server initialized, on port %s" % (self.port))

    def serve_forever(self):
        """Launches the server: SERVE FOREVER"""
        log.debug("Asked to serve forever")
        self.configure()
        self.mainloop()

    def configure(self):
        log.debug("Entering configuration")
        log.debug("Binding...")
        self.s.bind(('', self.port))
        self.s.listen(1)
        log.info("Successfully bound to port %s" % (self.port))

    def send_to_btcli(self, file_to_add=""):
        if(file_to_add != ""):
            log.info("Adding file %s to btcli" % (file_to_add))
            log.debug(subprocess.check_output(["btcli", "add", file_to_add]))
        else:
            log.error("no filename given to add to btcli!")

    def mainloop(self):
        log.debug("Entering mainloop")
        while True:
            # client receptionning loop
            conn, addr = self.s.accept()
            log.info("Client connected")
            while True:
                data = conn.recv(1024)
                log.debug("recvied: %s" % data)
                if not data:
                    log.debug("No data recieved, assuming client disconnected")
                    break
                if data[0:2] == "PUT":
                    log.debug("Recieved put request")
                    conn.send("200 OK Proceed to file upload")
                    _, file_name, file_length = data.split(' ')
                    torrent = conn.recv(file_length)
                    log.debug("recieved file")
                    conn.send("200 OK File uploaded")

                    if not os.path.exists(os.path.abspath() + "/torrents"):
                        log.info("Torrent files directory not found, creating...")
                        os.makedirs(os.path.abspath() + "/torrents")
                        log.info("torrent directory successfully created")

                    with open(os.path.abspath() + "/torrents/" + file_name, 'wb') as f:
                        f.write(torrent)
                        log.debug("Successfully written torrent file")

                    self.send_to_btcli(os.path.abspath() + "/torrents/" + file_name)
                else:
                    log.warn("PUT not detected")
            conn.close()

if __name__ == '__main__':
    try:
        CascadeServer(22222).serve_forever()
    except KeyboardInterrupt:
        print "Stopping..."
        print "OK"
<commit_msg>corrected PUT bug, and os.path bug<commit_after>#!/usr/bin/env python

# MIT License
# ===========

# Copyright (c) 2012 Paul Ollivier <paul.ollivier.contact@gmail.com>

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import socket
import os
import subprocess
import logging as log

log.basicConfig(filename="CascadeServer.log", level=log.DEBUG)

class CascadeServer:
    """Main Cascade Server class. Handles the file upload, and adding it to btcli"""
    def __init__(self, port):
        log.debug("Initializing CascadeServer...")
        self.port = port
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        log.info("Server initialized, on port %s" % (self.port))

    def serve_forever(self):
        """Launches the server: SERVE FOREVER"""
        log.debug("Asked to serve forever")
        self.configure()
        self.mainloop()

    def configure(self):
        log.debug("Entering configuration")
        log.debug("Binding...")
        self.s.bind(('', self.port))
        self.s.listen(1)
        log.info("Successfully bound to port %s" % (self.port))

    def send_to_btcli(self, file_to_add=""):
        if(file_to_add != ""):
            log.info("Adding file %s to btcli" % (file_to_add))
            log.debug(subprocess.check_output(["btcli", "add", file_to_add]))
        else:
            log.error("no filename given to add to btcli!")

    def mainloop(self):
        log.debug("Entering mainloop")
        while True:
            # client receptionning loop
            conn, addr = self.s.accept()
            log.info("Client connected")
            while True:
                data = conn.recv(1024)
                log.debug("recvied: %s" % data)
                if not data:
                    log.debug("No data recieved, assuming client disconnected")
                    break
                log.debug("data[0:3] : %s" % data[0:3])
                if data[0:3] == "PUT":
                    log.debug("Recieved put request")
                    conn.sendall("200 OK Proceed to file upload")
                    _, file_name, file_length = data.split(' ')
                    log.debug("File info: name: %s size %s" % (file_name, file_length))
                    torrent = conn.recv(int(file_length))
                    log.debug("recieved file")
                    conn.send("200 OK File uploaded")

                    if not os.path.exists(os.path.abspath(os.getcwd()) + "/torrents"):
                        log.info("Torrent files directory not found, creating...")
                        os.makedirs(os.path.abspath(os.getcwd()) + "/torrents")
                        log.info("torrent directory successfully created")

                    with open(os.path.abspath(os.getcwd()) + "/torrents/" + file_name, 'wb') as f:
                        f.write(torrent)
                        log.debug("Successfully written torrent file")

                    self.send_to_btcli(os.path.abspath(os.getcwd()) + "/torrents/" + file_name)
                else:
                    log.warn("PUT not detected")
            conn.close()

if __name__ == '__main__':
    try:
        CascadeServer(22222).serve_forever()
    except KeyboardInterrupt:
        print "Stopping..."
        print "OK"
<|endoftext|>


<commit_before>##############################################################################
#
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from osv import fields,osv
import ir
import netsvc

import time
import tools
import pooler

class ir_model(osv.osv):
	_name = 'ir.model'
	_rec_name = 'model'
	_columns = {
		'name': fields.char('Model name', size=64, translate=True),
		'model': fields.char('Object name', size=64, required=True),
		'info': fields.text('Information'),
		'field_id': fields.one2many('ir.model.fields', 'model_id', 'Fields', required=True),
	}
	_defaults = {
		'name': lambda *a: 'No Name',
	}
ir_model()

class ir_model_fields(osv.osv):
	_name = 'ir.model.fields'
	_columns = {
		'name': fields.char('Name', size=64),
		'model': fields.char('Model Name', size=64, required=True),
# on pourrait egalement changer ca en many2one, mais le prob c'est qu'alors faut
# faire une jointure a chaque fois qu'on recherche vu que le client ne connait que le nom
# de l'objet et pas son id
		'relation': fields.char('Model Relation', size=64),
		'model_id': fields.many2one('ir.model', 'Model id', required=True, select=True),
# in fact, this is the field label
		'field_description': fields.char('Field Description', size=256),
		'ttype': fields.char('Field Type', size=64),
		'relate': fields.boolean('Click and Relate'),

		'groups': fields.many2many('res.groups', 'ir_model_fields_group_rel', 'field_id', 'group_id', 'Groups'),
		'group_name': fields.char('Group Name', size=128),
		'view_load': fields.boolean('View Auto-Load'),
	}
	_defaults = {
		'relate': lambda *a: 0,
		'view_load': lambda *a: 0,
		'name': lambda *a: 'No Name',
		'field_description': lambda *a: 'No description available',
	}
	_order = "id"
ir_model_fields()

class ir_model_access(osv.osv):
	_name = 'ir.model.access'
	_columns = {
		'name': fields.char('Name', size=64, required=True),
		'model_id': fields.many2one('ir.model', 'Model', required=True),
		'group_id': fields.many2one('res.groups', 'Group'),
		'perm_read': fields.boolean('Read Access'),
		'perm_write': fields.boolean('Write Access'),
		'perm_create': fields.boolean('Create Access'),
		'perm_unlink': fields.boolean('Delete Permission'),
	}
	def check(self, cr, uid, model_name, mode='read'):
		assert mode in ['read','write','create','unlink'], 'Invalid access mode for security'
		if uid==1:
			return True
		cr.execute('select bool_or(perm_'+mode+') from ir_model_access a join ir_model m on (a.model_id=m.id) join res_groups_users_rel gu on (gu.gid = a.group_id) where m.model= %s and gu.uid= %s',(model_name,uid,))
		r= cr.fetchall()
		if r[0][0] == None:
			cr.execute(' select bool_or(perm_'+mode+') from ir_model_access a join ir_model m on (a.model_id=m.id) where a.group_id is null and m.model=%s',(model_name,))
			r= cr.fetchall()
			if r[0][0] == None : return True

		if not r[0][0]:
			raise osv.except_osv('Access denied !', 'You can not %s this resource !' % mode)
		return True

	check = tools.cache()(check)

	#
	# Methods to clean the cache on the Check Method.
	#
	def write(self, cr, uid, *args, **argv):
		res = super(ir_model_access, self).write(cr, uid, *args, **argv)
		self.check()
		return res
	def create(self, cr, uid, *args, **argv):
		res = super(ir_model_access, self).create(cr, uid, *args, **argv)
		self.check()
		return res
	def unlink(self, cr, uid, *args, **argv):
		res = super(ir_model_access, self).unlink(cr, uid, *args, **argv)
		self.check()
		return res
ir_model_access()

class ir_model_data(osv.osv):
	_name = 'ir.model.data'
	_columns = {
		'name': fields.char('XML Identifier', required=True, size=64),
		'model': fields.char('Model', required=True, size=64),
		'module': fields.char('Module', required=True, size=64),
		'res_id': fields.integer('Resource ID'),
		'noupdate': fields.boolean('Non Updatable'),
		'date_update': fields.datetime('Update Date'),
		'date_init': fields.datetime('Init Date')
	}
	_defaults = {
		'date_init': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
		'date_update': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
		'noupdate': lambda *a: False
	}

	def __init__(self, pool):
		osv.osv.__init__(self, pool)
		self.loads = {}
		self.doinit = True
		self.unlink_mark = {}

	def _get_id(self,cr, uid, module, xml_id):
		ids = self.search(cr, uid, [('module','=',module),('name','=', xml_id)])
		assert len(ids)==1, '%d reference(s) to %s. You should have only one !' % (len(ids),xml_id)
		return ids[0]

	def _update_dummy(self,cr, uid, model, module, xml_id=False, store=True):
		if not xml_id:
			return False
		try:
			id = self.read(cr, uid, [self._get_id(cr, uid, module, xml_id)], ['res_id'])[0]['res_id']
			self.loads[(module,xml_id)] = (model,id)
		except:
			id = False
		return id

	def _update(self,cr, uid, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False):
		warning = True
		if xml_id and ('.' in xml_id):
			assert len(xml_id.split('.'))==2, '"%s" contains too many dots. XML ids should not contain dots ! These are used to refer to other modules data, as in module.reference_id' % (xml_id)
			warning = False
			module, xml_id = xml_id.split('.')
		if (not xml_id) and (not self.doinit):
			return False
		action_id = False
		if xml_id:
			cr.execute('select id,res_id from ir_model_data where module=%s and name=%s', (module,xml_id))
			results = cr.fetchall()
			for action_id2,res_id2 in results:
				cr.execute('select id from '+self.pool.get(model)._table+' where id=%d', (res_id2,))
				result3 = cr.fetchone()
				if not result3:
					cr.execute('delete from ir_model_data where id=%d', (action_id2,))
				else:
					res_id,action_id = res_id2,action_id2

		if action_id and res_id:
			self.pool.get(model).write(cr, uid, [res_id], values)
			self.write(cr, uid, [action_id], {'date_update': time.strftime('%Y-%m-%d %H:%M:%S')})
		elif res_id:
			self.pool.get(model).write(cr, uid, [res_id], values)
			if xml_id:
				self.create(cr, uid, {'name':xml_id, 'model':model, 'module':module, 'res_id':res_id, 'noupdate':noupdate})
		else:
			if mode=='init' or (mode=='update' and xml_id):
				res_id = self.pool.get(model).create(cr, uid, values)
				if xml_id:
					self.create(cr, uid, {'name':xml_id, 'model':model, 'module':module, 'res_id':res_id, 'noupdate':noupdate})
		if xml_id:
			if warning:
				assert (module,xml_id) not in self.loads, "id '%s' is already defined in module '%s' !" % (xml_id, module)
			if res_id:
				self.loads[(module,xml_id)] = (model, res_id)
		return res_id

	def _unlink(self, cr, uid, model, ids, direct=False):
		#self.pool.get(model).unlink(cr, uid, ids)
		for id in ids:
			self.unlink_mark[(model, id)]=False
			cr.execute('delete from ir_model_data where res_id=%d and model=\'%s\'', (id,model))
		return True

	def ir_set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=None):
		obj = self.pool.get('ir.values')
		if type(models[0])==type([]) or type(models[0])==type(()):
			model,res_id = models[0]
		else:
			res_id=None
			model = models[0]

		if res_id:
			where = ' and res_id=%d' % (res_id,)
		else:
			where = ' and (res_id is null)'

		if key2:
			where += ' and key2=\'%s\'' % (key2,)
		else:
			where += ' and (key2 is null)'

		cr.execute('select * from ir_values where model=%s and key=%s and name=%s'+where,(model, key, name))
		res = cr.fetchone()
		if not res:
			res = ir.ir_set(cr, uid, key, key2, name, models, value, replace, isobject, meta)
		return True

	def _process_end(self, cr, uid, modules):
		if not modules:
			return True
		module_str = ["'%s'" % m for m in modules]
		cr.execute('select id,name,model,res_id,module from ir_model_data where module in ('+','.join(module_str)+') and not noupdate')
		wkf_todo = []
		for (id, name, model, res_id,module) in cr.fetchall():
			if (module,name) not in self.loads:
				self.unlink_mark[(model,res_id)] = id
				if model=='workflow.activity':
					cr.execute('select res_type,res_id from wkf_instance where id in (select inst_id from wkf_workitem where act_id=%d)', (res_id,))
					wkf_todo.extend(cr.fetchall())
					cr.execute("update wkf_transition set condition='True', role_id=NULL, signal=NULL,act_to=act_from,act_from=%d where act_to=%d", (res_id,res_id))
					cr.execute("delete from wkf_transition where act_to=%d", (res_id,))

		for model,id in wkf_todo:
			wf_service = netsvc.LocalService("workflow")
			wf_service.trg_write(uid, model, id, cr)

		cr.commit()
		for (model,id) in self.unlink_mark.keys():
			if self.pool.get(model):
				logger = netsvc.Logger()
				logger.notifyChannel('init', netsvc.LOG_INFO, 'Deleting %s@%s' % (id, model))
				try:
					self.pool.get(model).unlink(cr, uid, [id])
					if self.unlink_mark[(model,id)]:
						self.unlink(cr, uid, [self.unlink_mark[(model,id)]])
					cr.commit()
				except:
					logger.notifyChannel('init', netsvc.LOG_ERROR, 'Could not delete id: %d of model %s\tThere should be some relation that points to this resource\tYou should manually fix this and restart --update=module' % (id, model))
		return True
ir_model_data()

<commit_msg>Simulation bugfixes Asset cleaning Accounting general cleaning<commit_after>##############################################################################
#
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from osv import fields,osv
import ir
import netsvc

import time
import tools
import pooler

class ir_model(osv.osv):
	_name = 'ir.model'
	_rec_name = 'model'
	_columns = {
		'name': fields.char('Model name', size=64, translate=True),
		'model': fields.char('Object name', size=64, required=True),
		'info': fields.text('Information'),
		'field_id': fields.one2many('ir.model.fields', 'model_id', 'Fields', required=True),
	}
	_defaults = {
		'name': lambda *a: 'No Name',
	}
ir_model()

class ir_model_fields(osv.osv):
	_name = 'ir.model.fields'
	_columns = {
		'name': fields.char('Name', size=64),
		'model': fields.char('Model Name', size=64, required=True),
# on pourrait egalement changer ca en many2one, mais le prob c'est qu'alors faut
# faire une jointure a chaque fois qu'on recherche vu que le client ne connait que le nom
# de l'objet et pas son id
		'relation': fields.char('Model Relation', size=64),
		'model_id': fields.many2one('ir.model', 'Model id', required=True, select=True),
# in fact, this is the field label
		'field_description': fields.char('Field Description', size=256),
		'ttype': fields.char('Field Type', size=64),
		'relate': fields.boolean('Click and Relate'),

		'groups': fields.many2many('res.groups', 'ir_model_fields_group_rel', 'field_id', 'group_id', 'Groups'),
		'group_name': fields.char('Group Name', size=128),
		'view_load': fields.boolean('View Auto-Load'),
	}
	_defaults = {
		'relate': lambda *a: 0,
		'view_load': lambda *a: 0,
		'name': lambda *a: 'No Name',
		'field_description': lambda *a: 'No description available',
	}
	_order = "id"
ir_model_fields()

class ir_model_access(osv.osv):
	_name = 'ir.model.access'
	_columns = {
		'name': fields.char('Name', size=64, required=True),
		'model_id': fields.many2one('ir.model', 'Model', required=True),
		'group_id': fields.many2one('res.groups', 'Group'),
		'perm_read': fields.boolean('Read Access'),
		'perm_write': fields.boolean('Write Access'),
		'perm_create': fields.boolean('Create Access'),
		'perm_unlink': fields.boolean('Delete Permission'),
	}
	def check(self, cr, uid, model_name, mode='read'):
		return True
		assert mode in ['read','write','create','unlink'], 'Invalid access mode for security'
		if uid==1:
			return True
		cr.execute('select bool_or(perm_'+mode+') from ir_model_access a join ir_model m on (a.model_id=m.id) join res_groups_users_rel gu on (gu.gid = a.group_id) where m.model= %s and gu.uid= %s',(model_name,uid,))
		r= cr.fetchall()
		if r[0][0] == None:
			cr.execute(' select bool_or(perm_'+mode+') from ir_model_access a join ir_model m on (a.model_id=m.id) where a.group_id is null and m.model=%s',(model_name,))
			r= cr.fetchall()
			if r[0][0] == None : return True

		if not r[0][0]:
			raise osv.except_osv('Access denied !', 'You can not %s this resource !' % mode)
		return True

	check = tools.cache()(check)

	#
	# Methods to clean the cache on the Check Method.
	#
	def write(self, cr, uid, *args, **argv):
		res = super(ir_model_access, self).write(cr, uid, *args, **argv)
		self.check()
		return res
	def create(self, cr, uid, *args, **argv):
		res = super(ir_model_access, self).create(cr, uid, *args, **argv)
		self.check()
		return res
	def unlink(self, cr, uid, *args, **argv):
		res = super(ir_model_access, self).unlink(cr, uid, *args, **argv)
		self.check()
		return res
ir_model_access()

class ir_model_data(osv.osv):
	_name = 'ir.model.data'
	_columns = {
		'name': fields.char('XML Identifier', required=True, size=64),
		'model': fields.char('Model', required=True, size=64),
		'module': fields.char('Module', required=True, size=64),
		'res_id': fields.integer('Resource ID'),
		'noupdate': fields.boolean('Non Updatable'),
		'date_update': fields.datetime('Update Date'),
		'date_init': fields.datetime('Init Date')
	}
	_defaults = {
		'date_init': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
		'date_update': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
		'noupdate': lambda *a: False
	}

	def __init__(self, pool):
		osv.osv.__init__(self, pool)
		self.loads = {}
		self.doinit = True
		self.unlink_mark = {}

	def _get_id(self,cr, uid, module, xml_id):
		ids = self.search(cr, uid, [('module','=',module),('name','=', xml_id)])
		assert len(ids)==1, '%d reference(s) to %s. You should have only one !' % (len(ids),xml_id)
		return ids[0]

	def _update_dummy(self,cr, uid, model, module, xml_id=False, store=True):
		if not xml_id:
			return False
		try:
			id = self.read(cr, uid, [self._get_id(cr, uid, module, xml_id)], ['res_id'])[0]['res_id']
			self.loads[(module,xml_id)] = (model,id)
		except:
			id = False
		return id

	def _update(self,cr, uid, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False):
		warning = True
		if xml_id and ('.' in xml_id):
			assert len(xml_id.split('.'))==2, '"%s" contains too many dots. XML ids should not contain dots ! These are used to refer to other modules data, as in module.reference_id' % (xml_id)
			warning = False
			module, xml_id = xml_id.split('.')
		if (not xml_id) and (not self.doinit):
			return False
		action_id = False
		if xml_id:
			cr.execute('select id,res_id from ir_model_data where module=%s and name=%s', (module,xml_id))
			results = cr.fetchall()
			for action_id2,res_id2 in results:
				cr.execute('select id from '+self.pool.get(model)._table+' where id=%d', (res_id2,))
				result3 = cr.fetchone()
				if not result3:
					cr.execute('delete from ir_model_data where id=%d', (action_id2,))
				else:
					res_id,action_id = res_id2,action_id2

		if action_id and res_id:
			self.pool.get(model).write(cr, uid, [res_id], values)
			self.write(cr, uid, [action_id], {'date_update': time.strftime('%Y-%m-%d %H:%M:%S')})
		elif res_id:
			self.pool.get(model).write(cr, uid, [res_id], values)
			if xml_id:
				self.create(cr, uid, {'name':xml_id, 'model':model, 'module':module, 'res_id':res_id, 'noupdate':noupdate})
		else:
			if mode=='init' or (mode=='update' and xml_id):
				res_id = self.pool.get(model).create(cr, uid, values)
				if xml_id:
					self.create(cr, uid, {'name':xml_id, 'model':model, 'module':module, 'res_id':res_id, 'noupdate':noupdate})
		if xml_id:
			if warning:
				assert (module,xml_id) not in self.loads, "id '%s' is already defined in module '%s' !" % (xml_id, module)
			if res_id:
				self.loads[(module,xml_id)] = (model, res_id)
		return res_id

	def _unlink(self, cr, uid, model, ids, direct=False):
		#self.pool.get(model).unlink(cr, uid, ids)
		for id in ids:
			self.unlink_mark[(model, id)]=False
			cr.execute('delete from ir_model_data where res_id=%d and model=\'%s\'', (id,model))
		return True

	def ir_set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=None):
		obj = self.pool.get('ir.values')
		if type(models[0])==type([]) or type(models[0])==type(()):
			model,res_id = models[0]
		else:
			res_id=None
			model = models[0]

		if res_id:
			where = ' and res_id=%d' % (res_id,)
		else:
			where = ' and (res_id is null)'

		if key2:
			where += ' and key2=\'%s\'' % (key2,)
		else:
			where += ' and (key2 is null)'

		cr.execute('select * from ir_values where model=%s and key=%s and name=%s'+where,(model, key, name))
		res = cr.fetchone()
		if not res:
			res = ir.ir_set(cr, uid, key, key2, name, models, value, replace, isobject, meta)
		return True

	def _process_end(self, cr, uid, modules):
		if not modules:
			return True
		module_str = ["'%s'" % m for m in modules]
		cr.execute('select id,name,model,res_id,module from ir_model_data where module in ('+','.join(module_str)+') and not noupdate')
		wkf_todo = []
		for (id, name, model, res_id,module) in cr.fetchall():
			if (module,name) not in self.loads:
				self.unlink_mark[(model,res_id)] = id
				if model=='workflow.activity':
					cr.execute('select res_type,res_id from wkf_instance where id in (select inst_id from wkf_workitem where act_id=%d)', (res_id,))
					wkf_todo.extend(cr.fetchall())
					cr.execute("update wkf_transition set condition='True', role_id=NULL, signal=NULL,act_to=act_from,act_from=%d where act_to=%d", (res_id,res_id))
					cr.execute("delete from wkf_transition where act_to=%d", (res_id,))

		for model,id in wkf_todo:
			wf_service = netsvc.LocalService("workflow")
			wf_service.trg_write(uid, model, id, cr)

		cr.commit()
		for (model,id) in self.unlink_mark.keys():
			if self.pool.get(model):
				logger = netsvc.Logger()
				logger.notifyChannel('init', netsvc.LOG_INFO, 'Deleting %s@%s' % (id, model))
				try:
					self.pool.get(model).unlink(cr, uid, [id])
					if self.unlink_mark[(model,id)]:
						self.unlink(cr, uid, [self.unlink_mark[(model,id)]])
					cr.commit()
				except:
					logger.notifyChannel('init', netsvc.LOG_ERROR, 'Could not delete id: %d of model %s\tThere should be some relation that points to this resource\tYou should manually fix this and restart --update=module' % (id, model))
		return True
ir_model_data()

<|endoftext|>